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
74 74
75 connect(_tree, SIGNAL(importSize(float,float,float)), SIGNAL(importSize(float,float,float)));75 connect(_tree, SIGNAL(importSize(float,float,float)), SIGNAL(importSize(float,float,float)));
76 connect(_tree, SIGNAL(importProgress(int)), SIGNAL(importProgress(int)));76 connect(_tree, SIGNAL(importProgress(int)), SIGNAL(importProgress(int)));
77 77
78 _voxelShaderInitialized = false;78 _useVoxelShader = false;
79 _initializingVoxelShader = false;
80 _writeVoxelShaderData = NULL;79 _writeVoxelShaderData = NULL;
81 _readVoxelShaderData = NULL;80 _readVoxelShaderData = NULL;
82 81
@@ -132,28 +131,7 @@ void VoxelSystem::clearFreeBufferIndexes() { · 49 unmodified lines
132}131}
133 132
134VoxelSystem::~VoxelSystem() {133VoxelSystem::~VoxelSystem() {
135 if (_initialized) {134 cleanupVoxelMemory();
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
157 delete _tree;135 delete _tree;
158 pthread_mutex_destroy(&_bufferWriteLock);136 pthread_mutex_destroy(&_bufferWriteLock);
159 pthread_mutex_destroy(&_treeLock);137 pthread_mutex_destroy(&_treeLock);
@@ -161,43 +139,172 @@ VoxelSystem::~VoxelSystem() { · 1 unmodified line
161 VoxelNode::removeDeleteHook(this);139 VoxelNode::removeDeleteHook(this);
162}140}
163 141
164bool VoxelSystem::getUseVoxelShader() {142void VoxelSystem::setMaxVoxels(int maxVoxels) {
165 bool useVoxelShader = false; //Menu::getInstance()->isOptionChecked(MenuOption::UseVoxelShader);143 pthread_mutex_lock(&_bufferWriteLock);
166 144 bool wasInitialized = _initialized;
167 return useVoxelShader;145 if (wasInitialized) {
146 cleanupVoxelMemory();
147 }
148 _maxVoxels = maxVoxels;
149 if (wasInitialized) {
150 init();
151 }
152 pthread_mutex_unlock(&_bufferWriteLock);
168}153}
169 154
170void VoxelSystem::initVoxelShader() {155void VoxelSystem::setUseVoxelShader(bool useVoxelShader) {
171 GLuint* indicesArray = new GLuint[_maxVoxels];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
168void VoxelSystem::cleanupVoxelMemory() {
169 if (_initialized) {
170 if (_useVoxelShader) {
171 // these are used when in VoxelShader mode.
172 glDeleteBuffers(1, &_vboVoxelsID);
173 glDeleteBuffers(1, &_vboVoxelsIndicesID);
172 174
173 // populate the indicesArray175 delete[] _writeVoxelShaderData;
174 // this will not change given new voxels, so we can set it all up now176 delete[] _readVoxelShaderData;
175 for (int n = 0; n < _maxVoxels; n++) {177 } else {
176 indicesArray[n] = n;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 }
177 }191 }
192 _initialized = false; // no longer initialized
193}
194
195void VoxelSystem::initVoxelMemory() {
196 if (_useVoxelShader) {
197 GLuint* indicesArray = new GLuint[_maxVoxels];
178 198
179 // bind the indices VBO to the actual indices array199 // populate the indicesArray
180 glGenBuffers(1, &_vboVoxelsIndicesID);200 // this will not change given new voxels, so we can set it all up now
181 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vboVoxelsIndicesID);201 for (int n = 0; n < _maxVoxels; n++) {
182 glBufferData(GL_ELEMENT_ARRAY_BUFFER, _maxVoxels, indicesArray, GL_STATIC_DRAW);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);
183 209
184 210
185 glGenBuffers(1, &_vboVoxelsID);211 glGenBuffers(1, &_vboVoxelsID);
186 glBindBuffer(GL_ARRAY_BUFFER, _vboVoxelsID);212 glBindBuffer(GL_ARRAY_BUFFER, _vboVoxelsID);
187 glBufferData(GL_ARRAY_BUFFER, _maxVoxels * sizeof(VoxelShaderVBOData), NULL, GL_DYNAMIC_DRAW);213 glBufferData(GL_ARRAY_BUFFER, _maxVoxels * sizeof(VoxelShaderVBOData), NULL, GL_DYNAMIC_DRAW);
188 214
189 // delete the indices and normals arrays that are no longer needed215 // delete the indices and normals arrays that are no longer needed
190 delete[] indicesArray;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));
191 287
192 // we will track individual dirty sections with these arrays of bools288 // prep the data structures for incoming voxel data
193 _writeVoxelDirtyArray = new bool[_maxVoxels];289 _writeVerticesArray = new GLfloat[VERTEX_POINTS_PER_VOXEL * _maxVoxels];
194 memset(_writeVoxelDirtyArray, false, _maxVoxels * sizeof(bool));290 _readVerticesArray = new GLfloat[VERTEX_POINTS_PER_VOXEL * _maxVoxels];
195 _readVoxelDirtyArray = new bool[_maxVoxels];291
196 memset(_readVoxelDirtyArray, false, _maxVoxels * sizeof(bool));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();
197 302
198 // prep the data structures for incoming voxel data303 _perlinModulateProgram.bind();
199 _writeVoxelShaderData = new VoxelShaderVBOData[_maxVoxels];304 _perlinModulateProgram.setUniformValue("permutationNormalTexture", 0);
200 _readVoxelShaderData = new VoxelShaderVBOData[_maxVoxels];305 _perlinModulateProgram.release();
306 }
307 }
201}308}
202 309
203void VoxelSystem::loadVoxelsFile(const char* fileName, bool wantColorRandomizer) {310void VoxelSystem::loadVoxelsFile(const char* fileName, bool wantColorRandomizer) {
@@ -457,7 +564,7 @@ void VoxelSystem::copyWrittenDataToReadArraysPartialVBOs() { · 253 unmodified lines
457 564
458void VoxelSystem::copyWrittenDataSegmentToReadArrays(glBufferIndex segmentStart, glBufferIndex segmentEnd) {565void VoxelSystem::copyWrittenDataSegmentToReadArrays(glBufferIndex segmentStart, glBufferIndex segmentEnd) {
459 int segmentLength = (segmentEnd - segmentStart) + 1;566 int segmentLength = (segmentEnd - segmentStart) + 1;
460 if (getUseVoxelShader()) {567 if (_useVoxelShader) {
461 GLsizeiptr segmentSizeBytes = segmentLength * sizeof(VoxelShaderVBOData);568 GLsizeiptr segmentSizeBytes = segmentLength * sizeof(VoxelShaderVBOData);
462 void* readDataAt = &_readVoxelShaderData[segmentStart];569 void* readDataAt = &_readVoxelShaderData[segmentStart];
463 void* writeDataAt = &_writeVoxelShaderData[segmentStart];570 void* writeDataAt = &_writeVoxelShaderData[segmentStart];
@@ -598,7 +705,7 @@ int VoxelSystem::updateNodeInArraysAsPartialVBO(VoxelNode* node) { · 134 unmodified lines
598void VoxelSystem::updateNodeInArrays(glBufferIndex nodeIndex, const glm::vec3& startVertex,705void VoxelSystem::updateNodeInArrays(glBufferIndex nodeIndex, const glm::vec3& startVertex,
599 float voxelScale, const nodeColor& color) {706 float voxelScale, const nodeColor& color) {
600 707
601 if (getUseVoxelShader()) {708 if (_useVoxelShader) {
602 VoxelShaderVBOData* writeVerticesAt = &_writeVoxelShaderData[nodeIndex];709 VoxelShaderVBOData* writeVerticesAt = &_writeVoxelShaderData[nodeIndex];
603 writeVerticesAt->x = startVertex.x * TREE_SCALE;710 writeVerticesAt->x = startVertex.x * TREE_SCALE;
604 writeVerticesAt->y = startVertex.y * TREE_SCALE;711 writeVerticesAt->y = startVertex.y * TREE_SCALE;
@@ -647,89 +754,7 @@ void VoxelSystem::init() { · 42 unmodified lines
647 _voxelsInReadArrays = 0;754 _voxelsInReadArrays = 0;
648 755
649 // VBO for the verticesArray756 // VBO for the verticesArray
650 //initVoxelShader();757 initVoxelMemory();
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 }
733 _initialized = true;758 _initialized = true;
734}759}
735 760
@@ -801,7 +826,7 @@ void VoxelSystem::updateVBOs() { · 65 unmodified lines
801}826}
802 827
803void VoxelSystem::updateVBOSegment(glBufferIndex segmentStart, glBufferIndex segmentEnd) {828void VoxelSystem::updateVBOSegment(glBufferIndex segmentStart, glBufferIndex segmentEnd) {
804 if (getUseVoxelShader()) {829 if (_useVoxelShader) {
805 int segmentLength = (segmentEnd - segmentStart) + 1;830 int segmentLength = (segmentEnd - segmentStart) + 1;
806 GLintptr segmentStartAt = segmentStart * sizeof(VoxelShaderVBOData);831 GLintptr segmentStartAt = segmentStart * sizeof(VoxelShaderVBOData);
807 GLsizeiptr segmentSizeBytes = segmentLength * sizeof(VoxelShaderVBOData);832 GLsizeiptr segmentSizeBytes = segmentLength * sizeof(VoxelShaderVBOData);
@@ -839,9 +864,9 @@ void VoxelSystem::render(bool texture) { · 31 unmodified lines
839 864
840 updateVBOs();865 updateVBOs();
841 866
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));
843 868
844 if (getUseVoxelShader()) {869 if (_useVoxelShader) {
845 870
846 Application::getInstance()->getVoxelShader().begin();871 Application::getInstance()->getVoxelShader().begin();
847 872
@@ -1349,7 +1374,7 @@ public: · 501 unmodified lines
1349 duplicateVBOIndex(0),1374 duplicateVBOIndex(0),
1350 leafNodes(0)1375 leafNodes(0)
1351 {1376 {
1352 memset(hasIndexFound, false, MAX_VOXELS_PER_SYSTEM * sizeof(bool));1377 memset(hasIndexFound, false, DEFAULT_MAX_VOXELS_PER_SYSTEM * sizeof(bool));
1353 };1378 };
1354 1379
1355 unsigned long totalNodes;1380 unsigned long totalNodes;
@@ -1364,7 +1389,7 @@ public: · 8 unmodified lines
1364 1389
1365 unsigned long expectedMax;1390 unsigned long expectedMax;
1366 1391
1367 bool hasIndexFound[MAX_VOXELS_PER_SYSTEM];1392 bool hasIndexFound[DEFAULT_MAX_VOXELS_PER_SYSTEM];
1368};1393};
1369 1394
1370bool VoxelSystem::collectStatsForTreesAndVBOsOperation(VoxelNode* node, void* extraData) {1395bool VoxelSystem::collectStatsForTreesAndVBOsOperation(VoxelNode* node, void* extraData) {
@@ -1439,7 +1464,7 @@ void VoxelSystem::collectStatsForTreesAndVBOs() { · 68 unmodified lines
1439 glBufferIndex minInVBO = GLBUFFER_INDEX_UNKNOWN;1464 glBufferIndex minInVBO = GLBUFFER_INDEX_UNKNOWN;
1440 glBufferIndex maxInVBO = 0;1465 glBufferIndex maxInVBO = 0;
1441 1466
1442 for (glBufferIndex i = 0; i < MAX_VOXELS_PER_SYSTEM; i++) {1467 for (glBufferIndex i = 0; i < DEFAULT_MAX_VOXELS_PER_SYSTEM; i++) {
1443 if (args.hasIndexFound[i]) {1468 if (args.hasIndexFound[i]) {
1444 minInVBO = std::min(minInVBO,i);1469 minInVBO = std::min(minInVBO,i);
1445 maxInVBO = std::max(maxInVBO,i);1470 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