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

SLGLVertexArray adds Helper Functions for quick Line & Point Drawing. More...

#include <SLGLVertexArrayExt.h>

Inheritance diagram for SLGLVertexArrayExt:
[legend]

Public Member Functions

 SLGLVertexArrayExt ()
 
virtual ~SLGLVertexArrayExt ()
 
void generateVertexPos (SLVVec2f *p)
 Adds or updates & generates a position vertex attribute for colored line or point drawing. More...
 
void generateVertexPos (SLVVec3f *p)
 Adds or updates & generates a position vertex attribute for colored line or point drawing. More...
 
void generateVertexPos (SLVVec4f *p)
 Adds or updates & generates a position vertex attribute for colored line or point drawing. More...
 
void drawArrayAsColored (SLGLPrimitiveType primitiveType, SLCol4f color, SLfloat lineOrPointSize=1.0f, SLuint indexFirstVertex=0, SLuint countVertices=0)
 Draws the array as the specified primitive with the color. More...
 
void drawElementAsColored (SLGLPrimitiveType primitiveType, SLCol4f color, SLfloat pointSize, SLuint indexFirstVertex=0, SLuint countVertices=0)
 Draws the VAO by element indices with the specified primitive with the color. More...
 
- Public Member Functions inherited from SLGLVertexArray
 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 ()
 

Private Member Functions

void generateVertexPos (SLuint numVertices, SLint elementSize, void *dataPointer)
 Adds or updates & generates a position vertex attribute for colored line or point drawing. More...
 

Additional Inherited Members

- Static Public Attributes inherited from SLGLVertexArray
static SLuint totalDrawCalls = 0
 
static SLuint totalPrimitivesRendered = 0
 static total no. of draw calls More...
 
- Protected Attributes inherited from SLGLVertexArray
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 adds Helper Functions for quick Line & Point Drawing.

These functions where separated from SLGLVertexAttribute because they use the predefined shader ColorUniform and have there for references to the classes SLScene and SLGLProgram.

Definition at line 22 of file SLGLVertexArrayExt.h.

Constructor & Destructor Documentation

◆ SLGLVertexArrayExt()

SLGLVertexArrayExt::SLGLVertexArrayExt ( )
inline

Definition at line 25 of file SLGLVertexArrayExt.h.

25 { ; }

◆ ~SLGLVertexArrayExt()

virtual SLGLVertexArrayExt::~SLGLVertexArrayExt ( )
inlinevirtual

Definition at line 26 of file SLGLVertexArrayExt.h.

26 { ; }

Member Function Documentation

◆ drawArrayAsColored()

void SLGLVertexArrayExt::drawArrayAsColored ( SLGLPrimitiveType  primitiveType,
SLCol4f  color,
SLfloat  pointSize = 1.0f,
SLuint  indexFirstVertex = 0,
SLuint  countVertices = 0 
)

Draws the array as the specified primitive with the color.

Draws the vertex positions as array with a specified primitive & color

Definition at line 46 of file SLGLVertexArrayExt.cpp.

51 {
52  assert(countVertices <= _numVertices);
53 
54  if (!_vbo.id())
55  SL_EXIT_MSG("No VBO generated for VAO in drawArrayAsColored.");
56 
57  // Prepare shader
59  SLGLState* stateGL = SLGLState::instance();
60  sp->useProgram();
61  sp->uniformMatrix4fv("u_mMatrix", 1, (SLfloat*)&stateGL->modelMatrix);
62  sp->uniformMatrix4fv("u_vMatrix", 1, (SLfloat*)&stateGL->viewMatrix);
63  sp->uniformMatrix4fv("u_pMatrix", 1, (SLfloat*)&stateGL->projectionMatrix);
64  sp->uniform1f("u_oneOverGamma", 1.0f);
65  stateGL->currentMaterial(nullptr);
66 
67  // Set uniform color
68  glUniform4fv(sp->getUniformLocation("u_matDiff"), 1, (SLfloat*)&color);
69 
70 #if not defined(SL_GLES) && not defined(SL_EMSCRIPTEN)
71  if (pointSize != 1.0f)
72  if (primitiveType == PT_points)
73  glPointSize(pointSize);
74 #endif
75 
76  //////////////////////////////////////
77  drawArrayAs(primitiveType,
78  (SLsizei)indexFirstVertex,
79  (SLsizei)countVertices);
80  //////////////////////////////////////
81 
82 #if not defined(SL_GLES) && not defined(SL_EMSCRIPTEN)
83  if (pointSize != 1.0f)
84  if (primitiveType == PT_points)
85  glPointSize(1.0f);
86 #endif
87 
89 }
float SLfloat
Definition: SL.h:173
#define SL_EXIT_MSG(message)
Definition: SL.h:240
int SLsizei
Definition: SL.h:172
@ PT_points
Definition: SLGLEnums.h:31
@ SP_colorUniform
#define GET_GL_ERROR
Definition: SLGLState.h:56
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
SLGLVertexBuffer _vbo
NO. of vertices in array.
SLuint _numVertices
OpenGL id of transform feedback object.
void drawArrayAs(SLGLPrimitiveType primitiveType, SLint firstVertex=0, SLsizei countVertices=0)
Draws the VAO as an array with a primitive type.
SLuint id() const

