History

added various render mode options, including voxels as points

ZappoMan committed 13 years ago

Commit f6981fe1 parent 8baa8635 files · +158 −73Browse files

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

Unified · Split

interface/src/VoxelSystem.cpp+137−60
@@ -90,6 +90,8 @@ VoxelSystem::VoxelSystem(float treeScale, int maxVoxels) · 89 unmodified lines
9090 connect(_tree, SIGNAL(importProgress(int)), SIGNAL(importProgress(int)));
9191
9292 _useVoxelShader = false;
93+ _voxelsAsPoints = false;
94+ _voxelShaderModeWhenVoxelsAsPointsEnabled = false;
9395
9496 _writeVoxelShaderData = NULL;
9597 _readVoxelShaderData = NULL;
@@ -219,7 +221,6 @@ void VoxelSystem::setMaxVoxels(int maxVoxels) { · 123 unmodified lines
219221 if (maxVoxels == _maxVoxels) {
220222 return;
221223 }
222− pthread_mutex_lock(&_bufferWriteLock);
223224 bool wasInitialized = _initialized;
224225 if (wasInitialized) {
225226 clearAllNodesBufferIndex();
@@ -227,9 +228,8 @@ void VoxelSystem::setMaxVoxels(int maxVoxels) { · 1 unmodified line
227228 }
228229 _maxVoxels = maxVoxels;
229230 if (wasInitialized) {
230− init();
231+ initVoxelMemory();
231232 }
232− pthread_mutex_unlock(&_bufferWriteLock);
233233 if (wasInitialized) {
234234 forceRedrawEntireTree();
235235 }
@@ -240,7 +240,6 @@ void VoxelSystem::setUseVoxelShader(bool useVoxelShader) { · 4 unmodified lines
240240 return;
241241 }
242242
243− pthread_mutex_lock(&_bufferWriteLock);
244243 bool wasInitialized = _initialized;
245244 if (wasInitialized) {
246245 clearAllNodesBufferIndex();
@@ -248,10 +247,50 @@ void VoxelSystem::setUseVoxelShader(bool useVoxelShader) { · 1 unmodified line
248247 }
249248 _useVoxelShader = useVoxelShader;
250249 if (wasInitialized) {
251− init();
250+ initVoxelMemory();
251+ }
252+
253+ if (wasInitialized) {
254+ forceRedrawEntireTree();
255+ }
256+}
257+
258+void VoxelSystem::setVoxelsAsPoints(bool voxelsAsPoints) {
259+ if (_voxelsAsPoints == voxelsAsPoints) {
260+ return;
261+ }
262+
263+ bool wasInitialized = _initialized;
264+
265+ // If we're "turning on" Voxels as points, we need to double check that we're in voxel shader mode.
266+ // Voxels as points uses the VoxelShader memory model, so if we're not in voxel shader mode,
267+ // then set it to voxel shader mode.
268+ if (voxelsAsPoints) {
269+ Menu::getInstance()->getUseVoxelShader()->setEnabled(false);
270+
271+ // If enabling this... then do it before checking voxel shader status, that way, if voxel
272+ // shader is already enabled, we just start drawing as points.
273+ _voxelsAsPoints = true;
274+
275+ if (!_useVoxelShader) {
276+ setUseVoxelShader(true);
277+ _voxelShaderModeWhenVoxelsAsPointsEnabled = false;
278+ } else {
279+ _voxelShaderModeWhenVoxelsAsPointsEnabled = true;
280+ }
281+ } else {
282+ Menu::getInstance()->getUseVoxelShader()->setEnabled(true);
283+ // if we're turning OFF voxels as point mode, then we check what the state of voxel shader was when we enabled
284+ // voxels as points, if it was OFF, then we return it to that value.
285+ if (_voxelShaderModeWhenVoxelsAsPointsEnabled == false) {
286+ setUseVoxelShader(false);
287+ }
288+ // If disabling this... then do it AFTER checking previous voxel shader status, that way, if voxel
289+ // shader is was not enabled, we switch back to normal mode before turning off points.
290+ _voxelsAsPoints = false;
252291 }
253− pthread_mutex_unlock(&_bufferWriteLock);
254292
293+ // Set our voxels as points
255294 if (wasInitialized) {
256295 forceRedrawEntireTree();
257296 }
@@ -259,6 +298,8 @@ void VoxelSystem::setUseVoxelShader(bool useVoxelShader) { · 1 unmodified line
259298
260299 void VoxelSystem::cleanupVoxelMemory() {
261300 if (_initialized) {
301+ pthread_mutex_lock(&_bufferWriteLock);
302+ _initialized = false; // no longer initialized
262303 if (_useVoxelShader) {
263304 // these are used when in VoxelShader mode.
264305 glDeleteBuffers(1, &_vboVoxelsID);
@@ -266,6 +307,9 @@ void VoxelSystem::cleanupVoxelMemory() { · 1 unmodified line
266307
267308 delete[] _writeVoxelShaderData;
268309 delete[] _readVoxelShaderData;
310+
311+ _writeVoxelShaderData = _readVoxelShaderData = NULL;
312+
269313 } else {
270314 // Destroy glBuffers
271315 glDeleteBuffers(1, &_vboVerticesID);
@@ -282,11 +326,18 @@ void VoxelSystem::cleanupVoxelMemory() { · 10 unmodified lines
282326 delete[] _writeVerticesArray;
283327 delete[] _readColorsArray;
284328 delete[] _writeColorsArray;
329+
330+ _readVerticesArray = NULL;
331+ _writeVerticesArray = NULL;
332+ _readColorsArray = NULL;
333+ _writeColorsArray = NULL;
334+
285335 }
286336 delete[] _writeVoxelDirtyArray;
287337 delete[] _readVoxelDirtyArray;
338+ _writeVoxelDirtyArray = _readVoxelDirtyArray = NULL;
339+ pthread_mutex_unlock(&_bufferWriteLock);
288340 }
289− _initialized = false; // no longer initialized
290341 }
291342
292343 void VoxelSystem::setupFaceIndices(GLuint& faceVBOID, GLubyte faceIdentityIndices[]) {
@@ -318,11 +369,11 @@ void VoxelSystem::setupFaceIndices(GLuint& faceVBOID, GLubyte faceIdentityIndice · 25 unmodified lines
318369 }
319370
320371 void VoxelSystem::initVoxelMemory() {
321− _initialMemoryUsageGPU = getFreeMemoryGPU();
372+ pthread_mutex_lock(&_bufferWriteLock);
373+
322374 _memoryUsageRAM = 0;
323375 _memoryUsageVBO = 0; // our VBO allocations as we know them
324376 if (_useVoxelShader) {
325− qDebug("Using Voxel Shader...\n");
326377 GLuint* indicesArray = new GLuint[_maxVoxels];
327378
328379 // populate the indicesArray
@@ -418,6 +469,10 @@ void VoxelSystem::initVoxelMemory() { · 89 unmodified lines
418469 _perlinModulateProgram.release();
419470 }
420471 }
472+
473+ _initialized = true;
474+
475+ pthread_mutex_unlock(&_bufferWriteLock);
421476 }
422477
423478 void VoxelSystem::loadVoxelsFile(const char* fileName, bool wantColorRandomizer) {
@@ -544,7 +599,12 @@ int VoxelSystem::parseData(unsigned char* sourceBuffer, int numBytes) { · 120 unmodified lines
544599
545600 void VoxelSystem::setupNewVoxelsForDrawing() {
546601 PerformanceWarning warn(Menu::getInstance()->isOptionChecked(MenuOption::PipelineWarnings),
547− "setupNewVoxelsForDrawing()"); // would like to include _voxelsInArrays, _voxelsUpdated
602+ "setupNewVoxelsForDrawing()");
603+
604+ if (!_initialized) {
605+ return; // bail early if we're not initialized
606+ }
607+
548608 uint64_t start = usecTimestampNow();
549609 uint64_t sinceLastTime = (start - _setupNewVoxelsForDrawingLastFinished) / 1000;
550610
@@ -587,7 +647,11 @@ void VoxelSystem::setupNewVoxelsForDrawing() { · 36 unmodified lines
587647 }
588648
589649 // lock on the buffer write lock so we can't modify the data when the GPU is reading it
590− pthread_mutex_lock(&_bufferWriteLock);
650+ {
651+ PerformanceWarning warn(Menu::getInstance()->isOptionChecked(MenuOption::PipelineWarnings),
652+ "setupNewVoxelsForDrawing()... pthread_mutex_lock(&_bufferWriteLock)");
653+ pthread_mutex_lock(&_bufferWriteLock);
654+ }
591655
592656 if (_voxelsUpdated) {
593657 _voxelsDirty=true;
@@ -957,14 +1021,13 @@ void VoxelSystem::init() { · 363 unmodified lines
9571021 _setupNewVoxelsForDrawingLastElapsed = 0;
9581022 _lastViewCullingElapsed = _lastViewCulling = 0;
9591023
960− // When we change voxels representations in the arrays, we'll update this
9611024 _voxelsDirty = false;
9621025 _voxelsInWriteArrays = 0;
9631026 _voxelsInReadArrays = 0;
9641027
9651028 // VBO for the verticesArray
1029+ _initialMemoryUsageGPU = getFreeMemoryGPU();
9661030 initVoxelMemory();
967− _initialized = true;
9681031
9691032 // our own _removedVoxels doesn't need to be notified of voxel deletes
9701033 VoxelNode::removeDeleteHook(&_removedVoxels);
@@ -1071,16 +1134,14 @@ void VoxelSystem::render(bool texture) { · 100 unmodified lines
10711134 return;
10721135 }
10731136
1074− // get the lock so that the update thread won't change anything
1075− pthread_mutex_lock(&_bufferWriteLock);
1076−
10771137 updateVBOs();
1078−
1138+
1139+ bool dontCallOpenGLDraw = Menu::getInstance()->isOptionChecked(MenuOption::DontCallOpenGLForVoxels);
1140+ // if not don't... then do...
10791141 if (_useVoxelShader) {
10801142 PerformanceWarning warn(Menu::getInstance()->isOptionChecked(MenuOption::PipelineWarnings),
10811143 "render().. _useVoxelShader openGL..");
1082−
1083− Application::getInstance()->getVoxelShader().begin();
1144+
10841145
10851146 //Define this somewhere in your header file
10861147 #define BUFFER_OFFSET(i) ((void*)(i))
@@ -1088,14 +1149,29 @@ void VoxelSystem::render(bool texture) { · 1 unmodified line
10881149 glBindBuffer(GL_ARRAY_BUFFER, _vboVoxelsID);
10891150 glEnableClientState(GL_VERTEX_ARRAY);
10901151 glVertexPointer(3, GL_FLOAT, sizeof(VoxelShaderVBOData), BUFFER_OFFSET(0)); //The starting point of the VBO, for the vertices
1091− int loc = Application::getInstance()->getVoxelShader().attributeLocation("voxelSizeIn");
1092− glEnableVertexAttribArray(loc);
1093− glVertexAttribPointer(loc, 1, GL_FLOAT, false, sizeof(VoxelShaderVBOData), BUFFER_OFFSET(3*sizeof(float)));
1152+
1153+ int attributeLocation;
1154+
1155+ if (!_voxelsAsPoints) {
1156+ Application::getInstance()->getVoxelShader().begin();
1157+
1158+ attributeLocation = Application::getInstance()->getVoxelShader().attributeLocation("voxelSizeIn");
1159+ glEnableVertexAttribArray(attributeLocation);
1160+ glVertexAttribPointer(attributeLocation, 1, GL_FLOAT, false, sizeof(VoxelShaderVBOData), BUFFER_OFFSET(3*sizeof(float)));
1161+ } else {
1162+ const float POINT_SIZE = 4.0;
1163+ glPointSize(POINT_SIZE);
1164+ }
1165+
1166+
10941167 glEnableClientState(GL_COLOR_ARRAY);
10951168 glColorPointer(3, GL_UNSIGNED_BYTE, sizeof(VoxelShaderVBOData), BUFFER_OFFSET(4*sizeof(float)));//The starting point of colors
10961169
10971170 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vboVoxelsIndicesID);
1098− glDrawElements(GL_POINTS, _voxelsInReadArrays, GL_UNSIGNED_INT, BUFFER_OFFSET(0)); //The starting point of the IBO
1171+
1172+ if (!dontCallOpenGLDraw) {
1173+ glDrawElements(GL_POINTS, _voxelsInReadArrays, GL_UNSIGNED_INT, BUFFER_OFFSET(0)); //The starting point of the IBO
1174+ }
10991175
11001176 // deactivate vertex and color arrays after drawing
11011177 glDisableClientState(GL_VERTEX_ARRAY);
@@ -1105,11 +1181,13 @@ void VoxelSystem::render(bool texture) { · 3 unmodified lines
11051181 glBindBuffer(GL_ARRAY_BUFFER, 0);
11061182 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
11071183
1108− Application::getInstance()->getVoxelShader().end();
1109−
1184+ if (!_voxelsAsPoints) {
1185+ Application::getInstance()->getVoxelShader().end();
1186+ glDisableVertexAttribArray(attributeLocation);
1187+ }
11101188 } else {
11111189 PerformanceWarning warn(Menu::getInstance()->isOptionChecked(MenuOption::PipelineWarnings), "render().. openGL...");
1112−
1190+
11131191 // tell OpenGL where to find vertex and color information
11141192 glEnableClientState(GL_VERTEX_ARRAY);
11151193 glEnableClientState(GL_COLOR_ARRAY);
@@ -1121,47 +1199,48 @@ void VoxelSystem::render(bool texture) { · 5 unmodified lines
11211199 glColorPointer(3, GL_UNSIGNED_BYTE, 0, 0);
11221200
11231201 applyScaleAndBindProgram(texture);
1124−
1202+
11251203 // for performance, enable backface culling
11261204 glEnable(GL_CULL_FACE);
11271205
11281206 // draw voxels in 6 passes
11291207
1130− glNormal3f(0,1.0f,0);
1131− glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vboIndicesTop);
1132− glDrawRangeElementsEXT(GL_TRIANGLES, 0, INDICES_PER_FACE * _voxelsInReadArrays - 1,
1133− INDICES_PER_FACE * _voxelsInReadArrays, GL_UNSIGNED_INT, 0);
1134−
1135− glNormal3f(0,-1.0f,0);
1136− glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vboIndicesBottom);
1137− glDrawRangeElementsEXT(GL_TRIANGLES, 0, INDICES_PER_FACE * _voxelsInReadArrays - 1,
1138− INDICES_PER_FACE * _voxelsInReadArrays, GL_UNSIGNED_INT, 0);
1139−
1140− glNormal3f(-1.0f,0,0);
1141− glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vboIndicesLeft);
1142− glDrawRangeElementsEXT(GL_TRIANGLES, 0, INDICES_PER_FACE * _voxelsInReadArrays - 1,
1143− INDICES_PER_FACE * _voxelsInReadArrays, GL_UNSIGNED_INT, 0);
1144−
1145− glNormal3f(1.0f,0,0);
1146− glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vboIndicesRight);
1147− glDrawRangeElementsEXT(GL_TRIANGLES, 0, INDICES_PER_FACE * _voxelsInReadArrays - 1,
1148− INDICES_PER_FACE * _voxelsInReadArrays, GL_UNSIGNED_INT, 0);
1149−
1150− glNormal3f(0,0,-1.0f);
1151− glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vboIndicesFront);
1152− glDrawRangeElementsEXT(GL_TRIANGLES, 0, INDICES_PER_FACE * _voxelsInReadArrays - 1,
1153− INDICES_PER_FACE * _voxelsInReadArrays, GL_UNSIGNED_INT, 0);
1154−
1155− glNormal3f(0,0,1.0f);
1156− glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vboIndicesBack);
1157− glDrawRangeElementsEXT(GL_TRIANGLES, 0, INDICES_PER_FACE * _voxelsInReadArrays - 1,
1158− INDICES_PER_FACE * _voxelsInReadArrays, GL_UNSIGNED_INT, 0);
1159−
1208+ if (!dontCallOpenGLDraw) {
1209+ glNormal3f(0,1.0f,0);
1210+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vboIndicesTop);
1211+ glDrawRangeElementsEXT(GL_TRIANGLES, 0, INDICES_PER_FACE * _voxelsInReadArrays - 1,
1212+ INDICES_PER_FACE * _voxelsInReadArrays, GL_UNSIGNED_INT, 0);
1213+
1214+ glNormal3f(0,-1.0f,0);
1215+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vboIndicesBottom);
1216+ glDrawRangeElementsEXT(GL_TRIANGLES, 0, INDICES_PER_FACE * _voxelsInReadArrays - 1,
1217+ INDICES_PER_FACE * _voxelsInReadArrays, GL_UNSIGNED_INT, 0);
1218+
1219+ glNormal3f(-1.0f,0,0);
1220+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vboIndicesLeft);
1221+ glDrawRangeElementsEXT(GL_TRIANGLES, 0, INDICES_PER_FACE * _voxelsInReadArrays - 1,
1222+ INDICES_PER_FACE * _voxelsInReadArrays, GL_UNSIGNED_INT, 0);
1223+
1224+ glNormal3f(1.0f,0,0);
1225+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vboIndicesRight);
1226+ glDrawRangeElementsEXT(GL_TRIANGLES, 0, INDICES_PER_FACE * _voxelsInReadArrays - 1,
1227+ INDICES_PER_FACE * _voxelsInReadArrays, GL_UNSIGNED_INT, 0);
1228+
1229+ glNormal3f(0,0,-1.0f);
1230+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vboIndicesFront);
1231+ glDrawRangeElementsEXT(GL_TRIANGLES, 0, INDICES_PER_FACE * _voxelsInReadArrays - 1,
1232+ INDICES_PER_FACE * _voxelsInReadArrays, GL_UNSIGNED_INT, 0);
1233+
1234+ glNormal3f(0,0,1.0f);
1235+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vboIndicesBack);
1236+ glDrawRangeElementsEXT(GL_TRIANGLES, 0, INDICES_PER_FACE * _voxelsInReadArrays - 1,
1237+ INDICES_PER_FACE * _voxelsInReadArrays, GL_UNSIGNED_INT, 0);
1238+ }
11601239
11611240 glDisable(GL_CULL_FACE);
11621241
11631242 removeScaleAndReleaseProgram(texture);
1164−
1243+
11651244 // deactivate vertex and color arrays after drawing
11661245 glDisableClientState(GL_VERTEX_ARRAY);
11671246 glDisableClientState(GL_COLOR_ARRAY);
@@ -1170,8 +1249,6 @@ void VoxelSystem::render(bool texture) { · 2 unmodified lines
11701249 glBindBuffer(GL_ARRAY_BUFFER, 0);
11711250 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
11721251 }
1173−
1174− pthread_mutex_unlock(&_bufferWriteLock);
11751252 }
11761253
11771254 void VoxelSystem::applyScaleAndBindProgram(bool texture) {

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