History

cleaned up some code for distanceToCamera()

ZappoMan committed 13 years ago

Commit 62e7c031 parent cfcfacf1 file · +2 −61Browse files

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

Unified · Split

interface/src/VoxelSystem.cpp+2−61
@@ -299,7 +299,6 @@ void VoxelSystem::updateVBOs() { · 298 unmodified lines
299 if (_voxelsDirty) {299 if (_voxelsDirty) {
300 glBufferIndex segmentStart = 0;300 glBufferIndex segmentStart = 0;
301 glBufferIndex segmentEnd = 0;301 glBufferIndex segmentEnd = 0;
302
303 bool inSegment = false;302 bool inSegment = false;
304 for (glBufferIndex i = 0; i < _voxelsInArrays; i++) {303 for (glBufferIndex i = 0; i < _voxelsInArrays; i++) {
305 if (!inSegment) {304 if (!inSegment) {
@@ -313,20 +312,14 @@ void VoxelSystem::updateVBOs() { · 7 unmodified lines
313 segmentEnd = i;312 segmentEnd = i;
314 inSegment = false;313 inSegment = false;
315 int segmentLength = (segmentEnd - segmentStart) + 1;314 int segmentLength = (segmentEnd - segmentStart) + 1;
316
317 // vertices for segment - note: we might not need to do this
318 GLintptr segmentStartAt = segmentStart * VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat);315 GLintptr segmentStartAt = segmentStart * VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat);
319 GLsizeiptr segmentSizeBytes = segmentLength * VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat);316 GLsizeiptr segmentSizeBytes = segmentLength * VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat);
320 GLfloat* readVerticesFrom = _readVerticesArray + (segmentStart * VERTEX_POINTS_PER_VOXEL);317 GLfloat* readVerticesFrom = _readVerticesArray + (segmentStart * VERTEX_POINTS_PER_VOXEL);
321
322 glBindBuffer(GL_ARRAY_BUFFER, _vboVerticesID);318 glBindBuffer(GL_ARRAY_BUFFER, _vboVerticesID);
323 glBufferSubData(GL_ARRAY_BUFFER, segmentStartAt, segmentSizeBytes, readVerticesFrom);319 glBufferSubData(GL_ARRAY_BUFFER, segmentStartAt, segmentSizeBytes, readVerticesFrom);
324
325 // colors for segment
326 segmentStartAt = segmentStart * VERTEX_POINTS_PER_VOXEL * sizeof(GLubyte);320 segmentStartAt = segmentStart * VERTEX_POINTS_PER_VOXEL * sizeof(GLubyte);
327 segmentSizeBytes = segmentLength * VERTEX_POINTS_PER_VOXEL * sizeof(GLubyte);321 segmentSizeBytes = segmentLength * VERTEX_POINTS_PER_VOXEL * sizeof(GLubyte);
328 GLubyte* readColorsFrom = _readColorsArray + (segmentStart * VERTEX_POINTS_PER_VOXEL);322 GLubyte* readColorsFrom = _readColorsArray + (segmentStart * VERTEX_POINTS_PER_VOXEL);
329
330 glBindBuffer(GL_ARRAY_BUFFER, _vboColorsID);323 glBindBuffer(GL_ARRAY_BUFFER, _vboColorsID);
331 glBufferSubData(GL_ARRAY_BUFFER, segmentStartAt, segmentSizeBytes, readColorsFrom);324 glBufferSubData(GL_ARRAY_BUFFER, segmentStartAt, segmentSizeBytes, readColorsFrom);
332 }325 }
@@ -378,7 +371,6 @@ void VoxelSystem::simulate(float deltaTime) { · 45 unmodified lines
378int VoxelSystem::_nodeCount = 0;371int VoxelSystem::_nodeCount = 0;
379 372
380bool VoxelSystem::randomColorOperation(VoxelNode* node, void* extraData) {373bool VoxelSystem::randomColorOperation(VoxelNode* node, void* extraData) {
381
382 _nodeCount++;374 _nodeCount++;
383 if (node->isColored()) {375 if (node->isColored()) {
384 nodeColor newColor = { 0,0,0,1 };376 nodeColor newColor = { 0,0,0,1 };
@@ -398,15 +390,12 @@ void VoxelSystem::randomizeVoxelColors() { · 13 unmodified lines
398}390}
399 391
400bool VoxelSystem::falseColorizeRandomOperation(VoxelNode* node, void* extraData) {392bool VoxelSystem::falseColorizeRandomOperation(VoxelNode* node, void* extraData) {
401
402 _nodeCount++;393 _nodeCount++;
403
404 // always false colorize394 // always false colorize
405 unsigned char newR = randomColorValue(150);395 unsigned char newR = randomColorValue(150);
406 unsigned char newG = randomColorValue(150);396 unsigned char newG = randomColorValue(150);
407 unsigned char newB = randomColorValue(150);397 unsigned char newB = randomColorValue(150);
408 node->setFalseColor(newR,newG,newB);398 node->setFalseColor(newR,newG,newB);
409
410 return true; // keep going!399 return true; // keep going!
411}400}
412 401
@@ -433,12 +422,8 @@ void VoxelSystem::trueColorize() { · 20 unmodified lines
433// Will false colorize voxels that are not in view422// Will false colorize voxels that are not in view
434bool VoxelSystem::falseColorizeInViewOperation(VoxelNode* node, void* extraData) {423bool VoxelSystem::falseColorizeInViewOperation(VoxelNode* node, void* extraData) {
435 const ViewFrustum* viewFrustum = (const ViewFrustum*) extraData;424 const ViewFrustum* viewFrustum = (const ViewFrustum*) extraData;
436
437 _nodeCount++;425 _nodeCount++;
438
439 // only do this for truely colored voxels...
440 if (node->isColored()) {426 if (node->isColored()) {
441 // If the voxel is outside of the view frustum, then false color it red
442 if (!node->isInView(*viewFrustum)) {427 if (!node->isInView(*viewFrustum)) {
443 // Out of view voxels are colored RED428 // Out of view voxels are colored RED
444 unsigned char newR = 255;429 unsigned char newR = 255;
@@ -447,7 +432,6 @@ bool VoxelSystem::falseColorizeInViewOperation(VoxelNode* node, void* extraData) · 2 unmodified lines
447 node->setFalseColor(newR,newG,newB);432 node->setFalseColor(newR,newG,newB);
448 }433 }
449 }434 }
450
451 return true; // keep going!435 return true; // keep going!
452}436}
453 437
@@ -461,29 +445,9 @@ void VoxelSystem::falseColorizeInView(ViewFrustum* viewFrustum) { · 7 unmodified lines
461// Will false colorize voxels based on distance from view445// Will false colorize voxels based on distance from view
462bool VoxelSystem::falseColorizeDistanceFromViewOperation(VoxelNode* node, void* extraData) {446bool VoxelSystem::falseColorizeDistanceFromViewOperation(VoxelNode* node, void* extraData) {
463 ViewFrustum* viewFrustum = (ViewFrustum*) extraData;447 ViewFrustum* viewFrustum = (ViewFrustum*) extraData;
464
465 // only do this for truly colored voxels...
466 if (node->isColored()) {448 if (node->isColored()) {
467 glm::vec3 nodePosition;449 float distance = node->distanceToCamera(*viewFrustum);
468 float* startVertex = firstVertexForCode(node->octalCode);
469 nodePosition.x = startVertex[0];
470 nodePosition.y = startVertex[1];
471 nodePosition.z = startVertex[2];
472 delete startVertex;
473
474 // scale up the node position
475 nodePosition = nodePosition*(float)TREE_SCALE;
476
477 float halfUnitForVoxel = powf(0.5, *node->octalCode) * (0.5 * TREE_SCALE);
478 glm::vec3 viewerPosition = viewFrustum->getPosition();
479
480 float distance = sqrtf(powf(viewerPosition.x - nodePosition.x - halfUnitForVoxel, 2) +
481 powf(viewerPosition.y - nodePosition.y - halfUnitForVoxel, 2) +
482 powf(viewerPosition.z - nodePosition.z - halfUnitForVoxel, 2));
483
484 // actually colorize
485 _nodeCount++;450 _nodeCount++;
486
487 float distanceRatio = (_minDistance==_maxDistance) ? 1 : (distance - _minDistance)/(_maxDistance - _minDistance);451 float distanceRatio = (_minDistance==_maxDistance) ? 1 : (distance - _minDistance)/(_maxDistance - _minDistance);
488 452
489 // We want to colorize this in 16 bug chunks of color453 // We want to colorize this in 16 bug chunks of color
@@ -507,28 +471,9 @@ float VoxelSystem::_minDistance = FLT_MAX; · 17 unmodified lines
507// we wouldn't need to do two passes of the tree471// we wouldn't need to do two passes of the tree
508bool VoxelSystem::getDistanceFromViewRangeOperation(VoxelNode* node, void* extraData) {472bool VoxelSystem::getDistanceFromViewRangeOperation(VoxelNode* node, void* extraData) {
509 ViewFrustum* viewFrustum = (ViewFrustum*) extraData;473 ViewFrustum* viewFrustum = (ViewFrustum*) extraData;
510
511 // only do this for truly colored voxels...474 // only do this for truly colored voxels...
512 if (node->isColored()) {475 if (node->isColored()) {
513 476 float distance = node->distanceToCamera(*viewFrustum);
514 // We need our distance for both up and down
515 glm::vec3 nodePosition;
516 float* startVertex = firstVertexForCode(node->octalCode);
517 nodePosition.x = startVertex[0];
518 nodePosition.y = startVertex[1];
519 nodePosition.z = startVertex[2];
520 delete startVertex;
521
522 // scale up the node position
523 nodePosition = nodePosition*(float)TREE_SCALE;
524
525 float halfUnitForVoxel = powf(0.5, *node->octalCode) * (0.5 * TREE_SCALE);
526 glm::vec3 viewerPosition = viewFrustum->getPosition();
527
528 float distance = sqrtf(powf(viewerPosition.x - nodePosition.x - halfUnitForVoxel, 2) +
529 powf(viewerPosition.y - nodePosition.y - halfUnitForVoxel, 2) +
530 powf(viewerPosition.z - nodePosition.z - halfUnitForVoxel, 2));
531
532 // calculate the range of distances477 // calculate the range of distances
533 if (distance > _maxDistance) {478 if (distance > _maxDistance) {
534 _maxDistance = distance;479 _maxDistance = distance;
@@ -536,7 +481,6 @@ bool VoxelSystem::getDistanceFromViewRangeOperation(VoxelNode* node, void* extra · 1 unmodified line
536 if (distance < _minDistance) {481 if (distance < _minDistance) {
537 _minDistance = distance;482 _minDistance = distance;
538 }483 }
539
540 _nodeCount++;484 _nodeCount++;
541 }485 }
542 return true; // keep going!486 return true; // keep going!
@@ -544,14 +488,11 @@ bool VoxelSystem::getDistanceFromViewRangeOperation(VoxelNode* node, void* extra · 1 unmodified line
544 488
545void VoxelSystem::falseColorizeDistanceFromView(ViewFrustum* viewFrustum) {489void VoxelSystem::falseColorizeDistanceFromView(ViewFrustum* viewFrustum) {
546 _nodeCount = 0;490 _nodeCount = 0;
547
548 _maxDistance = 0.0;491 _maxDistance = 0.0;
549 _minDistance = FLT_MAX;492 _minDistance = FLT_MAX;
550 _tree->recurseTreeWithOperation(getDistanceFromViewRangeOperation,(void*)viewFrustum);493 _tree->recurseTreeWithOperation(getDistanceFromViewRangeOperation,(void*)viewFrustum);
551 printLog("determining distance range for %d nodes\n",_nodeCount);494 printLog("determining distance range for %d nodes\n",_nodeCount);
552
553 _nodeCount = 0;495 _nodeCount = 0;
554
555 _tree->recurseTreeWithOperation(falseColorizeDistanceFromViewOperation,(void*)viewFrustum);496 _tree->recurseTreeWithOperation(falseColorizeDistanceFromViewOperation,(void*)viewFrustum);
556 printLog("setting in distance false color for %d nodes\n",_nodeCount);497 printLog("setting in distance false color for %d nodes\n",_nodeCount);
557 setupNewVoxelsForDrawing();498 setupNewVoxelsForDrawing();