SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SLGLVertexBuffer.h
Go to the documentation of this file.
1 /**
2  * \file SLGLVertexBuffer.h
3  * \brief Wrapper class around OpenGL Vertex Buffer Objects (VBO)
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 #ifndef SLGLVERTEXBUFFER_H
12 #define SLGLVERTEXBUFFER_H
13 
14 #include <SLGLEnums.h>
15 #include <SLVec2.h>
16 #include <SLVec3.h>
17 #include <SLVec4.h>
18 
19 //-----------------------------------------------------------------------------
20 //! Struct for vertex attribute information
22 {
23  SLGLAttributeType type; //!< type of vertex attribute
24  SLint elementSize; //!< size of attribute element (SLVec3f has 3)
25  SLGLBufferType dataType; //! Data Type (BT_float, BT_ubyte,...)
26  SLuint offsetBytes; //!< offset of the attribute data in the buffer
27  SLuint bufferSizeBytes; //!< size of the attribute part in the buffer
28  void* dataPointer; //!< pointer to the attributes source data
29  SLint location; //!< GLSL input variable location index
30 };
31 //-----------------------------------------------------------------------------
32 typedef vector<SLGLAttribute> SLVVertexAttrib;
33 //-----------------------------------------------------------------------------
34 
35 //-----------------------------------------------------------------------------
36 //! SLGLVertexBuffer encapsulates an OpenGL buffer for vertex attributes
37 /*! SLGLVertexBuffer is only meant to be used within the SLGLVertexArray class.
38 Attributes can be either be in sequential order (first all positions, then all
39 normals, etc.) or interleaved (all attributes together for one vertex). See
40 SLGLVertexBuffer::generate for more information.\n
41 Vertex index buffer are not handled in this class. They are generated in
42 SLGLVertexArray.
43 */
45 {
46 public:
49 
50  //! Deletes all vertex array & vertex buffer objects
51  void deleteGL();
52 
53  //! Calls deleteGL & clears the attributes
54  void clear();
55 
56  //! Returns the vector index if a vertex attribute exists otherwise -1
58 
59  //! Updates a specific vertex attribute in the VBO
61  SLint elementSize,
62  void* dataPointer);
63 
64  //! Updates a specific vertex attribute in the VBO
66  SLVuint& data) { updateAttrib(type, 1, (void*)&data[0]); }
67 
68  //! Updates a specific vertex attribute in the VBO
70  SLVfloat& data) { updateAttrib(type, 1, (void*)&data[0]); }
71 
72  //! Updates a specific vertex attribute in the VBO
74  SLVVec2f& data) { updateAttrib(type, 2, (void*)&data[0]); }
75 
76  //! Updates a specific vertex attribute in the VBO
78  SLVVec3f& data) { updateAttrib(type, 3, (void*)&data[0]); }
79 
80  //! Updates a specific vertex attribute in the VBO
82  SLVVec4f& data) { updateAttrib(type, 4, (void*)&data[0]); }
83 
84  //! Generates the VBO
85  void generate(SLuint numVertices,
86  SLGLBufferUsage usage = BU_static,
87  SLbool outputInterleaved = true);
88 
89  //! Binds & enables the vertex attribute for OpenGL < 3.0 and during VAO creation
90  void bindAndEnableAttrib(SLuint divisor = 0) const;
91 
92  //! disables the vertex attribute for OpenGL < 3.0
93  void disableAttrib();
94 
95  // Getters
96  SLuint id() const { return _id; }
97  SLuint size() const { return _id; }
100 
101  // Setters
102 
103  // Some statistics
104  static SLuint totalBufferCount; //! static total no. of buffers in use
105  static SLuint totalBufferSize; //! static total size of all buffers in bytes
106 
107  //! Returns the size of a buffer data type
108  static SLuint sizeOfType(SLGLBufferType type);
109 
110 protected:
111  SLuint _id; //! OpenGL id of vertex buffer object
112  SLuint _numVertices; //! NO. of vertices in array
113  SLVVertexAttrib _attribs; //! Vector of vertex attributes
114  SLbool _outputIsInterleaved; //! Flag if VBO should be generated interleaved
115  SLbool _inputIsInterleaved; //! Flag if VBO should be generated interleaved
116  SLuint _strideBytes; //! Distance for interleaved attributes in bytes
117  SLuint _sizeBytes; //! Total size of float VBO in bytes
118  SLGLBufferUsage _usage; //! buffer usage (static, dynamic or stream)
119 };
120 //-----------------------------------------------------------------------------
121 
122 #endif
unsigned int SLuint
Definition: SL.h:171
bool SLbool
Definition: SL.h:175
vector< SLfloat > SLVfloat
Definition: SL.h:200
vector< SLuint > SLVuint
Definition: SL.h:197
int SLint
Definition: SL.h:170
Enumerations containing OpenGL constants.
SLGLAttributeType
Enumeration for float vertex attribute types.
Definition: SLGLEnums.h:45
SLGLBufferUsage
Enumeration for buffer usage types also supported by OpenGL ES.
Definition: SLGLEnums.h:90
@ BU_static
Buffer will be modified once and used many times.
Definition: SLGLEnums.h:91
SLGLBufferType
Enumeration for buffer data types.
Definition: SLGLEnums.h:20
vector< SLGLAttribute > SLVVertexAttrib
vector< SLVec2f > SLVVec2f
Definition: SLVec2.h:143
vector< SLVec3f > SLVVec3f
Definition: SLVec3.h:325
vector< SLVec4f > SLVVec4f
Definition: SLVec4.h:239
SLGLVertexBuffer encapsulates an OpenGL buffer for vertex attributes.
void updateAttrib(SLGLAttributeType type, SLVuint &data)
Updates a specific vertex attribute in the VBO.
SLint attribIndex(SLGLAttributeType type)
Returns the vector index if a vertex attribute exists otherwise -1.
void generate(SLuint numVertices, SLGLBufferUsage usage=BU_static, SLbool outputInterleaved=true)
Generates the VBO.
SLuint _sizeBytes
Distance for interleaved attributes in bytes.
static SLuint totalBufferSize
static total no. of buffers in use
SLuint size() const
void updateAttrib(SLGLAttributeType type, SLVVec3f &data)
Updates a specific vertex attribute in the VBO.
SLbool _outputIsInterleaved
Vector of vertex attributes.
void updateAttrib(SLGLAttributeType type, SLVVec2f &data)
Updates a specific vertex attribute in the VBO.
SLGLBufferUsage _usage
Total size of float VBO in bytes.
void updateAttrib(SLGLAttributeType type, SLVfloat &data)
Updates a specific vertex attribute in the VBO.
static SLuint totalBufferCount
SLVVertexAttrib _attribs
NO. of vertices in array.
SLuint id() const
SLuint _numVertices
OpenGL id of vertex buffer object.
SLGLVertexBuffer()
Constructor initializing with default values.
SLbool _inputIsInterleaved
Flag if VBO should be generated interleaved.
void clear()
Calls deleteGL & clears the attributes.
static SLuint sizeOfType(SLGLBufferType type)
static total size of all buffers in bytes
void updateAttrib(SLGLAttributeType type, SLVVec4f &data)
Updates a specific vertex attribute in the VBO.
void deleteGL()
Deletes all vertex array & vertex buffer objects.
SLuint _strideBytes
Flag if VBO should be generated interleaved.
SLVVertexAttrib & attribs()
SLbool outputIsInterleaved() const
void disableAttrib()
disables the vertex attribute for OpenGL < 3.0
void bindAndEnableAttrib(SLuint divisor=0) const
Binds & enables the vertex attribute for OpenGL < 3.0 and during VAO creation.
void updateAttrib(SLGLAttributeType type, SLint elementSize, void *dataPointer)
Updates a specific vertex attribute in the VBO.
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)
SLuint offsetBytes
Data Type (BT_float, BT_ubyte,...)
SLGLBufferType dataType
SLuint bufferSizeBytes
size of the attribute part in the buffer
void * dataPointer
pointer to the attributes source data