SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SLGLVertexArray Class Reference

SLGLVertexArray encapsulates the core OpenGL drawing. More...

#include <SLGLVertexArray.h>

Inheritance diagram for SLGLVertexArray:
[legend]

Public Member Functions

 SLGLVertexArray ()
 
 ~SLGLVertexArray ()
 
void deleteGL ()
 Deletes all vertex array & vertex buffer objects. More...
 
void clearAttribs ()
 Clears the attribute definition. More...
 
SLuint vaoID () const
 Returns either the VAO id or the VBO id. More...
 
SLuint tfoID () const
 Returns the TFO id. More...
 
void setAttrib (SLGLAttributeType type, SLint elementSize, SLint location, void *dataPointer, SLGLBufferType dataType=BT_float)
 Adds a vertex attribute with data pointer and an element size. More...
 
void setAttrib (SLGLAttributeType type, SLint location, SLVuint *data)
 Adds a vertex attribute with vector of SLuint. More...
 
void setAttrib (SLGLAttributeType type, SLint location, SLVfloat *data)
 Adds a vertex attribute with vector of SLfloat. More...
 
void setAttrib (SLGLAttributeType type, SLint location, SLVVec2f *data)
 Adds a vertex attribute with vector of SLVec2f. More...
 
void setAttrib (SLGLAttributeType type, SLint location, SLVVec3f *data)
 Adds a vertex attribute with vector of SLVec3f. More...
 
void setAttrib (SLGLAttributeType type, SLint location, SLVVec4f *data)
 Adds a vertex attribute with vector of SLVec4f. More...
 
void setAttrib (SLGLAttributeType type, SLint location, SLVVec4i *data)
 Adds a vertex attribute with vector of SLVec4i. More...
 
void setIndices (SLuint numIndicesElements, SLGLBufferType indexDataType, void *indexDataElements, SLuint numIndicesEdges=0, void *indexDataEdges=nullptr)
 Adds the index array for indexed element drawing. More...
 
void setIndices (SLVubyte *indicesElements, SLVubyte *indicesEdges=nullptr)
 Adds the index array for indexed element drawing with a vector of ubyte. More...
 
void setIndices (SLVushort *indicesElements, SLVushort *indicesEdges=nullptr)
 Adds the index array for indexed element drawing with a vector of ushort. More...
 
void setIndices (SLVuint *indicesElements, SLVuint *indicesEdges=nullptr)
 Adds the index array for indexed element drawing with a vector of uint. More...
 
void setInstanceVBO (SLGLVertexBuffer *vbo, SLuint divisor=0)
 Attach a VBO that has been created outside of this VAO. More...
 
void updateAttrib (SLGLAttributeType type, SLint elementSize, void *dataPointer)
 Updates a specific vertex attribute in the VBO. More...
 
void updateAttrib (SLGLAttributeType type, SLVuint *data)
 Updates a specific vertex attribute in the VBO. More...
 
void updateAttrib (SLGLAttributeType type, SLVfloat *data)
 Updates a specific vertex attribute in the VBO. More...
 
void updateAttrib (SLGLAttributeType type, SLVVec2f *data)
 Updates a specific vertex attribute in the VBO. More...
 
void updateAttrib (SLGLAttributeType type, SLVVec3f *data)
 Updates a specific vertex attribute in the VBO. More...
 
void updateAttrib (SLGLAttributeType type, SLVVec4f *data)
 Updates a specific vertex attribute in the VBO. More...
 
void generate (SLuint numVertices, SLGLBufferUsage usage=BU_static, SLbool outputInterleaved=true, SLuint divisor=0)
 Generates the VA & VB objects for a NO. of vertices. More...
 
void generateTF (SLuint numVertices, SLGLBufferUsage usage=BU_static, SLbool outputInterleaved=true, SLuint divisor=0)
 Generates the VA & VB & TF objects. More...
 
void beginTF (SLuint tfoID)
 Begin transform feedback. More...
 
void endTF ()
 End transform feedback. More...
 
void drawElementsAs (SLGLPrimitiveType primitiveType, SLuint numIndexes=0, SLuint indexOffsetBytes=0)
 Draws the VAO by element indices with a primitive type. More...
 
void drawArrayAs (SLGLPrimitiveType primitiveType, SLint firstVertex=0, SLsizei countVertices=0)
 Draws the VAO as an array with a primitive type. More...
 
void drawElementsInstanced (SLGLPrimitiveType primitiveType, SLuint countInstance=0, SLuint numIndexes=0, SLuint indexOffset=0)
 Draws the VAO as an array with instance primitive type. More...
 
void drawEdges (SLCol4f color, SLfloat lineWidth=1.0f)
 Draws the hard edges of the VAO with the edge indices. More...
 
SLuint numVertices () const
 
SLuint numIndicesElements () const
 
SLuint numIndicesEdges () const
 
SLGLVertexBuffervbo ()
 

Static Public Attributes

static SLuint totalDrawCalls = 0
 
static SLuint totalPrimitivesRendered = 0
 static total no. of draw calls More...
 

Protected Attributes

SLuint _instances
 static total no. of primitives rendered More...
 
SLuint _vaoID
 Number of instances of drawing. More...
 
SLuint _tfoID
 OpenGL id of vertex array object. More...
 
SLuint _numVertices
 OpenGL id of transform feedback object. More...
 
