History

Fixed avatar related files memory wastes

atlante45 committed 13 years ago

Commit 688bc171 parent e124c1612 files · +153 −114Browse files

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

Unified · Split

interface/src/VoxelSystem.cpp+42−28
@@ -125,6 +125,12 @@ VoxelSystem::~VoxelSystem() { · 124 unmodified lines
125 pthread_mutex_destroy(&_bufferWriteLock);125 pthread_mutex_destroy(&_bufferWriteLock);
126 pthread_mutex_destroy(&_treeLock);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 VoxelNode::removeDeleteHook(this);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 return startVertex + glm::vec3(identityVertex[0], identityVertex[1], identityVertex[2]) * voxelScale;538 return startVertex + glm::vec3(identityVertex[0], identityVertex[1], identityVertex[2]) * voxelScale;
533}539}
534 540
535ProgramObject* VoxelSystem::_perlinModulateProgram = 0;541bool VoxelSystem::_perlinModulateProgramInitialized = false;
542ProgramObject VoxelSystem::_perlinModulateProgram;
536 543
537void VoxelSystem::init() {544void VoxelSystem::init() {
545 if (_initialized) {
546 qDebug("[ERROR] VoxelSystem is already initialized.\n");
547 return;
548 }
549
538 _callsToTreesToArrays = 0;550 _callsToTreesToArrays = 0;
539 _setupNewVoxelsForDrawingLastFinished = 0;551 _setupNewVoxelsForDrawingLastFinished = 0;
540 _setupNewVoxelsForDrawingLastElapsed = 0;552 _setupNewVoxelsForDrawingLastElapsed = 0;
@@ -545,19 +557,6 @@ void VoxelSystem::init() { · 4 unmodified lines
545 _voxelsInWriteArrays = 0;557 _voxelsInWriteArrays = 0;
546 _voxelsInReadArrays = 0;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 GLuint* indicesArray = new GLuint[INDICES_PER_VOXEL * _maxVoxels];560 GLuint* indicesArray = new GLuint[INDICES_PER_VOXEL * _maxVoxels];
562 561
563 // populate the indicesArray562 // populate the indicesArray
@@ -611,20 +610,35 @@ void VoxelSystem::init() { · 47 unmodified lines
611 // delete the indices and normals arrays that are no longer needed610 // delete the indices and normals arrays that are no longer needed
612 delete[] indicesArray;611 delete[] indicesArray;
613 delete[] normalsArray;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 // create our simple fragment shader if we're the first system to init629 // create our simple fragment shader if we're the first system to init
616 if (_perlinModulateProgram != 0) {630 if (!_perlinModulateProgramInitialized) {
617 return;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
630void VoxelSystem::updateFullVBOs() {644void VoxelSystem::updateFullVBOs() {
@@ -749,7 +763,7 @@ void VoxelSystem::applyScaleAndBindProgram(bool texture) { · 118 unmodified lines
749 glScalef(_treeScale, _treeScale, _treeScale);763 glScalef(_treeScale, _treeScale, _treeScale);
750 764
751 if (texture) {765 if (texture) {
752 _perlinModulateProgram->bind();766 _perlinModulateProgram.bind();
753 glBindTexture(GL_TEXTURE_2D, Application::getInstance()->getTextureCache()->getPermutationNormalTextureID());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 glPopMatrix();773 glPopMatrix();
760 774
761 if (texture) {775 if (texture) {
762 _perlinModulateProgram->release();776 _perlinModulateProgram.release();
763 glBindTexture(GL_TEXTURE_2D, 0);777 glBindTexture(GL_TEXTURE_2D, 0);
764 }778 }
765}779}