0023418: Crash on the object displaying when running DRAW on remote station. OpenGL...
[occt.git] / src / OpenGl / OpenGl_VertexBuffer.hxx
CommitLineData
5e27df78 1// Created by: Kirill GAVRILOV
2// Copyright (c) 2012 OPEN CASCADE SAS
3//
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.
8//
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.
11//
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.
18
19#ifndef _OpenGl_VertexBuffer_H__
20#define _OpenGl_VertexBuffer_H__
21
22#include <OpenGl_GlCore20.hxx>
23#include <OpenGl_Resource.hxx>
24
25class Handle(OpenGl_Context);
26class OpenGl_Context;
27
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.
30class OpenGl_VertexBuffer : public OpenGl_Resource
31{
32
33public:
34
35 //! Helpful constants
36 static const GLuint NO_BUFFER = 0;
37
38public:
39
40 //! Create uninitialized VBO.
41 Standard_EXPORT OpenGl_VertexBuffer();
42
43 //! Destroy object.
44 Standard_EXPORT virtual ~OpenGl_VertexBuffer();
45
46 Standard_EXPORT virtual GLenum GetTarget() const;
47
48 //! @return true if current object was initialized
49 inline bool IsValid() const
50 {
51 return myBufferId != NO_BUFFER;
52 }
53
54 //! @return the number of components per generic vertex attribute.
55 inline GLuint GetComponentsNb() const
56 {
57 return myComponentsNb;
58 }
59
60 //! @return number of vertex attributes / number of vertices.
61 inline GLsizei GetElemsNb() const
62 {
63 return myElemsNb;
64 }
65
66 //! @return data type of each component in the array.
67 inline GLenum GetDataType() const
68 {
69 return myDataType;
70 }
71
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);
75
76 //! Destroy object - will release GPU memory if any.
77 Standard_EXPORT virtual void Release (const OpenGl_Context* theGlCtx);
78
79 //! Bind this VBO.
80 Standard_EXPORT void Bind (const Handle(OpenGl_Context)& theGlCtx) const;
81
82 //! Unbind this VBO.
83 Standard_EXPORT void Unbind (const Handle(OpenGl_Context)& theGlCtx) const;
84
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);
93
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);
102
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);
111
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);
122
123 //! Bind this VBO to active GLSL program.
124 Standard_EXPORT void BindVertexAttrib (const Handle(OpenGl_Context)& theGlCtx,
125 const GLuint theAttribLoc) const;
126
127 //! Unbind any VBO from active GLSL program.
128 Standard_EXPORT void UnbindVertexAttrib (const Handle(OpenGl_Context)& theGlCtx,
129 const GLuint theAttribLoc) const;
130
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;
136
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;
142
143protected:
144
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.)
149
150public:
151
152 DEFINE_STANDARD_RTTI(OpenGl_VertexBuffer) // Type definition
153
154};
155
156DEFINE_STANDARD_HANDLE(OpenGl_VertexBuffer, OpenGl_Resource)
157
158#endif // _OpenGl_VertexBuffer_H__