SLGLVertexBuffer _vbo
 NO. of vertices in array. More...
 
SLuint _idVBOIndices
 Vertex buffer object for float attributes. More...
 
size_t _numIndicesElements
 OpenGL id of index vbo. More...
 
void_indexDataElements
 NO. of vertex indices in array for triangles, lines or points. More...
 
size_t _numIndicesEdges
 Pointer to index data for elements. More...
 
void_indexDataEdges
 NO. of vertex indices in array for hard edges. More...
 
SLGLBufferType _indexDataType
 Pointer to index data for hard edges. More...
 
SLGLVertexBuffer_instanceVbo
 index data type (ubyte, ushort, uint) More...
 
SLuint _instanceDivisor
 Vertex buffer object containing the positions for instanced drawing. More...
 

Detailed Description

SLGLVertexArray encapsulates the core OpenGL drawing.

An SLGLVertexArray instance handles all OpenGL drawing with an OpenGL Vertex Array Object (VAO), a vertex buffer objects (VBO) for the attributes and an index buffer for element drawing. Attributes can be stored in a float VBO of type SLGLVertexBuffer.
VAOs where introduces OpenGL 3.0 and reduce the overhead per draw call. All vertex attributes (e.g. position, normals, texture coords, etc.) must be float at the input. All float attributes will be in one VBO (_vbo). Vertices can be drawn either directly as in the array (SLGLVertexArray::drawArrayAs) or by element (SLGLVertexArray::drawElementsAs) with a separate indices buffer.
The setup of a VAO has multiple steps:

Definition at line 39 of file SLGLVertexArray.h.

Constructor & Destructor Documentation

◆ SLGLVertexArray()

SLGLVertexArray::SLGLVertexArray ( )

Constructor initializing with default values

Definition at line 22 of file SLGLVertexArray.cpp.

23 {
24  _vaoID = 0;
25  _vbo.clear();
26  _idVBOIndices = 0;
28  _numIndicesEdges = 0;
29  _numVertices = 0;
30  _indexDataElements = nullptr;
31  _indexDataEdges = nullptr;
32  _instanceVbo = nullptr;
33 }
SLGLVertexBuffer _vbo
NO. of vertices in array.
size_t _numIndicesEdges
Pointer to index data for elements.
SLGLVertexBuffer * _instanceVbo
index data type (ubyte, ushort, uint)
SLuint _idVBOIndices
Vertex buffer object for float attributes.
void * _indexDataEdges
NO. of vertex indices in array for hard edges.
size_t _numIndicesElements
OpenGL id of index vbo.
SLuint _vaoID
Number of instances of drawing.
SLuint _numVertices
OpenGL id of transform feedback object.
void * _indexDataElements
NO. of vertex indices in array for triangles, lines or points.
void clear()
Calls deleteGL & clears the attributes.

◆ ~SLGLVertexArray()

SLGLVertexArray::~SLGLVertexArray ( )
inline

Definition at line 43 of file SLGLVertexArray.h.

43 { deleteGL(); }
void deleteGL()
Deletes all vertex array & vertex buffer objects.

Member Function Documentation

◆ beginTF()

void SLGLVertexArray::beginTF ( SLuint  tfoID)

Begin transform feedback.

Discard the rendering because we just compute next position with the transform feedback. We need to bind a transform feedback object but not the same from this vao, because we want to read from one vao and write on another.

Definition at line 364 of file SLGLVertexArray.cpp.

365 {
366  // Disable rendering
367  glEnable(GL_RASTERIZER_DISCARD);
368 
369  // Bind the feedback object for the buffers to be drawn next
370  glBindTransformFeedback(GL_TRANSFORM_FEEDBACK, tfoID);
371 
372  // Draw points from input buffer with transform feedback
373  glBeginTransformFeedback(GL_POINTS);
374 }
SLuint tfoID() const
Returns the TFO id.

◆ clearAttribs()

void SLGLVertexArray::clearAttribs ( )
inline

Clears the attribute definition.

Definition at line 49 of file SLGLVertexArray.h.

50  {
51  deleteGL();
52  _vbo.clear();
53  }

◆ deleteGL()

void SLGLVertexArray::deleteGL ( )

Deletes all vertex array & vertex buffer objects.

Deletes the OpenGL objects for the vertex array and the vertex buffer. The vector _attribs with the attribute information is not cleared.

Definition at line 38 of file SLGLVertexArray.cpp.

39 {
40  if (_vaoID)
41  glDeleteVertexArrays(1, &_vaoID);
42  _vaoID = 0;
43 
44  if (_vbo.id())
45  _vbo.clear();
46 
47  if (_idVBOIndices)
48  {
49  glDeleteBuffers(1, &_idVBOIndices);
50  _idVBOIndices = 0;
54  }
55 }
unsigned int SLuint
Definition: SL.h:171
SLGLBufferType _indexDataType
Pointer to index data for hard edges.
static SLuint totalBufferSize
static total no. of buffers in use
static SLuint totalBufferCount
SLuint id() const
static SLuint sizeOfType(SLGLBufferType type)
static total size of all buffers in bytes

◆ drawArrayAs()

void SLGLVertexArray::drawArrayAs ( SLGLPrimitiveType  primitiveType,
SLint  firstVertex = 0,
SLsizei  countVertices = 0 
)

