Showing the changes to interface/src/VoxelSystem.cpp (1 of 3 files). Show all
Unified · Split
| @@ -8,6 +8,28 @@ · 7 unmodified lines | ||
| 8 | 8 | |
| 9 | 9 | #include "VoxelSystem.h" |
| 10 | 10 | |
| 11 | +const float MAX_UNIT_ANY_AXIS = 20.0f; | |
| 12 | +const float CUBE_WIDTH = 0.05f; | |
| 13 | +const int VERTICES_PER_VOXEL = 8; | |
| 14 | +const int VERTEX_POINTS_PER_VOXEL = 3 * VERTICES_PER_VOXEL; | |
| 15 | +const int INDICES_PER_VOXEL = 3 * 12; | |
| 16 | + | |
| 17 | +GLfloat identityVertices[] = { -1, -1, 1, | |
| 18 | + 1, -1, 1, | |
| 19 | + 1, -1, -1, | |
| 20 | + -1, -1, -1, | |
| 21 | + 1, 1, 1, | |
| 22 | + -1, 1, 1, | |
| 23 | + -1, 1, -1, | |
| 24 | + 1, 1, -1 }; | |
| 25 | + | |
| 26 | +GLubyte identityIndices[] = { 0,1,2, 0,2,3, | |
| 27 | + 0,4,1, 0,4,5, | |
| 28 | + 0,3,6, 0,5,6, | |
| 29 | + 1,2,4, 2,4,7, | |
| 30 | + 2,3,6, 2,6,7, | |
| 31 | + 4,5,6, 4,6,7 }; | |
| 32 | + | |
| 11 | 33 | |
| 12 | 34 | bool onSphereShell(float radius, float scale, glm::vec3 * position) { |
| 13 | 35 | float vRadius = glm::length(*position); |
| @@ -18,6 +40,49 @@ void VoxelSystem::init() { · 4 unmodified lines | ||
| 18 | 40 | root = new Voxel; |
| 19 | 41 | } |
| 20 | 42 | |
| 43 | +void VoxelSystem::init(int numberOfRandomVoxels) { | |
| 44 | + // create the arrays needed to pass to glDrawElements later | |
| 45 | + // position / color are random for now | |
| 46 | + | |
| 47 | + voxelsRendered = numberOfRandomVoxels; | |
| 48 | + | |
| 49 | + // there are 3 points for each vertices, 24 vertices in each cube | |
| 50 | + verticesArray = new GLfloat[VERTEX_POINTS_PER_VOXEL * numberOfRandomVoxels]; | |
| 51 | + | |
| 52 | + // there are 12 triangles in each cube, with three indices for each triangle | |
| 53 | + indicesArray = new GLuint[INDICES_PER_VOXEL * numberOfRandomVoxels]; | |
| 54 | + | |
| 55 | + // new seed based on time now so voxels are different each time | |
| 56 | + srand((unsigned)time(0)); | |
| 57 | + | |
| 58 | + for (int n = 0; n < numberOfRandomVoxels; n++) { | |
| 59 | + // pick a random point for the center of the cube | |
| 60 | + glm::vec3 position = glm::vec3( | |
| 61 | + ((float) rand() / ((float) RAND_MAX / MAX_UNIT_ANY_AXIS)), | |
| 62 | + ((float) rand() / ((float) RAND_MAX / MAX_UNIT_ANY_AXIS)), | |
| 63 | + ((float) rand() / ((float) RAND_MAX / MAX_UNIT_ANY_AXIS)) | |
| 64 | + ); | |
| 65 | + | |
| 66 | + GLfloat *currentVerticesPos = verticesArray + (n * VERTEX_POINTS_PER_VOXEL); | |
| 67 | + | |
| 68 | + // fill the vertices array | |
| 69 | + for (int v = 0; v < VERTEX_POINTS_PER_VOXEL; v++) { | |
| 70 | + currentVerticesPos[v] = position[v % 3] + (identityVertices[v] * CUBE_WIDTH); | |
| 71 | + } | |
| 72 | + | |
| 73 | + // fill the indices array | |
| 74 | + int voxelIndexOffset = n * INDICES_PER_VOXEL; | |
| 75 | + GLuint *currentIndicesPos = indicesArray + voxelIndexOffset; | |
| 76 | + int startIndex = (n * VERTICES_PER_VOXEL); | |
| 77 | + | |
| 78 | + for (int i = 0; i < INDICES_PER_VOXEL; i++) { | |
| 79 | + // add indices for this side of the cube | |
| 80 | + currentIndicesPos[i] = startIndex + identityIndices[i]; | |
| 81 | + } | |
| 82 | + } | |
| 83 | + | |
| 84 | +} | |
| 85 | + | |
| 21 | 86 | // |
| 22 | 87 | // Recursively initialize the voxel tree |
| 23 | 88 | // |
| @@ -118,11 +183,18 @@ int VoxelSystem::render(Voxel * voxel, float scale, glm::vec3 * distance) { · 94 unmodified lines | ||
| 118 | 183 | return vRendered; |
| 119 | 184 | } |
| 120 | 185 | |
| 121 | −void VoxelSystem::simulate(float deltaTime) { | |
| 186 | +void VoxelSystem::render() { | |
| 187 | + glEnableClientState(GL_VERTEX_ARRAY); | |
| 188 | + glVertexPointer(3, GL_FLOAT, 0, verticesArray); | |
| 189 | + | |
| 190 | + // draw a cube | |
| 191 | + glDrawElements(GL_TRIANGLES, 36 * voxelsRendered, GL_UNSIGNED_INT, indicesArray); | |
| 122 | 192 | |
| 193 | + // deactivate vertex arrays after drawing | |
| 194 | + glDisableClientState(GL_VERTEX_ARRAY); | |
| 123 | 195 | } |
| 124 | 196 | |
| 125 | − | |
| 126 | − | |
| 127 | − | |
| 197 | +void VoxelSystem::simulate(float deltaTime) { | |
| 198 | + | |
| 199 | +} | |
| 128 | 200 | |