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
7//7//
8 8
9#include <cstring>9#include <cstring>
10#include <cmath>
10#include <SharedUtil.h>11#include <SharedUtil.h>
12#include <OctalCode.h>
11#include <AgentList.h>13#include <AgentList.h>
12#include "VoxelSystem.h"14#include "VoxelSystem.h"
13 15
14const int MAX_VOXELS_PER_SYSTEM = 500000;16const int MAX_VOXELS_PER_SYSTEM = 250000;
15 17
16const int VERTICES_PER_VOXEL = 8;18const int VERTICES_PER_VOXEL = 8;
17const int VERTEX_POINTS_PER_VOXEL = 3 * VERTICES_PER_VOXEL;19const int VERTEX_POINTS_PER_VOXEL = 3 * VERTICES_PER_VOXEL;
18const int COLOR_VALUES_PER_VOXEL = 3 * VERTICES_PER_VOXEL;
19const int INDICES_PER_VOXEL = 3 * 12;20const int INDICES_PER_VOXEL = 3 * 12;
20 21
21const float CUBE_WIDTH = 0.025f;22float identityVertices[] = { 0, 0, 0,
22 23 1, 0, 0,
23float identityVertices[] = { -1, -1, 1,24 1, 1, 0,
24 1, -1, 1,25 0, 1, 0,
25 1, -1, -1,26 0, 0, 1,
26 -1, -1, -1,27 1, 0, 1,
27 1, 1, 1,28 1, 1, 1,
28 -1, 1, 1,29 0, 1, 1 };
29 -1, 1, -1,
30 1, 1, -1 };
31 30
32GLubyte identityIndices[] = { 0,1,2, 0,2,3,31GLubyte identityIndices[] = { 0,1,2, 0,2,3,
33 0,4,1, 0,4,5,32 0,1,5, 0,4,5,
34 0,3,6, 0,5,6,33 0,3,7, 0,4,7,
35 1,2,4, 2,4,7,34 1,2,6, 1,5,6,
36 2,3,6, 2,6,7,35 2,3,7, 2,6,7,
37 4,5,6, 4,6,7 };36 4,5,6, 4,6,7 };
38 37
39VoxelSystem::VoxelSystem() {38VoxelSystem::VoxelSystem() {
@@ -56,48 +55,48 @@ void VoxelSystem::parseData(void *data, int size) { · 16 unmodified lines
56 // ask the VoxelTree to read the bitstream into the tree55 // ask the VoxelTree to read the bitstream into the tree
57 tree->readBitstreamToTree(voxelData, size - 1);56 tree->readBitstreamToTree(voxelData, size - 1);
58 57
59 // output the VoxelTree data to see if it matches the server58 // reset the verticesEndPointer so we're writing to the beginning of the array
60 tree->printTreeForDebugging(tree->rootNode);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 data69int VoxelSystem::treeToArrays(VoxelNode *currentNode) {
63// char *voxelDataPtr = (char *) data + 1;70 int voxelsAdded = 0;
64//71
65// GLfloat *position = new GLfloat[3];72 for (int i = 0; i < 8; i++) {
66// GLubyte *color = new GLubyte[3];73 // check if there is a child here
67//74 if (currentNode->children[i] != NULL) {
68// // get pointers to position of last append of data75 voxelsAdded += treeToArrays(currentNode->children[i]);
69// GLfloat *parseVerticesPtr = lastAddPointer;76 }
70// GLubyte *parseColorsPtr = colorsArray + (lastAddPointer - verticesArray);77 }
71// 78
72// int voxelsInData = 0; 79 // if we didn't get any voxels added then we're a leaf
73// 80 // add our vertex and color information to the interleaved array
74// // pull voxels out of the received data and put them into our internal memory structure81 if (voxelsAdded == 0) {
75// while ((voxelDataPtr - (char *) data) < size) {82 float * startVertex = firstVertexForCode(currentNode->octalCode);
76// 83 float voxelScale = 1 / powf(2, *currentNode->octalCode);
77// memcpy(position, voxelDataPtr, 3 * sizeof(float));84
78// voxelDataPtr += 3 * sizeof(float);85 printf("Adding a voxel at %f, %f, %f\n", startVertex[0], startVertex[1], startVertex[2]);
79// memcpy(color, voxelDataPtr, 3);86
80// voxelDataPtr += 3;87 // populate the array with points for the 8 vertices
81// 88 // and RGB color for each added vertex
82// for (int v = 0; v < VERTEX_POINTS_PER_VOXEL; v++) {89 for (int j = 0; j < VERTEX_POINTS_PER_VOXEL; j++ ) {
83// parseVerticesPtr[v] = position[v % 3] + (identityVertices[v] * CUBE_WIDTH);90 *verticesEndPointer = startVertex[j % 3] + (identityVertices[j] * voxelScale);
84// }91 *(colorsArray + (verticesEndPointer - verticesArray)) = currentNode->color[j % 3];
85// 92
86// parseVerticesPtr += VERTEX_POINTS_PER_VOXEL;93 verticesEndPointer++;
87// 94 }
88// for (int c = 0; c < COLOR_VALUES_PER_VOXEL; c++) {95
89// parseColorsPtr[c] = color[c % 3];96 voxelsAdded++;
90// }97 }
91// 98
92// parseColorsPtr += COLOR_VALUES_PER_VOXEL;99 return voxelsAdded;
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;
101}100}
102 101
103VoxelSystem* VoxelSystem::clone() const {102VoxelSystem* VoxelSystem::clone() const {
@@ -107,8 +106,8 @@ VoxelSystem* VoxelSystem::clone() const { · 3 unmodified lines
107 106
108void VoxelSystem::init() {107void VoxelSystem::init() {
109 // prep the data structures for incoming voxel data108 // prep the data structures for incoming voxel data
110 lastDrawPointer = lastAddPointer = verticesArray = new GLfloat[VERTEX_POINTS_PER_VOXEL * MAX_VOXELS_PER_SYSTEM];109 verticesArray = new GLfloat[VERTEX_POINTS_PER_VOXEL * MAX_VOXELS_PER_SYSTEM];
111 colorsArray = new GLubyte[COLOR_VALUES_PER_VOXEL * MAX_VOXELS_PER_SYSTEM];110 colorsArray = new GLubyte[VERTEX_POINTS_PER_VOXEL * MAX_VOXELS_PER_SYSTEM];
112 111
113 GLuint *indicesArray = new GLuint[INDICES_PER_VOXEL * MAX_VOXELS_PER_SYSTEM];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 // VBO for colorsArray133 // VBO for colorsArray
135 glGenBuffers(1, &vboColorsID);134 glGenBuffers(1, &vboColorsID);
136 glBindBuffer(GL_ARRAY_BUFFER, vboColorsID);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 // VBO for the indicesArray138 // VBO for the indicesArray
140 glGenBuffers(1, &vboIndicesID);139 glGenBuffers(1, &vboIndicesID);
@@ -146,23 +145,17 @@ void VoxelSystem::init() { · 5 unmodified lines
146}145}
147 146
148void VoxelSystem::render() {147void VoxelSystem::render() {
149 // check if there are new voxels to draw
150 int vertexValuesToDraw = lastAddPointer - lastDrawPointer;
151 148
152 if (vertexValuesToDraw > 0) {149 if (voxelsToRender) {
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
157 glBindBuffer(GL_ARRAY_BUFFER, vboVerticesID);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 glBindBuffer(GL_ARRAY_BUFFER, vboColorsID);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 draw158 voxelsToRender = false;
165 lastDrawPointer += vertexValuesToDraw;
166 }159 }
167 160
168 // tell OpenGL where to find vertex and color information161 // tell OpenGL where to find vertex and color information