Draws the VAO as an array with a primitive type.

Draws the vertex attributes as a specified primitive type as the vertices are defined in the attribute arrays.

Definition at line 442 of file SLGLVertexArray.cpp.

445 {
446  assert((_vbo.id()) && "No VBO generated for VAO.");
447 
448  glBindVertexArray(_vaoID);
449 
450  if (countVertices == 0)
451  countVertices = (SLsizei)_numVertices;
452 
453  //////////////////////////////////
454  glDrawArrays(primitiveType,
455  firstVertex,
456  countVertices);
457  //////////////////////////////////
458 
459  // Update statistics
460  totalDrawCalls++;
461  SLint numVertices = countVertices - firstVertex;
462  switch (primitiveType)
463  {
464  case PT_triangles:
466  break;
467  case PT_lines:
469  break;
470  case PT_points:
472  break;
473  default: break;
474  }
475 
476  glBindVertexArray(0);
477 
478  GET_GL_ERROR;
479 }
int SLsizei
Definition: SL.h:172
int SLint
Definition: SL.h:170
@ PT_points
Definition: SLGLEnums.h:31
@ PT_lines
Definition: SLGLEnums.h:32
@ PT_triangles
Definition: SLGLEnums.h:35
#define GET_GL_ERROR
Definition: SLGLState.h:56
SLuint numVertices() const
static SLuint totalDrawCalls
static SLuint totalPrimitivesRendered
static total no. of draw calls

◆ drawEdges()

void SLGLVertexArray::drawEdges ( SLCol4f  color,
SLfloat  lineWidth = 1.0f 
)

Draws the hard edges of the VAO with the edge indices.

Draws the hard edges with the specified color. The VAO has no or one active index buffer. For drawArrayAs no indices are needed. For drawElementsAs the index buffer is used. For triangle meshes also hard edges are generated. Their indices are stored behind the indices of the triangles.

Definition at line 540 of file SLGLVertexArray.cpp.

542 {
543  if (!_vbo.id())
544  SL_EXIT_MSG("No VBO generated for VAO in drawArrayAsColored.");
545 
546  // Prepare shader
548  SLGLState* stateGL = SLGLState::instance();
549  sp->useProgram();
550  sp->uniformMatrix4fv("u_mMatrix", 1, (SLfloat*)&stateGL->modelMatrix);
551  sp->uniformMatrix4fv("u_vMatrix", 1, (SLfloat*)&stateGL->viewMatrix);
552  sp->uniformMatrix4fv("u_pMatrix", 1, (SLfloat*)&stateGL->projectionMatrix);
553  sp->uniform1f("u_oneOverGamma", 1.0f);
554  stateGL->currentMaterial(nullptr);
555 
556  // Set uniform color
557  glUniform4fv(sp->getUniformLocation("u_matDiff"), 1, (SLfloat*)&color);
558 
559 #if not defined(SL_GLES) && not defined(SL_EMSCRIPTEN)
560  if (lineWidth != 1.0f)
561  glLineWidth(lineWidth);
562 #endif
563 
564  //////////////////////////////////////////////////////
568  //////////////////////////////////////////////////////
569 
570 #if not defined(SL_GLES) && not defined(SL_EMSCRIPTEN)
571  if (lineWidth != 1.0f)
572  glLineWidth(1.0f);
573 #endif
574 
575  GET_GL_ERROR;
576 }
float SLfloat
Definition: SL.h:173
#define SL_EXIT_MSG(message)
Definition: SL.h:240
@ SP_colorUniform
Encapsulation of an OpenGL shader program object.
Definition: SLGLProgram.h:56
SLint uniformMatrix4fv(const SLchar *name, SLsizei count, const SLfloat *value, GLboolean transpose=false) const
Passes a 4x4 float matrix values py pointer to the uniform variable "name".
SLint uniform1f(const SLchar *name, SLfloat v0) const
Passes the float value v0 to the uniform variable "name".
SLint getUniformLocation(const SLchar *name) const
void useProgram()
static SLGLProgramGeneric * get(SLStdShaderProg id)
Get program reference for given id.
Singleton class holding all OpenGL states.
Definition: SLGLState.h:71
SLMat4f modelMatrix
Init all states.
Definition: SLGLState.h:89
static SLGLState * instance()
Public static instance getter for singleton pattern.
Definition: SLGLState.h:74
SLMat4f viewMatrix
matrix for the active cameras view transform
Definition: SLGLState.h:91
SLMat4f projectionMatrix
matrix for projection transform
Definition: SLGLState.h:90
void currentMaterial(SLMaterial *mat)
Definition: SLGLState.h:120
void drawElementsAs(SLGLPrimitiveType primitiveType, SLuint numIndexes=0, SLuint indexOffsetBytes=0)
Draws the VAO by element indices with a primitive type.

◆ drawElementsAs()

void SLGLVertexArray::drawElementsAs ( SLGLPrimitiveType  primitiveType,
SLuint  numIndexes = 0,
SLuint  indexOffset = 0 
)

Draws the VAO by element indices with a primitive type.

Draws the vertex attributes as a specified primitive type by elements with the indices from the index buffer defined in setIndices.

Definition at line 394 of file SLGLVertexArray.cpp.

