Showing the changes to interface/src/VoxelSystem.cpp (1 of 3 files). Show all
Unified · Split
| @@ -9,9 +9,10 @@ · 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.005f; | 12 | const float CUBE_WIDTH = 0.01f; |
| 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 COLOR_VALUES_PER_VOXEL = 3 * VERTICES_PER_VOXEL; | ||
| 15 | const int INDICES_PER_VOXEL = 3 * 12; | 16 | const int INDICES_PER_VOXEL = 3 * 12; |
| 16 | 17 | ||
| 17 | GLfloat identityVertices[] = { -1, -1, 1, | 18 | GLfloat identityVertices[] = { -1, -1, 1, |
| @@ -40,6 +41,10 @@ void VoxelSystem::init() { · 22 unmodified lines | |||
| 40 | root = new Voxel; | 41 | root = new Voxel; |
| 41 | } | 42 | } |
| 42 | 43 | ||
| 44 | float randomFloat(float maximumValue) { | ||
| 45 | return ((float) rand() / ((float) RAND_MAX / maximumValue)); | ||
| 46 | } | ||
| 47 | |||
| 43 | void VoxelSystem::init(int numberOfRandomVoxels) { | 48 | void VoxelSystem::init(int numberOfRandomVoxels) { |
| 44 | // create the arrays needed to pass to glDrawElements later | 49 | // create the arrays needed to pass to glDrawElements later |
| 45 | // position / color are random for now | 50 | // position / color are random for now |
| @@ -49,6 +54,9 @@ void VoxelSystem::init(int numberOfRandomVoxels) { · 3 unmodified lines | |||
| 49 | // there are 3 points for each vertices, 24 vertices in each cube | 54 | // there are 3 points for each vertices, 24 vertices in each cube |
| 50 | GLfloat *verticesArray = new GLfloat[VERTEX_POINTS_PER_VOXEL * numberOfRandomVoxels]; | 55 | GLfloat *verticesArray = new GLfloat[VERTEX_POINTS_PER_VOXEL * numberOfRandomVoxels]; |
| 51 | 56 | ||
| 57 | // we need a color for each vertex in each voxel | ||
| 58 | GLfloat *colorsArray = new GLfloat[COLOR_VALUES_PER_VOXEL * numberOfRandomVoxels]; | ||
| 59 | |||
| 52 | // there are 12 triangles in each cube, with three indices for each triangle | 60 | // there are 12 triangles in each cube, with three indices for each triangle |
| 53 | GLuint *indicesArray = new GLuint[INDICES_PER_VOXEL * numberOfRandomVoxels]; | 61 | GLuint *indicesArray = new GLuint[INDICES_PER_VOXEL * numberOfRandomVoxels]; |
| 54 | 62 | ||
| @@ -58,17 +66,29 @@ void VoxelSystem::init(int numberOfRandomVoxels) { · 3 unmodified lines | |||
| 58 | for (int n = 0; n < numberOfRandomVoxels; n++) { | 66 | for (int n = 0; n < numberOfRandomVoxels; n++) { |
| 59 | // pick a random point for the center of the cube | 67 | // pick a random point for the center of the cube |
| 60 | glm::vec3 position = glm::vec3( | 68 | glm::vec3 position = glm::vec3( |
| 61 | ((float) rand() / ((float) RAND_MAX / MAX_UNIT_ANY_AXIS)), | 69 | randomFloat(MAX_UNIT_ANY_AXIS * 2) - MAX_UNIT_ANY_AXIS, |
| 62 | ((float) rand() / ((float) RAND_MAX / MAX_UNIT_ANY_AXIS)), | 70 | randomFloat(MAX_UNIT_ANY_AXIS * 2) - MAX_UNIT_ANY_AXIS, |
| 63 | ((float) rand() / ((float) RAND_MAX / MAX_UNIT_ANY_AXIS)) | 71 | randomFloat(MAX_UNIT_ANY_AXIS * 2) - MAX_UNIT_ANY_AXIS |
| 64 | ); | 72 | ); |
| 65 | 73 | ||
| 74 | // fill the vertices array | ||
| 66 | GLfloat *currentVerticesPos = verticesArray + (n * VERTEX_POINTS_PER_VOXEL); | 75 | GLfloat *currentVerticesPos = verticesArray + (n * VERTEX_POINTS_PER_VOXEL); |
| 67 | 76 | ||
| 68 | // fill the vertices array | ||
| 69 | for (int v = 0; v < VERTEX_POINTS_PER_VOXEL; v++) { | 77 | for (int v = 0; v < VERTEX_POINTS_PER_VOXEL; v++) { |
| 70 | currentVerticesPos[v] = position[v % 3] + (identityVertices[v] * CUBE_WIDTH); | 78 | currentVerticesPos[v] = position[v % 3] + (identityVertices[v] * CUBE_WIDTH); |
| 71 | } | 79 | } |
| 80 | |||
| 81 | // fill the colors array | ||
| 82 | GLfloat *currentColorPos = colorsArray + (n * COLOR_VALUES_PER_VOXEL); | ||
| 83 | float voxelR = randomFloat(1); | ||
| 84 | float voxelG = randomFloat(1); | ||
| 85 | float voxelB = randomFloat(1); | ||
| 86 | |||
| 87 | for (int c = 0; c < VERTICES_PER_VOXEL; c++) { | ||
| 88 | currentColorPos[0 + (c * 3)] = voxelR; | ||
| 89 | currentColorPos[1 + (c * 3)] = voxelG; | ||
| 90 | currentColorPos[2 + (c * 3)] = voxelB; | ||
| 91 | } | ||
| 72 | 92 | ||
| 73 | // fill the indices array | 93 | // fill the indices array |
| 74 | int voxelIndexOffset = n * INDICES_PER_VOXEL; | 94 | int voxelIndexOffset = n * INDICES_PER_VOXEL; |
| @@ -81,27 +101,25 @@ void VoxelSystem::init(int numberOfRandomVoxels) { · 6 unmodified lines | |||
| 81 | } | 101 | } |
| 82 | } | 102 | } |
| 83 | 103 | ||
| 84 | // generate new VBO for the verticesArray | 104 | // VBO for the verticesArray |
| 85 | glGenBuffers(1, &vboVerticesID); | 105 | glGenBuffers(1, &vboVerticesID); |
| 86 | |||
| 87 | // bind VBO in order to use | ||
| 88 | glBindBuffer(GL_ARRAY_BUFFER, vboVerticesID); | 106 | 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); | 107 | glBufferData(GL_ARRAY_BUFFER, VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat) * numberOfRandomVoxels, verticesArray, GL_STATIC_DRAW); |
| 92 | 108 | ||
| 93 | // generate new VBO for the indicesArray | 109 | // VBO for colorsArray |
| 94 | glGenBuffers(1, &vboIndicesID); | 110 | glGenBuffers(1, &vboColorsID); |
| 111 | glBindBuffer(GL_ARRAY_BUFFER, vboColorsID); | ||
| 112 | glBufferData(GL_ARRAY_BUFFER, VERTICES_PER_VOXEL * sizeof(GLfloat) * numberOfRandomVoxels, colorsArray, GL_STATIC_DRAW); | ||
| 95 | 113 | ||
| 96 | // bind VBO in order to use | 114 | // VBO for the indicesArray |
| 115 | glGenBuffers(1, &vboIndicesID); | ||
| 97 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboIndicesID); | 116 | 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); | 117 | glBufferData(GL_ELEMENT_ARRAY_BUFFER, numberOfRandomVoxels * INDICES_PER_VOXEL * sizeof(GLuint), indicesArray, GL_STATIC_DRAW); |
| 101 | 118 | ||
| 102 | // delete the verticesArray, indicesArray | 119 | // delete the verticesArray, indicesArray |
| 103 | delete[] verticesArray; | 120 | delete[] verticesArray; |
| 104 | delete[] indicesArray; | 121 | delete[] indicesArray; |
| 122 | delete[] colorsArray; | ||
| 105 | } | 123 | } |
| 106 | 124 | ||
| 107 | // | 125 | // |
| @@ -205,19 +223,22 @@ int VoxelSystem::render(Voxel * voxel, float scale, glm::vec3 * distance) { · 97 unmodified lines | |||
| 205 | } | 223 | } |
| 206 | 224 | ||
| 207 | void VoxelSystem::render() { | 225 | 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 | ||
| 213 | glEnableClientState(GL_VERTEX_ARRAY); | 226 | glEnableClientState(GL_VERTEX_ARRAY); |
| 227 | glEnableClientState(GL_COLOR_ARRAY); | ||
| 228 | |||
| 229 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboIndicesID); | ||
| 214 | 230 | ||
| 231 | glBindBuffer(GL_ARRAY_BUFFER, vboVerticesID); | ||
| 215 | glVertexPointer(3, GL_FLOAT, 0, 0); | 232 | glVertexPointer(3, GL_FLOAT, 0, 0); |
| 216 | 233 | ||
| 234 | glBindBuffer(GL_ARRAY_BUFFER, vboColorsID); | ||
| 235 | glColorPointer(3, GL_FLOAT, 0, 0); | ||
| 236 | |||
| 217 | glDrawElements(GL_TRIANGLES, 36 * voxelsRendered, GL_UNSIGNED_INT, 0); | 237 | glDrawElements(GL_TRIANGLES, 36 * voxelsRendered, GL_UNSIGNED_INT, 0); |
| 218 | 238 | ||
| 219 | // deactivate vertex arrays after drawing | 239 | // deactivate vertex and color arrays after drawing |
| 220 | glDisableClientState(GL_VERTEX_ARRAY); | 240 | glDisableClientState(GL_VERTEX_ARRAY); |
| 241 | glDisableClientState(GL_COLOR_ARRAY); | ||
| 221 | 242 | ||
| 222 | // bind with 0 to switch back to normal operation | 243 | // bind with 0 to switch back to normal operation |
| 223 | glBindBuffer(GL_ARRAY_BUFFER, 0); | 244 | glBindBuffer(GL_ARRAY_BUFFER, 0); |