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
125125 pthread_mutex_destroy(&_bufferWriteLock);
126126 pthread_mutex_destroy(&_treeLock);
127127
128+ // Destroy glBuffers
129+ glDeleteBuffers(1, &_vboVerticesID);
130+ glDeleteBuffers(1, &_vboNormalsID);
131+ glDeleteBuffers(1, &_vboColorsID);
132+ glDeleteBuffers(1, &_vboIndicesID);
133+
128134 VoxelNode::removeDeleteHook(this);
129135 }
130136
@@ -532,9 +538,15 @@ glm::vec3 VoxelSystem::computeVoxelVertex(const glm::vec3& startVertex, float vo · 401 unmodified lines
532538 return startVertex + glm::vec3(identityVertex[0], identityVertex[1], identityVertex[2]) * voxelScale;
533539 }
534540
535−ProgramObject* VoxelSystem::_perlinModulateProgram = 0;
541+bool VoxelSystem::_perlinModulateProgramInitialized = false;
542+ProgramObject VoxelSystem::_perlinModulateProgram;
536543
537544 void VoxelSystem::init() {
545+ if (_initialized) {
546+ qDebug("[ERROR] VoxelSystem is already initialized.\n");
547+ return;
548+ }
549+
538550 _callsToTreesToArrays = 0;
539551 _setupNewVoxelsForDrawingLastFinished = 0;
540552 _setupNewVoxelsForDrawingLastElapsed = 0;
@@ -545,19 +557,6 @@ void VoxelSystem::init() { · 4 unmodified lines
545557 _voxelsInWriteArrays = 0;
546558 _voxelsInReadArrays = 0;
547559
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−
561560 GLuint* indicesArray = new GLuint[INDICES_PER_VOXEL * _maxVoxels];
562561
563562 // populate the indicesArray
@@ -611,20 +610,35 @@ void VoxelSystem::init() { · 47 unmodified lines
611610 // delete the indices and normals arrays that are no longer needed
612611 delete[] indicesArray;
613612 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+
615629 // 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;
618641 }
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();
628642 }
629643
630644 void VoxelSystem::updateFullVBOs() {
@@ -749,7 +763,7 @@ void VoxelSystem::applyScaleAndBindProgram(bool texture) { · 118 unmodified lines
749763 glScalef(_treeScale, _treeScale, _treeScale);
750764
751765 if (texture) {
752− _perlinModulateProgram->bind();
766+ _perlinModulateProgram.bind();
753767 glBindTexture(GL_TEXTURE_2D, Application::getInstance()->getTextureCache()->getPermutationNormalTextureID());
754768 }
755769 }
@@ -759,7 +773,7 @@ void VoxelSystem::removeScaleAndReleaseProgram(bool texture) { · 3 unmodified lines
759773 glPopMatrix();
760774
761775 if (texture) {
762− _perlinModulateProgram->release();
776+ _perlinModulateProgram.release();
763777 glBindTexture(GL_TEXTURE_2D, 0);
764778 }
765779 }