397 {
398  assert(_numIndicesElements && _idVBOIndices && "No index VBO generated for VAO");
399 
400  // From OpenGL 3.0 on we have the OpenGL Vertex Arrays
401  // Binding the VAO saves all the commands after the else (per draw call!)
402  glBindVertexArray(_vaoID);
403  GET_GL_ERROR;
404 
405  // Do the draw call with indices
406  if (numIndexes == 0)
407  numIndexes = (SLuint)_numIndicesElements;
408 
410 
411  /////////////////////////////////////////////////////////////////////
412  glDrawElements(primitiveType,
413  (SLsizei)numIndexes,
415  (void*)(size_t)(indexOffset * (SLuint)indexTypeSize));
416  /////////////////////////////////////////////////////////////////////
417 
418  GET_GL_ERROR;
419  totalDrawCalls++;
420  switch (primitiveType)
421  {
422  case PT_triangles:
423  totalPrimitivesRendered += (numIndexes / 3);
424  break;
425  case PT_lines:
426  totalPrimitivesRendered += (numIndexes / 2);
427  break;
428  case PT_points:
429  totalPrimitivesRendered += numIndexes;
430  break;
431  default: break;
432  }
433 
434  glBindVertexArray(0);
435  GET_GL_ERROR;
436 }

◆ drawElementsInstanced()

void SLGLVertexArray::drawElementsInstanced ( SLGLPrimitiveType  primitiveType,
SLuint  countInstances = 0,
SLuint  numIndexes = 0,
SLuint  indexOffset = 0 
)

Draws the VAO as an array with instance primitive type.

Wrapper around glDrawElementsInstanced using a second VBO (_instanceVbo) that contains all positions for the instances. This used e.g. for SLParticleSystems on systems without geometry shaders.

Definition at line 485 of file SLGLVertexArray.cpp.

489 {
490  assert(_numIndicesElements &&
491  _idVBOIndices &&
492  "No index VBO generated for VAO");
493 
494  // From OpenGL 3.0 on we have the OpenGL Vertex Arrays
495  // Binding the VAO saves all the commands after the else (per draw call!)
496  glBindVertexArray(_vaoID);
497  GET_GL_ERROR;
498 
499  // Do the draw call with indices
500  if (numIndexes == 0)
501  numIndexes = (SLuint)_numIndicesElements;
502 
504 
505  ////////////////////////////////////////////////////////
506  glDrawElementsInstanced(primitiveType,
507  (GLsizei)numIndexes,
509  (void*)(size_t)(indexOffset * (SLuint)indexTypeSize),
510  (GLsizei)countInstances);
511  ////////////////////////////////////////////////////////
512 
513  GET_GL_ERROR;
514 
515  // Update statistics
516  totalDrawCalls++;
517  switch (primitiveType)
518  {
519  case PT_triangles:
520  totalPrimitivesRendered += (numIndexes / 3);
521  break;
522  case PT_lines:
523  totalPrimitivesRendered += (numIndexes / 2);
524  break;
525  case PT_points:
526  totalPrimitivesRendered += numIndexes;
527  break;
528  default: break;
529  }
530  glBindVertexArray(0);
531 
532  GET_GL_ERROR;
533 }

◆ endTF()

void SLGLVertexArray::endTF ( )

End transform feedback.

We activate back the rendering and stop the transform feedback.

Definition at line 379 of file SLGLVertexArray.cpp.

380 {
381  // End transform feedback
382  glEndTransformFeedback();
383 
384  // Enable rendering
385  glDisable(GL_RASTERIZER_DISCARD);
386 
387  // Un-bind the feedback object.
388  glBindTransformFeedback(GL_TRANSFORM_FEEDBACK, 0);
389 }

◆ generate()

void SLGLVertexArray::generate ( SLuint  numVertices,
SLGLBufferUsage  usage = BU_static,
SLbool  outputInterleaved = true,
SLuint  divisor = 0 
)

Generates the VA & VB objects for a NO. of vertices.

Generates the OpenGL objects for the vertex array and the vertex buffer object. If the input data is an interleaved array (all attribute data pointer where identical) also the output buffer will be generated as an interleaved array. Vertex arrays with attributes that are updated can not be interleaved. Vertex attributes with separate arrays can generate an interleaved or a sequential vertex buffer.


Sequential attribute layout:
| Positions | Normals | TexCoords |
Attribs: | Position0 | Position1 | Normal0 | Normal1 | UV1_0 | UV1_1 |
Elements: | PX | PY | PZ | PX | PY | PZ | NX | NY | NZ | NX | NY | NZ | TX | TY | TX | TY |
Bytes: |#### #### ####|#### #### ####|#### #### ####|#### #### ####|#### ####|#### ####|
| | |
|<------ offset Normals ----->| |
|<----------------------- offset UVs ---------------------->|

Interleaved attribute layout:
| Vertex 0 | Vertex 1 |
Attribs: | Position0 | Normal0 | UV1_0 | Position1 | Normal1 | UV1_1 |
Elements: | PX | PY | PZ | NX | NY | NZ | TX | TY | PX | PY | PZ | NX | NY | NZ | TX | TY |
Bytes: |#### #### ####|#### #### ####|#### ####|#### #### ####|#### #### ####|#### ####|
| | | |
|<-offsetN=32->| | |
|<------- offsetUV=32 ------->| |
| |
|<---------- strideBytes=32 ----------->|

