Showing the changes to interface/src/VoxelSystem.cpp (1 of 6 files). Show all
Unified · Split
| @@ -19,6 +19,7 @@ · 18 unmodified lines | ||
| 19 | 19 | #include <PerfStat.h> |
| 20 | 20 | #include <OctalCode.h> |
| 21 | 21 | #include <pthread.h> |
| 22 | +#include "Application.h" | |
| 22 | 23 | #include "Log.h" |
| 23 | 24 | #include "VoxelConstants.h" |
| 24 | 25 | #include "InterfaceConfig.h" |
| @@ -37,14 +38,15 @@ GLfloat identityNormals[] = { 0,0,-1, 0,0,-1, 0,0,-1, 0,0,-1, · 12 unmodified lines | ||
| 37 | 38 | -1,0,0, +1,0,0, +1,0,0, -1,0,0, |
| 38 | 39 | -1,0,0, +1,0,0, +1,0,0, -1,0,0 }; |
| 39 | 40 | |
| 40 | −GLubyte identityIndices[] = { 0,2,1, 0,3,2, // Z- . | |
| 41 | +GLubyte identityIndices[] = { 0,2,1, 0,3,2, // Z- | |
| 41 | 42 | 8,9,13, 8,13,12, // Y- |
| 42 | 43 | 16,23,19, 16,20,23, // X- |
| 43 | 44 | 17,18,22, 17,22,21, // X+ |
| 44 | 45 | 10,11,15, 10,15,14, // Y+ |
| 45 | − 4,5,6, 4,6,7 }; // Z+ . | |
| 46 | + 4,5,6, 4,6,7 }; // Z+ | |
| 46 | 47 | |
| 47 | −VoxelSystem::VoxelSystem() : AgentData(NULL) { | |
| 48 | +VoxelSystem::VoxelSystem(float treeScale, int maxVoxels) : | |
| 49 | + AgentData(NULL), _treeScale(treeScale), _maxVoxels(maxVoxels) { | |
| 48 | 50 | _voxelsInReadArrays = _voxelsInWriteArrays = _voxelsUpdated = 0; |
| 49 | 51 | _writeRenderFullVBO = true; |
| 50 | 52 | _readRenderFullVBO = true; |
| @@ -312,12 +314,11 @@ void VoxelSystem::copyWrittenDataToReadArrays(bool fullVBOs) { · 261 unmodified lines | ||
| 312 | 314 | } |
| 313 | 315 | |
| 314 | 316 | int VoxelSystem::newTreeToArrays(VoxelNode* node) { |
| 315 | − assert(_viewFrustum); // you must set up _viewFrustum before calling this | |
| 316 | 317 | int voxelsUpdated = 0; |
| 317 | 318 | bool shouldRender = false; // assume we don't need to render it |
| 318 | 319 | // if it's colored, we might need to render it! |
| 319 | 320 | if (node->isColored()) { |
| 320 | − float distanceToNode = node->distanceToCamera(*_viewFrustum); | |
| 321 | + float distanceToNode = node->distanceToCamera(*Application::getInstance()->getViewFrustum()); | |
| 321 | 322 | float boundary = boundaryDistanceForRenderLevel(node->getLevel()); |
| 322 | 323 | float childBoundary = boundaryDistanceForRenderLevel(node->getLevel() + 1); |
| 323 | 324 | bool inBoundary = (distanceToNode <= boundary); |
| @@ -352,7 +353,7 @@ int VoxelSystem::newTreeToArrays(VoxelNode* node) { · 28 unmodified lines | ||
| 352 | 353 | |
| 353 | 354 | int VoxelSystem::updateNodeInArraysAsFullVBO(VoxelNode* node) { |
| 354 | 355 | // If we've run out of room, then just bail... |
| 355 | − if (_voxelsInWriteArrays >= MAX_VOXELS_PER_SYSTEM) { | |
| 356 | + if (_voxelsInWriteArrays >= _maxVoxels) { | |
| 356 | 357 | return 0; |
| 357 | 358 | } |
| 358 | 359 | |
| @@ -382,7 +383,7 @@ int VoxelSystem::updateNodeInArraysAsFullVBO(VoxelNode* node) { · 23 unmodified lines | ||
| 382 | 383 | |
| 383 | 384 | int VoxelSystem::updateNodeInArraysAsPartialVBO(VoxelNode* node) { |
| 384 | 385 | // If we've run out of room, then just bail... |
| 385 | − if (_voxelsInWriteArrays >= MAX_VOXELS_PER_SYSTEM) { | |
| 386 | + if (_voxelsInWriteArrays >= _maxVoxels) { | |
| 386 | 387 | return 0; |
| 387 | 388 | } |
| 388 | 389 | |
| @@ -425,6 +426,9 @@ int VoxelSystem::updateNodeInArraysAsPartialVBO(VoxelNode* node) { · 36 unmodified lines | ||
| 425 | 426 | return 0; // not-updated |
| 426 | 427 | } |
| 427 | 428 | |
| 429 | +ProgramObject* VoxelSystem::_perlinModulateProgram = 0; | |
| 430 | +GLuint VoxelSystem::_permutationNormalTextureID = 0; | |
| 431 | + | |
| 428 | 432 | void VoxelSystem::init() { |
| 429 | 433 | |
| 430 | 434 | _renderWarningsOn = false; |
| @@ -440,23 +444,23 @@ void VoxelSystem::init() { · 9 unmodified lines | ||
| 440 | 444 | _unusedArraySpace = 0; |
| 441 | 445 | |
| 442 | 446 | // we will track individual dirty sections with these arrays of bools |
| 443 | − _writeVoxelDirtyArray = new bool[MAX_VOXELS_PER_SYSTEM]; | |
| 444 | − memset(_writeVoxelDirtyArray, false, MAX_VOXELS_PER_SYSTEM * sizeof(bool)); | |
| 445 | − _readVoxelDirtyArray = new bool[MAX_VOXELS_PER_SYSTEM]; | |
| 446 | − memset(_readVoxelDirtyArray, false, MAX_VOXELS_PER_SYSTEM * sizeof(bool)); | |
| 447 | + _writeVoxelDirtyArray = new bool[_maxVoxels]; | |
| 448 | + memset(_writeVoxelDirtyArray, false, _maxVoxels * sizeof(bool)); | |
| 449 | + _readVoxelDirtyArray = new bool[_maxVoxels]; | |
| 450 | + memset(_readVoxelDirtyArray, false, _maxVoxels * sizeof(bool)); | |
| 447 | 451 | |
| 448 | 452 | // prep the data structures for incoming voxel data |
| 449 | − _writeVerticesArray = new GLfloat[VERTEX_POINTS_PER_VOXEL * MAX_VOXELS_PER_SYSTEM]; | |
| 450 | − _readVerticesArray = new GLfloat[VERTEX_POINTS_PER_VOXEL * MAX_VOXELS_PER_SYSTEM]; | |
| 453 | + _writeVerticesArray = new GLfloat[VERTEX_POINTS_PER_VOXEL * _maxVoxels]; | |
| 454 | + _readVerticesArray = new GLfloat[VERTEX_POINTS_PER_VOXEL * _maxVoxels]; | |
| 451 | 455 | |
| 452 | − _writeColorsArray = new GLubyte[VERTEX_POINTS_PER_VOXEL * MAX_VOXELS_PER_SYSTEM]; | |
| 453 | − _readColorsArray = new GLubyte[VERTEX_POINTS_PER_VOXEL * MAX_VOXELS_PER_SYSTEM]; | |
| 456 | + _writeColorsArray = new GLubyte[VERTEX_POINTS_PER_VOXEL * _maxVoxels]; | |
| 457 | + _readColorsArray = new GLubyte[VERTEX_POINTS_PER_VOXEL * _maxVoxels]; | |
| 454 | 458 | |
| 455 | − GLuint* indicesArray = new GLuint[INDICES_PER_VOXEL * MAX_VOXELS_PER_SYSTEM]; | |
| 459 | + GLuint* indicesArray = new GLuint[INDICES_PER_VOXEL * _maxVoxels]; | |
| 456 | 460 | |
| 457 | 461 | // populate the indicesArray |
| 458 | 462 | // this will not change given new voxels, so we can set it all up now |
| 459 | − for (int n = 0; n < MAX_VOXELS_PER_SYSTEM; n++) { | |
| 463 | + for (int n = 0; n < _maxVoxels; n++) { | |
| 460 | 464 | // fill the indices array |
| 461 | 465 | int voxelIndexOffset = n * INDICES_PER_VOXEL; |
| 462 | 466 | GLuint* currentIndicesPos = indicesArray + voxelIndexOffset; |
| @@ -468,11 +472,11 @@ void VoxelSystem::init() { · 5 unmodified lines | ||
| 468 | 472 | } |
| 469 | 473 | } |
| 470 | 474 | |
| 471 | − GLfloat* normalsArray = new GLfloat[VERTEX_POINTS_PER_VOXEL * MAX_VOXELS_PER_SYSTEM]; | |
| 475 | + GLfloat* normalsArray = new GLfloat[VERTEX_POINTS_PER_VOXEL * _maxVoxels]; | |
| 472 | 476 | GLfloat* normalsArrayEndPointer = normalsArray; |
| 473 | 477 | |
| 474 | 478 | // populate the normalsArray |
| 475 | − for (int n = 0; n < MAX_VOXELS_PER_SYSTEM; n++) { | |
| 479 | + for (int n = 0; n < _maxVoxels; n++) { | |
| 476 | 480 | for (int i = 0; i < VERTEX_POINTS_PER_VOXEL; i++) { |
| 477 | 481 | *(normalsArrayEndPointer++) = identityNormals[i]; |
| 478 | 482 | } |
| @@ -481,32 +485,35 @@ void VoxelSystem::init() { · 2 unmodified lines | ||
| 481 | 485 | // VBO for the verticesArray |
| 482 | 486 | glGenBuffers(1, &_vboVerticesID); |
| 483 | 487 | glBindBuffer(GL_ARRAY_BUFFER, _vboVerticesID); |
| 484 | − glBufferData(GL_ARRAY_BUFFER, VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat) * MAX_VOXELS_PER_SYSTEM, NULL, GL_DYNAMIC_DRAW); | |
| 488 | + glBufferData(GL_ARRAY_BUFFER, VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat) * _maxVoxels, NULL, GL_DYNAMIC_DRAW); | |
| 485 | 489 | |
| 486 | 490 | // VBO for the normalsArray |
| 487 | 491 | glGenBuffers(1, &_vboNormalsID); |
| 488 | 492 | glBindBuffer(GL_ARRAY_BUFFER, _vboNormalsID); |
| 489 | 493 | glBufferData(GL_ARRAY_BUFFER, |
| 490 | − VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat) * MAX_VOXELS_PER_SYSTEM, | |
| 494 | + VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat) * _maxVoxels, | |
| 491 | 495 | normalsArray, GL_STATIC_DRAW); |
| 492 | 496 | |
| 493 | 497 | // VBO for colorsArray |
| 494 | 498 | glGenBuffers(1, &_vboColorsID); |
| 495 | 499 | glBindBuffer(GL_ARRAY_BUFFER, _vboColorsID); |
| 496 | − glBufferData(GL_ARRAY_BUFFER, VERTEX_POINTS_PER_VOXEL * sizeof(GLubyte) * MAX_VOXELS_PER_SYSTEM, NULL, GL_DYNAMIC_DRAW); | |
| 500 | + glBufferData(GL_ARRAY_BUFFER, VERTEX_POINTS_PER_VOXEL * sizeof(GLubyte) * _maxVoxels, NULL, GL_DYNAMIC_DRAW); | |
| 497 | 501 | |
| 498 | 502 | // VBO for the indicesArray |
| 499 | 503 | glGenBuffers(1, &_vboIndicesID); |
| 500 | 504 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vboIndicesID); |
| 501 | 505 | glBufferData(GL_ELEMENT_ARRAY_BUFFER, |
| 502 | − INDICES_PER_VOXEL * sizeof(GLuint) * MAX_VOXELS_PER_SYSTEM, | |
| 506 | + INDICES_PER_VOXEL * sizeof(GLuint) * _maxVoxels, | |
| 503 | 507 | indicesArray, GL_STATIC_DRAW); |
| 504 | 508 | |
| 505 | 509 | // delete the indices and normals arrays that are no longer needed |
| 506 | 510 | delete[] indicesArray; |
| 507 | 511 | delete[] normalsArray; |
| 508 | 512 | |
| 509 | − // create our simple fragment shader | |
| 513 | + // create our simple fragment shader if we're the first system to init | |
| 514 | + if (_perlinModulateProgram != 0) { | |
| 515 | + return; | |
| 516 | + } | |
| 510 | 517 | switchToResourcesParentIfRequired(); |
| 511 | 518 | _perlinModulateProgram = new ProgramObject(); |
| 512 | 519 | _perlinModulateProgram->addShaderFromSourceFile(QGLShader::Vertex, "resources/shaders/perlin_modulate.vert"); |
| @@ -661,7 +668,7 @@ void VoxelSystem::render(bool texture) { · 148 unmodified lines | ||
| 661 | 668 | |
| 662 | 669 | // draw the number of voxels we have |
| 663 | 670 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vboIndicesID); |
| 664 | − glScalef(TREE_SCALE, TREE_SCALE, TREE_SCALE); | |
| 671 | + glScalef(_treeScale, _treeScale, _treeScale); | |
| 665 | 672 | glDrawRangeElementsEXT(GL_TRIANGLES, 0, VERTICES_PER_VOXEL * _voxelsInReadArrays - 1, |
| 666 | 673 | 36 * _voxelsInReadArrays, GL_UNSIGNED_INT, 0); |
| 667 | 674 | |
| @@ -855,7 +862,7 @@ bool VoxelSystem::removeOutOfViewOperation(VoxelNode* node, void* extraData) { · 187 unmodified lines | ||
| 855 | 862 | for (int i = 0; i < NUMBER_OF_CHILDREN; i++) { |
| 856 | 863 | VoxelNode* childNode = node->getChildAtIndex(i); |
| 857 | 864 | if (childNode) { |
| 858 | − ViewFrustum::location inFrustum = childNode->inFrustum(*thisVoxelSystem->_viewFrustum); | |
| 865 | + ViewFrustum::location inFrustum = childNode->inFrustum(*Application::getInstance()->getViewFrustum()); | |
| 859 | 866 | switch (inFrustum) { |
| 860 | 867 | case ViewFrustum::OUTSIDE: { |
| 861 | 868 | args->nodesOutside++; |
| @@ -907,9 +914,9 @@ bool VoxelSystem::hasViewChanged() { · 45 unmodified lines | ||
| 907 | 914 | } |
| 908 | 915 | |
| 909 | 916 | // If our viewFrustum has changed since our _lastKnowViewFrustum |
| 910 | − if (_viewFrustum && !_lastStableViewFrustum.matches(_viewFrustum)) { | |
| 917 | + if (!_lastStableViewFrustum.matches(Application::getInstance()->getViewFrustum())) { | |
| 911 | 918 | result = true; |
| 912 | − _lastStableViewFrustum = *_viewFrustum; // save last stable | |
| 919 | + _lastStableViewFrustum = *Application::getInstance()->getViewFrustum(); // save last stable | |
| 913 | 920 | } |
| 914 | 921 | return result; |
| 915 | 922 | } |