Showing the changes to interface/src/VoxelSystem.cpp (1 of 12 files). Show all
Unified · Split
| @@ -125,6 +125,12 @@ VoxelSystem::~VoxelSystem() { · 124 unmodified lines | ||
| 125 | 125 | pthread_mutex_destroy(&_bufferWriteLock); |
| 126 | 126 | pthread_mutex_destroy(&_treeLock); |
| 127 | 127 | |
| 128 | + // Destroy glBuffers | |
| 129 | + glDeleteBuffers(1, &_vboVerticesID); | |
| 130 | + glDeleteBuffers(1, &_vboNormalsID); | |
| 131 | + glDeleteBuffers(1, &_vboColorsID); | |
| 132 | + glDeleteBuffers(1, &_vboIndicesID); | |
| 133 | + | |
| 128 | 134 | VoxelNode::removeDeleteHook(this); |
| 129 | 135 | } |
| 130 | 136 | |
| @@ -532,9 +538,15 @@ glm::vec3 VoxelSystem::computeVoxelVertex(const glm::vec3& startVertex, float vo · 401 unmodified lines | ||
| 532 | 538 | return startVertex + glm::vec3(identityVertex[0], identityVertex[1], identityVertex[2]) * voxelScale; |
| 533 | 539 | } |
| 534 | 540 | |
| 535 | −ProgramObject* VoxelSystem::_perlinModulateProgram = 0; | |
| 541 | +bool VoxelSystem::_perlinModulateProgramInitialized = false; | |
| 542 | +ProgramObject VoxelSystem::_perlinModulateProgram; | |
| 536 | 543 | |
| 537 | 544 | void VoxelSystem::init() { |
| 545 | + if (_initialized) { | |
| 546 | + qDebug("[ERROR] VoxelSystem is already initialized.\n"); | |
| 547 | + return; | |
| 548 | + } | |
| 549 | + | |
| 538 | 550 | _callsToTreesToArrays = 0; |
| 539 | 551 | _setupNewVoxelsForDrawingLastFinished = 0; |
| 540 | 552 | _setupNewVoxelsForDrawingLastElapsed = 0; |
| @@ -545,19 +557,6 @@ void VoxelSystem::init() { · 4 unmodified lines | ||
| 545 | 557 | _voxelsInWriteArrays = 0; |
| 546 | 558 | _voxelsInReadArrays = 0; |
| 547 | 559 | |
| 548 | − // we will track individual dirty sections with these arrays of bools | |
| 549 | − _writeVoxelDirtyArray = new bool[_maxVoxels]; | |
| 550 | − memset(_writeVoxelDirtyArray, false, _maxVoxels * sizeof(bool)); | |
| 551 | − _readVoxelDirtyArray = new bool[_maxVoxels]; | |
| 552 | − memset(_readVoxelDirtyArray, false, _maxVoxels * sizeof(bool)); | |
| 553 | − | |
| 554 | − // prep the data structures for incoming voxel data | |
| 555 | − _writeVerticesArray = new GLfloat[VERTEX_POINTS_PER_VOXEL * _maxVoxels]; | |
| 556 | − _readVerticesArray = new GLfloat[VERTEX_POINTS_PER_VOXEL * _maxVoxels]; | |
| 557 | − | |
| 558 | − _writeColorsArray = new GLubyte[VERTEX_POINTS_PER_VOXEL * _maxVoxels]; | |
| 559 | − _readColorsArray = new GLubyte[VERTEX_POINTS_PER_VOXEL * _maxVoxels]; | |
| 560 | − | |
| 561 | 560 | GLuint* indicesArray = new GLuint[INDICES_PER_VOXEL * _maxVoxels]; |
| 562 | 561 | |
| 563 | 562 | // populate the indicesArray |
| @@ -611,20 +610,35 @@ void VoxelSystem::init() { · 47 unmodified lines | ||
| 611 | 610 | // delete the indices and normals arrays that are no longer needed |
| 612 | 611 | delete[] indicesArray; |
| 613 | 612 | delete[] normalsArray; |
| 614 | − | |
| 613 | + | |
| 614 | + | |
| 615 | + // we will track individual dirty sections with these arrays of bools | |
| 616 | + _writeVoxelDirtyArray = new bool[_maxVoxels]; | |
| 617 | + memset(_writeVoxelDirtyArray, false, _maxVoxels * sizeof(bool)); | |
| 618 | + _readVoxelDirtyArray = new bool[_maxVoxels]; | |
| 619 | + memset(_readVoxelDirtyArray, false, _maxVoxels * sizeof(bool)); | |
| 620 | + | |
| 621 | + // prep the data structures for incoming voxel data | |
| 622 | + _writeVerticesArray = new GLfloat[VERTEX_POINTS_PER_VOXEL * _maxVoxels]; | |
| 623 | + _readVerticesArray = new GLfloat[VERTEX_POINTS_PER_VOXEL * _maxVoxels]; | |
| 624 | + | |
| 625 | + _writeColorsArray = new GLubyte[VERTEX_POINTS_PER_VOXEL * _maxVoxels]; | |
| 626 | + _readColorsArray = new GLubyte[VERTEX_POINTS_PER_VOXEL * _maxVoxels]; | |
| 627 | + | |
| 628 | + | |
| 615 | 629 | // create our simple fragment shader if we're the first system to init |
| 616 | − if (_perlinModulateProgram != 0) { | |
| 617 | − return; | |
| 630 | + if (!_perlinModulateProgramInitialized) { | |
| 631 | + switchToResourcesParentIfRequired(); | |
| 632 | + _perlinModulateProgram.addShaderFromSourceFile(QGLShader::Vertex, "resources/shaders/perlin_modulate.vert"); | |
| 633 | + _perlinModulateProgram.addShaderFromSourceFile(QGLShader::Fragment, "resources/shaders/perlin_modulate.frag"); | |
| 634 | + _perlinModulateProgram.link(); | |
| 635 | + | |
| 636 | + _perlinModulateProgram.bind(); | |
| 637 | + _perlinModulateProgram.setUniformValue("permutationNormalTexture", 0); | |
| 638 | + _perlinModulateProgram.release(); | |
| 639 | + | |
| 640 | + _perlinModulateProgramInitialized = true; | |
| 618 | 641 | } |
| 619 | − switchToResourcesParentIfRequired(); | |
| 620 | − _perlinModulateProgram = new ProgramObject(); | |
| 621 | − _perlinModulateProgram->addShaderFromSourceFile(QGLShader::Vertex, "resources/shaders/perlin_modulate.vert"); | |
| 622 | − _perlinModulateProgram->addShaderFromSourceFile(QGLShader::Fragment, "resources/shaders/perlin_modulate.frag"); | |
| 623 | − _perlinModulateProgram->link(); | |
| 624 | − | |
| 625 | − _perlinModulateProgram->bind(); | |
| 626 | − _perlinModulateProgram->setUniformValue("permutationNormalTexture", 0); | |
| 627 | − _perlinModulateProgram->release(); | |
| 628 | 642 | } |
| 629 | 643 | |
| 630 | 644 | void VoxelSystem::updateFullVBOs() { |
| @@ -749,7 +763,7 @@ void VoxelSystem::applyScaleAndBindProgram(bool texture) { · 118 unmodified lines | ||
| 749 | 763 | glScalef(_treeScale, _treeScale, _treeScale); |
| 750 | 764 | |
| 751 | 765 | if (texture) { |
| 752 | − _perlinModulateProgram->bind(); | |
| 766 | + _perlinModulateProgram.bind(); | |
| 753 | 767 | glBindTexture(GL_TEXTURE_2D, Application::getInstance()->getTextureCache()->getPermutationNormalTextureID()); |
| 754 | 768 | } |
| 755 | 769 | } |
| @@ -759,7 +773,7 @@ void VoxelSystem::removeScaleAndReleaseProgram(bool texture) { · 3 unmodified lines | ||
| 759 | 773 | glPopMatrix(); |
| 760 | 774 | |
| 761 | 775 | if (texture) { |
| 762 | − _perlinModulateProgram->release(); | |
| 776 | + _perlinModulateProgram.release(); | |
| 763 | 777 | glBindTexture(GL_TEXTURE_2D, 0); |
| 764 | 778 | } |
| 765 | 779 | } |