◆ drawElementAsColored()

void SLGLVertexArrayExt::drawElementAsColored ( SLGLPrimitiveType  primitiveType,
SLCol4f  color,
SLfloat  pointSize,
SLuint  indexFirstVertex = 0,
SLuint  countVertices = 0 
)

Draws the VAO by element indices with the specified primitive with the color.

Draws the vertex positions as array with a specified primitive & color

Definition at line 93 of file SLGLVertexArrayExt.cpp.

98 {
99  assert(countVertices <= _numVertices);
100 
101  if (!_vbo.id())
102  SL_EXIT_MSG("No VBO generated for VAO in drawArrayAsColored.");
103 
104  // Prepare shader
106  SLGLState* stateGL = SLGLState::instance();
107  sp->useProgram();
108  sp->uniformMatrix4fv("u_mMatrix", 1, (SLfloat*)&stateGL->modelMatrix);
109  sp->uniformMatrix4fv("u_vMatrix", 1, (SLfloat*)&stateGL->viewMatrix);
110  sp->uniformMatrix4fv("u_pMatrix", 1, (SLfloat*)&stateGL->projectionMatrix);
111  sp->uniform1f("u_oneOverGamma", 1.0f);
112  stateGL->currentMaterial(nullptr);
113 
114  // Set uniform color
115  glUniform4fv(sp->getUniformLocation("u_matDiff"), 1, (SLfloat*)&color);
116 
117 #if not defined(SL_GLES) && not defined(SL_EMSCRIPTEN)
118  if (pointSize != 1.0f)
119  if (primitiveType == PT_points)
120  glPointSize(pointSize);
121 #endif
122 
123  ////////////////////////////////
124  drawElementsAs(primitiveType,
125  indexFirstVertex,
126  countVertices);
127  ////////////////////////////////
128 
129 #if not defined(SL_GLES) && not defined(SL_EMSCRIPTEN)
130  if (pointSize != 1.0f)
131  if (primitiveType == PT_points)
132  glPointSize(1.0f);
133 #endif
134 
135  GET_GL_ERROR;
136 }
void drawElementsAs(SLGLPrimitiveType primitiveType, SLuint numIndexes=0, SLuint indexOffsetBytes=0)
Draws the VAO by element indices with a primitive type.

◆ generateVertexPos() [1/4]

void SLGLVertexArrayExt::generateVertexPos ( SLuint  numVertices,
SLint  elementSize,
void dataPointer 
)
private

Adds or updates & generates a position vertex attribute for colored line or point drawing.

Helper function that sets the vertex position attribute and generates or updates the vertex buffer from it. It is used together with the drawArrayAsColored function.

Definition at line 22 of file SLGLVertexArrayExt.cpp.

25 {
26  assert(dataPointer);
27  assert(elementSize);
28  assert(numVertices);
29 
31  sp->useProgram();
32  SLint location = AT_position;
33 
34  // Add attribute if it doesn't exist
35  if (_vbo.attribIndex(AT_position) == -1)
36  {
37  setAttrib(AT_position, elementSize, location, dataPointer);
39  }
40  else
41  updateAttrib(AT_position, elementSize, dataPointer);
42 }
int SLint
Definition: SL.h:170
@ AT_position
Vertex position as a 2, 3 or 4 component vectors.
Definition: SLGLEnums.h:58
@ BU_static
Buffer will be modified once and used many times.
Definition: SLGLEnums.h:91
SLuint numVertices() const
void updateAttrib(SLGLAttributeType type, SLint elementSize, void *dataPointer)
Updates a specific vertex attribute in the VBO.
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.
void generate(SLuint numVertices, SLGLBufferUsage usage=BU_static, SLbool outputInterleaved=true, SLuint divisor=0)
Generates the VA & VB objects for a NO. of vertices.
SLint attribIndex(SLGLAttributeType type)
Returns the vector index if a vertex attribute exists otherwise -1.

◆ generateVertexPos() [2/4]

void SLGLVertexArrayExt::generateVertexPos ( SLVVec2f p)
inline

Adds or updates & generates a position vertex attribute for colored line or point drawing.

Definition at line 29 of file SLGLVertexArrayExt.h.

29 { generateVertexPos((SLuint)p->size(), 2, &p->operator[](0)); }
unsigned int SLuint
Definition: SL.h:171
void generateVertexPos(SLVVec2f *p)
Adds or updates & generates a position vertex attribute for colored line or point drawing.

◆ generateVertexPos() [3/4]

void SLGLVertexArrayExt::generateVertexPos ( SLVVec3f p)
inline

Adds or updates & generates a position vertex attribute for colored line or point drawing.

Definition at line 32 of file SLGLVertexArrayExt.h.

32 { generateVertexPos((SLuint)p->size(), 3, &p->operator[](0)); }

◆ generateVertexPos() [4/4]

void SLGLVertexArrayExt::generateVertexPos ( SLVVec4f p)
inline

Adds or updates & generates a position vertex attribute for colored line or point drawing.

Definition at line 35 of file SLGLVertexArrayExt.h.

35 { generateVertexPos((SLuint)p->size(), 4, &p->operator[](0)); }

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