History

use vertex buffer objects to speed up voxel rendering

Stephen Birarda committed 13 years ago

Commit e16cd4f1 parent 0fbb5343 files · +39 −9Browse files

Showing the changes to interface/src/VoxelSystem.cpp (1 of 3 files). Show all

Unified · Split

interface/src/VoxelSystem.cpp+36−6
@@ -9,7 +9,7 @@ · 8 unmodified lines
99 #include "VoxelSystem.h"
1010
1111 const float MAX_UNIT_ANY_AXIS = 20.0f;
12−const float CUBE_WIDTH = 0.05f;
12+const float CUBE_WIDTH = 0.005f;
1313 const int VERTICES_PER_VOXEL = 8;
1414 const int VERTEX_POINTS_PER_VOXEL = 3 * VERTICES_PER_VOXEL;
1515 const int INDICES_PER_VOXEL = 3 * 12;
@@ -47,10 +47,10 @@ void VoxelSystem::init(int numberOfRandomVoxels) { · 31 unmodified lines
4747 voxelsRendered = numberOfRandomVoxels;
4848
4949 // there are 3 points for each vertices, 24 vertices in each cube
50− verticesArray = new GLfloat[VERTEX_POINTS_PER_VOXEL * numberOfRandomVoxels];
50+ GLfloat *verticesArray = new GLfloat[VERTEX_POINTS_PER_VOXEL * numberOfRandomVoxels];
5151
5252 // there are 12 triangles in each cube, with three indices for each triangle
53− indicesArray = new GLuint[INDICES_PER_VOXEL * numberOfRandomVoxels];
53+ GLuint *indicesArray = new GLuint[INDICES_PER_VOXEL * numberOfRandomVoxels];
5454
5555 // new seed based on time now so voxels are different each time
5656 srand((unsigned)time(0));
@@ -81,6 +81,27 @@ void VoxelSystem::init(int numberOfRandomVoxels) { · 24 unmodified lines
8181 }
8282 }
8383
84+ // generate new VBO for the verticesArray
85+ glGenBuffers(1, &vboVerticesID);
86+
87+ // bind VBO in order to use
88+ glBindBuffer(GL_ARRAY_BUFFER, vboVerticesID);
89+
90+ // upload data to VBO
91+ glBufferData(GL_ARRAY_BUFFER, VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat) * numberOfRandomVoxels, verticesArray, GL_STATIC_DRAW);
92+
93+ // generate new VBO for the indicesArray
94+ glGenBuffers(1, &vboIndicesID);
95+
96+ // bind VBO in order to use
97+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboIndicesID);
98+
99+ // upload data to VBO
100+ glBufferData(GL_ELEMENT_ARRAY_BUFFER, numberOfRandomVoxels * INDICES_PER_VOXEL * sizeof(GLuint), indicesArray, GL_STATIC_DRAW);
101+
102+ // delete the verticesArray, indicesArray
103+ delete[] verticesArray;
104+ delete[] indicesArray;
84105 }
85106
86107 //
@@ -184,14 +205,23 @@ int VoxelSystem::render(Voxel * voxel, float scale, glm::vec3 * distance) { · 97 unmodified lines
184205 }
185206
186207 void VoxelSystem::render() {
208+ // bind VBOs for vertices and indices array
209+ glBindBuffer(GL_ARRAY_BUFFER, vboVerticesID);
210+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboIndicesID);
211+
212+ // draw the cubes
187213 glEnableClientState(GL_VERTEX_ARRAY);
188− glVertexPointer(3, GL_FLOAT, 0, verticesArray);
189214
190− // draw a cube
191− glDrawElements(GL_TRIANGLES, 36 * voxelsRendered, GL_UNSIGNED_INT, indicesArray);
215+ glVertexPointer(3, GL_FLOAT, 0, 0);
216+
217+ glDrawElements(GL_TRIANGLES, 36 * voxelsRendered, GL_UNSIGNED_INT, 0);
192218
193219 // deactivate vertex arrays after drawing
194220 glDisableClientState(GL_VERTEX_ARRAY);
221+
222+ // bind with 0 to switch back to normal operation
223+ glBindBuffer(GL_ARRAY_BUFFER, 0);
224+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
195225 }
196226
197227 void VoxelSystem::simulate(float deltaTime) {