0030748: Visualization - Marker displayed in immediate layer ruins QT Quick view...
[occt.git] / src / OpenGl / OpenGl_VertexBuffer.hxx
CommitLineData
5e27df78 1// Created by: Kirill GAVRILOV
d5f74e42 2// Copyright (c) 2013-2014 OPEN CASCADE SAS
5e27df78 3//
973c2be1 4// This file is part of Open CASCADE Technology software library.
5e27df78 5//
d5f74e42 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
973c2be1 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.
5e27df78 11//
973c2be1 12// Alternatively, this file may be used under the terms of Open CASCADE
13// commercial license or contractual agreement.
5e27df78 14
15#ifndef _OpenGl_VertexBuffer_H__
16#define _OpenGl_VertexBuffer_H__
17
18#include <OpenGl_GlCore20.hxx>
19#include <OpenGl_Resource.hxx>
871fa103 20#include <OpenGl_Context.hxx>
7d3e64ef 21#include <OpenGl_ShaderProgram.hxx>
5e27df78 22
871fa103 23#include <Graphic3d_IndexBuffer.hxx>
5e27df78 24
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.
27class OpenGl_VertexBuffer : public OpenGl_Resource
28{
29
30public:
31
32 //! Helpful constants
33 static const GLuint NO_BUFFER = 0;
34
35public:
36
37 //! Create uninitialized VBO.
38 Standard_EXPORT OpenGl_VertexBuffer();
39
40 //! Destroy object.
41 Standard_EXPORT virtual ~OpenGl_VertexBuffer();
42
43 Standard_EXPORT virtual GLenum GetTarget() const;
44
da87ddc3 45 //! Return TRUE if this is a virtual (for backward compatibility) VBO object.
46 virtual bool IsVirtual() const { return false; }
47
5e27df78 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
da87ddc3 60 //! @return number of vertex attributes / number of vertices specified within ::Init()
5e27df78 61 inline GLsizei GetElemsNb() const
62 {
63 return myElemsNb;
64 }
65
da87ddc3 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; }
69
5e27df78 70 //! @return data type of each component in the array.
71 inline GLenum GetDataType() const
72 {
73 return myDataType;
74 }
75
7d3e64ef 76 //! @return offset to data, NULL by default
77 inline GLubyte* GetDataOffset() const
78 {
79 return myOffset;
80 }
81
5e27df78 82 //! Creates VBO name (id) if not yet generated.
83 //! Data should be initialized by another method.
7d3e64ef 84 Standard_EXPORT virtual bool Create (const Handle(OpenGl_Context)& theGlCtx);
5e27df78 85
86 //! Destroy object - will release GPU memory if any.
79104795 87 Standard_EXPORT virtual void Release (OpenGl_Context* theGlCtx) Standard_OVERRIDE;
5e27df78 88
89 //! Bind this VBO.
7d3e64ef 90 Standard_EXPORT virtual void Bind (const Handle(OpenGl_Context)& theGlCtx) const;
5e27df78 91
92 //! Unbind this VBO.
7d3e64ef 93 Standard_EXPORT virtual void Unbind (const Handle(OpenGl_Context)& theGlCtx) const;
5e27df78 94
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.).
871fa103 99 bool Init (const Handle(OpenGl_Context)& theGlCtx,
100 const GLuint theComponentsNb,
101 const GLsizei theElemsNb,
102 const GLfloat* theData)
103 {
104 return init (theGlCtx, theComponentsNb, theElemsNb, theData, GL_FLOAT);
105 }
5e27df78 106
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.).
871fa103 111 bool Init (const Handle(OpenGl_Context)& theGlCtx,
112 const GLuint theComponentsNb,
113 const GLsizei theElemsNb,
114 const GLuint* theData)
115 {
116 return init (theGlCtx, theComponentsNb, theElemsNb, theData, GL_UNSIGNED_INT);
117 }
118
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)
127 {
128 return init (theGlCtx, theComponentsNb, theElemsNb, theData, GL_UNSIGNED_SHORT);
129 }
5e27df78 130
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.).
871fa103 135 bool Init (const Handle(OpenGl_Context)& theGlCtx,
136 const GLuint theComponentsNb,
137 const GLsizei theElemsNb,
138 const GLubyte* theData)
139 {
140 return init (theGlCtx, theComponentsNb, theElemsNb, theData, GL_UNSIGNED_BYTE);
141 }
5e27df78 142
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);
99d99a6d 147 //! @param theElemsNb - elements count (theElemFrom + theElemsNb <= GetElemsNb());
5e27df78 148 //! @param theData - pointer to GLfloat data.
871fa103 149 bool SubData (const Handle(OpenGl_Context)& theGlCtx,
150 const GLsizei theElemFrom,
151 const GLsizei theElemsNb,
152 const GLfloat* theData)
153 {
154 return subData (theGlCtx, theElemFrom, theElemsNb, theData, GL_FLOAT);
155 }
5e27df78 156
99d99a6d 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.
871fa103 163 bool SubData (const Handle(OpenGl_Context)& theGlCtx,
164 const GLsizei theElemFrom,
165 const GLsizei theElemsNb,
166 const GLuint* theData)
167 {
168 return subData (theGlCtx, theElemFrom, theElemsNb, theData, GL_UNSIGNED_INT);
169 }
170
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)
181 {
182 return subData (theGlCtx, theElemFrom, theElemsNb, theData, GL_UNSIGNED_SHORT);
183 }
99d99a6d 184
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.
871fa103 191 bool SubData (const Handle(OpenGl_Context)& theGlCtx,
192 const GLsizei theElemFrom,
193 const GLsizei theElemsNb,
194 const GLubyte* theData)
195 {
196 return subData (theGlCtx, theElemFrom, theElemsNb, theData, GL_UNSIGNED_BYTE);
197 }
99d99a6d 198
5e27df78 199 //! Bind this VBO to active GLSL program.
200 Standard_EXPORT void BindVertexAttrib (const Handle(OpenGl_Context)& theGlCtx,
201 const GLuint theAttribLoc) const;
202
203 //! Unbind any VBO from active GLSL program.
204 Standard_EXPORT void UnbindVertexAttrib (const Handle(OpenGl_Context)& theGlCtx,
205 const GLuint theAttribLoc) const;
206
7d3e64ef 207 //! Bind this VBO and enable specified attribute in OpenGl_Context::ActiveProgram() or FFP.
5e27df78 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).
7d3e64ef 210 void BindAttribute (const Handle(OpenGl_Context)& theCtx,
211 const Graphic3d_TypeOfAttribute theMode) const
212 {
213 if (IsValid())
214 {
215 Bind (theCtx);
216 bindAttribute (theCtx, theMode, static_cast<GLint> (myComponentsNb), myDataType, 0, myOffset);
217 }
218 }
5e27df78 219
7d3e64ef 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
225 {
226 if (IsValid())
227 {
228 Unbind (theCtx);
229 unbindAttribute (theCtx, theMode);
230 }
231 }
5e27df78 232
871fa103 233public: //! @name advanced methods
234
15669413 235 //! Returns estimated GPU memory usage for holding data without considering overheads and allocation alignment rules.
236 virtual Standard_Size EstimatedDataSize() const Standard_OVERRIDE
237 {
238 return IsValid()
239 ? sizeOfGlType (myDataType) * myComponentsNb * myElemsNb
240 : 0;
241 }
242
871fa103 243 //! @return size of specified GL type
244 static size_t sizeOfGlType (const GLenum theType)
245 {
246 switch (theType)
247 {
248 case GL_BYTE:
249 case GL_UNSIGNED_BYTE: return sizeof(GLubyte);
250 case GL_SHORT:
251 case GL_UNSIGNED_SHORT: return sizeof(GLushort);
ca3c13d1 252 #ifdef GL_INT
871fa103 253 case GL_INT:
ca3c13d1 254 #endif
871fa103 255 case GL_UNSIGNED_INT: return sizeof(GLuint);
256 case GL_FLOAT: return sizeof(GLfloat);
ca3c13d1 257 #ifdef GL_DOUBLE
871fa103 258 case GL_DOUBLE: return sizeof(GLdouble);
ca3c13d1 259 #endif
871fa103 260 default: return 0;
261 }
262 }
263
264 //! Initialize buffer with new data.
7d3e64ef 265 Standard_EXPORT virtual bool init (const Handle(OpenGl_Context)& theGlCtx,
266 const GLuint theComponentsNb,
267 const GLsizei theElemsNb,
268 const void* theData,
269 const GLenum theDataType,
270 const GLsizei theStride);
871fa103 271
272 //! Initialize buffer with new data.
273 bool init (const Handle(OpenGl_Context)& theGlCtx,
274 const GLuint theComponentsNb,
275 const GLsizei theElemsNb,
276 const void* theData,
277 const GLenum theDataType)
278 {
279 return init (theGlCtx, theComponentsNb, theElemsNb, theData, theDataType, GLsizei(theComponentsNb) * GLsizei(sizeOfGlType (theDataType)));
280 }
281
282 //! Update part of the buffer with new data.
7d3e64ef 283 Standard_EXPORT virtual bool subData (const Handle(OpenGl_Context)& theGlCtx,
284 const GLsizei theElemFrom,
285 const GLsizei theElemsNb,
286 const void* theData,
287 const GLenum theDataType);
288
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);
297
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);
302
303private:
ca3c13d1 304#if !defined(GL_ES_VERSION_2_0)
871fa103 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,
7d3e64ef 311 const GLvoid* theOffset);
871fa103 312
313 //! Disable FFP array pointer.
314 static void unbindFixed (const Handle(OpenGl_Context)& theGlCtx,
7d3e64ef 315 const Graphic3d_TypeOfAttribute theMode);
8613985b 316
317 //! Disable FFP color array pointer.
f114566d 318 Standard_EXPORT static void unbindFixedColor (const Handle(OpenGl_Context)& theCtx);
8613985b 319
ca3c13d1 320#endif
871fa103 321public: //! @name methods for interleaved attributes array
322
7d3e64ef 323 //! @return true if buffer contains per-vertex color attribute
324 Standard_EXPORT virtual bool HasColorAttribute() const;
325
326 //! @return true if buffer contains per-vertex normal attribute
327 Standard_EXPORT virtual bool HasNormalAttribute() const;
871fa103 328
7d3e64ef 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;
871fa103 332
7d3e64ef 333 //! Bind vertex position attribute only. Default implementation does nothing.
334 Standard_EXPORT virtual void BindPositionAttribute (const Handle(OpenGl_Context)& theGlCtx) const;
871fa103 335
7d3e64ef 336 //! Unbind all vertex attributes. Default implementation does nothing.
337 Standard_EXPORT virtual void UnbindAllAttributes (const Handle(OpenGl_Context)& theGlCtx) const;
871fa103 338
5e27df78 339protected:
340
7d3e64ef 341 GLubyte* myOffset; //!< offset to data
342 GLuint myBufferId; //!< VBO name (index)
343 GLuint myComponentsNb; //!< Number of components per generic vertex attribute, must be 1, 2, 3, or 4
344 GLsizei myElemsNb; //!< Number of vertex attributes / number of vertices
345 GLenum myDataType; //!< Data type (GL_FLOAT, GL_UNSIGNED_INT, GL_UNSIGNED_BYTE etc.)
5e27df78 346
347public:
348
92efcf78 349 DEFINE_STANDARD_RTTIEXT(OpenGl_VertexBuffer,OpenGl_Resource) // Type definition
5e27df78 350
351};
352
353DEFINE_STANDARD_HANDLE(OpenGl_VertexBuffer, OpenGl_Resource)
354
7d3e64ef 355#include <OpenGl_VertexBuffer.lxx>
356
5e27df78 357#endif // _OpenGl_VertexBuffer_H__