GlobeEngine
BufferArray.h
Go to the documentation of this file.
1 
12 #ifndef GlobeEngine_BufferArray_h
13 #define GlobeEngine_BufferArray_h
14 
15 #include "OpenGL_Includes.h"
16 
17 namespace ge {
18  template <int TYPE> class BufferArray
19  {
20  public:
22  this->bufferHandles = NULL;
23  this->clearHandles();
24  };
25 
27 #ifdef __APPLE__
28  for (int i = 0; i < bufferHandleCount; i++){
29  this->bindBuffer(i);
30  glBufferData(TYPE, 0, NULL, GL_STATIC_DRAW);
31  }
32 #endif
33  this->clearHandles();
34  };
35 
36  void generateHandles(unsigned int _count){
37  if (this->bufferHandleCount != _count) {
38  this->clearHandles();
39  this->bufferHandles = new GLuint[_count];
40  this->bufferHandleCount = _count;
41  //this->unbindBuffers();
42  glGenBuffers(_count, this->bufferHandles);
43  }
44  }
45 
46  void copyToBuffer(unsigned int _layerid, GLsizeiptr _size, const GLvoid* _data, GLenum _access = GL_STATIC_DRAW) {
47  this->bindBuffer(_layerid);
48  glBufferData(TYPE, _size, _data, _access);
49  }
50 
51 #ifdef GENGINE_GL_ES
52 #else
53  template <typename T>
54  T* mapBuffer(unsigned int _layerid, GLuint _size,
55  GLenum _usage = GL_STATIC_DRAW, GLbitfield _mode = GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT) {
56  this->bindBuffer(_layerid);
57  glBufferData(TYPE, _size * sizeof(T), NULL, _usage);
58  return reinterpret_cast<T*>(glMapBufferRange(TYPE, 0, _size * sizeof(T), _mode));
59  }
60 
61  void unmapBuffer(unsigned int _layerid){
62  this->bindBuffer(_layerid);
63  glUnmapBuffer(TYPE);
64  }
65 
67  for (int i = 0; i < bufferHandleCount; i++){
68  this->bindBuffer(i);
69  glUnmapBuffer(TYPE);
70  }
71  }
72 
73 #endif
74  void bindBuffer(unsigned int _layerid) {
75  glBindBuffer(TYPE, this->bufferHandles[_layerid]);
76  };
77 
78  void unbindBuffer(){
79  glBindBuffer(TYPE, 0);
80  }
81 
82  GLuint getBufferHandle(unsigned int _layerid){
83  if (_layerid < bufferHandleCount){
84  return this->bufferHandles[_layerid];
85  }
86  else{
87  return std::numeric_limits< unsigned int >::max();
88  }
89  }
90 
91  // this throws a linker error in case a not supported type
92  // uses this function
93  void setAttributeDivisor(GLuint _attributeIndex, GLuint _divisor) {
94  glVertexAttribDivisor(_attributeIndex, _divisor);
95  }
96 
97  private:
98  void clearHandles(){
99  if (bufferHandles != NULL) {
100  glDeleteBuffers(bufferHandleCount, bufferHandles);
101  delete[] bufferHandles;
102  }
103  bufferHandles = NULL;
104  bufferHandleCount = 0;
105  }
106  private:
107  GLuint* bufferHandles;
108  GLuint bufferHandleCount;
109  };
110 
112 #ifndef GENGINE_GL_ES
115  typedef BufferArray<GL_PIXEL_PACK_BUFFER> PixelPackBufferArray; // Texture data download from graphics card
116 #endif
117 
118 #ifndef GENGINE_GL_BELOW_410
120 #endif
121 
122 }
123 #endif
void copyToBuffer(unsigned int _layerid, GLsizeiptr _size, const GLvoid *_data, GLenum _access=GL_STATIC_DRAW)
Definition: BufferArray.h:46
BufferArray< GL_PIXEL_UNPACK_BUFFER > PixelUnpackBufferArray
Definition: BufferArray.h:114
BufferArray< GL_SHADER_STORAGE_BUFFER > ShaderStorageBufferArray
Definition: BufferArray.h:119
void setAttributeDivisor(GLuint _attributeIndex, GLuint _divisor)
Definition: BufferArray.h:93
void bindBuffer(unsigned int _layerid)
Definition: BufferArray.h:74
T * mapBuffer(unsigned int _layerid, GLuint _size, GLenum _usage=GL_STATIC_DRAW, GLbitfield _mode=GL_MAP_WRITE_BIT|GL_MAP_INVALIDATE_BUFFER_BIT)
Definition: BufferArray.h:54
~BufferArray()
Definition: BufferArray.h:26
void generateHandles(unsigned int _count)
Definition: BufferArray.h:36
BufferArray< GL_PIXEL_PACK_BUFFER > PixelPackBufferArray
Definition: BufferArray.h:115
BufferArray< GL_ARRAY_BUFFER > VertexBufferArray
Definition: BufferArray.h:111
void unmapBuffer(unsigned int _layerid)
Definition: BufferArray.h:61
void unbindBuffer()
Definition: BufferArray.h:78
Definition: BufferArray.h:18
void unmapAllBuffers()
Definition: BufferArray.h:66
GLuint getBufferHandle(unsigned int _layerid)
Definition: BufferArray.h:82
Definition: AvalancheTrainingSimulationEngine.h:28
BufferArray()
Definition: BufferArray.h:21
BufferArray< GL_UNIFORM_BUFFER > UniformBufferArray
Definition: BufferArray.h:113