The VAO has no or one active index buffer. For drawArrayAs no indices are needed. For drawElementsAs the index buffer is used. For triangle meshes also hard edges are generated. Their indices are stored behind the indices of the triangles.

Definition at line 189 of file SLGLVertexArray.cpp.

193 {
194  assert(numVertices);
195 
196  // if buffers exist delete them first
197  deleteGL();
198 
200 
201  // Generate and bind VAO
202  glGenVertexArrays(1, &_vaoID);
203  glBindVertexArray(_vaoID);
204 
205  ///////////////////////////////
206  // Create Vertex Buffer Objects
207  ///////////////////////////////
208 
209  // Generate the vertex buffer object for float attributes
210  if (_vbo.attribs().size())
211  {
212  _vbo.generate(numVertices, usage, outputInterleaved);
213  _vbo.bindAndEnableAttrib(divisor);
214  }
215 
216  if (_instanceVbo != nullptr)
217  {
219  }
220 
221  /////////////////////////////////////////////////////////////////
222  // Create Element Array Buffer for Indices for elements and edges
223  /////////////////////////////////////////////////////////////////
224 
227  {
228  // create temp. buffer with both index arrays
231  (SLuint)typeSize;
232  SLubyte* tmpBuf = new SLubyte[tmBufSize];
233  memcpy(tmpBuf,
235  _numIndicesElements * (SLuint)typeSize);
236  memcpy(tmpBuf + _numIndicesElements * (SLuint)typeSize,
238  _numIndicesEdges * (SLuint)typeSize);
239 
240  glGenBuffers(1, &_idVBOIndices);
241  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,
242  _idVBOIndices);
243  glBufferData(GL_ELEMENT_ARRAY_BUFFER,
244  tmBufSize,
245  tmpBuf,
246  GL_STATIC_DRAW);
249  delete[] tmpBuf;
250  }
251  else if (_numIndicesElements && _indexDataElements) // for elements only
252  {
254  glGenBuffers(1, &_idVBOIndices);
255  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _idVBOIndices);
256  glBufferData(GL_ELEMENT_ARRAY_BUFFER,
257  (GLsizeiptr)_numIndicesElements * (SLuint)typeSize,
259  GL_STATIC_DRAW);
262  (SLuint)typeSize;
263  }
264 
265  glBindVertexArray(0);
266  GET_GL_ERROR;
267 }
unsigned char SLubyte
Definition: SL.h:167
SLuint _instanceDivisor
Vertex buffer object containing the positions for instanced drawing.
void generate(SLuint numVertices, SLGLBufferUsage usage=BU_static, SLbool outputInterleaved=true)
Generates the VBO.
SLVVertexAttrib & attribs()
void bindAndEnableAttrib(SLuint divisor=0) const
Binds & enables the vertex attribute for OpenGL < 3.0 and during VAO creation.

◆ generateTF()

void SLGLVertexArray::generateTF ( SLuint  numVertices,
SLGLBufferUsage  usage = BU_static,
SLbool  outputInterleaved = true,
SLuint  divisor = 0 
)

Generates the VA & VB & TF objects.

Same as generate but with transform feedback

Definition at line 270 of file SLGLVertexArray.cpp.

274 {
275  assert(numVertices);
276 
277  // if buffers exist delete them first
278  deleteGL();
279 
281 
282  // Generate TFO
283  glGenTransformFeedbacks(1, &_tfoID);
284 
285  // Generate and bind VAO
286  glGenVertexArrays(1, &_vaoID);
287  glBindVertexArray(_vaoID);
288 
289  ///////////////////////////////
290  // Create Vertex Buffer Objects
291  ///////////////////////////////
292 
293  // Generate the vertex buffer object for float attributes
294  if (_vbo.attribs().size())
295  {
296  _vbo.generate(numVertices, usage, outputInterleaved);
297  _vbo.bindAndEnableAttrib(divisor);
298  }
299 
300  // ???
301  if (_instanceVbo != nullptr)
302  {
304  }
305 
306  ///////////////////////////////
307  // Bind transform feedback
308  ///////////////////////////////
309 
310  glBindTransformFeedback(GL_TRANSFORM_FEEDBACK, _tfoID);
311  glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, _vbo.id());
312 
313  /////////////////////////////////////////////////////////////////
314  // Create Element Array Buffer for Indices for elements and edges
315  /////////////////////////////////////////////////////////////////
316 
319  {
320  // create temp. buffer with both index arrays
323  (SLuint)typeSize;
324  SLubyte* tmpBuf = new SLubyte[tmBufSize];
325  memcpy(tmpBuf,
327  _numIndicesElements * (SLuint)typeSize);
328  memcpy(tmpBuf + _numIndicesElements * (SLuint)typeSize,
330  _numIndicesEdges * (SLuint)typeSize);
331 
332  glGenBuffers(1, &_idVBOIndices);
333  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _idVBOIndices);
334  glBufferData(GL_ELEMENT_ARRAY_BUFFER,
335  tmBufSize,
336  tmpBuf,
337  GL_STATIC_DRAW);
340  delete[] tmpBuf;
341  }
342  else if (_numIndicesElements && _indexDataElements) // for elements only
343  {
345  glGenBuffers(1, &_idVBOIndices);
346  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _idVBOIndices);
347  glBufferData(GL_ELEMENT_ARRAY_BUFFER,
348  (GLsizeiptr)_numIndicesElements * (SLuint)typeSize,
350  GL_STATIC_DRAW);
353  (SLuint)typeSize;
354  }
355 
356  glBindVertexArray(0);
357  GET_GL_ERROR;
358 }
SLuint _tfoID
OpenGL id of vertex array object.

