History

Merge branch 'master' of https://github.com/worklist/hifi into view_frustum_work

ZappoMan committed 13 years ago

Commit 0bec7152 parents 785ef88 50fe1db31 files · +983 −840Browse files

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

Unified · Split

interface/src/VoxelSystem.cpp+92−67
@@ -23,25 +23,27 @@ · 22 unmodified lines
2323
2424 const int MAX_VOXELS_PER_SYSTEM = 250000;
2525
26−const int VERTICES_PER_VOXEL = 8;
26+const int VERTICES_PER_VOXEL = 24;
2727 const int VERTEX_POINTS_PER_VOXEL = 3 * VERTICES_PER_VOXEL;
2828 const int INDICES_PER_VOXEL = 3 * 12;
2929
30−float identityVertices[] = { 0, 0, 0,
31− 1, 0, 0,
32− 1, 1, 0,
33− 0, 1, 0,
34− 0, 0, 1,
35− 1, 0, 1,
36− 1, 1, 1,
37− 0, 1, 1 };
38−
39−GLubyte identityIndices[] = { 0,1,2, 0,2,3,
40− 0,1,5, 0,4,5,
41− 0,3,7, 0,4,7,
42− 1,2,6, 1,5,6,
43− 2,3,7, 2,6,7,
44− 4,5,6, 4,6,7 };
30+float identityVertices[] = { 0,0,0, 1,0,0, 1,1,0, 0,1,0, 0,0,1, 1,0,1, 1,1,1, 0,1,1,
31+ 0,0,0, 1,0,0, 1,1,0, 0,1,0, 0,0,1, 1,0,1, 1,1,1, 0,1,1,
32+ 0,0,0, 1,0,0, 1,1,0, 0,1,0, 0,0,1, 1,0,1, 1,1,1, 0,1,1 };
33+
34+GLfloat identityNormals[] = { 0,0,-1, 0,0,-1, 0,0,-1, 0,0,-1,
35+ 0,0,+1, 0,0,+1, 0,0,+1, 0,0,+1,
36+ 0,-1,0, 0,-1,0, 0,+1,0, 0,+1,0,
37+ 0,-1,0, 0,-1,0, 0,+1,0, 0,+1,0,
38+ -1,0,0, +1,0,0, +1,0,0, -1,0,0,
39+ -1,0,0, +1,0,0, +1,0,0, -1,0,0 };
40+
41+GLubyte identityIndices[] = { 0,2,1, 0,3,2, // Z- .
42+ 8,9,13, 8,13,12, // Y-
43+ 16,23,19, 16,20,23, // X-
44+ 17,18,22, 17,22,21, // X+
45+ 10,11,15, 10,15,14, // Y+
46+ 4,5,6, 4,6,7 }; // Z+ .
4547
4648 VoxelSystem::VoxelSystem() {
4749 voxelsRendered = 0;
@@ -49,7 +51,7 @@ VoxelSystem::VoxelSystem() { · 1 unmodified line
4951 pthread_mutex_init(&bufferWriteLock, NULL);
5052 }
5153
52−VoxelSystem::~VoxelSystem() {
54+VoxelSystem::~VoxelSystem() {
5355 delete[] readVerticesArray;
5456 delete[] writeVerticesArray;
5557 delete[] readColorsArray;
@@ -58,8 +60,8 @@ VoxelSystem::~VoxelSystem() { · 2 unmodified lines
5860 pthread_mutex_destroy(&bufferWriteLock);
5961 }
6062
61−void VoxelSystem::setViewerHead(Head *newViewerHead) {
62− viewerHead = newViewerHead;
63+void VoxelSystem::setViewerAvatar(Avatar *newViewerAvatar) {
64+ viewerAvatar = newViewerAvatar;
6365 }
6466
6567 //////////////////////////////////////////////////////////////////////////////////////////
@@ -70,9 +72,9 @@ void VoxelSystem::setViewerHead(Head *newViewerHead) { · 4 unmodified lines
7072 // Complaints: Brad :)
7173 // To Do: Need to add color data to the file.
7274 void VoxelSystem::loadVoxelsFile(const char* fileName, bool wantColorRandomizer) {
73−
75+
7476 tree->loadVoxelsFile(fileName,wantColorRandomizer);
75−
77+
7678 copyWrittenDataToReadArrays();
7779 }
7880
@@ -119,7 +121,7 @@ int VoxelSystem::parseData(unsigned char* sourceBuffer, int numBytes) { · 40 unmodified lines
119121
120122 unsigned char command = *sourceBuffer;
121123 unsigned char *voxelData = sourceBuffer + 1;
122−
124+
123125 switch(command) {
124126 case PACKET_HEADER_VOXEL_DATA:
125127 {
@@ -204,7 +206,7 @@ int VoxelSystem::parseData(unsigned char* sourceBuffer, int numBytes) { · 78 unmodified lines
204206 case PACKET_HEADER_Z_COMMAND:
205207
206208 // the Z command is a special command that allows the sender to send high level semantic
207− // requests, like erase all, or add sphere scene, different receivers may handle these
209+ // requests, like erase all, or add sphere scene, different receivers may handle these
208210 // messages differently
209211 char* packetData = (char *)sourceBuffer;
210212 char* command = &packetData[1]; // start of the command
@@ -225,7 +227,7 @@ int VoxelSystem::parseData(unsigned char* sourceBuffer, int numBytes) { · 14 unmodified lines
225227 }
226228 break;
227229 }
228−
230+
229231 setupNewVoxelsForDrawing();
230232 return numBytes;
231233 }
@@ -258,7 +260,7 @@ int VoxelSystem::treeToArrays(VoxelNode *currentNode, const glm::vec3& nodePosi · 26 unmodified lines
258260 int voxelsAdded = 0;
259261
260262 float halfUnitForVoxel = powf(0.5, *currentNode->octalCode) * (0.5 * TREE_SCALE);
261− glm::vec3 viewerPosition = viewerHead->getBodyPosition();
263+ glm::vec3 viewerPosition = viewerAvatar->getPosition();
262264
263265 // debug LOD code
264266 glm::vec3 debugNodePosition;
@@ -285,11 +287,11 @@ int VoxelSystem::treeToArrays(VoxelNode *currentNode, const glm::vec3& nodePosi · 20 unmodified lines
285287 for (int i = 0; i < 8; i++) {
286288 // check if there is a child here
287289 if (currentNode->children[i] != NULL) {
288−
290+
289291 glm::vec3 childNodePosition;
290292 copyFirstVertexForCode(currentNode->children[i]->octalCode,(float*)&childNodePosition);
291293 childNodePosition *= (float)TREE_SCALE; // scale it up
292−
294+
293295 /**** disabled ************************************************************************************************
294296 // Note: Stephen, I intentionally left this in so you would talk to me about it. Here's the deal, this code
295297 // doesn't seem to work correctly. It returns X and Z flipped and the values are negative. Since we use the
@@ -299,7 +301,7 @@ int VoxelSystem::treeToArrays(VoxelNode *currentNode, const glm::vec3& nodePosi · 3 unmodified lines
299301 // calculate the child's position based on the parent position
300302 for (int j = 0; j < 3; j++) {
301303 childNodePosition[j] = nodePosition[j];
302−
304+
303305 if (oneAtBit(branchIndexWithDescendant(currentNode->octalCode,currentNode->children[i]->octalCode),(7 - j))) {
304306 childNodePosition[j] -= (powf(0.5, *currentNode->children[i]->octalCode) * TREE_SCALE);
305307 }
@@ -316,13 +318,13 @@ int VoxelSystem::treeToArrays(VoxelNode *currentNode, const glm::vec3& nodePosi · 10 unmodified lines
316318 float startVertex[3];
317319 copyFirstVertexForCode(currentNode->octalCode,(float*)&startVertex);
318320 float voxelScale = 1 / powf(2, *currentNode->octalCode);
319−
321+
320322 // populate the array with points for the 8 vertices
321323 // and RGB color for each added vertex
322324 for (int j = 0; j < VERTEX_POINTS_PER_VOXEL; j++ ) {
323325 *writeVerticesEndPointer = startVertex[j % 3] + (identityVertices[j] * voxelScale);
324326 *(writeColorsArray + (writeVerticesEndPointer - writeVerticesArray)) = currentNode->getColor()[j % 3];
325−
327+
326328 writeVerticesEndPointer++;
327329 }
328330 voxelsAdded++;
@@ -344,7 +346,7 @@ void VoxelSystem::init() { · 15 unmodified lines
344346 readColorsArray = new GLubyte[VERTEX_POINTS_PER_VOXEL * MAX_VOXELS_PER_SYSTEM];
345347
346348 GLuint *indicesArray = new GLuint[INDICES_PER_VOXEL * MAX_VOXELS_PER_SYSTEM];
347−
349+
348350 // populate the indicesArray
349351 // this will not change given new voxels, so we can set it all up now
350352 for (int n = 0; n < MAX_VOXELS_PER_SYSTEM; n++) {
@@ -352,84 +354,107 @@ void VoxelSystem::init() { · 1 unmodified line
352354 int voxelIndexOffset = n * INDICES_PER_VOXEL;
353355 GLuint *currentIndicesPos = indicesArray + voxelIndexOffset;
354356 int startIndex = (n * VERTICES_PER_VOXEL);
355−
357+
356358 for (int i = 0; i < INDICES_PER_VOXEL; i++) {
357359 // add indices for this side of the cube
358360 currentIndicesPos[i] = startIndex + identityIndices[i];
359361 }
360362 }
361−
363+
364+ GLfloat *normalsArray = new GLfloat[VERTEX_POINTS_PER_VOXEL * MAX_VOXELS_PER_SYSTEM];
365+ GLfloat *normalsArrayEndPointer = normalsArray;
366+
367+ // populate the normalsArray
368+ for (int n = 0; n < MAX_VOXELS_PER_SYSTEM; n++) {
369+ for (int i = 0; i < VERTEX_POINTS_PER_VOXEL; i++) {
370+ *(normalsArrayEndPointer++) = identityNormals[i];
371+ }
372+ }
373+
362374 // VBO for the verticesArray
363375 glGenBuffers(1, &vboVerticesID);
364376 glBindBuffer(GL_ARRAY_BUFFER, vboVerticesID);
365377 glBufferData(GL_ARRAY_BUFFER, VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat) * MAX_VOXELS_PER_SYSTEM, NULL, GL_DYNAMIC_DRAW);
366−
378+
379+ // VBO for the normalsArray
380+ glGenBuffers(1, &vboNormalsID);
381+ glBindBuffer(GL_ARRAY_BUFFER, vboNormalsID);
382+ glBufferData(GL_ARRAY_BUFFER,
383+ VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat) * MAX_VOXELS_PER_SYSTEM,
384+ normalsArray, GL_STATIC_DRAW);
385+
367386 // VBO for colorsArray
368387 glGenBuffers(1, &vboColorsID);
369388 glBindBuffer(GL_ARRAY_BUFFER, vboColorsID);
370389 glBufferData(GL_ARRAY_BUFFER, VERTEX_POINTS_PER_VOXEL * sizeof(GLubyte) * MAX_VOXELS_PER_SYSTEM, NULL, GL_DYNAMIC_DRAW);
371−
390+
372391 // VBO for the indicesArray
373392 glGenBuffers(1, &vboIndicesID);
374393 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboIndicesID);
375394 glBufferData(GL_ELEMENT_ARRAY_BUFFER,
376395 INDICES_PER_VOXEL * sizeof(GLuint) * MAX_VOXELS_PER_SYSTEM,
377396 indicesArray, GL_STATIC_DRAW);
378−
379− // delete the indices array that is no longer needed
397+
398+ // delete the indices and normals arrays that are no longer needed
380399 delete[] indicesArray;
400+ delete[] normalsArray;
381401 }
382402
383403 void VoxelSystem::render() {
384404
385405 glPushMatrix();
386−
406+
387407 if (readVerticesEndPointer != readVerticesArray) {
388408 // try to lock on the buffer write
389409 // just avoid pulling new data if it is currently being written
390410 if (pthread_mutex_trylock(&bufferWriteLock) == 0) {
391−
411+
392412 glBindBuffer(GL_ARRAY_BUFFER, vboVerticesID);
393413 glBufferSubData(GL_ARRAY_BUFFER, 0, (readVerticesEndPointer - readVerticesArray) * sizeof(GLfloat), readVerticesArray);
394−
414+
395415 glBindBuffer(GL_ARRAY_BUFFER, vboColorsID);
396416 glBufferSubData(GL_ARRAY_BUFFER, 0, (readVerticesEndPointer - readVerticesArray) * sizeof(GLubyte), readColorsArray);
397−
417+
398418 readVerticesEndPointer = readVerticesArray;
399−
419+
400420 pthread_mutex_unlock(&bufferWriteLock);
401421 }
402422 }
403423
404424 // tell OpenGL where to find vertex and color information
405425 glEnableClientState(GL_VERTEX_ARRAY);
426+ glEnableClientState(GL_NORMAL_ARRAY);
406427 glEnableClientState(GL_COLOR_ARRAY);
407−
428+
408429 glBindBuffer(GL_ARRAY_BUFFER, vboVerticesID);
409430 glVertexPointer(3, GL_FLOAT, 0, 0);
410−
431+
432+ glBindBuffer(GL_ARRAY_BUFFER, vboNormalsID);
433+ glNormalPointer(GL_FLOAT, 0, 0);
434+
411435 glBindBuffer(GL_ARRAY_BUFFER, vboColorsID);
412436 glColorPointer(3, GL_UNSIGNED_BYTE, 0, 0);
413−
437+
414438 // draw the number of voxels we have
415439 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboIndicesID);
416440 glScalef(10, 10, 10);
417441 glDrawElements(GL_TRIANGLES, 36 * voxelsRendered, GL_UNSIGNED_INT, 0);
418−
442+
419443 // deactivate vertex and color arrays after drawing
420444 glDisableClientState(GL_VERTEX_ARRAY);
445+ glDisableClientState(GL_NORMAL_ARRAY);
421446 glDisableClientState(GL_COLOR_ARRAY);
422−
447+
423448 // bind with 0 to switch back to normal operation
424449 glBindBuffer(GL_ARRAY_BUFFER, 0);
425450 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
426−
451+
427452 // scale back down to 1 so heads aren't massive
428453 glPopMatrix();
429454 }
430455
431456 void VoxelSystem::simulate(float deltaTime) {
432−
457+
433458 }
434459
435460 int VoxelSystem::_nodeCount = 0;
@@ -440,7 +465,7 @@ bool VoxelSystem::randomColorOperation(VoxelNode* node, bool down, void* extraDa · 4 unmodified lines
440465 if (down) {
441466 return true;
442467 }
443−
468+
444469 _nodeCount++;
445470 if (node->isColored()) {
446471 nodeColor newColor = { 0,0,0,1 };
@@ -471,9 +496,9 @@ bool VoxelSystem::falseColorizeRandomOperation(VoxelNode* node, bool down, void* · 24 unmodified lines
471496 if (down) {
472497 return true;
473498 }
474−
499+
475500 _nodeCount++;
476−
501+
477502 // always false colorize
478503 unsigned char newR = randomColorValue(150);
479504 unsigned char newG = randomColorValue(150);
@@ -500,7 +525,7 @@ bool VoxelSystem::trueColorizeOperation(VoxelNode* node, bool down, void* extraD · 20 unmodified lines
500525 if (down) {
501526 return true;
502527 }
503−
528+
504529 _nodeCount++;
505530 node->setFalseColored(false);
506531 //printf("setting true color for node %d\n",_nodeCount);
@@ -521,23 +546,23 @@ bool VoxelSystem::falseColorizeInViewOperation(VoxelNode* node, bool down, void* · 14 unmodified lines
521546 if (down) {
522547 return true;
523548 }
524−
549+
525550 ViewFrustum* viewFrustum = (ViewFrustum*) extraData;
526−
551+
527552 _nodeCount++;
528−
553+
529554 // only do this for truely colored voxels...
530555 if (node->isColored()) {
531556 // first calculate the AAbox for the voxel
532557 AABox voxelBox;
533558 node->getAABox(voxelBox);
534−
559+
535560 voxelBox.scale(TREE_SCALE);
536−
561+
537562 printf("voxelBox corner=(%f,%f,%f) x=%f\n",
538563 voxelBox.getCorner().x, voxelBox.getCorner().y, voxelBox.getCorner().z,
539564 voxelBox.getSize().x);
540−
565+
541566 // If the voxel is outside of the view frustum, then false color it red
542567 if (ViewFrustum::OUTSIDE == viewFrustum->boxInFrustum(voxelBox)) {
543568 // Out of view voxels are colored RED
@@ -554,7 +579,7 @@ bool VoxelSystem::falseColorizeInViewOperation(VoxelNode* node, bool down, void* · 10 unmodified lines
554579 } else {
555580 printf("voxel not colored, don't consider it\n");
556581 }
557−
582+
558583 return true; // keep going!
559584 }
560585
@@ -576,7 +601,7 @@ bool VoxelSystem::falseColorizeDistanceFromViewOperation(VoxelNode* node, bool d · 15 unmodified lines
576601 }
577602
578603 ViewFrustum* viewFrustum = (ViewFrustum*) extraData;
579−
604+
580605 // only do this for truly colored voxels...
581606 if (node->isColored()) {
582607
@@ -590,10 +615,10 @@ bool VoxelSystem::falseColorizeDistanceFromViewOperation(VoxelNode* node, bool d · 7 unmodified lines
590615
591616 // scale up the node position
592617 nodePosition = nodePosition*(float)TREE_SCALE;
593−
618+
594619 float halfUnitForVoxel = powf(0.5, *node->octalCode) * (0.5 * TREE_SCALE);
595620 glm::vec3 viewerPosition = viewFrustum->getPosition();
596−
621+
597622 //printf("halfUnitForVoxel=%f\n",halfUnitForVoxel);
598623 //printf("viewer.x=%f y=%f z=%f \n", viewerPosition.x, viewerPosition.y, viewerPosition.z);
599624 //printf("node.x=%f y=%f z=%f \n", nodePosition.x, nodePosition.y, nodePosition.z);
@@ -606,7 +631,7 @@ bool VoxelSystem::falseColorizeDistanceFromViewOperation(VoxelNode* node, bool d · 6 unmodified lines
606631 _nodeCount++;
607632
608633 float distanceRatio = (_minDistance==_maxDistance) ? 1 : (distance - _minDistance)/(_maxDistance - _minDistance);
609−
634+
610635 // We want to colorize this in 16 bug chunks of color
611636 const unsigned char maxColor = 255;
612637 const unsigned char colorBands = 16;
@@ -615,7 +640,7 @@ bool VoxelSystem::falseColorizeDistanceFromViewOperation(VoxelNode* node, bool d · 2 unmodified lines
615640 unsigned char newR = (colorBand*(gradientOver/colorBands))+(maxColor-gradientOver);
616641 unsigned char newG = 0;
617642 unsigned char newB = 0;
618− //printf("Setting color down=%s distance=%f min=%f max=%f distanceRatio=%f color=%d \n",
643+ //printf("Setting color down=%s distance=%f min=%f max=%f distanceRatio=%f color=%d \n",
619644 // (down ? "TRUE" : "FALSE"), distance, _minDistance, _maxDistance, distanceRatio, (int)newR);
620645
621646 node->setFalseColor(newR,newG,newB);
@@ -641,7 +666,7 @@ bool VoxelSystem::getDistanceFromViewRangeOperation(VoxelNode* node, bool down, · 19 unmodified lines
641666 //printf("getDistanceFromViewRangeOperation() down=%s\n",(down ? "TRUE" : "FALSE"));
642667
643668 ViewFrustum* viewFrustum = (ViewFrustum*) extraData;
644−
669+
645670 // only do this for truly colored voxels...
646671 if (node->isColored()) {
647672
@@ -687,7 +712,7 @@ void VoxelSystem::falseColorizeDistanceFromView(ViewFrustum* viewFrustum) { · 39 unmodified lines
687712 printf("determining distance range for %d nodes\n",_nodeCount);
688713
689714 _nodeCount = 0;
690−
715+
691716 tree->recurseTreeWithOperation(falseColorizeDistanceFromViewOperation,(void*)viewFrustum);
692717 printf("setting in distance false color for %d nodes\n",_nodeCount);
693718 setupNewVoxelsForDrawing();