History

add support for global normals for voxels

ZappoMan committed 13 years ago

Commit 8cffa8d1 parent dbfc0955 files · +262 −107Browse files

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

Unified · Split

interface/src/VoxelSystem.cpp+240−107
@@ -33,9 +33,11 @@ · 32 unmodified lines
3333 #include "VoxelConstants.h"
3434 #include "VoxelSystem.h"
3535
36−float identityVertices[] = { 0,0,0, 1,0,0, 1,1,0, 0,1,0, 0,0,1, 1,0,1, 1,1,1, 0,1,1,
37− 0,0,0, 1,0,0, 1,1,0, 0,1,0, 0,0,1, 1,0,1, 1,1,1, 0,1,1,
38− 0,0,0, 1,0,0, 1,1,0, 0,1,0, 0,0,1, 1,0,1, 1,1,1, 0,1,1 };
36+float identityVerticesGlobalNormals[] = { 0,0,0, 1,0,0, 1,1,0, 0,1,0, 0,0,1, 1,0,1, 1,1,1, 0,1,1 };
37+
38+float identityVertices[] = { 0,0,0, 1,0,0, 1,1,0, 0,1,0, 0,0,1, 1,0,1, 1,1,1, 0,1,1, //0-7
39+ 0,0,0, 1,0,0, 1,1,0, 0,1,0, 0,0,1, 1,0,1, 1,1,1, 0,1,1, //8-15
40+ 0,0,0, 1,0,0, 1,1,0, 0,1,0, 0,0,1, 1,0,1, 1,1,1, 0,1,1 }; // 16-23
3941
4042 GLfloat identityNormals[] = { 0,0,-1, 0,0,-1, 0,0,-1, 0,0,-1,
4143 0,0,+1, 0,0,+1, 0,0,+1, 0,0,+1,
@@ -51,6 +53,13 @@ GLubyte identityIndices[] = { 0,2,1, 0,3,2, // Z- · 9 unmodified lines
5153 10,11,15, 10,15,14, // Y+
5254 4,5,6, 4,6,7 }; // Z+
5355
56+GLubyte identityIndicesTop[] = { 2, 3, 7, 2, 7, 6 };
57+GLubyte identityIndicesBottom[] = { 0, 1, 5, 0, 5, 4 };
58+GLubyte identityIndicesLeft[] = { 0, 7, 3, 0, 4, 7 };
59+GLubyte identityIndicesRight[] = { 1, 2, 6, 1, 6, 5 };
60+GLubyte identityIndicesFront[] = { 0, 2, 1, 0, 3, 2 };
61+GLubyte identityIndicesBack[] = { 4, 5, 6, 4, 6, 7 };
62+
5463 VoxelSystem::VoxelSystem(float treeScale, int maxVoxels)
5564 : NodeData(NULL),
5665 _treeScale(treeScale),
@@ -77,6 +86,7 @@ VoxelSystem::VoxelSystem(float treeScale, int maxVoxels) · 20 unmodified lines
7786
7887 _useVoxelShader = false;
7988 _useByteNormals = false;
89+ _useGlobalNormals = false;
8090
8191 _writeVoxelShaderData = NULL;
8292 _readVoxelShaderData = NULL;
@@ -145,6 +155,7 @@ void VoxelSystem::setUseByteNormals(bool useByteNormals) { · 62 unmodified lines
145155 pthread_mutex_lock(&_bufferWriteLock);
146156 bool wasInitialized = _initialized;
147157 if (wasInitialized) {
158+ clearAllNodesBufferIndex();
148159 cleanupVoxelMemory();
149160 }
150161 _useByteNormals = useByteNormals;
@@ -158,11 +169,29 @@ void VoxelSystem::setUseByteNormals(bool useByteNormals) { · 7 unmodified lines
158169 }
159170 }
160171
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+}
161189
162190 void VoxelSystem::setMaxVoxels(int maxVoxels) {
163191 pthread_mutex_lock(&_bufferWriteLock);
164192 bool wasInitialized = _initialized;
165193 if (wasInitialized) {
194+ clearAllNodesBufferIndex();
166195 cleanupVoxelMemory();
167196 }
168197 _maxVoxels = maxVoxels;
@@ -206,9 +235,19 @@ void VoxelSystem::cleanupVoxelMemory() { · 37 unmodified lines
206235 } else {
207236 // Destroy glBuffers
208237 glDeleteBuffers(1, &_vboVerticesID);
209− glDeleteBuffers(1, &_vboNormalsID);
210238 glDeleteBuffers(1, &_vboColorsID);
211− glDeleteBuffers(1, &_vboIndicesID);
239+
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+ }
212251
213252 delete[] _readVerticesArray;
214253 delete[] _writeVerticesArray;
@@ -221,6 +260,34 @@ void VoxelSystem::cleanupVoxelMemory() { · 6 unmodified lines
221260 _initialized = false; // no longer initialized
222261 }
223262
263+void VoxelSystem::setupFaceIndices(GLuint& faceVBOID, GLubyte faceIdentityIndices[]) {
264+ GLuint* indicesArray = new GLuint[INDICES_PER_FACE * _maxVoxels];
265+
266+ // populate the indicesArray
267+ // this will not change given new voxels, so we can set it all up now
268+ for (int n = 0; n < _maxVoxels; n++) {
269+ // fill the indices array
270+ int voxelIndexOffset = n * INDICES_PER_FACE;
271+ GLuint* currentIndicesPos = indicesArray + voxelIndexOffset;
272+ int startIndex = (n * GLOBAL_NORMALS_VERTICES_PER_VOXEL);
273+
274+ for (int i = 0; i < INDICES_PER_FACE; i++) {
275+ // add indices for this side of the cube
276+ currentIndicesPos[i] = startIndex + faceIdentityIndices[i];
277+ }
278+ }
279+
280+ glGenBuffers(1, &faceVBOID);
281+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, faceVBOID);
282+ glBufferData(GL_ELEMENT_ARRAY_BUFFER,
283+ INDICES_PER_FACE * sizeof(GLuint) * _maxVoxels,
284+ indicesArray, GL_STATIC_DRAW);
285+ _memoryUsageVBO += INDICES_PER_FACE * sizeof(GLuint) * _maxVoxels;
286+
287+ // delete the indices and normals arrays that are no longer needed
288+ delete[] indicesArray;
289+}
290+
224291 void VoxelSystem::initVoxelMemory() {
225292 _initialMemoryUsageGPU = getFreeMemoryGPU();
226293 _memoryUsageRAM = 0;
@@ -265,91 +332,104 @@ void VoxelSystem::initVoxelMemory() { · 38 unmodified lines
265332 _readVoxelShaderData = new VoxelShaderVBOData[_maxVoxels];
266333 _memoryUsageRAM += (sizeof(VoxelShaderVBOData) * _maxVoxels);
267334 } else {
268− GLuint* indicesArray = new GLuint[INDICES_PER_VOXEL * _maxVoxels];
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+ }
269357
270− // populate the indicesArray
271− // this will not change given new voxels, so we can set it all up now
272− for (int n = 0; n < _maxVoxels; n++) {
273− // fill the indices array
274− int voxelIndexOffset = n * INDICES_PER_VOXEL;
275− GLuint* currentIndicesPos = indicesArray + voxelIndexOffset;
276− int startIndex = (n * VERTICES_PER_VOXEL);
277−
278− for (int i = 0; i < INDICES_PER_VOXEL; i++) {
279− // add indices for this side of the cube
280− currentIndicesPos[i] = startIndex + identityIndices[i];
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;
281390 }
282− }
283391
284− if (_useByteNormals) {
285− qDebug("Using Byte Normals...\n");
286− GLbyte* normalsArray = new GLbyte[VERTEX_POINTS_PER_VOXEL * _maxVoxels];
287− GLbyte* normalsArrayEndPointer = normalsArray;
392+ GLuint* indicesArray = new GLuint[INDICES_PER_VOXEL * _maxVoxels];
288393
289− // populate the normalsArray
394+ // populate the indicesArray
395+ // this will not change given new voxels, so we can set it all up now
290396 for (int n = 0; n < _maxVoxels; n++) {
291− for (int i = 0; i < VERTEX_POINTS_PER_VOXEL; i++) {
292− *(normalsArrayEndPointer++) = (identityNormals[i] * CHAR_MAX);
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];
293405 }
294406 }
295407
296− // VBO for the normalsArray
297− glGenBuffers(1, &_vboNormalsID);
298− glBindBuffer(GL_ARRAY_BUFFER, _vboNormalsID);
299− glBufferData(GL_ARRAY_BUFFER,
300− VERTEX_POINTS_PER_VOXEL * sizeof(GLbyte) * _maxVoxels,
301− normalsArray, GL_STATIC_DRAW);
302− _memoryUsageVBO += VERTEX_POINTS_PER_VOXEL * sizeof(GLbyte) * _maxVoxels;
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;
303415
304416 // delete the indices and normals arrays that are no longer needed
305− delete[] normalsArray;
306− } else {
307− qDebug("Using Float Normals...\n");
308− GLfloat* normalsArray = new GLfloat[VERTEX_POINTS_PER_VOXEL * _maxVoxels];
309− GLfloat* normalsArrayEndPointer = normalsArray;
417+ delete[] indicesArray;
310418
311− // populate the normalsArray
312− for (int n = 0; n < _maxVoxels; n++) {
313− for (int i = 0; i < VERTEX_POINTS_PER_VOXEL; i++) {
314− *(normalsArrayEndPointer++) = identityNormals[i];
315− }
316− }
317−
318− // VBO for the normalsArray
319− glGenBuffers(1, &_vboNormalsID);
320− glBindBuffer(GL_ARRAY_BUFFER, _vboNormalsID);
321− glBufferData(GL_ARRAY_BUFFER,
322− VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat) * _maxVoxels,
323− normalsArray, GL_STATIC_DRAW);
324− _memoryUsageVBO += VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat) * _maxVoxels;
325−
326− // delete the indices and normals arrays that are no longer needed
327− delete[] normalsArray;
328419 }
329−
330−
420+
421+ // 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;
331423 glGenBuffers(1, &_vboVerticesID);
332424 glBindBuffer(GL_ARRAY_BUFFER, _vboVerticesID);
333− glBufferData(GL_ARRAY_BUFFER, VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat) * _maxVoxels, NULL, GL_DYNAMIC_DRAW);
334− _memoryUsageVBO += VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat) * _maxVoxels;
425+ glBufferData(GL_ARRAY_BUFFER, vertexPointsPerVoxel * sizeof(GLfloat) * _maxVoxels, NULL, GL_DYNAMIC_DRAW);
426+ _memoryUsageVBO += vertexPointsPerVoxel * sizeof(GLfloat) * _maxVoxels;
335427
336428 // VBO for colorsArray
337429 glGenBuffers(1, &_vboColorsID);
338430 glBindBuffer(GL_ARRAY_BUFFER, _vboColorsID);
339− glBufferData(GL_ARRAY_BUFFER, VERTEX_POINTS_PER_VOXEL * sizeof(GLubyte) * _maxVoxels, NULL, GL_DYNAMIC_DRAW);
340− _memoryUsageVBO += VERTEX_POINTS_PER_VOXEL * sizeof(GLubyte) * _maxVoxels;
341−
342− // VBO for the indicesArray
343− glGenBuffers(1, &_vboIndicesID);
344− glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vboIndicesID);
345− glBufferData(GL_ELEMENT_ARRAY_BUFFER,
346− INDICES_PER_VOXEL * sizeof(GLuint) * _maxVoxels,
347− indicesArray, GL_STATIC_DRAW);
348− _memoryUsageVBO += INDICES_PER_VOXEL * sizeof(GLuint) * _maxVoxels;
349−
350− // delete the indices and normals arrays that are no longer needed
351− delete[] indicesArray;
352−
431+ glBufferData(GL_ARRAY_BUFFER, vertexPointsPerVoxel * sizeof(GLubyte) * _maxVoxels, NULL, GL_DYNAMIC_DRAW);
432+ _memoryUsageVBO += vertexPointsPerVoxel * sizeof(GLubyte) * _maxVoxels;
353433
354434 // we will track individual dirty sections with these arrays of bools
355435 _writeVoxelDirtyArray = new bool[_maxVoxels];
@@ -361,15 +441,15 @@ void VoxelSystem::initVoxelMemory() { · 5 unmodified lines
361441 _memoryUsageRAM += (sizeof(bool) * _maxVoxels);
362442
363443 // prep the data structures for incoming voxel data
364− _writeVerticesArray = new GLfloat[VERTEX_POINTS_PER_VOXEL * _maxVoxels];
365− _memoryUsageRAM += (sizeof(GLfloat) * VERTEX_POINTS_PER_VOXEL * _maxVoxels);
366− _readVerticesArray = new GLfloat[VERTEX_POINTS_PER_VOXEL * _maxVoxels];
367− _memoryUsageRAM += (sizeof(GLfloat) * VERTEX_POINTS_PER_VOXEL * _maxVoxels);
444+ _writeVerticesArray = new GLfloat[vertexPointsPerVoxel * _maxVoxels];
445+ _memoryUsageRAM += (sizeof(GLfloat) * vertexPointsPerVoxel * _maxVoxels);
446+ _readVerticesArray = new GLfloat[vertexPointsPerVoxel * _maxVoxels];
447+ _memoryUsageRAM += (sizeof(GLfloat) * vertexPointsPerVoxel * _maxVoxels);
368448
369− _writeColorsArray = new GLubyte[VERTEX_POINTS_PER_VOXEL * _maxVoxels];
370− _memoryUsageRAM += (sizeof(GLubyte) * VERTEX_POINTS_PER_VOXEL * _maxVoxels);
371− _readColorsArray = new GLubyte[VERTEX_POINTS_PER_VOXEL * _maxVoxels];
372− _memoryUsageRAM += (sizeof(GLubyte) * VERTEX_POINTS_PER_VOXEL * _maxVoxels);
449+ _writeColorsArray = new GLubyte[vertexPointsPerVoxel * _maxVoxels];
450+ _memoryUsageRAM += (sizeof(GLubyte) * vertexPointsPerVoxel * _maxVoxels);
451+ _readColorsArray = new GLubyte[vertexPointsPerVoxel * _maxVoxels];
452+ _memoryUsageRAM += (sizeof(GLubyte) * vertexPointsPerVoxel * _maxVoxels);
373453
374454
375455 // create our simple fragment shader if we're the first system to init
@@ -649,16 +729,19 @@ void VoxelSystem::copyWrittenDataSegmentToReadArrays(glBufferIndex segmentStart, · 273 unmodified lines
649729 void* writeDataAt = &_writeVoxelShaderData[segmentStart];
650730 memcpy(readDataAt, writeDataAt, segmentSizeBytes);
651731 } else {
652− GLintptr segmentStartAt = segmentStart * VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat);
653− GLsizeiptr segmentSizeBytes = segmentLength * VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat);
654− GLfloat* readVerticesAt = _readVerticesArray + (segmentStart * VERTEX_POINTS_PER_VOXEL);
655− GLfloat* writeVerticesAt = _writeVerticesArray + (segmentStart * VERTEX_POINTS_PER_VOXEL);
732+ // 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;
734+
735+ GLintptr segmentStartAt = segmentStart * vertexPointsPerVoxel * sizeof(GLfloat);
736+ GLsizeiptr segmentSizeBytes = segmentLength * vertexPointsPerVoxel * sizeof(GLfloat);
737+ GLfloat* readVerticesAt = _readVerticesArray + (segmentStart * vertexPointsPerVoxel);
738+ GLfloat* writeVerticesAt = _writeVerticesArray + (segmentStart * vertexPointsPerVoxel);
656739 memcpy(readVerticesAt, writeVerticesAt, segmentSizeBytes);
657740
658− segmentStartAt = segmentStart * VERTEX_POINTS_PER_VOXEL * sizeof(GLubyte);
659− segmentSizeBytes = segmentLength * VERTEX_POINTS_PER_VOXEL * sizeof(GLubyte);
660− GLubyte* readColorsAt = _readColorsArray + (segmentStart * VERTEX_POINTS_PER_VOXEL);
661− GLubyte* writeColorsAt = _writeColorsArray + (segmentStart * VERTEX_POINTS_PER_VOXEL);
741+ segmentStartAt = segmentStart * vertexPointsPerVoxel * sizeof(GLubyte);
742+ segmentSizeBytes = segmentLength * vertexPointsPerVoxel * sizeof(GLubyte);
743+ GLubyte* readColorsAt = _readColorsArray + (segmentStart * vertexPointsPerVoxel);
744+ GLubyte* writeColorsAt = _writeColorsArray + (segmentStart * vertexPointsPerVoxel);
662745 memcpy(readColorsAt, writeColorsAt, segmentSizeBytes);
663746 }
664747 }
@@ -797,10 +880,17 @@ void VoxelSystem::updateNodeInArrays(glBufferIndex nodeIndex, const glm::vec3& s · 132 unmodified lines
797880 }
798881 } else {
799882 if (_writeVerticesArray && _writeColorsArray) {
800− for (int j = 0; j < VERTEX_POINTS_PER_VOXEL; j++ ) {
801− GLfloat* writeVerticesAt = _writeVerticesArray + (nodeIndex * VERTEX_POINTS_PER_VOXEL);
802− GLubyte* writeColorsAt = _writeColorsArray + (nodeIndex * VERTEX_POINTS_PER_VOXEL);
803− *(writeVerticesAt+j) = startVertex[j % 3] + (identityVertices[j] * voxelScale);
883+ int vertexPointsPerVoxel = _useGlobalNormals ? GLOBAL_NORMALS_VERTEX_POINTS_PER_VOXEL : VERTEX_POINTS_PER_VOXEL;
884+ for (int j = 0; j < vertexPointsPerVoxel; j++ ) {
885+ GLfloat* writeVerticesAt = _writeVerticesArray + (nodeIndex * vertexPointsPerVoxel);
886+ 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+
804894 *(writeColorsAt +j) = color[j % 3];
805895 }
806896 }
@@ -912,15 +1002,16 @@ void VoxelSystem::updateVBOSegment(glBufferIndex segmentStart, glBufferIndex seg · 105 unmodified lines
9121002 glBindBuffer(GL_ARRAY_BUFFER, _vboVoxelsID);
9131003 glBufferSubData(GL_ARRAY_BUFFER, segmentStartAt, segmentSizeBytes, readVerticesFrom);
9141004 } else {
1005+ int vertexPointsPerVoxel = _useGlobalNormals ? GLOBAL_NORMALS_VERTEX_POINTS_PER_VOXEL : VERTEX_POINTS_PER_VOXEL;
9151006 int segmentLength = (segmentEnd - segmentStart) + 1;
916− GLintptr segmentStartAt = segmentStart * VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat);
917− GLsizeiptr segmentSizeBytes = segmentLength * VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat);
918− GLfloat* readVerticesFrom = _readVerticesArray + (segmentStart * VERTEX_POINTS_PER_VOXEL);
1007+ GLintptr segmentStartAt = segmentStart * vertexPointsPerVoxel * sizeof(GLfloat);
1008+ GLsizeiptr segmentSizeBytes = segmentLength * vertexPointsPerVoxel * sizeof(GLfloat);
1009+ GLfloat* readVerticesFrom = _readVerticesArray + (segmentStart * vertexPointsPerVoxel);
9191010 glBindBuffer(GL_ARRAY_BUFFER, _vboVerticesID);
9201011 glBufferSubData(GL_ARRAY_BUFFER, segmentStartAt, segmentSizeBytes, readVerticesFrom);
921− segmentStartAt = segmentStart * VERTEX_POINTS_PER_VOXEL * sizeof(GLubyte);
922− segmentSizeBytes = segmentLength * VERTEX_POINTS_PER_VOXEL * sizeof(GLubyte);
923− GLubyte* readColorsFrom = _readColorsArray + (segmentStart * VERTEX_POINTS_PER_VOXEL);
1012+ segmentStartAt = segmentStart * vertexPointsPerVoxel * sizeof(GLubyte);
1013+ segmentSizeBytes = segmentLength * vertexPointsPerVoxel * sizeof(GLubyte);
1014+ GLubyte* readColorsFrom = _readColorsArray + (segmentStart * vertexPointsPerVoxel);
9241015 glBindBuffer(GL_ARRAY_BUFFER, _vboColorsID);
9251016 glBufferSubData(GL_ARRAY_BUFFER, segmentStartAt, segmentSizeBytes, readColorsFrom);
9261017 }
@@ -966,27 +1057,66 @@ void VoxelSystem::render(bool texture) { · 39 unmodified lines
9661057 } else {
9671058 // tell OpenGL where to find vertex and color information
9681059 glEnableClientState(GL_VERTEX_ARRAY);
969− glEnableClientState(GL_NORMAL_ARRAY);
9701060 glEnableClientState(GL_COLOR_ARRAY);
9711061
9721062 glBindBuffer(GL_ARRAY_BUFFER, _vboVerticesID);
9731063 glVertexPointer(3, GL_FLOAT, 0, 0);
9741064
975− glBindBuffer(GL_ARRAY_BUFFER, _vboNormalsID);
976− glNormalPointer((_useByteNormals ? GL_BYTE : GL_FLOAT), 0, 0);
977−
9781065 glBindBuffer(GL_ARRAY_BUFFER, _vboColorsID);
9791066 glColorPointer(3, GL_UNSIGNED_BYTE, 0, 0);
9801067
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+
9811076 applyScaleAndBindProgram(texture);
9821077
9831078 // for performance, enable backface culling
9841079 glEnable(GL_CULL_FACE);
9851080
986− // draw the number of voxels we have
987− glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vboIndicesID);
988− glDrawRangeElementsEXT(GL_TRIANGLES, 0, VERTICES_PER_VOXEL * _voxelsInReadArrays - 1,
989− 36 * _voxelsInReadArrays, GL_UNSIGNED_INT, 0);
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+ }
1119+
9901120
9911121 glDisable(GL_CULL_FACE);
9921122
@@ -994,9 +1124,12 @@ void VoxelSystem::render(bool texture) { · 1 unmodified line
9941124
9951125 // deactivate vertex and color arrays after drawing
9961126 glDisableClientState(GL_VERTEX_ARRAY);
997− glDisableClientState(GL_NORMAL_ARRAY);
9981127 glDisableClientState(GL_COLOR_ARRAY);
9991128
1129+ if (!_useGlobalNormals) {
1130+ glDisableClientState(GL_NORMAL_ARRAY);
1131+ }
1132+
10001133 // bind with 0 to switch back to normal operation
10011134 glBindBuffer(GL_ARRAY_BUFFER, 0);
10021135 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