History

Added normals to voxel geometry, enabling lighting.

Andrzej Kapolka committed 13 years ago

Commit 63a581d1 parent f00a3082 files · +92 −66Browse files

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

Unified · Split

interface/src/VoxelSystem.cpp+89−64
@@ -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;
@@ -70,9 +72,9 @@ void VoxelSystem::setViewerHead(Head *newViewerHead) { · 14 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
@@ -118,7 +120,7 @@ int VoxelSystem::parseData(unsigned char* sourceBuffer, int numBytes) { · 39 unmodified lines
118120
119121 unsigned char command = *sourceBuffer;
120122 unsigned char *voxelData = sourceBuffer + 1;
121−
123+
122124 switch(command) {
123125 case PACKET_HEADER_VOXEL_DATA:
124126 // ask the VoxelTree to read the bitstream into the tree
@@ -131,7 +133,7 @@ int VoxelSystem::parseData(unsigned char* sourceBuffer, int numBytes) { · 6 unmodified lines
131133 case PACKET_HEADER_Z_COMMAND:
132134
133135 // the Z command is a special command that allows the sender to send high level semantic
134− // requests, like erase all, or add sphere scene, different receivers may handle these
136+ // requests, like erase all, or add sphere scene, different receivers may handle these
135137 // messages differently
136138 char* packetData = (char *)sourceBuffer;
137139 char* command = &packetData[1]; // start of the command
@@ -152,7 +154,7 @@ int VoxelSystem::parseData(unsigned char* sourceBuffer, int numBytes) { · 14 unmodified lines
152154 }
153155 break;
154156 }
155−
157+
156158 setupNewVoxelsForDrawing();
157159 return numBytes;
158160 }
@@ -212,11 +214,11 @@ int VoxelSystem::treeToArrays(VoxelNode *currentNode, const glm::vec3& nodePosi · 53 unmodified lines
212214 for (int i = 0; i < 8; i++) {
213215 // check if there is a child here
214216 if (currentNode->children[i] != NULL) {
215−
217+
216218 glm::vec3 childNodePosition;
217219 copyFirstVertexForCode(currentNode->children[i]->octalCode,(float*)&childNodePosition);
218220 childNodePosition *= (float)TREE_SCALE; // scale it up
219−
221+
220222 /**** disabled ************************************************************************************************
221223 // Note: Stephen, I intentionally left this in so you would talk to me about it. Here's the deal, this code
222224 // doesn't seem to work correctly. It returns X and Z flipped and the values are negative. Since we use the
@@ -226,7 +228,7 @@ int VoxelSystem::treeToArrays(VoxelNode *currentNode, const glm::vec3& nodePosi · 3 unmodified lines
226228 // calculate the child's position based on the parent position
227229 for (int j = 0; j < 3; j++) {
228230 childNodePosition[j] = nodePosition[j];
229−
231+
230232 if (oneAtBit(branchIndexWithDescendant(currentNode->octalCode,currentNode->children[i]->octalCode),(7 - j))) {
231233 childNodePosition[j] -= (powf(0.5, *currentNode->children[i]->octalCode) * TREE_SCALE);
232234 }
@@ -243,13 +245,13 @@ int VoxelSystem::treeToArrays(VoxelNode *currentNode, const glm::vec3& nodePosi · 10 unmodified lines
243245 float startVertex[3];
244246 copyFirstVertexForCode(currentNode->octalCode,(float*)&startVertex);
245247 float voxelScale = 1 / powf(2, *currentNode->octalCode);
246−
248+
247249 // populate the array with points for the 8 vertices
248250 // and RGB color for each added vertex
249251 for (int j = 0; j < VERTEX_POINTS_PER_VOXEL; j++ ) {
250252 *writeVerticesEndPointer = startVertex[j % 3] + (identityVertices[j] * voxelScale);
251253 *(writeColorsArray + (writeVerticesEndPointer - writeVerticesArray)) = currentNode->getColor()[j % 3];
252−
254+
253255 writeVerticesEndPointer++;
254256 }
255257 voxelsAdded++;
@@ -271,7 +273,7 @@ void VoxelSystem::init() { · 15 unmodified lines
271273 readColorsArray = new GLubyte[VERTEX_POINTS_PER_VOXEL * MAX_VOXELS_PER_SYSTEM];
272274
273275 GLuint *indicesArray = new GLuint[INDICES_PER_VOXEL * MAX_VOXELS_PER_SYSTEM];
274−
276+
275277 // populate the indicesArray
276278 // this will not change given new voxels, so we can set it all up now
277279 for (int n = 0; n < MAX_VOXELS_PER_SYSTEM; n++) {
@@ -279,84 +281,107 @@ void VoxelSystem::init() { · 1 unmodified line
279281 int voxelIndexOffset = n * INDICES_PER_VOXEL;
280282 GLuint *currentIndicesPos = indicesArray + voxelIndexOffset;
281283 int startIndex = (n * VERTICES_PER_VOXEL);
282−
284+
283285 for (int i = 0; i < INDICES_PER_VOXEL; i++) {
284286 // add indices for this side of the cube
285287 currentIndicesPos[i] = startIndex + identityIndices[i];
286288 }
287289 }
288−
290+
291+ GLfloat *normalsArray = new GLfloat[VERTEX_POINTS_PER_VOXEL * MAX_VOXELS_PER_SYSTEM];
292+ GLfloat *normalsArrayEndPointer = normalsArray;
293+
294+ // populate the normalsArray
295+ for (int n = 0; n < MAX_VOXELS_PER_SYSTEM; n++) {
296+ for (int i = 0; i < VERTEX_POINTS_PER_VOXEL; i++) {
297+ *(normalsArrayEndPointer++) = identityNormals[i];
298+ }
299+ }
300+
289301 // VBO for the verticesArray
290302 glGenBuffers(1, &vboVerticesID);
291303 glBindBuffer(GL_ARRAY_BUFFER, vboVerticesID);
292304 glBufferData(GL_ARRAY_BUFFER, VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat) * MAX_VOXELS_PER_SYSTEM, NULL, GL_DYNAMIC_DRAW);
293−
305+
306+ // VBO for the normalsArray
307+ glGenBuffers(1, &vboNormalsID);
308+ glBindBuffer(GL_ARRAY_BUFFER, vboNormalsID);
309+ glBufferData(GL_ARRAY_BUFFER,
310+ VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat) * MAX_VOXELS_PER_SYSTEM,
311+ normalsArray, GL_STATIC_DRAW);
312+
294313 // VBO for colorsArray
295314 glGenBuffers(1, &vboColorsID);
296315 glBindBuffer(GL_ARRAY_BUFFER, vboColorsID);
297316 glBufferData(GL_ARRAY_BUFFER, VERTEX_POINTS_PER_VOXEL * sizeof(GLubyte) * MAX_VOXELS_PER_SYSTEM, NULL, GL_DYNAMIC_DRAW);
298−
317+
299318 // VBO for the indicesArray
300319 glGenBuffers(1, &vboIndicesID);
301320 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboIndicesID);
302321 glBufferData(GL_ELEMENT_ARRAY_BUFFER,
303322 INDICES_PER_VOXEL * sizeof(GLuint) * MAX_VOXELS_PER_SYSTEM,
304323 indicesArray, GL_STATIC_DRAW);
305−
306− // delete the indices array that is no longer needed
324+
325+ // delete the indices and normals arrays that are no longer needed
307326 delete[] indicesArray;
327+ delete[] normalsArray;
308328 }
309329
310330 void VoxelSystem::render() {
311331
312332 glPushMatrix();
313−
333+
314334 if (readVerticesEndPointer != readVerticesArray) {
315335 // try to lock on the buffer write
316336 // just avoid pulling new data if it is currently being written
317337 if (pthread_mutex_trylock(&bufferWriteLock) == 0) {
318−
338+
319339 glBindBuffer(GL_ARRAY_BUFFER, vboVerticesID);
320340 glBufferSubData(GL_ARRAY_BUFFER, 0, (readVerticesEndPointer - readVerticesArray) * sizeof(GLfloat), readVerticesArray);
321−
341+
322342 glBindBuffer(GL_ARRAY_BUFFER, vboColorsID);
323343 glBufferSubData(GL_ARRAY_BUFFER, 0, (readVerticesEndPointer - readVerticesArray) * sizeof(GLubyte), readColorsArray);
324−
344+
325345 readVerticesEndPointer = readVerticesArray;
326−
346+
327347 pthread_mutex_unlock(&bufferWriteLock);
328348 }
329349 }
330350
331351 // tell OpenGL where to find vertex and color information
332352 glEnableClientState(GL_VERTEX_ARRAY);
353+ glEnableClientState(GL_NORMAL_ARRAY);
333354 glEnableClientState(GL_COLOR_ARRAY);
334−
355+
335356 glBindBuffer(GL_ARRAY_BUFFER, vboVerticesID);
336357 glVertexPointer(3, GL_FLOAT, 0, 0);
337−
358+
359+ glBindBuffer(GL_ARRAY_BUFFER, vboNormalsID);
360+ glNormalPointer(GL_FLOAT, 0, 0);
361+
338362 glBindBuffer(GL_ARRAY_BUFFER, vboColorsID);
339363 glColorPointer(3, GL_UNSIGNED_BYTE, 0, 0);
340−
364+
341365 // draw the number of voxels we have
342366 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboIndicesID);
343367 glScalef(10, 10, 10);
344368 glDrawElements(GL_TRIANGLES, 36 * voxelsRendered, GL_UNSIGNED_INT, 0);
345−
369+
346370 // deactivate vertex and color arrays after drawing
347371 glDisableClientState(GL_VERTEX_ARRAY);
372+ glDisableClientState(GL_NORMAL_ARRAY);
348373 glDisableClientState(GL_COLOR_ARRAY);
349−
374+
350375 // bind with 0 to switch back to normal operation
351376 glBindBuffer(GL_ARRAY_BUFFER, 0);
352377 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
353−
378+
354379 // scale back down to 1 so heads aren't massive
355380 glPopMatrix();
356381 }
357382
358383 void VoxelSystem::simulate(float deltaTime) {
359−
384+
360385 }
361386
362387 int VoxelSystem::_nodeCount = 0;
@@ -367,7 +392,7 @@ bool VoxelSystem::randomColorOperation(VoxelNode* node, bool down, void* extraDa · 4 unmodified lines
367392 if (down) {
368393 return true;
369394 }
370−
395+
371396 _nodeCount++;
372397 if (node->isColored()) {
373398 nodeColor newColor = { 0,0,0,1 };
@@ -398,9 +423,9 @@ bool VoxelSystem::falseColorizeRandomOperation(VoxelNode* node, bool down, void* · 24 unmodified lines
398423 if (down) {
399424 return true;
400425 }
401−
426+
402427 _nodeCount++;
403−
428+
404429 // always false colorize
405430 unsigned char newR = randomColorValue(150);
406431 unsigned char newG = randomColorValue(150);
@@ -427,7 +452,7 @@ bool VoxelSystem::trueColorizeOperation(VoxelNode* node, bool down, void* extraD · 20 unmodified lines
427452 if (down) {
428453 return true;
429454 }
430−
455+
431456 _nodeCount++;
432457 node->setFalseColored(false);
433458 //printf("setting true color for node %d\n",_nodeCount);
@@ -448,23 +473,23 @@ bool VoxelSystem::falseColorizeInViewOperation(VoxelNode* node, bool down, void* · 14 unmodified lines
448473 if (down) {
449474 return true;
450475 }
451−
476+
452477 ViewFrustum* viewFrustum = (ViewFrustum*) extraData;
453−
478+
454479 _nodeCount++;
455−
480+
456481 // only do this for truely colored voxels...
457482 if (node->isColored()) {
458483 // first calculate the AAbox for the voxel
459484 AABox voxelBox;
460485 node->getAABox(voxelBox);
461−
486+
462487 voxelBox.scale(TREE_SCALE);
463−
488+
464489 printf("voxelBox corner=(%f,%f,%f) x=%f\n",
465490 voxelBox.getCorner().x, voxelBox.getCorner().y, voxelBox.getCorner().z,
466491 voxelBox.getSize().x);
467−
492+
468493 // If the voxel is outside of the view frustum, then false color it red
469494 if (ViewFrustum::OUTSIDE == viewFrustum->boxInFrustum(voxelBox)) {
470495 // Out of view voxels are colored RED
@@ -481,7 +506,7 @@ bool VoxelSystem::falseColorizeInViewOperation(VoxelNode* node, bool down, void* · 10 unmodified lines
481506 } else {
482507 printf("voxel not colored, don't consider it\n");
483508 }
484−
509+
485510 return true; // keep going!
486511 }
487512
@@ -503,7 +528,7 @@ bool VoxelSystem::falseColorizeDistanceFromViewOperation(VoxelNode* node, bool d · 15 unmodified lines
503528 }
504529
505530 ViewFrustum* viewFrustum = (ViewFrustum*) extraData;
506−
531+
507532 // only do this for truly colored voxels...
508533 if (node->isColored()) {
509534
@@ -517,10 +542,10 @@ bool VoxelSystem::falseColorizeDistanceFromViewOperation(VoxelNode* node, bool d · 7 unmodified lines
517542
518543 // scale up the node position
519544 nodePosition = nodePosition*(float)TREE_SCALE;
520−
545+
521546 float halfUnitForVoxel = powf(0.5, *node->octalCode) * (0.5 * TREE_SCALE);
522547 glm::vec3 viewerPosition = viewFrustum->getPosition();
523−
548+
524549 //printf("halfUnitForVoxel=%f\n",halfUnitForVoxel);
525550 //printf("viewer.x=%f y=%f z=%f \n", viewerPosition.x, viewerPosition.y, viewerPosition.z);
526551 //printf("node.x=%f y=%f z=%f \n", nodePosition.x, nodePosition.y, nodePosition.z);
@@ -533,7 +558,7 @@ bool VoxelSystem::falseColorizeDistanceFromViewOperation(VoxelNode* node, bool d · 6 unmodified lines
533558 _nodeCount++;
534559
535560 float distanceRatio = (_minDistance==_maxDistance) ? 1 : (distance - _minDistance)/(_maxDistance - _minDistance);
536−
561+
537562 // We want to colorize this in 16 bug chunks of color
538563 const unsigned char maxColor = 255;
539564 const unsigned char colorBands = 16;
@@ -542,7 +567,7 @@ bool VoxelSystem::falseColorizeDistanceFromViewOperation(VoxelNode* node, bool d · 2 unmodified lines
542567 unsigned char newR = (colorBand*(gradientOver/colorBands))+(maxColor-gradientOver);
543568 unsigned char newG = 0;
544569 unsigned char newB = 0;
545− //printf("Setting color down=%s distance=%f min=%f max=%f distanceRatio=%f color=%d \n",
570+ //printf("Setting color down=%s distance=%f min=%f max=%f distanceRatio=%f color=%d \n",
546571 // (down ? "TRUE" : "FALSE"), distance, _minDistance, _maxDistance, distanceRatio, (int)newR);
547572
548573 node->setFalseColor(newR,newG,newB);
@@ -568,7 +593,7 @@ bool VoxelSystem::getDistanceFromViewRangeOperation(VoxelNode* node, bool down, · 19 unmodified lines
568593 //printf("getDistanceFromViewRangeOperation() down=%s\n",(down ? "TRUE" : "FALSE"));
569594
570595 ViewFrustum* viewFrustum = (ViewFrustum*) extraData;
571−
596+
572597 // only do this for truly colored voxels...
573598 if (node->isColored()) {
574599
@@ -614,7 +639,7 @@ void VoxelSystem::falseColorizeDistanceFromView(ViewFrustum* viewFrustum) { · 39 unmodified lines
614639 printf("determining distance range for %d nodes\n",_nodeCount);
615640
616641 _nodeCount = 0;
617−
642+
618643 tree->recurseTreeWithOperation(falseColorizeDistanceFromViewOperation,(void*)viewFrustum);
619644 printf("setting in distance false color for %d nodes\n",_nodeCount);
620645 setupNewVoxelsForDrawing();

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t