Showing the changes to interface/src/VoxelSystem.cpp (1 of 2 files). Show all
Unified · Split
| @@ -7,33 +7,32 @@ · 6 unmodified lines | ||
| 7 | 7 | // |
| 8 | 8 | |
| 9 | 9 | #include <cstring> |
| 10 | +#include <cmath> | |
| 10 | 11 | #include <SharedUtil.h> |
| 12 | +#include <OctalCode.h> | |
| 11 | 13 | #include <AgentList.h> |
| 12 | 14 | #include "VoxelSystem.h" |
| 13 | 15 | |
| 14 | −const int MAX_VOXELS_PER_SYSTEM = 500000; | |
| 16 | +const int MAX_VOXELS_PER_SYSTEM = 250000; | |
| 15 | 17 | |
| 16 | 18 | const int VERTICES_PER_VOXEL = 8; |
| 17 | 19 | const int VERTEX_POINTS_PER_VOXEL = 3 * VERTICES_PER_VOXEL; |
| 18 | −const int COLOR_VALUES_PER_VOXEL = 3 * VERTICES_PER_VOXEL; | |
| 19 | 20 | const int INDICES_PER_VOXEL = 3 * 12; |
| 20 | 21 | |
| 21 | −const float CUBE_WIDTH = 0.025f; | |
| 22 | − | |
| 23 | −float identityVertices[] = { -1, -1, 1, | |
| 24 | − 1, -1, 1, | |
| 25 | − 1, -1, -1, | |
| 26 | − -1, -1, -1, | |
| 22 | +float identityVertices[] = { 0, 0, 0, | |
| 23 | + 1, 0, 0, | |
| 24 | + 1, 1, 0, | |
| 25 | + 0, 1, 0, | |
| 26 | + 0, 0, 1, | |
| 27 | + 1, 0, 1, | |
| 27 | 28 | 1, 1, 1, |
| 28 | − -1, 1, 1, | |
| 29 | − -1, 1, -1, | |
| 30 | − 1, 1, -1 }; | |
| 29 | + 0, 1, 1 }; | |
| 31 | 30 | |
| 32 | 31 | GLubyte identityIndices[] = { 0,1,2, 0,2,3, |
| 33 | − 0,4,1, 0,4,5, | |
| 34 | − 0,3,6, 0,5,6, | |
| 35 | − 1,2,4, 2,4,7, | |
| 36 | − 2,3,6, 2,6,7, | |
| 32 | + 0,1,5, 0,4,5, | |
| 33 | + 0,3,7, 0,4,7, | |
| 34 | + 1,2,6, 1,5,6, | |
| 35 | + 2,3,7, 2,6,7, | |
| 37 | 36 | 4,5,6, 4,6,7 }; |
| 38 | 37 | |
| 39 | 38 | VoxelSystem::VoxelSystem() { |
| @@ -56,48 +55,48 @@ void VoxelSystem::parseData(void *data, int size) { · 16 unmodified lines | ||
| 56 | 55 | // ask the VoxelTree to read the bitstream into the tree |
| 57 | 56 | tree->readBitstreamToTree(voxelData, size - 1); |
| 58 | 57 | |
| 59 | − // output the VoxelTree data to see if it matches the server | |
| 60 | − tree->printTreeForDebugging(tree->rootNode); | |
| 58 | + // reset the verticesEndPointer so we're writing to the beginning of the array | |
| 59 | + verticesEndPointer = verticesArray; | |
| 60 | + | |
| 61 | + // call recursive function to populate in memory arrays | |
| 62 | + // it will return the number of voxels added | |
| 63 | + voxelsRendered = treeToArrays(tree->rootNode); | |
| 64 | + | |
| 65 | + // set the boolean if there are any voxels to be rendered so we re-fill the VBOs | |
| 66 | + voxelsToRender = (voxelsRendered > 0); | |
| 67 | +} | |
| 61 | 68 | |
| 62 | −// // ignore the first char, it's a V to tell us that this is voxel data | |
| 63 | −// char *voxelDataPtr = (char *) data + 1; | |
| 64 | −// | |
| 65 | −// GLfloat *position = new GLfloat[3]; | |
| 66 | −// GLubyte *color = new GLubyte[3]; | |
| 67 | −// | |
| 68 | −// // get pointers to position of last append of data | |
| 69 | −// GLfloat *parseVerticesPtr = lastAddPointer; | |
| 70 | −// GLubyte *parseColorsPtr = colorsArray + (lastAddPointer - verticesArray); | |
| 71 | −// | |
| 72 | −// int voxelsInData = 0; | |
| 73 | −// | |
| 74 | −// // pull voxels out of the received data and put them into our internal memory structure | |
| 75 | −// while ((voxelDataPtr - (char *) data) < size) { | |
| 76 | −// | |
| 77 | −// memcpy(position, voxelDataPtr, 3 * sizeof(float)); | |
| 78 | −// voxelDataPtr += 3 * sizeof(float); | |
| 79 | −// memcpy(color, voxelDataPtr, 3); | |
| 80 | −// voxelDataPtr += 3; | |
| 81 | −// | |
| 82 | −// for (int v = 0; v < VERTEX_POINTS_PER_VOXEL; v++) { | |
| 83 | −// parseVerticesPtr[v] = position[v % 3] + (identityVertices[v] * CUBE_WIDTH); | |
| 84 | −// } | |
| 85 | −// | |
| 86 | −// parseVerticesPtr += VERTEX_POINTS_PER_VOXEL; | |
| 87 | −// | |
| 88 | −// for (int c = 0; c < COLOR_VALUES_PER_VOXEL; c++) { | |
| 89 | −// parseColorsPtr[c] = color[c % 3]; | |
| 90 | −// } | |
| 91 | −// | |
| 92 | −// parseColorsPtr += COLOR_VALUES_PER_VOXEL; | |
| 93 | −// | |
| 94 | −// | |
| 95 | −// voxelsInData++; | |
| 96 | −// } | |
| 97 | −// | |
| 98 | −// // increase the lastAddPointer to the new spot, increase the number of rendered voxels | |
| 99 | −// lastAddPointer = parseVerticesPtr; | |
| 100 | −// voxelsRendered += voxelsInData; | |
| 69 | +int VoxelSystem::treeToArrays(VoxelNode *currentNode) { | |
| 70 | + int voxelsAdded = 0; | |
| 71 | + | |
| 72 | + for (int i = 0; i < 8; i++) { | |
| 73 | + // check if there is a child here | |
| 74 | + if (currentNode->children[i] != NULL) { | |
| 75 | + voxelsAdded += treeToArrays(currentNode->children[i]); | |
| 76 | + } | |
| 77 | + } | |
| 78 | + | |
| 79 | + // if we didn't get any voxels added then we're a leaf | |
| 80 | + // add our vertex and color information to the interleaved array | |
| 81 | + if (voxelsAdded == 0) { | |
| 82 | + float * startVertex = firstVertexForCode(currentNode->octalCode); | |
| 83 | + float voxelScale = 1 / powf(2, *currentNode->octalCode); | |
| 84 | + | |
| 85 | + printf("Adding a voxel at %f, %f, %f\n", startVertex[0], startVertex[1], startVertex[2]); | |
| 86 | + | |
| 87 | + // populate the array with points for the 8 vertices | |
| 88 | + // and RGB color for each added vertex | |
| 89 | + for (int j = 0; j < VERTEX_POINTS_PER_VOXEL; j++ ) { | |
| 90 | + *verticesEndPointer = startVertex[j % 3] + (identityVertices[j] * voxelScale); | |
| 91 | + *(colorsArray + (verticesEndPointer - verticesArray)) = currentNode->color[j % 3]; | |
| 92 | + | |
| 93 | + verticesEndPointer++; | |
| 94 | + } | |
| 95 | + | |
| 96 | + voxelsAdded++; | |
| 97 | + } | |
| 98 | + | |
| 99 | + return voxelsAdded; | |
| 101 | 100 | } |
| 102 | 101 | |
| 103 | 102 | VoxelSystem* VoxelSystem::clone() const { |
| @@ -107,8 +106,8 @@ VoxelSystem* VoxelSystem::clone() const { · 3 unmodified lines | ||
| 107 | 106 | |
| 108 | 107 | void VoxelSystem::init() { |
| 109 | 108 | // prep the data structures for incoming voxel data |
| 110 | − lastDrawPointer = lastAddPointer = verticesArray = new GLfloat[VERTEX_POINTS_PER_VOXEL * MAX_VOXELS_PER_SYSTEM]; | |
| 111 | − colorsArray = new GLubyte[COLOR_VALUES_PER_VOXEL * MAX_VOXELS_PER_SYSTEM]; | |
| 109 | + verticesArray = new GLfloat[VERTEX_POINTS_PER_VOXEL * MAX_VOXELS_PER_SYSTEM]; | |
| 110 | + colorsArray = new GLubyte[VERTEX_POINTS_PER_VOXEL * MAX_VOXELS_PER_SYSTEM]; | |
| 112 | 111 | |
| 113 | 112 | GLuint *indicesArray = new GLuint[INDICES_PER_VOXEL * MAX_VOXELS_PER_SYSTEM]; |
| 114 | 113 | |
| @@ -134,7 +133,7 @@ void VoxelSystem::init() { · 19 unmodified lines | ||
| 134 | 133 | // VBO for colorsArray |
| 135 | 134 | glGenBuffers(1, &vboColorsID); |
| 136 | 135 | glBindBuffer(GL_ARRAY_BUFFER, vboColorsID); |
| 137 | − glBufferData(GL_ARRAY_BUFFER, COLOR_VALUES_PER_VOXEL * sizeof(GLubyte) * MAX_VOXELS_PER_SYSTEM, NULL, GL_DYNAMIC_DRAW); | |
| 136 | + glBufferData(GL_ARRAY_BUFFER, VERTEX_POINTS_PER_VOXEL * sizeof(GLubyte) * MAX_VOXELS_PER_SYSTEM, NULL, GL_DYNAMIC_DRAW); | |
| 138 | 137 | |
| 139 | 138 | // VBO for the indicesArray |
| 140 | 139 | glGenBuffers(1, &vboIndicesID); |
| @@ -146,23 +145,17 @@ void VoxelSystem::init() { · 5 unmodified lines | ||
| 146 | 145 | } |
| 147 | 146 | |
| 148 | 147 | void VoxelSystem::render() { |
| 149 | − // check if there are new voxels to draw | |
| 150 | − int vertexValuesToDraw = lastAddPointer - lastDrawPointer; | |
| 151 | 148 | |
| 152 | − if (vertexValuesToDraw > 0) { | |
| 153 | − // calculate the offset into each VBO, in vertex point values | |
| 154 | − int vertexBufferOffset = lastDrawPointer - verticesArray; | |
| 155 | − | |
| 156 | − // bind the vertices VBO, copy in new data | |
| 149 | + if (voxelsToRender) { | |
| 157 | 150 | glBindBuffer(GL_ARRAY_BUFFER, vboVerticesID); |
| 158 | − glBufferSubData(GL_ARRAY_BUFFER, vertexBufferOffset * sizeof(GLfloat), vertexValuesToDraw * sizeof(GLfloat), lastDrawPointer); | |
| 151 | + glBufferData(GL_ARRAY_BUFFER, VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat) * MAX_VOXELS_PER_SYSTEM, NULL, GL_DYNAMIC_DRAW); | |
| 152 | + glBufferSubData(GL_ARRAY_BUFFER, 0, (verticesEndPointer - verticesArray) * sizeof(GLfloat), verticesArray); | |
| 159 | 153 | |
| 160 | − // bind the colors VBO, copy in new data | |
| 161 | 154 | glBindBuffer(GL_ARRAY_BUFFER, vboColorsID); |
| 162 | − glBufferSubData(GL_ARRAY_BUFFER, vertexBufferOffset * sizeof(GLubyte), vertexValuesToDraw * sizeof(GLubyte), (colorsArray + (lastDrawPointer - verticesArray))); | |
| 155 | + glBufferData(GL_ARRAY_BUFFER, VERTEX_POINTS_PER_VOXEL * sizeof(GLubyte) * MAX_VOXELS_PER_SYSTEM, NULL, GL_DYNAMIC_DRAW); | |
| 156 | + glBufferSubData(GL_ARRAY_BUFFER, 0, (verticesEndPointer - verticesArray) * sizeof(GLubyte), colorsArray); | |
| 163 | 157 | |
| 164 | − // increment the lastDrawPointer to the lastAddPointer value used for this draw | |
| 165 | − lastDrawPointer += vertexValuesToDraw; | |
| 158 | + voxelsToRender = false; | |
| 166 | 159 | } |
| 167 | 160 | |
| 168 | 161 | // tell OpenGL where to find vertex and color information |