◆ numIndicesEdges()

SLuint SLGLVertexArray::numIndicesEdges ( ) const
inline

Definition at line 206 of file SLGLVertexArray.h.

206 { return (SLuint)_numIndicesEdges; }

◆ numIndicesElements()

SLuint SLGLVertexArray::numIndicesElements ( ) const
inline

Definition at line 205 of file SLGLVertexArray.h.

205 { return (SLuint)_numIndicesElements; }

◆ numVertices()

SLuint SLGLVertexArray::numVertices ( ) const
inline

Definition at line 204 of file SLGLVertexArray.h.

204 { return _numVertices; }

◆ setAttrib() [1/7]

void SLGLVertexArray::setAttrib ( SLGLAttributeType  type,
SLint  elementSize,
SLint  location,
void dataPointer,
SLGLBufferType  dataType = BT_float 
)

Adds a vertex attribute with data pointer and an element size.

Defines a vertex attribute for the later generation. It must be of a specific SLVertexAttribType. Each attribute can appear only once in an vertex array. If all attributes of a vertex array have the same data pointer the data input will be interpreted as an interleaved array. See example in SLGLOculus::init. Be aware that the VBO for the attribute will not be generated until generate is called. The data pointer must still be valid when SLGLVertexArray::generate is called.

Definition at line 66 of file SLGLVertexArray.cpp.

71 {
72  assert(dataPointer);
73  assert(elementSize);
74 
75  if (type == AT_position && location == -1)
76  SL_EXIT_MSG("The position attribute has no variable location.");
77 
78  if (_vbo.attribIndex(type) >= 0)
79  SL_EXIT_MSG("Attribute type already exists.");
80 
81  SLGLAttribute va;
82  va.type = type;
83  va.elementSize = elementSize;
84  va.dataType = dataType;
85  va.dataPointer = dataPointer;
86  va.location = location;
87  va.bufferSizeBytes = 0;
88 
89  _vbo.attribs().push_back(va);
90 }
@ AT_position
Vertex position as a 2, 3 or 4 component vectors.
Definition: SLGLEnums.h:58
SLint attribIndex(SLGLAttributeType type)
Returns the vector index if a vertex attribute exists otherwise -1.
Struct for vertex attribute information.
SLGLAttributeType type
type of vertex attribute
SLint location
GLSL input variable location index.
SLint elementSize
size of attribute element (SLVec3f has 3)
SLGLBufferType dataType
SLuint bufferSizeBytes
size of the attribute part in the buffer
void * dataPointer
pointer to the attributes source data

◆ setAttrib() [2/7]

void SLGLVertexArray::setAttrib ( SLGLAttributeType  type,
SLint  location,
SLVfloat data 
)
inline

Adds a vertex attribute with vector of SLfloat.

Definition at line 74 of file SLGLVertexArray.h.

76  { setAttrib(type, 1, location, &data->operator[](0)); }
void setAttrib(SLGLAttributeType type, SLint elementSize, SLint location, void *dataPointer, SLGLBufferType dataType=BT_float)
Adds a vertex attribute with data pointer and an element size.

◆ setAttrib() [3/7]

void SLGLVertexArray::setAttrib ( SLGLAttributeType  type,
SLint  location,
SLVuint data 
)
inline

Adds a vertex attribute with vector of SLuint.

Definition at line 69 of file SLGLVertexArray.h.

71  { setAttrib(type, 1, location, &data->operator[](0), BT_uint); }
@ BT_uint
vertex index type (0-2^32)
Definition: SLGLEnums.h:25

◆ setAttrib() [4/7]

void SLGLVertexArray::setAttrib ( SLGLAttributeType  type,
SLint  location,
SLVVec2f data 
)
inline

Adds a vertex attribute with vector of SLVec2f.

Definition at line 79 of file SLGLVertexArray.h.

81  { setAttrib(type, 2, location, &data->operator[](0)); }

◆ setAttrib() [5/7]

void SLGLVertexArray::setAttrib ( SLGLAttributeType  type,
SLint  location,
SLVVec3f data 
)
inline

Adds a vertex attribute with vector of SLVec3f.

Definition at line 84 of file SLGLVertexArray.h.

86  { setAttrib(type, 3, location, &data->operator[](0)); }

◆ setAttrib() [6/7]

void SLGLVertexArray::setAttrib ( SLGLAttributeType  type,
SLint  location,
SLVVec4f data 
)
inline

Adds a vertex attribute with vector of SLVec4f.

Definition at line 89 of file SLGLVertexArray.h.

91  { setAttrib(type, 4, location, &data->operator[](0)); }

◆ setAttrib() [7/7]

void SLGLVertexArray::setAttrib ( SLGLAttributeType  type,
SLint  location,
SLVVec4i data 
)
inline

Adds a vertex attribute with vector of SLVec4i.

Definition at line 94 of file SLGLVertexArray.h.

96  { setAttrib(type, 4, location, &data->operator[](0), BT_int); }
@ BT_int
int vertex attributes
Definition: SLGLEnums.h:22

