History

removed old normal support from voxel system only using global normals

ZappoMan committed 13 years ago

Commit b6fe8be1 parent 6e999555 files · +53 −210Browse files

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

Unified · Split

interface/src/VoxelSystem.cpp+52−195
@@ -85,9 +85,7 @@ VoxelSystem::VoxelSystem(float treeScale, int maxVoxels) · 84 unmodified lines
8585 connect(_tree, SIGNAL(importProgress(int)), SIGNAL(importProgress(int)));
8686
8787 _useVoxelShader = false;
88− _useByteNormals = false;
89− _useGlobalNormals = false;
90−
88+
9189 _writeVoxelShaderData = NULL;
9290 _readVoxelShaderData = NULL;
9391
@@ -151,42 +149,6 @@ VoxelSystem::~VoxelSystem() { · 57 unmodified lines
151149 VoxelNode::removeDeleteHook(this);
152150 }
153151
154−void VoxelSystem::setUseByteNormals(bool useByteNormals) {
155− pthread_mutex_lock(&_bufferWriteLock);
156− bool wasInitialized = _initialized;
157− if (wasInitialized) {
158− clearAllNodesBufferIndex();
159− cleanupVoxelMemory();
160− }
161− _useByteNormals = useByteNormals;
162− if (wasInitialized) {
163− init();
164− }
165− pthread_mutex_unlock(&_bufferWriteLock);
166−
167− if (wasInitialized) {
168− forceRedrawEntireTree();
169− }
170−}
171−
172−void VoxelSystem::setUseGlobalNormals(bool useGlobalNormals) {
173− pthread_mutex_lock(&_bufferWriteLock);
174− bool wasInitialized = _initialized;
175− if (wasInitialized) {
176− clearAllNodesBufferIndex();
177− cleanupVoxelMemory();
178− }
179− _useGlobalNormals = useGlobalNormals;
180− if (wasInitialized) {
181− init();
182− }
183− pthread_mutex_unlock(&_bufferWriteLock);
184−
185− if (wasInitialized) {
186− forceRedrawEntireTree();
187− }
188−}
189−
190152 void VoxelSystem::setMaxVoxels(int maxVoxels) {
191153 pthread_mutex_lock(&_bufferWriteLock);
192154 bool wasInitialized = _initialized;
@@ -237,17 +199,12 @@ void VoxelSystem::cleanupVoxelMemory() { · 44 unmodified lines
237199 glDeleteBuffers(1, &_vboVerticesID);
238200 glDeleteBuffers(1, &_vboColorsID);
239201
240− if (!_useGlobalNormals) {
241− glDeleteBuffers(1, &_vboNormalsID);
242− glDeleteBuffers(1, &_vboIndicesID);
243− } else {
244− glDeleteBuffers(1, &_vboIndicesTop);
245− glDeleteBuffers(1, &_vboIndicesBottom);
246− glDeleteBuffers(1, &_vboIndicesLeft);
247− glDeleteBuffers(1, &_vboIndicesRight);
248− glDeleteBuffers(1, &_vboIndicesFront);
249− glDeleteBuffers(1, &_vboIndicesBack);
250− }
202+ glDeleteBuffers(1, &_vboIndicesTop);
203+ glDeleteBuffers(1, &_vboIndicesBottom);
204+ glDeleteBuffers(1, &_vboIndicesLeft);
205+ glDeleteBuffers(1, &_vboIndicesRight);
206+ glDeleteBuffers(1, &_vboIndicesFront);
207+ glDeleteBuffers(1, &_vboIndicesBack);
251208
252209 delete[] _readVerticesArray;
253210 delete[] _writeVerticesArray;
@@ -332,94 +289,19 @@ void VoxelSystem::initVoxelMemory() { · 78 unmodified lines
332289 _readVoxelShaderData = new VoxelShaderVBOData[_maxVoxels];
333290 _memoryUsageRAM += (sizeof(VoxelShaderVBOData) * _maxVoxels);
334291 } else {
335− if (_useGlobalNormals) {
336− // Global Normals mode uses a technique of not including normals on any voxel vertices, and instead
337− // rendering the voxel faces in 6 passes that use a global call to glNormal3f()
338− qDebug("Using Global Normals...\n");
339− setupFaceIndices(_vboIndicesTop, identityIndicesTop);
340− setupFaceIndices(_vboIndicesBottom, identityIndicesBottom);
341− setupFaceIndices(_vboIndicesLeft, identityIndicesLeft);
342− setupFaceIndices(_vboIndicesRight, identityIndicesRight);
343− setupFaceIndices(_vboIndicesFront, identityIndicesFront);
344− setupFaceIndices(_vboIndicesBack, identityIndicesBack);
345− } else {
346− if (_useByteNormals) {
347− qDebug("Using Byte Normals...\n");
348− GLbyte* normalsArray = new GLbyte[VERTEX_POINTS_PER_VOXEL * _maxVoxels];
349− GLbyte* normalsArrayEndPointer = normalsArray;
350−
351− // populate the normalsArray
352− for (int n = 0; n < _maxVoxels; n++) {
353− for (int i = 0; i < VERTEX_POINTS_PER_VOXEL; i++) {
354− *(normalsArrayEndPointer++) = (identityNormals[i] * CHAR_MAX);
355− }
356− }
357−
358− // VBO for the normalsArray
359− glGenBuffers(1, &_vboNormalsID);
360− glBindBuffer(GL_ARRAY_BUFFER, _vboNormalsID);
361− glBufferData(GL_ARRAY_BUFFER,
362− VERTEX_POINTS_PER_VOXEL * sizeof(GLbyte) * _maxVoxels,
363− normalsArray, GL_STATIC_DRAW);
364− _memoryUsageVBO += VERTEX_POINTS_PER_VOXEL * sizeof(GLbyte) * _maxVoxels;
365−
366− // delete the indices and normals arrays that are no longer needed
367− delete[] normalsArray;
368− } else {
369− qDebug("Using Float Normals...\n");
370− GLfloat* normalsArray = new GLfloat[VERTEX_POINTS_PER_VOXEL * _maxVoxels];
371− GLfloat* normalsArrayEndPointer = normalsArray;
372−
373− // populate the normalsArray
374− for (int n = 0; n < _maxVoxels; n++) {
375− for (int i = 0; i < VERTEX_POINTS_PER_VOXEL; i++) {
376− *(normalsArrayEndPointer++) = identityNormals[i];
377− }
378− }
379−
380− // VBO for the normalsArray
381− glGenBuffers(1, &_vboNormalsID);
382− glBindBuffer(GL_ARRAY_BUFFER, _vboNormalsID);
383− glBufferData(GL_ARRAY_BUFFER,
384− VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat) * _maxVoxels,
385− normalsArray, GL_STATIC_DRAW);
386− _memoryUsageVBO += VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat) * _maxVoxels;
387−
388− // delete the indices and normals arrays that are no longer needed
389− delete[] normalsArray;
390− }
391292
392− GLuint* indicesArray = new GLuint[INDICES_PER_VOXEL * _maxVoxels];
393−
394− // populate the indicesArray
395− // this will not change given new voxels, so we can set it all up now
396− for (int n = 0; n < _maxVoxels; n++) {
397− // fill the indices array
398− int voxelIndexOffset = n * INDICES_PER_VOXEL;
399− GLuint* currentIndicesPos = indicesArray + voxelIndexOffset;
400− int startIndex = (n * VERTICES_PER_VOXEL);
401−
402− for (int i = 0; i < INDICES_PER_VOXEL; i++) {
403− // add indices for this side of the cube
404− currentIndicesPos[i] = startIndex + identityIndices[i];
405− }
406− }
407−
408− // VBO for the indicesArray
409− glGenBuffers(1, &_vboIndicesID);
410− glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vboIndicesID);
411− glBufferData(GL_ELEMENT_ARRAY_BUFFER,
412− INDICES_PER_VOXEL * sizeof(GLuint) * _maxVoxels,
413− indicesArray, GL_STATIC_DRAW);
414− _memoryUsageVBO += INDICES_PER_VOXEL * sizeof(GLuint) * _maxVoxels;
415−
416− // delete the indices and normals arrays that are no longer needed
417− delete[] indicesArray;
418−
419− }
293+ // Global Normals mode uses a technique of not including normals on any voxel vertices, and instead
294+ // rendering the voxel faces in 6 passes that use a global call to glNormal3f()
295+ qDebug("Using Global Normals...\n");
296+ setupFaceIndices(_vboIndicesTop, identityIndicesTop);
297+ setupFaceIndices(_vboIndicesBottom, identityIndicesBottom);
298+ setupFaceIndices(_vboIndicesLeft, identityIndicesLeft);
299+ setupFaceIndices(_vboIndicesRight, identityIndicesRight);
300+ setupFaceIndices(_vboIndicesFront, identityIndicesFront);
301+ setupFaceIndices(_vboIndicesBack, identityIndicesBack);
420302
421303 // Depending on if we're using per vertex normals, we will need more or less vertex points per voxel
422− int vertexPointsPerVoxel = _useGlobalNormals ? GLOBAL_NORMALS_VERTEX_POINTS_PER_VOXEL : VERTEX_POINTS_PER_VOXEL;
304+ int vertexPointsPerVoxel = GLOBAL_NORMALS_VERTEX_POINTS_PER_VOXEL;
423305 glGenBuffers(1, &_vboVerticesID);
424306 glBindBuffer(GL_ARRAY_BUFFER, _vboVerticesID);
425307 glBufferData(GL_ARRAY_BUFFER, vertexPointsPerVoxel * sizeof(GLfloat) * _maxVoxels, NULL, GL_DYNAMIC_DRAW);
@@ -730,7 +612,7 @@ void VoxelSystem::copyWrittenDataSegmentToReadArrays(glBufferIndex segmentStart, · 304 unmodified lines
730612 memcpy(readDataAt, writeDataAt, segmentSizeBytes);
731613 } else {
732614 // Depending on if we're using per vertex normals, we will need more or less vertex points per voxel
733− int vertexPointsPerVoxel = _useGlobalNormals ? GLOBAL_NORMALS_VERTEX_POINTS_PER_VOXEL : VERTEX_POINTS_PER_VOXEL;
615+ int vertexPointsPerVoxel = GLOBAL_NORMALS_VERTEX_POINTS_PER_VOXEL;
734616
735617 GLintptr segmentStartAt = segmentStart * vertexPointsPerVoxel * sizeof(GLfloat);
736618 GLsizeiptr segmentSizeBytes = segmentLength * vertexPointsPerVoxel * sizeof(GLfloat);
@@ -880,17 +762,11 @@ void VoxelSystem::updateNodeInArrays(glBufferIndex nodeIndex, const glm::vec3& s · 143 unmodified lines
880762 }
881763 } else {
882764 if (_writeVerticesArray && _writeColorsArray) {
883− int vertexPointsPerVoxel = _useGlobalNormals ? GLOBAL_NORMALS_VERTEX_POINTS_PER_VOXEL : VERTEX_POINTS_PER_VOXEL;
765+ int vertexPointsPerVoxel = GLOBAL_NORMALS_VERTEX_POINTS_PER_VOXEL;
884766 for (int j = 0; j < vertexPointsPerVoxel; j++ ) {
885767 GLfloat* writeVerticesAt = _writeVerticesArray + (nodeIndex * vertexPointsPerVoxel);
886768 GLubyte* writeColorsAt = _writeColorsArray + (nodeIndex * vertexPointsPerVoxel);
887−
888− if (_useGlobalNormals) {
889− *(writeVerticesAt+j) = startVertex[j % 3] + (identityVerticesGlobalNormals[j] * voxelScale);
890− } else {
891− *(writeVerticesAt+j) = startVertex[j % 3] + (identityVertices[j] * voxelScale);
892− }
893−
769+ *(writeVerticesAt+j) = startVertex[j % 3] + (identityVerticesGlobalNormals[j] * voxelScale);
894770 *(writeColorsAt +j) = color[j % 3];
895771 }
896772 }
@@ -1002,7 +878,7 @@ void VoxelSystem::updateVBOSegment(glBufferIndex segmentStart, glBufferIndex seg · 105 unmodified lines
1002878 glBindBuffer(GL_ARRAY_BUFFER, _vboVoxelsID);
1003879 glBufferSubData(GL_ARRAY_BUFFER, segmentStartAt, segmentSizeBytes, readVerticesFrom);
1004880 } else {
1005− int vertexPointsPerVoxel = _useGlobalNormals ? GLOBAL_NORMALS_VERTEX_POINTS_PER_VOXEL : VERTEX_POINTS_PER_VOXEL;
881+ int vertexPointsPerVoxel = GLOBAL_NORMALS_VERTEX_POINTS_PER_VOXEL;
1006882 int segmentLength = (segmentEnd - segmentStart) + 1;
1007883 GLintptr segmentStartAt = segmentStart * vertexPointsPerVoxel * sizeof(GLfloat);
1008884 GLsizeiptr segmentSizeBytes = segmentLength * vertexPointsPerVoxel * sizeof(GLfloat);
@@ -1065,57 +941,42 @@ void VoxelSystem::render(bool texture) { · 56 unmodified lines
1065941 glBindBuffer(GL_ARRAY_BUFFER, _vboColorsID);
1066942 glColorPointer(3, GL_UNSIGNED_BYTE, 0, 0);
1067943
1068− if (!_useGlobalNormals) {
1069− glEnableClientState(GL_NORMAL_ARRAY);
1070− glBindBuffer(GL_ARRAY_BUFFER, _vboNormalsID);
1071− glNormalPointer((_useByteNormals ? GL_BYTE : GL_FLOAT), 0, 0);
1072− } else {
1073− glNormal3f(0,1.0f,0); // hack for now
1074− }
1075−
1076944 applyScaleAndBindProgram(texture);
1077945
1078946 // for performance, enable backface culling
1079947 glEnable(GL_CULL_FACE);
1080948
1081− if (!_useGlobalNormals) {
1082− // draw the number of voxels we have
1083− glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vboIndicesID);
1084− glDrawRangeElementsEXT(GL_TRIANGLES, 0, VERTICES_PER_VOXEL * _voxelsInReadArrays - 1,
1085− 36 * _voxelsInReadArrays, GL_UNSIGNED_INT, 0);
1086− } else {
1087− // draw voxels in 6 passes
1088−
1089− glNormal3f(0,1.0f,0);
1090− glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vboIndicesTop);
1091− glDrawRangeElementsEXT(GL_TRIANGLES, 0, INDICES_PER_FACE * _voxelsInReadArrays - 1,
1092− INDICES_PER_FACE * _voxelsInReadArrays, GL_UNSIGNED_INT, 0);
1093−
1094− glNormal3f(0,-1.0f,0);
1095− glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vboIndicesBottom);
1096− glDrawRangeElementsEXT(GL_TRIANGLES, 0, INDICES_PER_FACE * _voxelsInReadArrays - 1,
1097− INDICES_PER_FACE * _voxelsInReadArrays, GL_UNSIGNED_INT, 0);
1098−
1099− glNormal3f(-1.0f,0,0);
1100− glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vboIndicesLeft);
1101− glDrawRangeElementsEXT(GL_TRIANGLES, 0, INDICES_PER_FACE * _voxelsInReadArrays - 1,
1102− INDICES_PER_FACE * _voxelsInReadArrays, GL_UNSIGNED_INT, 0);
1103−
1104− glNormal3f(1.0f,0,0);
1105− glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vboIndicesRight);
1106− glDrawRangeElementsEXT(GL_TRIANGLES, 0, INDICES_PER_FACE * _voxelsInReadArrays - 1,
1107− INDICES_PER_FACE * _voxelsInReadArrays, GL_UNSIGNED_INT, 0);
1108−
1109− glNormal3f(0,0,-1.0f);
1110− glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vboIndicesFront);
1111− glDrawRangeElementsEXT(GL_TRIANGLES, 0, INDICES_PER_FACE * _voxelsInReadArrays - 1,
1112− INDICES_PER_FACE * _voxelsInReadArrays, GL_UNSIGNED_INT, 0);
1113−
1114− glNormal3f(0,0,1.0f);
1115− glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vboIndicesBack);
1116− glDrawRangeElementsEXT(GL_TRIANGLES, 0, INDICES_PER_FACE * _voxelsInReadArrays - 1,
1117− INDICES_PER_FACE * _voxelsInReadArrays, GL_UNSIGNED_INT, 0);
1118− }
949+ // draw voxels in 6 passes
950+
951+ glNormal3f(0,1.0f,0);
952+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vboIndicesTop);
953+ glDrawRangeElementsEXT(GL_TRIANGLES, 0, INDICES_PER_FACE * _voxelsInReadArrays - 1,
954+ INDICES_PER_FACE * _voxelsInReadArrays, GL_UNSIGNED_INT, 0);
955+
956+ glNormal3f(0,-1.0f,0);
957+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vboIndicesBottom);
958+ glDrawRangeElementsEXT(GL_TRIANGLES, 0, INDICES_PER_FACE * _voxelsInReadArrays - 1,
959+ INDICES_PER_FACE * _voxelsInReadArrays, GL_UNSIGNED_INT, 0);
960+
961+ glNormal3f(-1.0f,0,0);
962+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vboIndicesLeft);
963+ glDrawRangeElementsEXT(GL_TRIANGLES, 0, INDICES_PER_FACE * _voxelsInReadArrays - 1,
964+ INDICES_PER_FACE * _voxelsInReadArrays, GL_UNSIGNED_INT, 0);
965+
966+ glNormal3f(1.0f,0,0);
967+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vboIndicesRight);
968+ glDrawRangeElementsEXT(GL_TRIANGLES, 0, INDICES_PER_FACE * _voxelsInReadArrays - 1,
969+ INDICES_PER_FACE * _voxelsInReadArrays, GL_UNSIGNED_INT, 0);
970+
971+ glNormal3f(0,0,-1.0f);
972+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vboIndicesFront);
973+ glDrawRangeElementsEXT(GL_TRIANGLES, 0, INDICES_PER_FACE * _voxelsInReadArrays - 1,
974+ INDICES_PER_FACE * _voxelsInReadArrays, GL_UNSIGNED_INT, 0);
975+
976+ glNormal3f(0,0,1.0f);
977+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vboIndicesBack);
978+ glDrawRangeElementsEXT(GL_TRIANGLES, 0, INDICES_PER_FACE * _voxelsInReadArrays - 1,
979+ INDICES_PER_FACE * _voxelsInReadArrays, GL_UNSIGNED_INT, 0);
1119980
1120981
1121982 glDisable(GL_CULL_FACE);
@@ -1126,10 +987,6 @@ void VoxelSystem::render(bool texture) { · 4 unmodified lines
1126987 glDisableClientState(GL_VERTEX_ARRAY);
1127988 glDisableClientState(GL_COLOR_ARRAY);
1128989
1129− if (!_useGlobalNormals) {
1130− glDisableClientState(GL_NORMAL_ARRAY);
1131− }
1132−
1133990 // bind with 0 to switch back to normal operation
1134991 glBindBuffer(GL_ARRAY_BUFFER, 0);
1135992 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

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