1 // Created by: Kirill GAVRILOV
2 // Copyright (c) 2012 OPEN CASCADE SAS
4 // The content of this file is subject to the Open CASCADE Technology Public
5 // License Version 6.5 (the "License"). You may not use the content of this file
6 // except in compliance with the License. Please obtain a copy of the License
7 // at http://www.opencascade.org and read it completely before using this file.
9 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
10 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
12 // The Original Code and all software distributed under the License is
13 // distributed on an "AS IS" basis, without warranty of any kind, and the
14 // Initial Developer hereby disclaims all such warranties, including without
15 // limitation, any warranties of merchantability, fitness for a particular
16 // purpose or non-infringement. Please see the License for the specific terms
17 // and conditions governing the rights and limitations under the License.
19 #include <OpenGl_VertexBuffer.hxx>
21 #include <OpenGl_Context.hxx>
22 #include <Standard_Assert.hxx>
24 IMPLEMENT_STANDARD_HANDLE (OpenGl_VertexBuffer, OpenGl_Resource)
25 IMPLEMENT_STANDARD_RTTIEXT(OpenGl_VertexBuffer, OpenGl_Resource)
27 // =======================================================================
28 // function : OpenGl_VertexBuffer
30 // =======================================================================
31 OpenGl_VertexBuffer::OpenGl_VertexBuffer()
33 myBufferId (NO_BUFFER),
41 // =======================================================================
42 // function : ~OpenGl_VertexBuffer
44 // =======================================================================
45 OpenGl_VertexBuffer::~OpenGl_VertexBuffer()
50 // =======================================================================
51 // function : GetTarget
53 // =======================================================================
54 GLenum OpenGl_VertexBuffer::GetTarget() const
56 return GL_ARRAY_BUFFER;
59 // =======================================================================
62 // =======================================================================
63 bool OpenGl_VertexBuffer::Create (const Handle(OpenGl_Context)& theGlCtx)
65 if (myBufferId == NO_BUFFER)
67 theGlCtx->core15->glGenBuffers (1, &myBufferId);
69 return myBufferId != NO_BUFFER;
72 // =======================================================================
75 // =======================================================================
76 void OpenGl_VertexBuffer::Release (const OpenGl_Context* theGlCtx)
78 if (myBufferId == NO_BUFFER)
83 // application can not handle this case by exception - this is bug in code
84 Standard_ASSERT_RETURN (theGlCtx != NULL,
85 "OpenGl_VertexBuffer destroyed without GL context! Possible GPU memory leakage...",);
87 theGlCtx->core15->glDeleteBuffers (1, &myBufferId);
88 myBufferId = NO_BUFFER;
91 // =======================================================================
94 // =======================================================================
95 void OpenGl_VertexBuffer::Bind (const Handle(OpenGl_Context)& theGlCtx) const
97 theGlCtx->core15->glBindBuffer (GetTarget(), myBufferId);
100 // =======================================================================
103 // =======================================================================
104 void OpenGl_VertexBuffer::Unbind (const Handle(OpenGl_Context)& theGlCtx) const
106 theGlCtx->core15->glBindBuffer (GetTarget(), NO_BUFFER);
109 // =======================================================================
112 // =======================================================================
113 bool OpenGl_VertexBuffer::Init (const Handle(OpenGl_Context)& theGlCtx,
114 const GLuint theComponentsNb,
115 const GLsizei theElemsNb,
116 const GLfloat* theData)
118 if (!Create (theGlCtx))
124 myDataType = GL_FLOAT;
125 myComponentsNb = theComponentsNb;
126 myElemsNb = theElemsNb;
127 theGlCtx->core15->glBufferData (GetTarget(), GLsizeiptr(myElemsNb) * GLsizeiptr(myComponentsNb) * sizeof(GLfloat), theData, GL_STATIC_DRAW);
128 bool isDone = (glGetError() == GL_NO_ERROR); // GL_OUT_OF_MEMORY
133 // =======================================================================
134 // function : SubData
136 // =======================================================================
137 bool OpenGl_VertexBuffer::SubData (const Handle(OpenGl_Context)& theGlCtx,
138 const GLsizei theElemFrom,
139 const GLsizei theElemsNb,
140 const GLfloat* theData)
142 if (!IsValid() || myDataType != GL_FLOAT ||
143 theElemFrom < 0 || ((theElemFrom + theElemsNb) > myElemsNb))
149 theGlCtx->core15->glBufferSubData (GetTarget(),
150 GLintptr(theElemFrom) * GLintptr(myComponentsNb) * sizeof(GLfloat), // offset in bytes
151 GLsizeiptr(theElemsNb) * GLsizeiptr(myComponentsNb) * sizeof(GLfloat), // size in bytes
153 bool isDone = (glGetError() == GL_NO_ERROR); // some dummy error
158 // =======================================================================
161 // =======================================================================
162 bool OpenGl_VertexBuffer::Init (const Handle(OpenGl_Context)& theGlCtx,
163 const GLuint theComponentsNb,
164 const GLsizei theElemsNb,
165 const GLuint* theData)
167 if (!Create (theGlCtx))
173 myDataType = GL_UNSIGNED_INT;
174 myComponentsNb = theComponentsNb;
175 myElemsNb = theElemsNb;
176 theGlCtx->core15->glBufferData (GetTarget(), GLsizeiptr(myElemsNb) * GLsizeiptr(myComponentsNb) * sizeof(GLuint), theData, GL_STATIC_DRAW);
177 bool isDone = (glGetError() == GL_NO_ERROR); // GL_OUT_OF_MEMORY
182 // =======================================================================
185 // =======================================================================
186 bool OpenGl_VertexBuffer::Init (const Handle(OpenGl_Context)& theGlCtx,
187 const GLuint theComponentsNb,
188 const GLsizei theElemsNb,
189 const GLubyte* theData)
191 if (!Create (theGlCtx))
197 myDataType = GL_UNSIGNED_BYTE;
198 myComponentsNb = theComponentsNb;
199 myElemsNb = theElemsNb;
200 theGlCtx->core15->glBufferData (GetTarget(), GLsizeiptr(myElemsNb) * GLsizeiptr(myComponentsNb) * sizeof(GLubyte), theData, GL_STATIC_DRAW);
201 bool isDone = (glGetError() == GL_NO_ERROR); // GL_OUT_OF_MEMORY
206 // =======================================================================
207 // function : BindVertexAttrib
209 // =======================================================================
210 void OpenGl_VertexBuffer::BindVertexAttrib (const Handle(OpenGl_Context)& theGlCtx,
211 const GLuint theAttribLoc) const
213 if (!IsValid() || theAttribLoc == GLuint (-1))
218 theGlCtx->core20->glEnableVertexAttribArray (theAttribLoc);
219 theGlCtx->core20->glVertexAttribPointer (theAttribLoc, GLint (myComponentsNb), myDataType, GL_FALSE, 0, NULL);
222 // =======================================================================
223 // function : UnbindVertexAttrib
225 // =======================================================================
226 void OpenGl_VertexBuffer::UnbindVertexAttrib (const Handle(OpenGl_Context)& theGlCtx,
227 const GLuint theAttribLoc) const
229 if (!IsValid() || theAttribLoc == GLuint (-1))
233 theGlCtx->core20->glDisableVertexAttribArray (theAttribLoc);
237 // =======================================================================
238 // function : BindFixed
240 // =======================================================================
241 void OpenGl_VertexBuffer::BindFixed (const Handle(OpenGl_Context)& theGlCtx,
242 const GLenum theMode) const
250 glEnableClientState (theMode);
253 case GL_VERTEX_ARRAY:
255 glVertexPointer (static_cast<GLint> (myComponentsNb), myDataType, 0, NULL);
258 case GL_NORMAL_ARRAY:
260 glNormalPointer (myDataType, 0, NULL);
263 case GL_TEXTURE_COORD_ARRAY:
265 glTexCoordPointer (static_cast<GLint> (myComponentsNb), myDataType, 0, NULL);
270 glColorPointer (static_cast<GLint> (myComponentsNb), myDataType, 0, NULL);
277 // =======================================================================
278 // function : UnbindFixed
280 // =======================================================================
281 void OpenGl_VertexBuffer::UnbindFixed (const Handle(OpenGl_Context)& theGlCtx,
282 const GLenum theMode) const
289 glDisableClientState (theMode);