1 // Created by: Kirill GAVRILOV
2 // Copyright (c) 2013-2014 OPEN CASCADE SAS
4 // This file is part of Open CASCADE Technology software library.
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License version 2.1 as published
8 // by the Free Software Foundation, with special exception defined in the file
9 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10 // distribution for complete text of the license and disclaimer of any warranty.
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
15 #ifndef _OpenGl_VertexBuffer_H__
16 #define _OpenGl_VertexBuffer_H__
18 #include <OpenGl_GlCore20.hxx>
19 #include <OpenGl_Resource.hxx>
20 #include <OpenGl_Context.hxx>
21 #include <OpenGl_ShaderProgram.hxx>
23 #include <Graphic3d_IndexBuffer.hxx>
25 //! Vertex Buffer Object - is a general storage object for vertex attributes (position, normal, color).
26 //! Notice that you should use OpenGl_IndexBuffer specialization for array of indices.
27 class OpenGl_VertexBuffer : public OpenGl_Resource
33 static const GLuint NO_BUFFER = 0;
37 //! Create uninitialized VBO.
38 Standard_EXPORT OpenGl_VertexBuffer();
41 Standard_EXPORT virtual ~OpenGl_VertexBuffer();
43 Standard_EXPORT virtual GLenum GetTarget() const;
45 //! Return TRUE if this is a virtual (for backward compatibility) VBO object.
46 virtual bool IsVirtual() const { return false; }
48 //! @return true if current object was initialized
49 inline bool IsValid() const
51 return myBufferId != NO_BUFFER;
54 //! @return the number of components per generic vertex attribute.
55 inline GLuint GetComponentsNb() const
57 return myComponentsNb;
60 //! @return number of vertex attributes / number of vertices specified within ::Init()
61 inline GLsizei GetElemsNb() const
66 //! Overrides the number of vertex attributes / number of vertexes.
67 //! It is up to user specifying this number correct (e.g. below initial value)!
68 void SetElemsNb (GLsizei theNbElems) { myElemsNb = theNbElems; }
70 //! @return data type of each component in the array.
71 inline GLenum GetDataType() const
76 //! @return offset to data, NULL by default
77 inline GLubyte* GetDataOffset() const
82 //! Creates VBO name (id) if not yet generated.
83 //! Data should be initialized by another method.
84 Standard_EXPORT virtual bool Create (const Handle(OpenGl_Context)& theGlCtx);
86 //! Destroy object - will release GPU memory if any.
87 Standard_EXPORT virtual void Release (OpenGl_Context* theGlCtx) Standard_OVERRIDE;
90 Standard_EXPORT virtual void Bind (const Handle(OpenGl_Context)& theGlCtx) const;
93 Standard_EXPORT virtual void Unbind (const Handle(OpenGl_Context)& theGlCtx) const;
95 //! Notice that VBO will be unbound after this call.
96 //! @param theComponentsNb - specifies the number of components per generic vertex attribute; must be 1, 2, 3, or 4;
97 //! @param theElemsNb - elements count;
98 //! @param theData - pointer to GLfloat data (vertices/normals etc.).
99 bool Init (const Handle(OpenGl_Context)& theGlCtx,
100 const GLuint theComponentsNb,
101 const GLsizei theElemsNb,
102 const GLfloat* theData)
104 return init (theGlCtx, theComponentsNb, theElemsNb, theData, GL_FLOAT);
107 //! Notice that VBO will be unbound after this call.
108 //! @param theComponentsNb - specifies the number of components per generic vertex attribute; must be 1, 2, 3, or 4;
109 //! @param theElemsNb - elements count;
110 //! @param theData - pointer to GLuint data (indices etc.).
111 bool Init (const Handle(OpenGl_Context)& theGlCtx,
112 const GLuint theComponentsNb,
113 const GLsizei theElemsNb,
114 const GLuint* theData)
116 return init (theGlCtx, theComponentsNb, theElemsNb, theData, GL_UNSIGNED_INT);
119 //! Notice that VBO will be unbound after this call.
120 //! @param theComponentsNb - specifies the number of components per generic vertex attribute; must be 1, 2, 3, or 4;
121 //! @param theElemsNb - elements count;
122 //! @param theData - pointer to GLushort data (indices etc.).
123 bool Init (const Handle(OpenGl_Context)& theGlCtx,
124 const GLuint theComponentsNb,
125 const GLsizei theElemsNb,
126 const GLushort* theData)
128 return init (theGlCtx, theComponentsNb, theElemsNb, theData, GL_UNSIGNED_SHORT);
131 //! Notice that VBO will be unbound after this call.
132 //! @param theComponentsNb - specifies the number of components per generic vertex attribute; must be 1, 2, 3, or 4;
133 //! @param theElemsNb - elements count;
134 //! @param theData - pointer to GLubyte data (indices/colors etc.).
135 bool Init (const Handle(OpenGl_Context)& theGlCtx,
136 const GLuint theComponentsNb,
137 const GLsizei theElemsNb,
138 const GLubyte* theData)
140 return init (theGlCtx, theComponentsNb, theElemsNb, theData, GL_UNSIGNED_BYTE);
143 //! Notice that VBO will be unbound after this call.
144 //! Function replaces portion of data within this VBO using glBufferSubData().
145 //! The VBO should be initialized before call.
146 //! @param theElemFrom - element id from which replace buffer data (>=0);
147 //! @param theElemsNb - elements count (theElemFrom + theElemsNb <= GetElemsNb());
148 //! @param theData - pointer to GLfloat data.
149 bool SubData (const Handle(OpenGl_Context)& theGlCtx,
150 const GLsizei theElemFrom,
151 const GLsizei theElemsNb,
152 const GLfloat* theData)
154 return subData (theGlCtx, theElemFrom, theElemsNb, theData, GL_FLOAT);
157 //! Notice that VBO will be unbound after this call.
158 //! Function replaces portion of data within this VBO using glBufferSubData().
159 //! The VBO should be initialized before call.
160 //! @param theElemFrom element id from which replace buffer data (>=0);
161 //! @param theElemsNb elements count (theElemFrom + theElemsNb <= GetElemsNb());
162 //! @param theData pointer to GLuint data.
163 bool SubData (const Handle(OpenGl_Context)& theGlCtx,
164 const GLsizei theElemFrom,
165 const GLsizei theElemsNb,
166 const GLuint* theData)
168 return subData (theGlCtx, theElemFrom, theElemsNb, theData, GL_UNSIGNED_INT);
171 //! Notice that VBO will be unbound after this call.
172 //! Function replaces portion of data within this VBO using glBufferSubData().
173 //! The VBO should be initialized before call.
174 //! @param theElemFrom element id from which replace buffer data (>=0);
175 //! @param theElemsNb elements count (theElemFrom + theElemsNb <= GetElemsNb());
176 //! @param theData pointer to GLushort data.
177 bool SubData (const Handle(OpenGl_Context)& theGlCtx,
178 const GLsizei theElemFrom,
179 const GLsizei theElemsNb,
180 const GLushort* theData)
182 return subData (theGlCtx, theElemFrom, theElemsNb, theData, GL_UNSIGNED_SHORT);
185 //! Notice that VBO will be unbound after this call.
186 //! Function replaces portion of data within this VBO using glBufferSubData().
187 //! The VBO should be initialized before call.
188 //! @param theElemFrom element id from which replace buffer data (>=0);
189 //! @param theElemsNb elements count (theElemFrom + theElemsNb <= GetElemsNb());
190 //! @param theData pointer to GLubyte data.
191 bool SubData (const Handle(OpenGl_Context)& theGlCtx,
192 const GLsizei theElemFrom,
193 const GLsizei theElemsNb,
194 const GLubyte* theData)
196 return subData (theGlCtx, theElemFrom, theElemsNb, theData, GL_UNSIGNED_BYTE);
199 //! Bind this VBO to active GLSL program.
200 Standard_EXPORT void BindVertexAttrib (const Handle(OpenGl_Context)& theGlCtx,
201 const GLuint theAttribLoc) const;
203 //! Unbind any VBO from active GLSL program.
204 Standard_EXPORT void UnbindVertexAttrib (const Handle(OpenGl_Context)& theGlCtx,
205 const GLuint theAttribLoc) const;
207 //! Bind this VBO and enable specified attribute in OpenGl_Context::ActiveProgram() or FFP.
208 //! @param theGlCtx - handle to bound GL context;
209 //! @param theMode - array mode (GL_VERTEX_ARRAY, GL_NORMAL_ARRAY, GL_COLOR_ARRAY, GL_INDEX_ARRAY, GL_TEXTURE_COORD_ARRAY).
210 void BindAttribute (const Handle(OpenGl_Context)& theCtx,
211 const Graphic3d_TypeOfAttribute theMode) const
216 bindAttribute (theCtx, theMode, static_cast<GLint> (myComponentsNb), myDataType, 0, myOffset);
220 //! Unbind this VBO and disable specified attribute in OpenGl_Context::ActiveProgram() or FFP.
221 //! @param theCtx handle to bound GL context
222 //! @param theMode array mode
223 void UnbindAttribute (const Handle(OpenGl_Context)& theCtx,
224 const Graphic3d_TypeOfAttribute theMode) const
229 unbindAttribute (theCtx, theMode);
233 public: //! @name advanced methods
235 //! Returns estimated GPU memory usage for holding data without considering overheads and allocation alignment rules.
236 virtual Standard_Size EstimatedDataSize() const Standard_OVERRIDE
239 ? sizeOfGlType (myDataType) * myComponentsNb * myElemsNb
243 //! @return size of specified GL type
244 static size_t sizeOfGlType (const GLenum theType)
249 case GL_UNSIGNED_BYTE: return sizeof(GLubyte);
251 case GL_UNSIGNED_SHORT: return sizeof(GLushort);
255 case GL_UNSIGNED_INT: return sizeof(GLuint);
256 case GL_FLOAT: return sizeof(GLfloat);
258 case GL_DOUBLE: return sizeof(GLdouble);
264 //! Initialize buffer with new data.
265 Standard_EXPORT virtual bool init (const Handle(OpenGl_Context)& theGlCtx,
266 const GLuint theComponentsNb,
267 const GLsizei theElemsNb,
269 const GLenum theDataType,
270 const GLsizei theStride);
272 //! Initialize buffer with new data.
273 bool init (const Handle(OpenGl_Context)& theGlCtx,
274 const GLuint theComponentsNb,
275 const GLsizei theElemsNb,
277 const GLenum theDataType)
279 return init (theGlCtx, theComponentsNb, theElemsNb, theData, theDataType, GLsizei(theComponentsNb) * GLsizei(sizeOfGlType (theDataType)));
282 //! Update part of the buffer with new data.
283 Standard_EXPORT virtual bool subData (const Handle(OpenGl_Context)& theGlCtx,
284 const GLsizei theElemFrom,
285 const GLsizei theElemsNb,
287 const GLenum theDataType);
289 //! Setup array pointer - either for active GLSL program OpenGl_Context::ActiveProgram()
290 //! or for FFP using bindFixed() when no program bound.
291 static void bindAttribute (const Handle(OpenGl_Context)& theGlCtx,
292 const Graphic3d_TypeOfAttribute theMode,
293 const GLint theNbComp,
294 const GLenum theDataType,
295 const GLsizei theStride,
296 const GLvoid* theOffset);
298 //! Disable GLSL array pointer - either for active GLSL program OpenGl_Context::ActiveProgram()
299 //! or for FFP using unbindFixed() when no program bound.
300 static void unbindAttribute (const Handle(OpenGl_Context)& theGlCtx,
301 const Graphic3d_TypeOfAttribute theMode);
304 #if !defined(GL_ES_VERSION_2_0)
305 //! Setup FFP array pointer.
306 static void bindFixed (const Handle(OpenGl_Context)& theGlCtx,
307 const Graphic3d_TypeOfAttribute theMode,
308 const GLint theNbComp,
309 const GLenum theDataType,
310 const GLsizei theStride,
311 const GLvoid* theOffset);
313 //! Disable FFP array pointer.
314 static void unbindFixed (const Handle(OpenGl_Context)& theGlCtx,
315 const Graphic3d_TypeOfAttribute theMode);
317 //! Disable FFP color array pointer.
318 Standard_EXPORT static void unbindFixedColor (const Handle(OpenGl_Context)& theCtx);
321 public: //! @name methods for interleaved attributes array
323 //! @return true if buffer contains per-vertex color attribute
324 Standard_EXPORT virtual bool HasColorAttribute() const;
326 //! @return true if buffer contains per-vertex normal attribute
327 Standard_EXPORT virtual bool HasNormalAttribute() const;
329 //! Bind all vertex attributes to active program OpenGl_Context::ActiveProgram() or for FFP.
330 //! Default implementation does nothing.
331 Standard_EXPORT virtual void BindAllAttributes (const Handle(OpenGl_Context)& theGlCtx) const;
333 //! Bind vertex position attribute only. Default implementation does nothing.
334 Standard_EXPORT virtual void BindPositionAttribute (const Handle(OpenGl_Context)& theGlCtx) const;
336 //! Unbind all vertex attributes. Default implementation does nothing.
337 Standard_EXPORT virtual void UnbindAllAttributes (const Handle(OpenGl_Context)& theGlCtx) const;
339 //! Dumps the content of me into the stream
340 Standard_EXPORT virtual void DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth = -1) const Standard_OVERRIDE;
344 GLubyte* myOffset; //!< offset to data
345 GLuint myBufferId; //!< VBO name (index)
346 GLuint myComponentsNb; //!< Number of components per generic vertex attribute, must be 1, 2, 3, or 4
347 GLsizei myElemsNb; //!< Number of vertex attributes / number of vertices
348 GLenum myDataType; //!< Data type (GL_FLOAT, GL_UNSIGNED_INT, GL_UNSIGNED_BYTE etc.)
352 DEFINE_STANDARD_RTTIEXT(OpenGl_VertexBuffer,OpenGl_Resource) // Type definition
356 DEFINE_STANDARD_HANDLE(OpenGl_VertexBuffer, OpenGl_Resource)
358 #include <OpenGl_VertexBuffer.lxx>
360 #endif // _OpenGl_VertexBuffer_H__