◆ setIndices() [1/4]

void SLGLVertexArray::setIndices ( SLuint  numIndicesElements,
SLGLBufferType  indexDataType,
void indexDataElements,
SLuint  numIndicesEdges = 0,
void indexDataEdges = nullptr 
)

Adds the index array for indexed element drawing.

Defines the vertex indices for the element drawing. Without indices vertex array can only be drawn with SLGLVertexArray::drawArrayAs. Be aware that the VBO for the indices will not be generated until generate is called. The data pointer must still be valid when generate is called.

Definition at line 108 of file SLGLVertexArray.cpp.

113 {
114  assert(numIndicesElements);
115  assert(indexDataElements);
116 
117  if (indexDataType == BT_ushort && _numVertices > 65535)
118  SL_EXIT_MSG("Index data type not sufficient.");
119  if (indexDataType == BT_ubyte && _numVertices > 255)
120  SL_EXIT_MSG("Index data type not sufficient.");
121 
123  _indexDataElements = indexDataElements;
124  _indexDataType = indexDataType;
126  _indexDataEdges = indexDataEdges;
127 }
@ BT_ubyte
vertex index type (0-2^8)
Definition: SLGLEnums.h:23
@ BT_ushort
vertex index type (0-2^16)
Definition: SLGLEnums.h:24
SLuint numIndicesElements() const
SLuint numIndicesEdges() const

◆ setIndices() [2/4]

void SLGLVertexArray::setIndices ( SLVubyte indicesElements,
SLVubyte indicesEdges = nullptr 
)
inline

Adds the index array for indexed element drawing with a vector of ubyte.

Definition at line 106 of file SLGLVertexArray.h.

108  {
109  setIndices((SLuint)indicesElements->size(),
110  BT_ubyte,
111  (void*)&indicesElements->operator[](0),
112  indicesEdges ? (SLuint)indicesEdges->size() : 0,
113  indicesEdges && indicesEdges->size() ? (void*)&indicesEdges->operator[](0) : nullptr);
114  };
void setIndices(SLuint numIndicesElements, SLGLBufferType indexDataType, void *indexDataElements, SLuint numIndicesEdges=0, void *indexDataEdges=nullptr)
Adds the index array for indexed element drawing.

◆ setIndices() [3/4]

void SLGLVertexArray::setIndices ( SLVuint indicesElements,
SLVuint indicesEdges = nullptr 
)
inline

Adds the index array for indexed element drawing with a vector of uint.

Definition at line 128 of file SLGLVertexArray.h.

130  {
131  setIndices((SLuint)indicesElements->size(),
132  BT_uint,
133  (void*)&indicesElements->operator[](0),
134  indicesEdges ? (SLuint)indicesEdges->size() : 0,
135  indicesEdges && indicesEdges->size() ? (void*)&indicesEdges->operator[](0) : nullptr);
136  }

◆ setIndices() [4/4]

void SLGLVertexArray::setIndices ( SLVushort indicesElements,
SLVushort indicesEdges = nullptr 
)
inline

Adds the index array for indexed element drawing with a vector of ushort.

Definition at line 117 of file SLGLVertexArray.h.

119  {
120  setIndices((SLuint)indicesElements->size(),
121  BT_ushort,
122  (void*)&indicesElements->operator[](0),
123  indicesEdges ? (SLuint)indicesEdges->size() : 0,
124  indicesEdges && indicesEdges->size() ? (void*)&indicesEdges->operator[](0) : nullptr);
125  };

◆ setInstanceVBO()

void SLGLVertexArray::setInstanceVBO ( SLGLVertexBuffer vbo,
SLuint  divisor = 0 
)

Attach a VBO that has been created outside of this VAO.

Assignment of the additional VBO for instanced drawing. The passed vbo contains the positions for the instanced drawing done in drawElementsInstanced. This used e.g. for SLParticleSystems on systems without geometry shaders.

Definition at line 97 of file SLGLVertexArray.cpp.

98 {
99  _instanceVbo = vbo;
100  _instanceDivisor = divisor;
101 }
SLGLVertexBuffer * vbo()

◆ tfoID()

SLuint SLGLVertexArray::tfoID ( ) const
inline

Returns the TFO id.

Definition at line 59 of file SLGLVertexArray.h.

59 { return _tfoID; }

◆ updateAttrib() [1/6]

void SLGLVertexArray::updateAttrib ( SLGLAttributeType  type,
SLint  elementSize,
void dataPointer 
)

Updates a specific vertex attribute in the VBO.

Updates the specified vertex attribute. This works only for sequential attributes and not for interleaved attributes. This is used e.g. for meshes with vertex skinning. See SLMesh::draw where we have joint attributes.

Definition at line 133 of file SLGLVertexArray.cpp.

136 {
137  assert(dataPointer && "No data pointer passed");
138  assert(elementSize > 0 && elementSize < 5 && "Element size invalid");
139 
140  // Get attribute index and check element size
141  SLint indexf = _vbo.attribIndex(type);
142  if (indexf == -1)
143  SL_EXIT_MSG("Attribute type does not exist in VAO.");
144 
145  if (!_vaoID)
146  glGenVertexArrays(1, &_vaoID);
147  glBindVertexArray(_vaoID);
148 
149  // update the appropriate VBO
150  if (indexf > -1)
151  _vbo.updateAttrib(type, elementSize, dataPointer);
152 
153  glBindVertexArray(0);
154  GET_GL_ERROR;
155 }
void updateAttrib(SLGLAttributeType type, SLint elementSize, void *dataPointer)
Updates a specific vertex attribute in the VBO.

