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 #ifndef _OpenGl_VertexBuffer_H__
20 #define _OpenGl_VertexBuffer_H__
22 #include <OpenGl_GlCore20.hxx>
23 #include <OpenGl_Resource.hxx>
25 class Handle(OpenGl_Context);
28 //! Vertex Buffer Object - is a general storage object for vertex attributes (position, normal, color).
29 //! Notice that you should use OpenGl_IndexBuffer specialization for array of indices.
30 class OpenGl_VertexBuffer : public OpenGl_Resource
36 static const GLuint NO_BUFFER = 0;
40 //! Create uninitialized VBO.
41 Standard_EXPORT OpenGl_VertexBuffer();
44 Standard_EXPORT virtual ~OpenGl_VertexBuffer();
46 Standard_EXPORT virtual GLenum GetTarget() const;
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.
61 inline GLsizei GetElemsNb() const
66 //! @return data type of each component in the array.
67 inline GLenum GetDataType() const
72 //! Creates VBO name (id) if not yet generated.
73 //! Data should be initialized by another method.
74 Standard_EXPORT bool Create (const Handle(OpenGl_Context)& theGlCtx);
76 //! Destroy object - will release GPU memory if any.
77 Standard_EXPORT virtual void Release (const OpenGl_Context* theGlCtx);
80 Standard_EXPORT void Bind (const Handle(OpenGl_Context)& theGlCtx) const;
83 Standard_EXPORT void Unbind (const Handle(OpenGl_Context)& theGlCtx) const;
85 //! Notice that VBO will be unbound after this call.
86 //! @param theComponentsNb - specifies the number of components per generic vertex attribute; must be 1, 2, 3, or 4;
87 //! @param theElemsNb - elements count;
88 //! @param theData - pointer to GLfloat data (vertices/normals etc.).
89 Standard_EXPORT bool Init (const Handle(OpenGl_Context)& theGlCtx,
90 const GLuint theComponentsNb,
91 const GLsizei theElemsNb,
92 const GLfloat* theData);
94 //! Notice that VBO will be unbound after this call.
95 //! @param theComponentsNb - specifies the number of components per generic vertex attribute; must be 1, 2, 3, or 4;
96 //! @param theElemsNb - elements count;
97 //! @param theData - pointer to GLuint data (indices etc.).
98 Standard_EXPORT bool Init (const Handle(OpenGl_Context)& theGlCtx,
99 const GLuint theComponentsNb,
100 const GLsizei theElemsNb,
101 const GLuint* theData);
103 //! Notice that VBO will be unbound after this call.
104 //! @param theComponentsNb - specifies the number of components per generic vertex attribute; must be 1, 2, 3, or 4;
105 //! @param theElemsNb - elements count;
106 //! @param theData - pointer to GLubyte data (indices/colors etc.).
107 Standard_EXPORT bool Init (const Handle(OpenGl_Context)& theGlCtx,
108 const GLuint theComponentsNb,
109 const GLsizei theElemsNb,
110 const GLubyte* theData);
112 //! Notice that VBO will be unbound after this call.
113 //! Function replaces portion of data within this VBO using glBufferSubData().
114 //! The VBO should be initialized before call.
115 //! @param theElemFrom - element id from which replace buffer data (>=0);
116 //! @param theElemsNb - elements count (theElemFrom + theElemsNb < GetElemsNb());
117 //! @param theData - pointer to GLfloat data.
118 Standard_EXPORT bool SubData (const Handle(OpenGl_Context)& theGlCtx,
119 const GLsizei theElemFrom,
120 const GLsizei theElemsNb,
121 const GLfloat* theData);
123 //! Bind this VBO to active GLSL program.
124 Standard_EXPORT void BindVertexAttrib (const Handle(OpenGl_Context)& theGlCtx,
125 const GLuint theAttribLoc) const;
127 //! Unbind any VBO from active GLSL program.
128 Standard_EXPORT void UnbindVertexAttrib (const Handle(OpenGl_Context)& theGlCtx,
129 const GLuint theAttribLoc) const;
131 //! Bind this VBO as fixed pipeline attribute.
132 //! @param theGlCtx - handle to bound GL context;
133 //! @param theMode - array mode (GL_VERTEX_ARRAY, GL_NORMAL_ARRAY, GL_COLOR_ARRAY, GL_INDEX_ARRAY, GL_TEXTURE_COORD_ARRAY).
134 Standard_EXPORT void BindFixed (const Handle(OpenGl_Context)& theGlCtx,
135 const GLenum theMode) const;
137 //! Unbind this VBO as fixed pipeline attribute.
138 //! @param theGlCtx - handle to bound GL context;
139 //! @param theMode - array mode.
140 Standard_EXPORT void UnbindFixed (const Handle(OpenGl_Context)& theGlCtx,
141 const GLenum theMode) const;
145 GLuint myBufferId; //!< VBO name (index)
146 GLuint myComponentsNb; //!< Number of components per generic vertex attribute, must be 1, 2, 3, or 4
147 GLsizei myElemsNb; //!< Number of vertex attributes / number of vertices
148 GLenum myDataType; //!< Data type (GL_FLOAT, GL_UNSIGNED_INT, GL_UNSIGNED_BYTE etc.)
152 DEFINE_STANDARD_RTTI(OpenGl_VertexBuffer) // Type definition
156 DEFINE_STANDARD_HANDLE(OpenGl_VertexBuffer, OpenGl_Resource)
158 #endif // _OpenGl_VertexBuffer_H__