Showing the changes to interface/src/VoxelSystem.cpp (1 of 6 files). Show all
Unified · Split
| @@ -231,10 +231,8 @@ void VoxelSystem::cleanupRemovedVoxels() { · 230 unmodified lines | ||
| 231 | 231 | } |
| 232 | 232 | |
| 233 | 233 | void VoxelSystem::copyWrittenDataToReadArraysFullVBOs() { |
| 234 | − int bytesOfVertices = (_voxelsInWriteArrays * VERTEX_POINTS_PER_VOXEL) * sizeof(GLfloat); | |
| 235 | − int bytesOfColors = (_voxelsInWriteArrays * VERTEX_POINTS_PER_VOXEL) * sizeof(GLubyte); | |
| 236 | − memcpy(_readVerticesArray, _writeVerticesArray, bytesOfVertices); | |
| 237 | − memcpy(_readColorsArray, _writeColorsArray, bytesOfColors ); | |
| 234 | + copyWrittenDataSegmentToReadArrays(0, _voxelsInWriteArrays - 1); | |
| 235 | + | |
| 238 | 236 | _voxelsInReadArrays = _voxelsInWriteArrays; |
| 239 | 237 | |
| 240 | 238 | // clear our dirty flags |
| @@ -261,47 +259,37 @@ void VoxelSystem::copyWrittenDataToReadArraysPartialVBOs() { · 20 unmodified lines | ||
| 261 | 259 | if (!thisVoxelDirty) { |
| 262 | 260 | // If we got here because because this voxel is NOT dirty, so the last dirty voxel was the one before |
| 263 | 261 | // this one and so that's where the "segment" ends |
| 264 | − segmentEnd = i - 1; | |
| 262 | + copyWrittenDataSegmentToReadArrays(segmentStart, i - 1); | |
| 265 | 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 | 268 | // if we got to the end of the array, and we're in an active dirty segment... |
| 284 | 269 | if (inSegment) { |
| 285 | − segmentEnd = _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); | |
| 270 | + copyWrittenDataSegmentToReadArrays(segmentStart, _voxelsInWriteArrays - 1); | |
| 299 | 271 | } |
| 300 | 272 | |
| 301 | 273 | // update our length |
| 302 | 274 | _voxelsInReadArrays = _voxelsInWriteArrays; |
| 303 | 275 | } |
| 304 | 276 | |
| 277 | +void 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 | + | |
| 305 | 293 | void VoxelSystem::copyWrittenDataToReadArrays(bool fullVBOs) { |
| 306 | 294 | PerformanceWarning warn(_renderWarningsOn, "copyWrittenDataToReadArrays()"); |
| 307 | 295 | if (_voxelsDirty && _voxelsUpdated) { |
| @@ -364,12 +352,7 @@ int VoxelSystem::updateNodeInArraysAsFullVBO(VoxelNode* node) { · 56 unmodified lines | ||
| 364 | 352 | |
| 365 | 353 | // populate the array with points for the 8 vertices |
| 366 | 354 | // and RGB color for each added vertex |
| 367 | − for (int j = 0; j < VERTEX_POINTS_PER_VOXEL; j++ ) { | |
| 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 | − } | |
| 355 | + updateNodeInArrays(nodeIndex, startVertex, voxelScale, node->getColor()); | |
| 373 | 356 | node->setBufferIndex(nodeIndex); |
| 374 | 357 | _writeVoxelDirtyArray[nodeIndex] = true; // just in case we switch to Partial mode |
| 375 | 358 | _voxelsInWriteArrays++; // our know vertices in the arrays |
| @@ -415,17 +398,23 @@ int VoxelSystem::updateNodeInArraysAsPartialVBO(VoxelNode* node) { · 39 unmodified lines | ||
| 415 | 398 | |
| 416 | 399 | // populate the array with points for the 8 vertices |
| 417 | 400 | // and RGB color for each added vertex |
| 418 | − for (int j = 0; j < VERTEX_POINTS_PER_VOXEL; j++ ) { | |
| 419 | − GLfloat* writeVerticesAt = _writeVerticesArray + (nodeIndex * VERTEX_POINTS_PER_VOXEL); | |
| 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 | − } | |
| 401 | + updateNodeInArrays(nodeIndex, startVertex, voxelScale, node->getColor()); | |
| 402 | + | |
| 424 | 403 | return 1; // updated! |
| 425 | 404 | } |
| 426 | 405 | return 0; // not-updated |
| 427 | 406 | } |
| 428 | 407 | |
| 408 | +void 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 | + | |
| 429 | 418 | ProgramObject* VoxelSystem::_perlinModulateProgram = 0; |
| 430 | 419 | GLuint VoxelSystem::_permutationNormalTextureID = 0; |
| 431 | 420 | |
| @@ -546,20 +535,7 @@ void VoxelSystem::init() { · 114 unmodified lines | ||
| 546 | 535 | } |
| 547 | 536 | |
| 548 | 537 | void VoxelSystem::updateFullVBOs() { |
| 549 | − glBufferIndex segmentStart = 0; | |
| 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); | |
| 538 | + updateVBOSegment(0, _voxelsInReadArrays); | |
| 563 | 539 | |
| 564 | 540 | // consider the _readVoxelDirtyArray[] clean! |
| 565 | 541 | memset(_readVoxelDirtyArray, false, _voxelsInReadArrays * sizeof(bool)); |
| @@ -581,39 +557,17 @@ void VoxelSystem::updatePartialVBOs() { · 15 unmodified lines | ||
| 581 | 557 | if (!thisVoxelDirty) { |
| 582 | 558 | // If we got here because because this voxel is NOT dirty, so the last dirty voxel was the one before |
| 583 | 559 | // this one and so that's where the "segment" ends |
| 584 | − segmentEnd = i - 1; | |
| 560 | + updateVBOSegment(segmentStart, i - 1); | |
| 585 | 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 | 563 | _readVoxelDirtyArray[i] = false; // consider us clean! |
| 599 | 564 | } |
| 600 | 565 | } |
| 601 | 566 | |
| 602 | 567 | // if we got to the end of the array, and we're in an active dirty segment... |
| 603 | − if (inSegment) { | |
| 604 | − segmentEnd = _voxelsInReadArrays - 1; | |
| 568 | + if (inSegment) { | |
| 569 | + updateVBOSegment(segmentStart, _voxelsInReadArrays - 1); | |
| 605 | 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 | 589 | _callsToTreesToArrays = 0; // clear it |
| 636 | 590 | } |
| 637 | 591 | |
| 592 | +void 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 | + | |
| 638 | 606 | void VoxelSystem::render(bool texture) { |
| 639 | 607 | PerformanceWarning warn(_renderWarningsOn, "render()"); |
| 640 | 608 | |