◆ updateAttrib() [2/6]

void SLGLVertexArray::updateAttrib ( SLGLAttributeType  type,
SLVfloat data 
)
inline

Updates a specific vertex attribute in the VBO.

Definition at line 151 of file SLGLVertexArray.h.

152  { updateAttrib(type, 1, (void*)&data->operator[](0)); }
void updateAttrib(SLGLAttributeType type, SLint elementSize, void *dataPointer)
Updates a specific vertex attribute in the VBO.

◆ updateAttrib() [3/6]

void SLGLVertexArray::updateAttrib ( SLGLAttributeType  type,
SLVuint data 
)
inline

Updates a specific vertex attribute in the VBO.

Definition at line 147 of file SLGLVertexArray.h.

148  { updateAttrib(type, 1, (void*)&data->operator[](0)); }

◆ updateAttrib() [4/6]

void SLGLVertexArray::updateAttrib ( SLGLAttributeType  type,
SLVVec2f data 
)
inline

Updates a specific vertex attribute in the VBO.

Definition at line 155 of file SLGLVertexArray.h.

156  { updateAttrib(type, 2, (void*)&data->operator[](0)); }

◆ updateAttrib() [5/6]

void SLGLVertexArray::updateAttrib ( SLGLAttributeType  type,
SLVVec3f data 
)
inline

Updates a specific vertex attribute in the VBO.

Definition at line 159 of file SLGLVertexArray.h.

160  { updateAttrib(type, 3, (void*)&data->operator[](0)); }

◆ updateAttrib() [6/6]

void SLGLVertexArray::updateAttrib ( SLGLAttributeType  type,
SLVVec4f data 
)
inline

Updates a specific vertex attribute in the VBO.

Definition at line 163 of file SLGLVertexArray.h.

164  { updateAttrib(type, 4, (void*)&data->operator[](0)); }

◆ vaoID()

SLuint SLGLVertexArray::vaoID ( ) const
inline

Returns either the VAO id or the VBO id.

Definition at line 56 of file SLGLVertexArray.h.

56 { return _vaoID; }

◆ vbo()

SLGLVertexBuffer* SLGLVertexArray::vbo ( )
inline

Definition at line 207 of file SLGLVertexArray.h.

207 { return &_vbo; }

Member Data Documentation

◆ _idVBOIndices

SLuint SLGLVertexArray::_idVBOIndices
protected

Vertex buffer object for float attributes.

Definition at line 219 of file SLGLVertexArray.h.

◆ _indexDataEdges

void* SLGLVertexArray::_indexDataEdges
protected

NO. of vertex indices in array for hard edges.

Definition at line 223 of file SLGLVertexArray.h.

◆ _indexDataElements

void* SLGLVertexArray::_indexDataElements
protected

NO. of vertex indices in array for triangles, lines or points.

Definition at line 221 of file SLGLVertexArray.h.

◆ _indexDataType

SLGLBufferType SLGLVertexArray::_indexDataType
protected

Pointer to index data for hard edges.

Definition at line 224 of file SLGLVertexArray.h.

◆ _instanceDivisor

SLuint SLGLVertexArray::_instanceDivisor
protected

Vertex buffer object containing the positions for instanced drawing.

Definition at line 226 of file SLGLVertexArray.h.

◆ _instances

SLuint SLGLVertexArray::_instances
protected

static total no. of primitives rendered

Definition at line 214 of file SLGLVertexArray.h.

◆ _instanceVbo

SLGLVertexBuffer* SLGLVertexArray::_instanceVbo
protected

index data type (ubyte, ushort, uint)

Definition at line 225 of file SLGLVertexArray.h.

◆ _numIndicesEdges

size_t SLGLVertexArray::_numIndicesEdges
protected

Pointer to index data for elements.

Definition at line 222 of file SLGLVertexArray.h.

◆ _numIndicesElements

size_t SLGLVertexArray::_numIndicesElements
protected

OpenGL id of index vbo.

Definition at line 220 of file SLGLVertexArray.h.

◆ _numVertices

SLuint SLGLVertexArray::_numVertices
protected

OpenGL id of transform feedback object.

Definition at line 217 of file SLGLVertexArray.h.

◆ _tfoID

SLuint SLGLVertexArray::_tfoID
protected

OpenGL id of vertex array object.

Definition at line 216 of file SLGLVertexArray.h.

◆ _vaoID

SLuint SLGLVertexArray::_vaoID
protected

Number of instances of drawing.

Definition at line 215 of file SLGLVertexArray.h.

◆ _vbo

SLGLVertexBuffer SLGLVertexArray::_vbo
protected

NO. of vertices in array.

Definition at line 218 of file SLGLVertexArray.h.

◆ totalDrawCalls

SLuint SLGLVertexArray::totalDrawCalls = 0
static

Definition at line 210 of file SLGLVertexArray.h.

◆ totalPrimitivesRendered

SLuint SLGLVertexArray::totalPrimitivesRendered = 0
static

static total no. of draw calls

Definition at line 211 of file SLGLVertexArray.h.


The documentation for this class was generated from the following files: