SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SLGLVertexArrayExt.cpp
Go to the documentation of this file.
1 /**
2  * \file SLGLVertexArrayExt.cpp
3  * \brief Extension class with functions for quick line & point drawing
4  * \date January 2016
5  * \authors Marcus Hudritsch
6  * \copyright http://opensource.org/licenses/GPL-3.0
7  * \remarks Please use clangformat to format the code. See more code style on
8  * https://github.com/cpvrlab/SLProject4/wiki/SLProject-Coding-Style
9 */
10 
11 #include <SLGLState.h>
12 #include <SLGLProgram.h>
13 #include <SLGLVertexArrayExt.h>
14 #include <SLGLProgramManager.h>
15 #include <SLMaterial.h>
16 
17 //-----------------------------------------------------------------------------
18 /*! Helper function that sets the vertex position attribute and generates or
19 updates the vertex buffer from it. It is used together with the
20 drawArrayAsColored function.
21 */
23  SLint elementSize,
24  void* dataPointer)
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 }
43 //-----------------------------------------------------------------------------
44 /*! Draws the vertex positions as array with a specified primitive & color
45  */
47  SLCol4f color,
48  SLfloat pointSize,
49  SLuint indexFirstVertex,
50  SLuint countVertices)
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 }
90 //-----------------------------------------------------------------------------
91 /*! Draws the vertex positions as array with a specified primitive & color
92  */
94  SLCol4f color,
95  SLfloat pointSize,
96  SLuint indexFirstVertex,
97  SLuint countVertices)
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 }
137 //-----------------------------------------------------------------------------
float SLfloat
Definition: SL.h:173
unsigned int SLuint
Definition: SL.h:171
#define SL_EXIT_MSG(message)
Definition: SL.h:240
int SLsizei
Definition: SL.h:172
int SLint
Definition: SL.h:170
@ AT_position
Vertex position as a 2, 3 or 4 component vectors.
Definition: SLGLEnums.h:58
SLGLPrimitiveType
Definition: SLGLEnums.h:30
@ PT_points
Definition: SLGLEnums.h:31
@ BU_static
Buffer will be modified once and used many times.
Definition: SLGLEnums.h:91
@ SP_colorUniform
Singleton class for global render state.
#define GET_GL_ERROR
Definition: SLGLState.h:56
Extension class with functions for quick line & point drawing.
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 generateVertexPos(SLVVec2f *p)
Adds or updates & generates a position vertex attribute for colored line or point drawing.
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.
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.
SLGLVertexBuffer _vbo
NO. of vertices in array.
SLuint numVertices() const
void updateAttrib(SLGLAttributeType type, SLint elementSize, void *dataPointer)
Updates a specific vertex attribute in the VBO.
void drawElementsAs(SLGLPrimitiveType primitiveType, SLuint numIndexes=0, SLuint indexOffsetBytes=0)
Draws the VAO by element indices with a primitive type.
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.
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.
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.
SLuint id() const