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

This commit is contained in:
Stephen Birarda 2013-03-20 13:25:11 -07:00
parent 65e9626e72
commit 634e9c4bf7
2 changed files with 68 additions and 73 deletions

View file

@ -7,33 +7,32 @@
// //
#include <cstring> #include <cstring>
#include <cmath>
#include <SharedUtil.h> #include <SharedUtil.h>
#include <OctalCode.h>
#include <AgentList.h> #include <AgentList.h>
#include "VoxelSystem.h" #include "VoxelSystem.h"
const int MAX_VOXELS_PER_SYSTEM = 500000; const int MAX_VOXELS_PER_SYSTEM = 250000;
const int VERTICES_PER_VOXEL = 8; const int VERTICES_PER_VOXEL = 8;
const int VERTEX_POINTS_PER_VOXEL = 3 * VERTICES_PER_VOXEL; const int VERTEX_POINTS_PER_VOXEL = 3 * VERTICES_PER_VOXEL;
const int COLOR_VALUES_PER_VOXEL = 3 * VERTICES_PER_VOXEL;
const int INDICES_PER_VOXEL = 3 * 12; const int INDICES_PER_VOXEL = 3 * 12;
const float CUBE_WIDTH = 0.025f; float identityVertices[] = { 0, 0, 0,
1, 0, 0,
float identityVertices[] = { -1, -1, 1, 1, 1, 0,
1, -1, 1, 0, 1, 0,
1, -1, -1, 0, 0, 1,
-1, -1, -1, 1, 0, 1,
1, 1, 1, 1, 1, 1,
-1, 1, 1, 0, 1, 1 };
-1, 1, -1,
1, 1, -1 };
GLubyte identityIndices[] = { 0,1,2, 0,2,3, GLubyte identityIndices[] = { 0,1,2, 0,2,3,
0,4,1, 0,4,5, 0,1,5, 0,4,5,
0,3,6, 0,5,6, 0,3,7, 0,4,7,
1,2,4, 2,4,7, 1,2,6, 1,5,6,
2,3,6, 2,6,7, 2,3,7, 2,6,7,
4,5,6, 4,6,7 }; 4,5,6, 4,6,7 };
VoxelSystem::VoxelSystem() { VoxelSystem::VoxelSystem() {
@ -56,48 +55,48 @@ void VoxelSystem::parseData(void *data, int size) {
// ask the VoxelTree to read the bitstream into the tree // ask the VoxelTree to read the bitstream into the tree
tree->readBitstreamToTree(voxelData, size - 1); tree->readBitstreamToTree(voxelData, size - 1);
// output the VoxelTree data to see if it matches the server // reset the verticesEndPointer so we're writing to the beginning of the array
tree->printTreeForDebugging(tree->rootNode); verticesEndPointer = verticesArray;
// call recursive function to populate in memory arrays
// it will return the number of voxels added
voxelsRendered = treeToArrays(tree->rootNode);
// set the boolean if there are any voxels to be rendered so we re-fill the VBOs
voxelsToRender = (voxelsRendered > 0);
}
// // ignore the first char, it's a V to tell us that this is voxel data int VoxelSystem::treeToArrays(VoxelNode *currentNode) {
// char *voxelDataPtr = (char *) data + 1; int voxelsAdded = 0;
//
// GLfloat *position = new GLfloat[3]; for (int i = 0; i < 8; i++) {
// GLubyte *color = new GLubyte[3]; // check if there is a child here
// if (currentNode->children[i] != NULL) {
// // get pointers to position of last append of data voxelsAdded += treeToArrays(currentNode->children[i]);
// GLfloat *parseVerticesPtr = lastAddPointer; }
// GLubyte *parseColorsPtr = colorsArray + (lastAddPointer - verticesArray); }
//
// int voxelsInData = 0; // if we didn't get any voxels added then we're a leaf
// // add our vertex and color information to the interleaved array
// // pull voxels out of the received data and put them into our internal memory structure if (voxelsAdded == 0) {
// while ((voxelDataPtr - (char *) data) < size) { float * startVertex = firstVertexForCode(currentNode->octalCode);
// float voxelScale = 1 / powf(2, *currentNode->octalCode);
// memcpy(position, voxelDataPtr, 3 * sizeof(float));
// voxelDataPtr += 3 * sizeof(float); printf("Adding a voxel at %f, %f, %f\n", startVertex[0], startVertex[1], startVertex[2]);
// memcpy(color, voxelDataPtr, 3);
// voxelDataPtr += 3; // populate the array with points for the 8 vertices
// // and RGB color for each added vertex
// for (int v = 0; v < VERTEX_POINTS_PER_VOXEL; v++) { for (int j = 0; j < VERTEX_POINTS_PER_VOXEL; j++ ) {
// parseVerticesPtr[v] = position[v % 3] + (identityVertices[v] * CUBE_WIDTH); *verticesEndPointer = startVertex[j % 3] + (identityVertices[j] * voxelScale);
// } *(colorsArray + (verticesEndPointer - verticesArray)) = currentNode->color[j % 3];
//
// parseVerticesPtr += VERTEX_POINTS_PER_VOXEL; verticesEndPointer++;
// }
// for (int c = 0; c < COLOR_VALUES_PER_VOXEL; c++) {
// parseColorsPtr[c] = color[c % 3]; voxelsAdded++;
// } }
//
// parseColorsPtr += COLOR_VALUES_PER_VOXEL; return voxelsAdded;
//
//
// voxelsInData++;
// }
//
// // increase the lastAddPointer to the new spot, increase the number of rendered voxels
// lastAddPointer = parseVerticesPtr;
// voxelsRendered += voxelsInData;
} }
VoxelSystem* VoxelSystem::clone() const { VoxelSystem* VoxelSystem::clone() const {
@ -107,8 +106,8 @@ VoxelSystem* VoxelSystem::clone() const {
void VoxelSystem::init() { void VoxelSystem::init() {
// prep the data structures for incoming voxel data // prep the data structures for incoming voxel data
lastDrawPointer = lastAddPointer = verticesArray = new GLfloat[VERTEX_POINTS_PER_VOXEL * MAX_VOXELS_PER_SYSTEM]; verticesArray = new GLfloat[VERTEX_POINTS_PER_VOXEL * MAX_VOXELS_PER_SYSTEM];
colorsArray = new GLubyte[COLOR_VALUES_PER_VOXEL * MAX_VOXELS_PER_SYSTEM]; colorsArray = new GLubyte[VERTEX_POINTS_PER_VOXEL * MAX_VOXELS_PER_SYSTEM];
GLuint *indicesArray = new GLuint[INDICES_PER_VOXEL * MAX_VOXELS_PER_SYSTEM]; GLuint *indicesArray = new GLuint[INDICES_PER_VOXEL * MAX_VOXELS_PER_SYSTEM];
@ -134,7 +133,7 @@ void VoxelSystem::init() {
// VBO for colorsArray // VBO for colorsArray
glGenBuffers(1, &vboColorsID); glGenBuffers(1, &vboColorsID);
glBindBuffer(GL_ARRAY_BUFFER, vboColorsID); glBindBuffer(GL_ARRAY_BUFFER, vboColorsID);
glBufferData(GL_ARRAY_BUFFER, COLOR_VALUES_PER_VOXEL * sizeof(GLubyte) * MAX_VOXELS_PER_SYSTEM, NULL, GL_DYNAMIC_DRAW); glBufferData(GL_ARRAY_BUFFER, VERTEX_POINTS_PER_VOXEL * sizeof(GLubyte) * MAX_VOXELS_PER_SYSTEM, NULL, GL_DYNAMIC_DRAW);
// VBO for the indicesArray // VBO for the indicesArray
glGenBuffers(1, &vboIndicesID); glGenBuffers(1, &vboIndicesID);
@ -146,23 +145,17 @@ void VoxelSystem::init() {
} }
void VoxelSystem::render() { void VoxelSystem::render() {
// check if there are new voxels to draw
int vertexValuesToDraw = lastAddPointer - lastDrawPointer;
if (vertexValuesToDraw > 0) { if (voxelsToRender) {
// calculate the offset into each VBO, in vertex point values
int vertexBufferOffset = lastDrawPointer - verticesArray;
// bind the vertices VBO, copy in new data
glBindBuffer(GL_ARRAY_BUFFER, vboVerticesID); glBindBuffer(GL_ARRAY_BUFFER, vboVerticesID);
glBufferSubData(GL_ARRAY_BUFFER, vertexBufferOffset * sizeof(GLfloat), vertexValuesToDraw * sizeof(GLfloat), lastDrawPointer); glBufferData(GL_ARRAY_BUFFER, VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat) * MAX_VOXELS_PER_SYSTEM, NULL, GL_DYNAMIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, (verticesEndPointer - verticesArray) * sizeof(GLfloat), verticesArray);
// bind the colors VBO, copy in new data
glBindBuffer(GL_ARRAY_BUFFER, vboColorsID); glBindBuffer(GL_ARRAY_BUFFER, vboColorsID);
glBufferSubData(GL_ARRAY_BUFFER, vertexBufferOffset * sizeof(GLubyte), vertexValuesToDraw * sizeof(GLubyte), (colorsArray + (lastDrawPointer - verticesArray))); glBufferData(GL_ARRAY_BUFFER, VERTEX_POINTS_PER_VOXEL * sizeof(GLubyte) * MAX_VOXELS_PER_SYSTEM, NULL, GL_DYNAMIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, (verticesEndPointer - verticesArray) * sizeof(GLubyte), colorsArray);
// increment the lastDrawPointer to the lastAddPointer value used for this draw voxelsToRender = false;
lastDrawPointer += vertexValuesToDraw;
} }
// tell OpenGL where to find vertex and color information // tell OpenGL where to find vertex and color information

View file

@ -36,13 +36,15 @@ public:
private: private:
int voxelsRendered; int voxelsRendered;
VoxelTree *tree; VoxelTree *tree;
bool voxelsToRender;
GLfloat *verticesArray; GLfloat *verticesArray;
GLubyte *colorsArray; GLubyte *colorsArray;
GLfloat *lastAddPointer; GLfloat *verticesEndPointer;
GLfloat *lastDrawPointer;
GLuint vboVerticesID; GLuint vboVerticesID;
GLuint vboColorsID; GLuint vboColorsID;
GLuint vboIndicesID; GLuint vboIndicesID;
int treeToArrays(VoxelNode *currentNode);
}; };
#endif #endif