History

menu cleanup, support for on the fly switching of maxVoxels and useVoxelShader

ZappoMan committed 13 years ago

Commit 78453401 parent 552d9fa6 files · +236 −158Browse files

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

Unified · Split

interface/src/VoxelSystem.cpp+168−143
@@ -74,9 +74,8 @@ VoxelSystem::VoxelSystem(float treeScale, int maxVoxels) · 73 unmodified lines
7474
7575 connect(_tree, SIGNAL(importSize(float,float,float)), SIGNAL(importSize(float,float,float)));
7676 connect(_tree, SIGNAL(importProgress(int)), SIGNAL(importProgress(int)));
77−
78− _voxelShaderInitialized = false;
79− _initializingVoxelShader = false;
77+
78+ _useVoxelShader = false;
8079 _writeVoxelShaderData = NULL;
8180 _readVoxelShaderData = NULL;
8281
@@ -132,28 +131,7 @@ void VoxelSystem::clearFreeBufferIndexes() { · 49 unmodified lines
132131 }
133132
134133 VoxelSystem::~VoxelSystem() {
135− if (_initialized) {
136− // Destroy glBuffers
137− glDeleteBuffers(1, &_vboVerticesID);
138− glDeleteBuffers(1, &_vboNormalsID);
139− glDeleteBuffers(1, &_vboColorsID);
140− glDeleteBuffers(1, &_vboIndicesID);
141−
142− delete[] _readVerticesArray;
143− delete[] _writeVerticesArray;
144− delete[] _readColorsArray;
145− delete[] _writeColorsArray;
146− delete[] _writeVoxelDirtyArray;
147− delete[] _readVoxelDirtyArray;
148−
149− // these are used when in VoxelShader mode.
150− glDeleteBuffers(1, &_vboVoxelsID);
151− glDeleteBuffers(1, &_vboVoxelsIndicesID);
152−
153− delete[] _writeVoxelShaderData;
154− delete[] _readVoxelShaderData;
155− }
156−
134+ cleanupVoxelMemory();
157135 delete _tree;
158136 pthread_mutex_destroy(&_bufferWriteLock);
159137 pthread_mutex_destroy(&_treeLock);
@@ -161,43 +139,172 @@ VoxelSystem::~VoxelSystem() { · 1 unmodified line
161139 VoxelNode::removeDeleteHook(this);
162140 }
163141
164−bool VoxelSystem::getUseVoxelShader() {
165− bool useVoxelShader = false; //Menu::getInstance()->isOptionChecked(MenuOption::UseVoxelShader);
166−
167− return useVoxelShader;
142+void VoxelSystem::setMaxVoxels(int maxVoxels) {
143+ pthread_mutex_lock(&_bufferWriteLock);
144+ bool wasInitialized = _initialized;
145+ if (wasInitialized) {
146+ cleanupVoxelMemory();
147+ }
148+ _maxVoxels = maxVoxels;
149+ if (wasInitialized) {
150+ init();
151+ }
152+ pthread_mutex_unlock(&_bufferWriteLock);
168153 }
169154
170−void VoxelSystem::initVoxelShader() {
171− GLuint* indicesArray = new GLuint[_maxVoxels];
155+void VoxelSystem::setUseVoxelShader(bool useVoxelShader) {
156+ pthread_mutex_lock(&_bufferWriteLock);
157+ bool wasInitialized = _initialized;
158+ if (wasInitialized) {
159+ cleanupVoxelMemory();
160+ }
161+ _useVoxelShader = useVoxelShader;
162+ if (wasInitialized) {
163+ init();
164+ }
165+ pthread_mutex_unlock(&_bufferWriteLock);
166+}
167+
168+void VoxelSystem::cleanupVoxelMemory() {
169+ if (_initialized) {
170+ if (_useVoxelShader) {
171+ // these are used when in VoxelShader mode.
172+ glDeleteBuffers(1, &_vboVoxelsID);
173+ glDeleteBuffers(1, &_vboVoxelsIndicesID);
172174
173− // populate the indicesArray
174− // this will not change given new voxels, so we can set it all up now
175− for (int n = 0; n < _maxVoxels; n++) {
176− indicesArray[n] = n;
175+ delete[] _writeVoxelShaderData;
176+ delete[] _readVoxelShaderData;
177+ } else {
178+ // Destroy glBuffers
179+ glDeleteBuffers(1, &_vboVerticesID);
180+ glDeleteBuffers(1, &_vboNormalsID);
181+ glDeleteBuffers(1, &_vboColorsID);
182+ glDeleteBuffers(1, &_vboIndicesID);
183+
184+ delete[] _readVerticesArray;
185+ delete[] _writeVerticesArray;
186+ delete[] _readColorsArray;
187+ delete[] _writeColorsArray;
188+ delete[] _writeVoxelDirtyArray;
189+ delete[] _readVoxelDirtyArray;
190+ }
177191 }
192+ _initialized = false; // no longer initialized
193+}
194+
195+void VoxelSystem::initVoxelMemory() {
196+ if (_useVoxelShader) {
197+ GLuint* indicesArray = new GLuint[_maxVoxels];
178198
179− // bind the indices VBO to the actual indices array
180− glGenBuffers(1, &_vboVoxelsIndicesID);
181− glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vboVoxelsIndicesID);
182− glBufferData(GL_ELEMENT_ARRAY_BUFFER, _maxVoxels, indicesArray, GL_STATIC_DRAW);
199+ // populate the indicesArray
200+ // this will not change given new voxels, so we can set it all up now
201+ for (int n = 0; n < _maxVoxels; n++) {
202+ indicesArray[n] = n;
203+ }
204+
205+ // bind the indices VBO to the actual indices array
206+ glGenBuffers(1, &_vboVoxelsIndicesID);
207+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vboVoxelsIndicesID);
208+ glBufferData(GL_ELEMENT_ARRAY_BUFFER, _maxVoxels, indicesArray, GL_STATIC_DRAW);
183209
184210
185− glGenBuffers(1, &_vboVoxelsID);
186− glBindBuffer(GL_ARRAY_BUFFER, _vboVoxelsID);
187− glBufferData(GL_ARRAY_BUFFER, _maxVoxels * sizeof(VoxelShaderVBOData), NULL, GL_DYNAMIC_DRAW);
211+ glGenBuffers(1, &_vboVoxelsID);
212+ glBindBuffer(GL_ARRAY_BUFFER, _vboVoxelsID);
213+ glBufferData(GL_ARRAY_BUFFER, _maxVoxels * sizeof(VoxelShaderVBOData), NULL, GL_DYNAMIC_DRAW);
188214
189− // delete the indices and normals arrays that are no longer needed
190− delete[] indicesArray;
215+ // delete the indices and normals arrays that are no longer needed
216+ delete[] indicesArray;
217+
218+ // we will track individual dirty sections with these arrays of bools
219+ _writeVoxelDirtyArray = new bool[_maxVoxels];
220+ memset(_writeVoxelDirtyArray, false, _maxVoxels * sizeof(bool));
221+ _readVoxelDirtyArray = new bool[_maxVoxels];
222+ memset(_readVoxelDirtyArray, false, _maxVoxels * sizeof(bool));
223+
224+ // prep the data structures for incoming voxel data
225+ _writeVoxelShaderData = new VoxelShaderVBOData[_maxVoxels];
226+ _readVoxelShaderData = new VoxelShaderVBOData[_maxVoxels];
227+ } else {
228+ GLuint* indicesArray = new GLuint[INDICES_PER_VOXEL * _maxVoxels];
229+
230+ // populate the indicesArray
231+ // this will not change given new voxels, so we can set it all up now
232+ for (int n = 0; n < _maxVoxels; n++) {
233+ // fill the indices array
234+ int voxelIndexOffset = n * INDICES_PER_VOXEL;
235+ GLuint* currentIndicesPos = indicesArray + voxelIndexOffset;
236+ int startIndex = (n * VERTICES_PER_VOXEL);
237+
238+ for (int i = 0; i < INDICES_PER_VOXEL; i++) {
239+ // add indices for this side of the cube
240+ currentIndicesPos[i] = startIndex + identityIndices[i];
241+ }
242+ }
243+
244+ GLfloat* normalsArray = new GLfloat[VERTEX_POINTS_PER_VOXEL * _maxVoxels];
245+ GLfloat* normalsArrayEndPointer = normalsArray;
246+
247+ // populate the normalsArray
248+ for (int n = 0; n < _maxVoxels; n++) {
249+ for (int i = 0; i < VERTEX_POINTS_PER_VOXEL; i++) {
250+ *(normalsArrayEndPointer++) = identityNormals[i];
251+ }
252+ }
253+
254+ glGenBuffers(1, &_vboVerticesID);
255+ glBindBuffer(GL_ARRAY_BUFFER, _vboVerticesID);
256+ glBufferData(GL_ARRAY_BUFFER, VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat) * _maxVoxels, NULL, GL_DYNAMIC_DRAW);
257+
258+ // VBO for the normalsArray
259+ glGenBuffers(1, &_vboNormalsID);
260+ glBindBuffer(GL_ARRAY_BUFFER, _vboNormalsID);
261+ glBufferData(GL_ARRAY_BUFFER,
262+ VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat) * _maxVoxels,
263+ normalsArray, GL_STATIC_DRAW);
264+
265+ // VBO for colorsArray
266+ glGenBuffers(1, &_vboColorsID);
267+ glBindBuffer(GL_ARRAY_BUFFER, _vboColorsID);
268+ glBufferData(GL_ARRAY_BUFFER, VERTEX_POINTS_PER_VOXEL * sizeof(GLubyte) * _maxVoxels, NULL, GL_DYNAMIC_DRAW);
269+
270+ // VBO for the indicesArray
271+ glGenBuffers(1, &_vboIndicesID);
272+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vboIndicesID);
273+ glBufferData(GL_ELEMENT_ARRAY_BUFFER,
274+ INDICES_PER_VOXEL * sizeof(GLuint) * _maxVoxels,
275+ indicesArray, GL_STATIC_DRAW);
276+
277+ // delete the indices and normals arrays that are no longer needed
278+ delete[] indicesArray;
279+ delete[] normalsArray;
280+
281+
282+ // we will track individual dirty sections with these arrays of bools
283+ _writeVoxelDirtyArray = new bool[_maxVoxels];
284+ memset(_writeVoxelDirtyArray, false, _maxVoxels * sizeof(bool));
285+ _readVoxelDirtyArray = new bool[_maxVoxels];
286+ memset(_readVoxelDirtyArray, false, _maxVoxels * sizeof(bool));
191287
192− // we will track individual dirty sections with these arrays of bools
193− _writeVoxelDirtyArray = new bool[_maxVoxels];
194− memset(_writeVoxelDirtyArray, false, _maxVoxels * sizeof(bool));
195− _readVoxelDirtyArray = new bool[_maxVoxels];
196− memset(_readVoxelDirtyArray, false, _maxVoxels * sizeof(bool));
288+ // prep the data structures for incoming voxel data
289+ _writeVerticesArray = new GLfloat[VERTEX_POINTS_PER_VOXEL * _maxVoxels];
290+ _readVerticesArray = new GLfloat[VERTEX_POINTS_PER_VOXEL * _maxVoxels];
291+
292+ _writeColorsArray = new GLubyte[VERTEX_POINTS_PER_VOXEL * _maxVoxels];
293+ _readColorsArray = new GLubyte[VERTEX_POINTS_PER_VOXEL * _maxVoxels];
294+
295+
296+ // create our simple fragment shader if we're the first system to init
297+ if (!_perlinModulateProgram.isLinked()) {
298+ switchToResourcesParentIfRequired();
299+ _perlinModulateProgram.addShaderFromSourceFile(QGLShader::Vertex, "resources/shaders/perlin_modulate.vert");
300+ _perlinModulateProgram.addShaderFromSourceFile(QGLShader::Fragment, "resources/shaders/perlin_modulate.frag");
301+ _perlinModulateProgram.link();
197302
198− // prep the data structures for incoming voxel data
199− _writeVoxelShaderData = new VoxelShaderVBOData[_maxVoxels];
200− _readVoxelShaderData = new VoxelShaderVBOData[_maxVoxels];
303+ _perlinModulateProgram.bind();
304+ _perlinModulateProgram.setUniformValue("permutationNormalTexture", 0);
305+ _perlinModulateProgram.release();
306+ }
307+ }
201308 }
202309
203310 void VoxelSystem::loadVoxelsFile(const char* fileName, bool wantColorRandomizer) {
@@ -457,7 +564,7 @@ void VoxelSystem::copyWrittenDataToReadArraysPartialVBOs() { · 253 unmodified lines
457564
458565 void VoxelSystem::copyWrittenDataSegmentToReadArrays(glBufferIndex segmentStart, glBufferIndex segmentEnd) {
459566 int segmentLength = (segmentEnd - segmentStart) + 1;
460− if (getUseVoxelShader()) {
567+ if (_useVoxelShader) {
461568 GLsizeiptr segmentSizeBytes = segmentLength * sizeof(VoxelShaderVBOData);
462569 void* readDataAt = &_readVoxelShaderData[segmentStart];
463570 void* writeDataAt = &_writeVoxelShaderData[segmentStart];
@@ -598,7 +705,7 @@ int VoxelSystem::updateNodeInArraysAsPartialVBO(VoxelNode* node) { · 134 unmodified lines
598705 void VoxelSystem::updateNodeInArrays(glBufferIndex nodeIndex, const glm::vec3& startVertex,
599706 float voxelScale, const nodeColor& color) {
600707
601− if (getUseVoxelShader()) {
708+ if (_useVoxelShader) {
602709 VoxelShaderVBOData* writeVerticesAt = &_writeVoxelShaderData[nodeIndex];
603710 writeVerticesAt->x = startVertex.x * TREE_SCALE;
604711 writeVerticesAt->y = startVertex.y * TREE_SCALE;
@@ -647,89 +754,7 @@ void VoxelSystem::init() { · 42 unmodified lines
647754 _voxelsInReadArrays = 0;
648755
649756 // VBO for the verticesArray
650− //initVoxelShader();
651−
652− if (true) {
653− GLuint* indicesArray = new GLuint[INDICES_PER_VOXEL * _maxVoxels];
654−
655− // populate the indicesArray
656− // this will not change given new voxels, so we can set it all up now
657− for (int n = 0; n < _maxVoxels; n++) {
658− // fill the indices array
659− int voxelIndexOffset = n * INDICES_PER_VOXEL;
660− GLuint* currentIndicesPos = indicesArray + voxelIndexOffset;
661− int startIndex = (n * VERTICES_PER_VOXEL);
662−
663− for (int i = 0; i < INDICES_PER_VOXEL; i++) {
664− // add indices for this side of the cube
665− currentIndicesPos[i] = startIndex + identityIndices[i];
666− }
667− }
668−
669− GLfloat* normalsArray = new GLfloat[VERTEX_POINTS_PER_VOXEL * _maxVoxels];
670− GLfloat* normalsArrayEndPointer = normalsArray;
671−
672− // populate the normalsArray
673− for (int n = 0; n < _maxVoxels; n++) {
674− for (int i = 0; i < VERTEX_POINTS_PER_VOXEL; i++) {
675− *(normalsArrayEndPointer++) = identityNormals[i];
676− }
677− }
678−
679− glGenBuffers(1, &_vboVerticesID);
680− glBindBuffer(GL_ARRAY_BUFFER, _vboVerticesID);
681− glBufferData(GL_ARRAY_BUFFER, VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat) * _maxVoxels, NULL, GL_DYNAMIC_DRAW);
682−
683− // VBO for the normalsArray
684− glGenBuffers(1, &_vboNormalsID);
685− glBindBuffer(GL_ARRAY_BUFFER, _vboNormalsID);
686− glBufferData(GL_ARRAY_BUFFER,
687− VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat) * _maxVoxels,
688− normalsArray, GL_STATIC_DRAW);
689−
690− // VBO for colorsArray
691− glGenBuffers(1, &_vboColorsID);
692− glBindBuffer(GL_ARRAY_BUFFER, _vboColorsID);
693− glBufferData(GL_ARRAY_BUFFER, VERTEX_POINTS_PER_VOXEL * sizeof(GLubyte) * _maxVoxels, NULL, GL_DYNAMIC_DRAW);
694−
695− // VBO for the indicesArray
696− glGenBuffers(1, &_vboIndicesID);
697− glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vboIndicesID);
698− glBufferData(GL_ELEMENT_ARRAY_BUFFER,
699− INDICES_PER_VOXEL * sizeof(GLuint) * _maxVoxels,
700− indicesArray, GL_STATIC_DRAW);
701−
702− // delete the indices and normals arrays that are no longer needed
703− delete[] indicesArray;
704− delete[] normalsArray;
705−
706−
707− // we will track individual dirty sections with these arrays of bools
708− _writeVoxelDirtyArray = new bool[_maxVoxels];
709− memset(_writeVoxelDirtyArray, false, _maxVoxels * sizeof(bool));
710− _readVoxelDirtyArray = new bool[_maxVoxels];
711− memset(_readVoxelDirtyArray, false, _maxVoxels * sizeof(bool));
712−
713− // prep the data structures for incoming voxel data
714− _writeVerticesArray = new GLfloat[VERTEX_POINTS_PER_VOXEL * _maxVoxels];
715− _readVerticesArray = new GLfloat[VERTEX_POINTS_PER_VOXEL * _maxVoxels];
716−
717− _writeColorsArray = new GLubyte[VERTEX_POINTS_PER_VOXEL * _maxVoxels];
718− _readColorsArray = new GLubyte[VERTEX_POINTS_PER_VOXEL * _maxVoxels];
719−
720−
721− // create our simple fragment shader if we're the first system to init
722− if (!_perlinModulateProgram.isLinked()) {
723− switchToResourcesParentIfRequired();
724− _perlinModulateProgram.addShaderFromSourceFile(QGLShader::Vertex, "resources/shaders/perlin_modulate.vert");
725− _perlinModulateProgram.addShaderFromSourceFile(QGLShader::Fragment, "resources/shaders/perlin_modulate.frag");
726− _perlinModulateProgram.link();
727−
728− _perlinModulateProgram.bind();
729− _perlinModulateProgram.setUniformValue("permutationNormalTexture", 0);
730− _perlinModulateProgram.release();
731− }
732− }
757+ initVoxelMemory();
733758 _initialized = true;
734759 }
735760
@@ -801,7 +826,7 @@ void VoxelSystem::updateVBOs() { · 65 unmodified lines
801826 }
802827
803828 void VoxelSystem::updateVBOSegment(glBufferIndex segmentStart, glBufferIndex segmentEnd) {
804− if (getUseVoxelShader()) {
829+ if (_useVoxelShader) {
805830 int segmentLength = (segmentEnd - segmentStart) + 1;
806831 GLintptr segmentStartAt = segmentStart * sizeof(VoxelShaderVBOData);
807832 GLsizeiptr segmentSizeBytes = segmentLength * sizeof(VoxelShaderVBOData);
@@ -839,9 +864,9 @@ void VoxelSystem::render(bool texture) { · 31 unmodified lines
839864
840865 updateVBOs();
841866
842− //printf("render() _voxelsInReadArrays=%lu voxel shader=%s\n", _voxelsInReadArrays, debug::valueOf(getUseVoxelShader()));
867+ //printf("render() _voxelsInReadArrays=%lu voxel shader=%s\n", _voxelsInReadArrays, debug::valueOf(_useVoxelShader));
843868
844− if (getUseVoxelShader()) {
869+ if (_useVoxelShader) {
845870
846871 Application::getInstance()->getVoxelShader().begin();
847872
@@ -1349,7 +1374,7 @@ public: · 501 unmodified lines
13491374 duplicateVBOIndex(0),
13501375 leafNodes(0)
13511376 {
1352− memset(hasIndexFound, false, MAX_VOXELS_PER_SYSTEM * sizeof(bool));
1377+ memset(hasIndexFound, false, DEFAULT_MAX_VOXELS_PER_SYSTEM * sizeof(bool));
13531378 };
13541379
13551380 unsigned long totalNodes;
@@ -1364,7 +1389,7 @@ public: · 8 unmodified lines
13641389
13651390 unsigned long expectedMax;
13661391
1367− bool hasIndexFound[MAX_VOXELS_PER_SYSTEM];
1392+ bool hasIndexFound[DEFAULT_MAX_VOXELS_PER_SYSTEM];
13681393 };
13691394
13701395 bool VoxelSystem::collectStatsForTreesAndVBOsOperation(VoxelNode* node, void* extraData) {
@@ -1439,7 +1464,7 @@ void VoxelSystem::collectStatsForTreesAndVBOs() { · 68 unmodified lines
14391464 glBufferIndex minInVBO = GLBUFFER_INDEX_UNKNOWN;
14401465 glBufferIndex maxInVBO = 0;
14411466
1442− for (glBufferIndex i = 0; i < MAX_VOXELS_PER_SYSTEM; i++) {
1467+ for (glBufferIndex i = 0; i < DEFAULT_MAX_VOXELS_PER_SYSTEM; i++) {
14431468 if (args.hasIndexFound[i]) {
14441469 minInVBO = std::min(minInVBO,i);
14451470 maxInVBO = std::max(maxInVBO,i);

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t