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

View file

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