History

Working on avatar voxel system.

Andrzej Kapolka committed 13 years ago

Commit 7425b391 parent 92979756 files · +216 −105Browse files

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

Unified · Split

interface/src/VoxelSystem.cpp+51−83
@@ -231,10 +231,8 @@ void VoxelSystem::cleanupRemovedVoxels() { · 230 unmodified lines
231}231}
232 232
233void VoxelSystem::copyWrittenDataToReadArraysFullVBOs() {233void VoxelSystem::copyWrittenDataToReadArraysFullVBOs() {
234 int bytesOfVertices = (_voxelsInWriteArrays * VERTEX_POINTS_PER_VOXEL) * sizeof(GLfloat);234 copyWrittenDataSegmentToReadArrays(0, _voxelsInWriteArrays - 1);
235 int bytesOfColors = (_voxelsInWriteArrays * VERTEX_POINTS_PER_VOXEL) * sizeof(GLubyte);235
236 memcpy(_readVerticesArray, _writeVerticesArray, bytesOfVertices);
237 memcpy(_readColorsArray, _writeColorsArray, bytesOfColors );
238 _voxelsInReadArrays = _voxelsInWriteArrays;236 _voxelsInReadArrays = _voxelsInWriteArrays;
239 237
240 // clear our dirty flags238 // clear our dirty flags
@@ -261,47 +259,37 @@ void VoxelSystem::copyWrittenDataToReadArraysPartialVBOs() { · 20 unmodified lines
261 if (!thisVoxelDirty) {259 if (!thisVoxelDirty) {
262 // If we got here because because this voxel is NOT dirty, so the last dirty voxel was the one before260 // If we got here because because this voxel is NOT dirty, so the last dirty voxel was the one before
263 // this one and so that's where the "segment" ends261 // this one and so that's where the "segment" ends
264 segmentEnd = i - 1; 262 copyWrittenDataSegmentToReadArrays(segmentStart, i - 1);
265 inSegment = false;263 inSegment = false;
266 int segmentLength = (segmentEnd - segmentStart) + 1;
267
268 GLintptr segmentStartAt = segmentStart * VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat);
269 GLsizeiptr segmentSizeBytes = segmentLength * VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat);
270 GLfloat* readVerticesAt = _readVerticesArray + (segmentStart * VERTEX_POINTS_PER_VOXEL);
271 GLfloat* writeVerticesAt = _writeVerticesArray + (segmentStart * VERTEX_POINTS_PER_VOXEL);
272 memcpy(readVerticesAt, writeVerticesAt, segmentSizeBytes);
273
274 segmentStartAt = segmentStart * VERTEX_POINTS_PER_VOXEL * sizeof(GLubyte);
275 segmentSizeBytes = segmentLength * VERTEX_POINTS_PER_VOXEL * sizeof(GLubyte);
276 GLubyte* readColorsAt = _readColorsArray + (segmentStart * VERTEX_POINTS_PER_VOXEL);
277 GLubyte* writeColorsAt = _writeColorsArray + (segmentStart * VERTEX_POINTS_PER_VOXEL);
278 memcpy(readColorsAt, writeColorsAt, segmentSizeBytes);
279 }264 }
280 }265 }
281 }266 }
282 267
283 // if we got to the end of the array, and we're in an active dirty segment...268 // if we got to the end of the array, and we're in an active dirty segment...
284 if (inSegment) {269 if (inSegment) {
285 segmentEnd = _voxelsInWriteArrays - 1; 270 copyWrittenDataSegmentToReadArrays(segmentStart, _voxelsInWriteArrays - 1);
286 int segmentLength = (segmentEnd - segmentStart) + 1;
287
288 GLintptr segmentStartAt = segmentStart * VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat);
289 GLsizeiptr segmentSizeBytes = segmentLength * VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat);
290 GLfloat* readVerticesAt = _readVerticesArray + (segmentStart * VERTEX_POINTS_PER_VOXEL);
291 GLfloat* writeVerticesAt = _writeVerticesArray + (segmentStart * VERTEX_POINTS_PER_VOXEL);
292 memcpy(readVerticesAt, writeVerticesAt, segmentSizeBytes);
293
294 segmentStartAt = segmentStart * VERTEX_POINTS_PER_VOXEL * sizeof(GLubyte);
295 segmentSizeBytes = segmentLength * VERTEX_POINTS_PER_VOXEL * sizeof(GLubyte);
296 GLubyte* readColorsAt = _readColorsArray + (segmentStart * VERTEX_POINTS_PER_VOXEL);
297 GLubyte* writeColorsAt = _writeColorsArray + (segmentStart * VERTEX_POINTS_PER_VOXEL);
298 memcpy(readColorsAt, writeColorsAt, segmentSizeBytes);
299 }271 }
300 272
301 // update our length273 // update our length
302 _voxelsInReadArrays = _voxelsInWriteArrays;274 _voxelsInReadArrays = _voxelsInWriteArrays;
303}275}
304 276
277void VoxelSystem::copyWrittenDataSegmentToReadArrays(glBufferIndex segmentStart, glBufferIndex segmentEnd) {
278 int segmentLength = (segmentEnd - segmentStart) + 1;
279
280 GLintptr segmentStartAt = segmentStart * VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat);
281 GLsizeiptr segmentSizeBytes = segmentLength * VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat);
282 GLfloat* readVerticesAt = _readVerticesArray + (segmentStart * VERTEX_POINTS_PER_VOXEL);
283 GLfloat* writeVerticesAt = _writeVerticesArray + (segmentStart * VERTEX_POINTS_PER_VOXEL);
284 memcpy(readVerticesAt, writeVerticesAt, segmentSizeBytes);
285
286 segmentStartAt = segmentStart * VERTEX_POINTS_PER_VOXEL * sizeof(GLubyte);
287 segmentSizeBytes = segmentLength * VERTEX_POINTS_PER_VOXEL * sizeof(GLubyte);
288 GLubyte* readColorsAt = _readColorsArray + (segmentStart * VERTEX_POINTS_PER_VOXEL);
289 GLubyte* writeColorsAt = _writeColorsArray + (segmentStart * VERTEX_POINTS_PER_VOXEL);
290 memcpy(readColorsAt, writeColorsAt, segmentSizeBytes);
291}
292
305void VoxelSystem::copyWrittenDataToReadArrays(bool fullVBOs) {293void VoxelSystem::copyWrittenDataToReadArrays(bool fullVBOs) {
306 PerformanceWarning warn(_renderWarningsOn, "copyWrittenDataToReadArrays()");294 PerformanceWarning warn(_renderWarningsOn, "copyWrittenDataToReadArrays()");
307 if (_voxelsDirty && _voxelsUpdated) {295 if (_voxelsDirty && _voxelsUpdated) {
@@ -364,12 +352,7 @@ int VoxelSystem::updateNodeInArraysAsFullVBO(VoxelNode* node) { · 56 unmodified lines
364 352
365 // populate the array with points for the 8 vertices353 // populate the array with points for the 8 vertices
366 // and RGB color for each added vertex354 // and RGB color for each added vertex
367 for (int j = 0; j < VERTEX_POINTS_PER_VOXEL; j++ ) {355 updateNodeInArrays(nodeIndex, startVertex, voxelScale, node->getColor());
368 GLfloat* writeVerticesAt = _writeVerticesArray + (nodeIndex * VERTEX_POINTS_PER_VOXEL);
369 GLubyte* writeColorsAt = _writeColorsArray + (nodeIndex * VERTEX_POINTS_PER_VOXEL);
370 *(writeVerticesAt+j) = startVertex[j % 3] + (identityVertices[j] * voxelScale);
371 *(writeColorsAt +j) = node->getColor()[j % 3];
372 }
373 node->setBufferIndex(nodeIndex);356 node->setBufferIndex(nodeIndex);
374 _writeVoxelDirtyArray[nodeIndex] = true; // just in case we switch to Partial mode357 _writeVoxelDirtyArray[nodeIndex] = true; // just in case we switch to Partial mode
375 _voxelsInWriteArrays++; // our know vertices in the arrays358 _voxelsInWriteArrays++; // our know vertices in the arrays
@@ -415,17 +398,23 @@ int VoxelSystem::updateNodeInArraysAsPartialVBO(VoxelNode* node) { · 39 unmodified lines
415 398
416 // populate the array with points for the 8 vertices399 // populate the array with points for the 8 vertices
417 // and RGB color for each added vertex400 // and RGB color for each added vertex
418 for (int j = 0; j < VERTEX_POINTS_PER_VOXEL; j++ ) {401 updateNodeInArrays(nodeIndex, startVertex, voxelScale, node->getColor());
419 GLfloat* writeVerticesAt = _writeVerticesArray + (nodeIndex * VERTEX_POINTS_PER_VOXEL);402
420 GLubyte* writeColorsAt = _writeColorsArray + (nodeIndex * VERTEX_POINTS_PER_VOXEL);
421 *(writeVerticesAt+j) = startVertex[j % 3] + (identityVertices[j] * voxelScale);
422 *(writeColorsAt +j) = node->getColor()[j % 3];
423 }
424 return 1; // updated!403 return 1; // updated!
425 }404 }
426 return 0; // not-updated405 return 0; // not-updated
427}406}
428 407
408void VoxelSystem::updateNodeInArrays(glBufferIndex nodeIndex, const glm::vec3& startVertex,
409 float voxelScale, const nodeColor& color) {
410 for (int j = 0; j < VERTEX_POINTS_PER_VOXEL; j++ ) {
411 GLfloat* writeVerticesAt = _writeVerticesArray + (nodeIndex * VERTEX_POINTS_PER_VOXEL);
412 GLubyte* writeColorsAt = _writeColorsArray + (nodeIndex * VERTEX_POINTS_PER_VOXEL);
413 *(writeVerticesAt+j) = startVertex[j % 3] + (identityVertices[j] * voxelScale);
414 *(writeColorsAt +j) = color[j % 3];
415 }
416}
417
429ProgramObject* VoxelSystem::_perlinModulateProgram = 0;418ProgramObject* VoxelSystem::_perlinModulateProgram = 0;
430GLuint VoxelSystem::_permutationNormalTextureID = 0;419GLuint VoxelSystem::_permutationNormalTextureID = 0;
431 420
@@ -546,20 +535,7 @@ void VoxelSystem::init() { · 114 unmodified lines
546}535}
547 536
548void VoxelSystem::updateFullVBOs() {537void VoxelSystem::updateFullVBOs() {
549 glBufferIndex segmentStart = 0;538 updateVBOSegment(0, _voxelsInReadArrays);
550 glBufferIndex segmentEnd = _voxelsInReadArrays;
551
552 int segmentLength = (segmentEnd - segmentStart) + 1;
553 GLintptr segmentStartAt = segmentStart * VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat);
554 GLsizeiptr segmentSizeBytes = segmentLength * VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat);
555 GLfloat* readVerticesFrom = _readVerticesArray + (segmentStart * VERTEX_POINTS_PER_VOXEL);
556 glBindBuffer(GL_ARRAY_BUFFER, _vboVerticesID);
557 glBufferSubData(GL_ARRAY_BUFFER, segmentStartAt, segmentSizeBytes, readVerticesFrom);
558 segmentStartAt = segmentStart * VERTEX_POINTS_PER_VOXEL * sizeof(GLubyte);
559 segmentSizeBytes = segmentLength * VERTEX_POINTS_PER_VOXEL * sizeof(GLubyte);
560 GLubyte* readColorsFrom = _readColorsArray + (segmentStart * VERTEX_POINTS_PER_VOXEL);
561 glBindBuffer(GL_ARRAY_BUFFER, _vboColorsID);
562 glBufferSubData(GL_ARRAY_BUFFER, segmentStartAt, segmentSizeBytes, readColorsFrom);
563 539
564 // consider the _readVoxelDirtyArray[] clean!540 // consider the _readVoxelDirtyArray[] clean!
565 memset(_readVoxelDirtyArray, false, _voxelsInReadArrays * sizeof(bool));541 memset(_readVoxelDirtyArray, false, _voxelsInReadArrays * sizeof(bool));
@@ -581,39 +557,17 @@ void VoxelSystem::updatePartialVBOs() { · 15 unmodified lines
581 if (!thisVoxelDirty) {557 if (!thisVoxelDirty) {
582 // If we got here because because this voxel is NOT dirty, so the last dirty voxel was the one before558 // If we got here because because this voxel is NOT dirty, so the last dirty voxel was the one before
583 // this one and so that's where the "segment" ends559 // this one and so that's where the "segment" ends
584 segmentEnd = i - 1; 560 updateVBOSegment(segmentStart, i - 1);
585 inSegment = false;561 inSegment = false;
586 int segmentLength = (segmentEnd - segmentStart) + 1;
587 GLintptr segmentStartAt = segmentStart * VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat);
588 GLsizeiptr segmentSizeBytes = segmentLength * VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat);
589 GLfloat* readVerticesFrom = _readVerticesArray + (segmentStart * VERTEX_POINTS_PER_VOXEL);
590 glBindBuffer(GL_ARRAY_BUFFER, _vboVerticesID);
591 glBufferSubData(GL_ARRAY_BUFFER, segmentStartAt, segmentSizeBytes, readVerticesFrom);
592 segmentStartAt = segmentStart * VERTEX_POINTS_PER_VOXEL * sizeof(GLubyte);
593 segmentSizeBytes = segmentLength * VERTEX_POINTS_PER_VOXEL * sizeof(GLubyte);
594 GLubyte* readColorsFrom = _readColorsArray + (segmentStart * VERTEX_POINTS_PER_VOXEL);
595 glBindBuffer(GL_ARRAY_BUFFER, _vboColorsID);
596 glBufferSubData(GL_ARRAY_BUFFER, segmentStartAt, segmentSizeBytes, readColorsFrom);
597 }562 }
598 _readVoxelDirtyArray[i] = false; // consider us clean!563 _readVoxelDirtyArray[i] = false; // consider us clean!
599 }564 }
600 }565 }
601 566
602 // if we got to the end of the array, and we're in an active dirty segment...567 // if we got to the end of the array, and we're in an active dirty segment...
603 if (inSegment) {568 if (inSegment) {
604 segmentEnd = _voxelsInReadArrays - 1; 569 updateVBOSegment(segmentStart, _voxelsInReadArrays - 1);
605 inSegment = false;570 inSegment = false;
606 int segmentLength = (segmentEnd - segmentStart) + 1;
607 GLintptr segmentStartAt = segmentStart * VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat);
608 GLsizeiptr segmentSizeBytes = segmentLength * VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat);
609 GLfloat* readVerticesFrom = _readVerticesArray + (segmentStart * VERTEX_POINTS_PER_VOXEL);
610 glBindBuffer(GL_ARRAY_BUFFER, _vboVerticesID);
611 glBufferSubData(GL_ARRAY_BUFFER, segmentStartAt, segmentSizeBytes, readVerticesFrom);
612 segmentStartAt = segmentStart * VERTEX_POINTS_PER_VOXEL * sizeof(GLubyte);
613 segmentSizeBytes = segmentLength * VERTEX_POINTS_PER_VOXEL * sizeof(GLubyte);
614 GLubyte* readColorsFrom = _readColorsArray + (segmentStart * VERTEX_POINTS_PER_VOXEL);
615 glBindBuffer(GL_ARRAY_BUFFER, _vboColorsID);
616 glBufferSubData(GL_ARRAY_BUFFER, segmentStartAt, segmentSizeBytes, readColorsFrom);
617 }571 }
618}572}
619 573
@@ -635,6 +589,20 @@ void VoxelSystem::updateVBOs() { · 15 unmodified lines
635 _callsToTreesToArrays = 0; // clear it589 _callsToTreesToArrays = 0; // clear it
636}590}
637 591
592void VoxelSystem::updateVBOSegment(glBufferIndex segmentStart, glBufferIndex segmentEnd) {
593 int segmentLength = (segmentEnd - segmentStart) + 1;
594 GLintptr segmentStartAt = segmentStart * VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat);
595 GLsizeiptr segmentSizeBytes = segmentLength * VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat);
596 GLfloat* readVerticesFrom = _readVerticesArray + (segmentStart * VERTEX_POINTS_PER_VOXEL);
597 glBindBuffer(GL_ARRAY_BUFFER, _vboVerticesID);
598 glBufferSubData(GL_ARRAY_BUFFER, segmentStartAt, segmentSizeBytes, readVerticesFrom);
599 segmentStartAt = segmentStart * VERTEX_POINTS_PER_VOXEL * sizeof(GLubyte);
600 segmentSizeBytes = segmentLength * VERTEX_POINTS_PER_VOXEL * sizeof(GLubyte);
601 GLubyte* readColorsFrom = _readColorsArray + (segmentStart * VERTEX_POINTS_PER_VOXEL);
602 glBindBuffer(GL_ARRAY_BUFFER, _vboColorsID);
603 glBufferSubData(GL_ARRAY_BUFFER, segmentStartAt, segmentSizeBytes, readColorsFrom);
604}
605
638void VoxelSystem::render(bool texture) {606void VoxelSystem::render(bool texture) {
639 PerformanceWarning warn(_renderWarningsOn, "render()");607 PerformanceWarning warn(_renderWarningsOn, "render()");
640 608

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