History

parse the bitstream tree and create an in memory representation to render

Stephen Birarda committed 13 years ago

Commit 634e9c41 parent 65e96262 files · +68 −73Browse files

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

Unified · Split

interface/src/VoxelSystem.cpp+64−71
@@ -7,33 +7,32 @@ · 6 unmodified lines
77 //
88
99 #include <cstring>
10+#include <cmath>
1011 #include <SharedUtil.h>
12+#include <OctalCode.h>
1113 #include <AgentList.h>
1214 #include "VoxelSystem.h"
1315
14−const int MAX_VOXELS_PER_SYSTEM = 500000;
16+const int MAX_VOXELS_PER_SYSTEM = 250000;
1517
1618 const int VERTICES_PER_VOXEL = 8;
1719 const int VERTEX_POINTS_PER_VOXEL = 3 * VERTICES_PER_VOXEL;
18−const int COLOR_VALUES_PER_VOXEL = 3 * VERTICES_PER_VOXEL;
1920 const int INDICES_PER_VOXEL = 3 * 12;
2021
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,
2728 1, 1, 1,
28− -1, 1, 1,
29− -1, 1, -1,
30− 1, 1, -1 };
29+ 0, 1, 1 };
3130
3231 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,
3736 4,5,6, 4,6,7 };
3837
3938 VoxelSystem::VoxelSystem() {
@@ -56,48 +55,48 @@ void VoxelSystem::parseData(void *data, int size) { · 16 unmodified lines
5655 // ask the VoxelTree to read the bitstream into the tree
5756 tree->readBitstreamToTree(voxelData, size - 1);
5857
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+}
6168
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;
101100 }
102101
103102 VoxelSystem* VoxelSystem::clone() const {
@@ -107,8 +106,8 @@ VoxelSystem* VoxelSystem::clone() const { · 3 unmodified lines
107106
108107 void VoxelSystem::init() {
109108 // 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];
112111
113112 GLuint *indicesArray = new GLuint[INDICES_PER_VOXEL * MAX_VOXELS_PER_SYSTEM];
114113
@@ -134,7 +133,7 @@ void VoxelSystem::init() { · 19 unmodified lines
134133 // VBO for colorsArray
135134 glGenBuffers(1, &vboColorsID);
136135 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);
138137
139138 // VBO for the indicesArray
140139 glGenBuffers(1, &vboIndicesID);
@@ -146,23 +145,17 @@ void VoxelSystem::init() { · 5 unmodified lines
146145 }
147146
148147 void VoxelSystem::render() {
149− // check if there are new voxels to draw
150− int vertexValuesToDraw = lastAddPointer - lastDrawPointer;
151148
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) {
157150 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);
159153
160− // bind the colors VBO, copy in new data
161154 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);
163157
164− // increment the lastDrawPointer to the lastAddPointer value used for this draw
165− lastDrawPointer += vertexValuesToDraw;
158+ voxelsToRender = false;
166159 }
167160
168161 // tell OpenGL where to find vertex and color information