Showing the changes to interface/src/VoxelSystem.cpp (1 of 3 files). Show all
Unified · Split
| @@ -9,7 +9,7 @@ · 8 unmodified lines | |||
| 9 | #include "VoxelSystem.h" | 9 | #include "VoxelSystem.h" |
| 10 | 10 | ||
| 11 | const float MAX_UNIT_ANY_AXIS = 20.0f; | 11 | const float MAX_UNIT_ANY_AXIS = 20.0f; |
| 12 | const float CUBE_WIDTH = 0.05f; | 12 | const float CUBE_WIDTH = 0.005f; |
| 13 | const int VERTICES_PER_VOXEL = 8; | 13 | const int VERTICES_PER_VOXEL = 8; |
| 14 | const int VERTEX_POINTS_PER_VOXEL = 3 * VERTICES_PER_VOXEL; | 14 | const int VERTEX_POINTS_PER_VOXEL = 3 * VERTICES_PER_VOXEL; |
| 15 | const int INDICES_PER_VOXEL = 3 * 12; | 15 | const int INDICES_PER_VOXEL = 3 * 12; |
| @@ -47,10 +47,10 @@ void VoxelSystem::init(int numberOfRandomVoxels) { · 31 unmodified lines | |||
| 47 | voxelsRendered = numberOfRandomVoxels; | 47 | voxelsRendered = numberOfRandomVoxels; |
| 48 | 48 | ||
| 49 | // there are 3 points for each vertices, 24 vertices in each cube | 49 | // 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]; |
| 51 | 51 | ||
| 52 | // there are 12 triangles in each cube, with three indices for each triangle | 52 | // 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]; |
| 54 | 54 | ||
| 55 | // new seed based on time now so voxels are different each time | 55 | // new seed based on time now so voxels are different each time |
| 56 | srand((unsigned)time(0)); | 56 | srand((unsigned)time(0)); |
| @@ -81,6 +81,27 @@ void VoxelSystem::init(int numberOfRandomVoxels) { · 24 unmodified lines | |||
| 81 | } | 81 | } |
| 82 | } | 82 | } |
| 83 | 83 | ||
| 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; | ||
| 84 | } | 105 | } |
| 85 | 106 | ||
| 86 | // | 107 | // |
| @@ -184,14 +205,23 @@ int VoxelSystem::render(Voxel * voxel, float scale, glm::vec3 * distance) { · 97 unmodified lines | |||
| 184 | } | 205 | } |
| 185 | 206 | ||
| 186 | void VoxelSystem::render() { | 207 | 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 | ||
| 187 | glEnableClientState(GL_VERTEX_ARRAY); | 213 | glEnableClientState(GL_VERTEX_ARRAY); |
| 188 | glVertexPointer(3, GL_FLOAT, 0, verticesArray); | ||
| 189 | 214 | ||
| 190 | // draw a cube | 215 | glVertexPointer(3, GL_FLOAT, 0, 0); |
| 191 | glDrawElements(GL_TRIANGLES, 36 * voxelsRendered, GL_UNSIGNED_INT, indicesArray); | 216 | |
| 217 | glDrawElements(GL_TRIANGLES, 36 * voxelsRendered, GL_UNSIGNED_INT, 0); | ||
| 192 | 218 | ||
| 193 | // deactivate vertex arrays after drawing | 219 | // deactivate vertex arrays after drawing |
| 194 | glDisableClientState(GL_VERTEX_ARRAY); | 220 | 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); | ||
| 195 | } | 225 | } |
| 196 | 226 | ||
| 197 | void VoxelSystem::simulate(float deltaTime) { | 227 | void VoxelSystem::simulate(float deltaTime) { |