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
33#include "VoxelConstants.h"33#include "VoxelConstants.h"
34#include "VoxelSystem.h"34#include "VoxelSystem.h"
35 35
36float 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,36float 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 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 0,0,0, 1,0,0, 1,1,0, 0,1,0, 0,0,1, 1,0,1, 1,1,1, 0,1,1 };38float 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
39 41
40GLfloat identityNormals[] = { 0,0,-1, 0,0,-1, 0,0,-1, 0,0,-1,42GLfloat identityNormals[] = { 0,0,-1, 0,0,-1, 0,0,-1, 0,0,-1,
41 0,0,+1, 0,0,+1, 0,0,+1, 0,0,+1,43 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
51 10,11,15, 10,15,14, // Y+53 10,11,15, 10,15,14, // Y+
52 4,5,6, 4,6,7 }; // Z+54 4,5,6, 4,6,7 }; // Z+
53 55
56GLubyte identityIndicesTop[] = { 2, 3, 7, 2, 7, 6 };
57GLubyte identityIndicesBottom[] = { 0, 1, 5, 0, 5, 4 };
58GLubyte identityIndicesLeft[] = { 0, 7, 3, 0, 4, 7 };
59GLubyte identityIndicesRight[] = { 1, 2, 6, 1, 6, 5 };
60GLubyte identityIndicesFront[] = { 0, 2, 1, 0, 3, 2 };
61GLubyte identityIndicesBack[] = { 4, 5, 6, 4, 6, 7 };
62
54VoxelSystem::VoxelSystem(float treeScale, int maxVoxels)63VoxelSystem::VoxelSystem(float treeScale, int maxVoxels)
55 : NodeData(NULL),64 : NodeData(NULL),
56 _treeScale(treeScale),65 _treeScale(treeScale),
@@ -77,6 +86,7 @@ VoxelSystem::VoxelSystem(float treeScale, int maxVoxels) · 20 unmodified lines
77 86
78 _useVoxelShader = false;87 _useVoxelShader = false;
79 _useByteNormals = false;88 _useByteNormals = false;
89 _useGlobalNormals = false;
80 90
81 _writeVoxelShaderData = NULL;91 _writeVoxelShaderData = NULL;
82 _readVoxelShaderData = NULL;92 _readVoxelShaderData = NULL;
@@ -145,6 +155,7 @@ void VoxelSystem::setUseByteNormals(bool useByteNormals) { · 62 unmodified lines
145 pthread_mutex_lock(&_bufferWriteLock);155 pthread_mutex_lock(&_bufferWriteLock);
146 bool wasInitialized = _initialized;156 bool wasInitialized = _initialized;
147 if (wasInitialized) {157 if (wasInitialized) {
158 clearAllNodesBufferIndex();
148 cleanupVoxelMemory();159 cleanupVoxelMemory();
149 }160 }
150 _useByteNormals = useByteNormals;161 _useByteNormals = useByteNormals;
@@ -158,11 +169,29 @@ void VoxelSystem::setUseByteNormals(bool useByteNormals) { · 7 unmodified lines
158 }169 }
159}170}
160 171
172void 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}
161 189
162void VoxelSystem::setMaxVoxels(int maxVoxels) {190void VoxelSystem::setMaxVoxels(int maxVoxels) {
163 pthread_mutex_lock(&_bufferWriteLock);191 pthread_mutex_lock(&_bufferWriteLock);
164 bool wasInitialized = _initialized;192 bool wasInitialized = _initialized;
165 if (wasInitialized) {193 if (wasInitialized) {
194 clearAllNodesBufferIndex();
166 cleanupVoxelMemory();195 cleanupVoxelMemory();
167 }196 }
168 _maxVoxels = maxVoxels;197 _maxVoxels = maxVoxels;
@@ -206,9 +235,19 @@ void VoxelSystem::cleanupVoxelMemory() { · 37 unmodified lines
206 } else {235 } else {
207 // Destroy glBuffers236 // Destroy glBuffers
208 glDeleteBuffers(1, &_vboVerticesID);237 glDeleteBuffers(1, &_vboVerticesID);
209 glDeleteBuffers(1, &_vboNormalsID);
210 glDeleteBuffers(1, &_vboColorsID);238 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 }
212 251
213 delete[] _readVerticesArray;252 delete[] _readVerticesArray;
214 delete[] _writeVerticesArray;253 delete[] _writeVerticesArray;
@@ -221,6 +260,34 @@ void VoxelSystem::cleanupVoxelMemory() { · 6 unmodified lines
221 _initialized = false; // no longer initialized260 _initialized = false; // no longer initialized
222}261}
223 262
263void 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
224void VoxelSystem::initVoxelMemory() {291void VoxelSystem::initVoxelMemory() {
225 _initialMemoryUsageGPU = getFreeMemoryGPU();292 _initialMemoryUsageGPU = getFreeMemoryGPU();
226 _memoryUsageRAM = 0;293 _memoryUsageRAM = 0;
@@ -265,91 +332,104 @@ void VoxelSystem::initVoxelMemory() { · 38 unmodified lines
265 _readVoxelShaderData = new VoxelShaderVBOData[_maxVoxels];332 _readVoxelShaderData = new VoxelShaderVBOData[_maxVoxels];
266 _memoryUsageRAM += (sizeof(VoxelShaderVBOData) * _maxVoxels);333 _memoryUsageRAM += (sizeof(VoxelShaderVBOData) * _maxVoxels);
267 } else {334 } 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 }
269 357
270 // populate the indicesArray358 // VBO for the normalsArray
271 // this will not change given new voxels, so we can set it all up now359 glGenBuffers(1, &_vboNormalsID);
272 for (int n = 0; n < _maxVoxels; n++) {360 glBindBuffer(GL_ARRAY_BUFFER, _vboNormalsID);
273 // fill the indices array361 glBufferData(GL_ARRAY_BUFFER,
274 int voxelIndexOffset = n * INDICES_PER_VOXEL;362 VERTEX_POINTS_PER_VOXEL * sizeof(GLbyte) * _maxVoxels,
275 GLuint* currentIndicesPos = indicesArray + voxelIndexOffset;363 normalsArray, GL_STATIC_DRAW);
276 int startIndex = (n * VERTICES_PER_VOXEL);364 _memoryUsageVBO += VERTEX_POINTS_PER_VOXEL * sizeof(GLbyte) * _maxVoxels;
277 365
278 for (int i = 0; i < INDICES_PER_VOXEL; i++) {366 // delete the indices and normals arrays that are no longer needed
279 // add indices for this side of the cube367 delete[] normalsArray;
280 currentIndicesPos[i] = startIndex + identityIndices[i];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;
281 }390 }
282 }
283 391
284 if (_useByteNormals) {392 GLuint* indicesArray = new GLuint[INDICES_PER_VOXEL * _maxVoxels];
285 qDebug("Using Byte Normals...\n");
286 GLbyte* normalsArray = new GLbyte[VERTEX_POINTS_PER_VOXEL * _maxVoxels];
287 GLbyte* normalsArrayEndPointer = normalsArray;
288 393
289 // populate the normalsArray394 // populate the indicesArray
395 // this will not change given new voxels, so we can set it all up now
290 for (int n = 0; n < _maxVoxels; n++) {396 for (int n = 0; n < _maxVoxels; n++) {
291 for (int i = 0; i < VERTEX_POINTS_PER_VOXEL; i++) {397 // fill the indices array
292 *(normalsArrayEndPointer++) = (identityNormals[i] * CHAR_MAX);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];
293 }405 }
294 }406 }
295 407
296 // VBO for the normalsArray408 // VBO for the indicesArray
297 glGenBuffers(1, &_vboNormalsID);409 glGenBuffers(1, &_vboIndicesID);
298 glBindBuffer(GL_ARRAY_BUFFER, _vboNormalsID);410 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vboIndicesID);
299 glBufferData(GL_ARRAY_BUFFER,411 glBufferData(GL_ELEMENT_ARRAY_BUFFER,
300 VERTEX_POINTS_PER_VOXEL * sizeof(GLbyte) * _maxVoxels,412 INDICES_PER_VOXEL * sizeof(GLuint) * _maxVoxels,
301 normalsArray, GL_STATIC_DRAW);413 indicesArray, GL_STATIC_DRAW);
302 _memoryUsageVBO += VERTEX_POINTS_PER_VOXEL * sizeof(GLbyte) * _maxVoxels;414 _memoryUsageVBO += INDICES_PER_VOXEL * sizeof(GLuint) * _maxVoxels;
303 415
304 // delete the indices and normals arrays that are no longer needed416 // delete the indices and normals arrays that are no longer needed
305 delete[] normalsArray;417 delete[] indicesArray;
306 } else {
307 qDebug("Using Float Normals...\n");
308 GLfloat* normalsArray = new GLfloat[VERTEX_POINTS_PER_VOXEL * _maxVoxels];
309 GLfloat* normalsArrayEndPointer = normalsArray;
310 418
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;
328 }419 }
329 420
330 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;
331 glGenBuffers(1, &_vboVerticesID);423 glGenBuffers(1, &_vboVerticesID);
332 glBindBuffer(GL_ARRAY_BUFFER, _vboVerticesID);424 glBindBuffer(GL_ARRAY_BUFFER, _vboVerticesID);
333 glBufferData(GL_ARRAY_BUFFER, VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat) * _maxVoxels, NULL, GL_DYNAMIC_DRAW);425 glBufferData(GL_ARRAY_BUFFER, vertexPointsPerVoxel * sizeof(GLfloat) * _maxVoxels, NULL, GL_DYNAMIC_DRAW);
334 _memoryUsageVBO += VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat) * _maxVoxels;426 _memoryUsageVBO += vertexPointsPerVoxel * sizeof(GLfloat) * _maxVoxels;
335 427
336 // VBO for colorsArray428 // VBO for colorsArray
337 glGenBuffers(1, &_vboColorsID);429 glGenBuffers(1, &_vboColorsID);
338 glBindBuffer(GL_ARRAY_BUFFER, _vboColorsID);430 glBindBuffer(GL_ARRAY_BUFFER, _vboColorsID);
339 glBufferData(GL_ARRAY_BUFFER, VERTEX_POINTS_PER_VOXEL * sizeof(GLubyte) * _maxVoxels, NULL, GL_DYNAMIC_DRAW);431 glBufferData(GL_ARRAY_BUFFER, vertexPointsPerVoxel * sizeof(GLubyte) * _maxVoxels, NULL, GL_DYNAMIC_DRAW);
340 _memoryUsageVBO += VERTEX_POINTS_PER_VOXEL * sizeof(GLubyte) * _maxVoxels;432 _memoryUsageVBO += vertexPointsPerVoxel * 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
353 433
354 // we will track individual dirty sections with these arrays of bools434 // we will track individual dirty sections with these arrays of bools
355 _writeVoxelDirtyArray = new bool[_maxVoxels];435 _writeVoxelDirtyArray = new bool[_maxVoxels];
@@ -361,15 +441,15 @@ void VoxelSystem::initVoxelMemory() { · 5 unmodified lines
361 _memoryUsageRAM += (sizeof(bool) * _maxVoxels);441 _memoryUsageRAM += (sizeof(bool) * _maxVoxels);
362 442
363 // prep the data structures for incoming voxel data443 // prep the data structures for incoming voxel data
364 _writeVerticesArray = new GLfloat[VERTEX_POINTS_PER_VOXEL * _maxVoxels];444 _writeVerticesArray = new GLfloat[vertexPointsPerVoxel * _maxVoxels];
365 _memoryUsageRAM += (sizeof(GLfloat) * VERTEX_POINTS_PER_VOXEL * _maxVoxels);445 _memoryUsageRAM += (sizeof(GLfloat) * vertexPointsPerVoxel * _maxVoxels);
366 _readVerticesArray = new GLfloat[VERTEX_POINTS_PER_VOXEL * _maxVoxels];446 _readVerticesArray = new GLfloat[vertexPointsPerVoxel * _maxVoxels];
367 _memoryUsageRAM += (sizeof(GLfloat) * VERTEX_POINTS_PER_VOXEL * _maxVoxels);447 _memoryUsageRAM += (sizeof(GLfloat) * vertexPointsPerVoxel * _maxVoxels);
368 448
369 _writeColorsArray = new GLubyte[VERTEX_POINTS_PER_VOXEL * _maxVoxels];449 _writeColorsArray = new GLubyte[vertexPointsPerVoxel * _maxVoxels];
370 _memoryUsageRAM += (sizeof(GLubyte) * VERTEX_POINTS_PER_VOXEL * _maxVoxels);450 _memoryUsageRAM += (sizeof(GLubyte) * vertexPointsPerVoxel * _maxVoxels);
371 _readColorsArray = new GLubyte[VERTEX_POINTS_PER_VOXEL * _maxVoxels];451 _readColorsArray = new GLubyte[vertexPointsPerVoxel * _maxVoxels];
372 _memoryUsageRAM += (sizeof(GLubyte) * VERTEX_POINTS_PER_VOXEL * _maxVoxels);452 _memoryUsageRAM += (sizeof(GLubyte) * vertexPointsPerVoxel * _maxVoxels);
373 453
374 454
375 // create our simple fragment shader if we're the first system to init455 // 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
649 void* writeDataAt = &_writeVoxelShaderData[segmentStart];729 void* writeDataAt = &_writeVoxelShaderData[segmentStart];
650 memcpy(readDataAt, writeDataAt, segmentSizeBytes);730 memcpy(readDataAt, writeDataAt, segmentSizeBytes);
651 } else {731 } else {
652 GLintptr segmentStartAt = segmentStart * VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat);732 // Depending on if we're using per vertex normals, we will need more or less vertex points per voxel
653 GLsizeiptr segmentSizeBytes = segmentLength * VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat);733 int vertexPointsPerVoxel = _useGlobalNormals ? GLOBAL_NORMALS_VERTEX_POINTS_PER_VOXEL : VERTEX_POINTS_PER_VOXEL;
654 GLfloat* readVerticesAt = _readVerticesArray + (segmentStart * VERTEX_POINTS_PER_VOXEL);734
655 GLfloat* writeVerticesAt = _writeVerticesArray + (segmentStart * VERTEX_POINTS_PER_VOXEL);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);
656 memcpy(readVerticesAt, writeVerticesAt, segmentSizeBytes);739 memcpy(readVerticesAt, writeVerticesAt, segmentSizeBytes);
657 740
658 segmentStartAt = segmentStart * VERTEX_POINTS_PER_VOXEL * sizeof(GLubyte);741 segmentStartAt = segmentStart * vertexPointsPerVoxel * sizeof(GLubyte);
659 segmentSizeBytes = segmentLength * VERTEX_POINTS_PER_VOXEL * sizeof(GLubyte);742 segmentSizeBytes = segmentLength * vertexPointsPerVoxel * sizeof(GLubyte);
660 GLubyte* readColorsAt = _readColorsArray + (segmentStart * VERTEX_POINTS_PER_VOXEL);743 GLubyte* readColorsAt = _readColorsArray + (segmentStart * vertexPointsPerVoxel);
661 GLubyte* writeColorsAt = _writeColorsArray + (segmentStart * VERTEX_POINTS_PER_VOXEL);744 GLubyte* writeColorsAt = _writeColorsArray + (segmentStart * vertexPointsPerVoxel);
662 memcpy(readColorsAt, writeColorsAt, segmentSizeBytes);745 memcpy(readColorsAt, writeColorsAt, segmentSizeBytes);
663 }746 }
664}747}
@@ -797,10 +880,17 @@ void VoxelSystem::updateNodeInArrays(glBufferIndex nodeIndex, const glm::vec3& s · 132 unmodified lines
797 }880 }
798 } else {881 } else {
799 if (_writeVerticesArray && _writeColorsArray) {882 if (_writeVerticesArray && _writeColorsArray) {
800 for (int j = 0; j < VERTEX_POINTS_PER_VOXEL; j++ ) {883 int vertexPointsPerVoxel = _useGlobalNormals ? GLOBAL_NORMALS_VERTEX_POINTS_PER_VOXEL : VERTEX_POINTS_PER_VOXEL;
801 GLfloat* writeVerticesAt = _writeVerticesArray + (nodeIndex * VERTEX_POINTS_PER_VOXEL);884 for (int j = 0; j < vertexPointsPerVoxel; j++ ) {
802 GLubyte* writeColorsAt = _writeColorsArray + (nodeIndex * VERTEX_POINTS_PER_VOXEL);885 GLfloat* writeVerticesAt = _writeVerticesArray + (nodeIndex * vertexPointsPerVoxel);
803 *(writeVerticesAt+j) = startVertex[j % 3] + (identityVertices[j] * voxelScale);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
804 *(writeColorsAt +j) = color[j % 3];894 *(writeColorsAt +j) = color[j % 3];
805 }895 }
806 }896 }
@@ -912,15 +1002,16 @@ void VoxelSystem::updateVBOSegment(glBufferIndex segmentStart, glBufferIndex seg · 105 unmodified lines
912 glBindBuffer(GL_ARRAY_BUFFER, _vboVoxelsID);1002 glBindBuffer(GL_ARRAY_BUFFER, _vboVoxelsID);
913 glBufferSubData(GL_ARRAY_BUFFER, segmentStartAt, segmentSizeBytes, readVerticesFrom);1003 glBufferSubData(GL_ARRAY_BUFFER, segmentStartAt, segmentSizeBytes, readVerticesFrom);
914 } else {1004 } else {
1005 int vertexPointsPerVoxel = _useGlobalNormals ? GLOBAL_NORMALS_VERTEX_POINTS_PER_VOXEL : VERTEX_POINTS_PER_VOXEL;
915 int segmentLength = (segmentEnd - segmentStart) + 1;1006 int segmentLength = (segmentEnd - segmentStart) + 1;
916 GLintptr segmentStartAt = segmentStart * VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat);1007 GLintptr segmentStartAt = segmentStart * vertexPointsPerVoxel * sizeof(GLfloat);
917 GLsizeiptr segmentSizeBytes = segmentLength * VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat);1008 GLsizeiptr segmentSizeBytes = segmentLength * vertexPointsPerVoxel * sizeof(GLfloat);
918 GLfloat* readVerticesFrom = _readVerticesArray + (segmentStart * VERTEX_POINTS_PER_VOXEL);1009 GLfloat* readVerticesFrom = _readVerticesArray + (segmentStart * vertexPointsPerVoxel);
919 glBindBuffer(GL_ARRAY_BUFFER, _vboVerticesID);1010 glBindBuffer(GL_ARRAY_BUFFER, _vboVerticesID);
920 glBufferSubData(GL_ARRAY_BUFFER, segmentStartAt, segmentSizeBytes, readVerticesFrom);1011 glBufferSubData(GL_ARRAY_BUFFER, segmentStartAt, segmentSizeBytes, readVerticesFrom);
921 segmentStartAt = segmentStart * VERTEX_POINTS_PER_VOXEL * sizeof(GLubyte);1012 segmentStartAt = segmentStart * vertexPointsPerVoxel * sizeof(GLubyte);
922 segmentSizeBytes = segmentLength * VERTEX_POINTS_PER_VOXEL * sizeof(GLubyte);1013 segmentSizeBytes = segmentLength * vertexPointsPerVoxel * sizeof(GLubyte);
923 GLubyte* readColorsFrom = _readColorsArray + (segmentStart * VERTEX_POINTS_PER_VOXEL);1014 GLubyte* readColorsFrom = _readColorsArray + (segmentStart * vertexPointsPerVoxel);
924 glBindBuffer(GL_ARRAY_BUFFER, _vboColorsID);1015 glBindBuffer(GL_ARRAY_BUFFER, _vboColorsID);
925 glBufferSubData(GL_ARRAY_BUFFER, segmentStartAt, segmentSizeBytes, readColorsFrom);1016 glBufferSubData(GL_ARRAY_BUFFER, segmentStartAt, segmentSizeBytes, readColorsFrom);
926 }1017 }
@@ -966,27 +1057,66 @@ void VoxelSystem::render(bool texture) { · 39 unmodified lines
966 } else {1057 } else {
967 // tell OpenGL where to find vertex and color information1058 // tell OpenGL where to find vertex and color information
968 glEnableClientState(GL_VERTEX_ARRAY);1059 glEnableClientState(GL_VERTEX_ARRAY);
969 glEnableClientState(GL_NORMAL_ARRAY);
970 glEnableClientState(GL_COLOR_ARRAY);1060 glEnableClientState(GL_COLOR_ARRAY);
971 1061
972 glBindBuffer(GL_ARRAY_BUFFER, _vboVerticesID);1062 glBindBuffer(GL_ARRAY_BUFFER, _vboVerticesID);
973 glVertexPointer(3, GL_FLOAT, 0, 0);1063 glVertexPointer(3, GL_FLOAT, 0, 0);
974 1064
975 glBindBuffer(GL_ARRAY_BUFFER, _vboNormalsID);
976 glNormalPointer((_useByteNormals ? GL_BYTE : GL_FLOAT), 0, 0);
977
978 glBindBuffer(GL_ARRAY_BUFFER, _vboColorsID);1065 glBindBuffer(GL_ARRAY_BUFFER, _vboColorsID);
979 glColorPointer(3, GL_UNSIGNED_BYTE, 0, 0);1066 glColorPointer(3, GL_UNSIGNED_BYTE, 0, 0);
980 1067
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
981 applyScaleAndBindProgram(texture);1076 applyScaleAndBindProgram(texture);
982 1077
983 // for performance, enable backface culling1078 // for performance, enable backface culling
984 glEnable(GL_CULL_FACE);1079 glEnable(GL_CULL_FACE);
985 1080
986 // draw the number of voxels we have1081 if (!_useGlobalNormals) {
987 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vboIndicesID);1082 // draw the number of voxels we have
988 glDrawRangeElementsEXT(GL_TRIANGLES, 0, VERTICES_PER_VOXEL * _voxelsInReadArrays - 1,1083 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vboIndicesID);
989 36 * _voxelsInReadArrays, GL_UNSIGNED_INT, 0);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
990 1120
991 glDisable(GL_CULL_FACE);1121 glDisable(GL_CULL_FACE);
992 1122
@@ -994,9 +1124,12 @@ void VoxelSystem::render(bool texture) { · 1 unmodified line
994 1124
995 // deactivate vertex and color arrays after drawing1125 // deactivate vertex and color arrays after drawing
996 glDisableClientState(GL_VERTEX_ARRAY);1126 glDisableClientState(GL_VERTEX_ARRAY);
997 glDisableClientState(GL_NORMAL_ARRAY);
998 glDisableClientState(GL_COLOR_ARRAY);1127 glDisableClientState(GL_COLOR_ARRAY);
999 1128
1129 if (!_useGlobalNormals) {
1130 glDisableClientState(GL_NORMAL_ARRAY);
1131 }
1132
1000 // bind with 0 to switch back to normal operation1133 // bind with 0 to switch back to normal operation
1001 glBindBuffer(GL_ARRAY_BUFFER, 0);1134 glBindBuffer(GL_ARRAY_BUFFER, 0);
1002 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);1135 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