Showing the changes to interface/src/VoxelSystem.cpp (1 of 12 files). Show all
Unified · Split
| @@ -74,6 +74,19 @@ VoxelSystem::VoxelSystem(float treeScale, int maxVoxels) · 73 unmodified lines | |||
| 74 | 74 | ||
| 75 | connect(_tree, SIGNAL(importSize(float,float,float)), SIGNAL(importSize(float,float,float))); | 75 | connect(_tree, SIGNAL(importSize(float,float,float)), SIGNAL(importSize(float,float,float))); |
| 76 | connect(_tree, SIGNAL(importProgress(int)), SIGNAL(importProgress(int))); | 76 | connect(_tree, SIGNAL(importProgress(int)), SIGNAL(importProgress(int))); |
| 77 | |||
| 78 | _voxelShaderInitialized = false; | ||
| 79 | _initializingVoxelShader = false; | ||
| 80 | _writeVoxelShaderData = NULL; | ||
| 81 | _readVoxelShaderData = NULL; | ||
| 82 | |||
| 83 | _readVerticesArray = NULL; | ||
| 84 | _writeVerticesArray = NULL; | ||
| 85 | _readColorsArray = NULL; | ||
| 86 | _writeColorsArray = NULL; | ||
| 87 | _writeVoxelDirtyArray = NULL; | ||
| 88 | _readVoxelDirtyArray = NULL; | ||
| 89 | |||
| 77 | } | 90 | } |
| 78 | 91 | ||
| 79 | void VoxelSystem::nodeDeleted(VoxelNode* node) { | 92 | void VoxelSystem::nodeDeleted(VoxelNode* node) { |
| @@ -125,13 +138,20 @@ VoxelSystem::~VoxelSystem() { · 45 unmodified lines | |||
| 125 | glDeleteBuffers(1, &_vboNormalsID); | 138 | glDeleteBuffers(1, &_vboNormalsID); |
| 126 | glDeleteBuffers(1, &_vboColorsID); | 139 | glDeleteBuffers(1, &_vboColorsID); |
| 127 | glDeleteBuffers(1, &_vboIndicesID); | 140 | glDeleteBuffers(1, &_vboIndicesID); |
| 128 | 141 | ||
| 129 | delete[] _readVerticesArray; | 142 | delete[] _readVerticesArray; |
| 130 | delete[] _writeVerticesArray; | 143 | delete[] _writeVerticesArray; |
| 131 | delete[] _readColorsArray; | 144 | delete[] _readColorsArray; |
| 132 | delete[] _writeColorsArray; | 145 | delete[] _writeColorsArray; |
| 133 | delete[] _writeVoxelDirtyArray; | 146 | delete[] _writeVoxelDirtyArray; |
| 134 | delete[] _readVoxelDirtyArray; | 147 | delete[] _readVoxelDirtyArray; |
| 148 | |||
| 149 | // these are used when in VoxelShader mode. | ||
| 150 | glDeleteBuffers(1, &_vboVoxelsID); | ||
| 151 | glDeleteBuffers(1, &_vboVoxelsIndicesID); | ||
| 152 | |||
| 153 | delete[] _writeVoxelShaderData; | ||
| 154 | delete[] _readVoxelShaderData; | ||
| 135 | } | 155 | } |
| 136 | 156 | ||
| 137 | delete _tree; | 157 | delete _tree; |
| @@ -141,6 +161,45 @@ VoxelSystem::~VoxelSystem() { · 3 unmodified lines | |||
| 141 | VoxelNode::removeDeleteHook(this); | 161 | VoxelNode::removeDeleteHook(this); |
| 142 | } | 162 | } |
| 143 | 163 | ||
| 164 | bool VoxelSystem::getUseVoxelShader() { | ||
| 165 | bool useVoxelShader = false; //Menu::getInstance()->isOptionChecked(MenuOption::UseVoxelShader); | ||
| 166 | |||
| 167 | return useVoxelShader; | ||
| 168 | } | ||
| 169 | |||
| 170 | void VoxelSystem::initVoxelShader() { | ||
| 171 | GLuint* indicesArray = new GLuint[_maxVoxels]; | ||
| 172 | |||
| 173 | // populate the indicesArray | ||
| 174 | // this will not change given new voxels, so we can set it all up now | ||
| 175 | for (int n = 0; n < _maxVoxels; n++) { | ||
| 176 | indicesArray[n] = n; | ||
| 177 | } | ||
| 178 | |||
| 179 | // bind the indices VBO to the actual indices array | ||
| 180 | glGenBuffers(1, &_vboVoxelsIndicesID); | ||
| 181 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vboVoxelsIndicesID); | ||
| 182 | glBufferData(GL_ELEMENT_ARRAY_BUFFER, _maxVoxels, indicesArray, GL_STATIC_DRAW); | ||
| 183 | |||
| 184 | |||
| 185 | glGenBuffers(1, &_vboVoxelsID); | ||
| 186 | glBindBuffer(GL_ARRAY_BUFFER, _vboVoxelsID); | ||
| 187 | glBufferData(GL_ARRAY_BUFFER, _maxVoxels * sizeof(VoxelShaderVBOData), NULL, GL_DYNAMIC_DRAW); | ||
| 188 | |||
| 189 | // delete the indices and normals arrays that are no longer needed | ||
| 190 | delete[] indicesArray; | ||
| 191 | |||
| 192 | // we will track individual dirty sections with these arrays of bools | ||
| 193 | _writeVoxelDirtyArray = new bool[_maxVoxels]; | ||
| 194 | memset(_writeVoxelDirtyArray, false, _maxVoxels * sizeof(bool)); | ||
| 195 | _readVoxelDirtyArray = new bool[_maxVoxels]; | ||
| 196 | memset(_readVoxelDirtyArray, false, _maxVoxels * sizeof(bool)); | ||
| 197 | |||
| 198 | // prep the data structures for incoming voxel data | ||
| 199 | _writeVoxelShaderData = new VoxelShaderVBOData[_maxVoxels]; | ||
| 200 | _readVoxelShaderData = new VoxelShaderVBOData[_maxVoxels]; | ||
| 201 | } | ||
| 202 | |||
| 144 | void VoxelSystem::loadVoxelsFile(const char* fileName, bool wantColorRandomizer) { | 203 | void VoxelSystem::loadVoxelsFile(const char* fileName, bool wantColorRandomizer) { |
| 145 | _tree->loadVoxelsFile(fileName, wantColorRandomizer); | 204 | _tree->loadVoxelsFile(fileName, wantColorRandomizer); |
| 146 | setupNewVoxelsForDrawing(); | 205 | setupNewVoxelsForDrawing(); |
| @@ -398,18 +457,24 @@ void VoxelSystem::copyWrittenDataToReadArraysPartialVBOs() { · 251 unmodified lines | |||
| 398 | 457 | ||
| 399 | void VoxelSystem::copyWrittenDataSegmentToReadArrays(glBufferIndex segmentStart, glBufferIndex segmentEnd) { | 458 | void VoxelSystem::copyWrittenDataSegmentToReadArrays(glBufferIndex segmentStart, glBufferIndex segmentEnd) { |
| 400 | int segmentLength = (segmentEnd - segmentStart) + 1; | 459 | int segmentLength = (segmentEnd - segmentStart) + 1; |
| 401 | 460 | if (getUseVoxelShader()) { | |
| 402 | GLintptr segmentStartAt = segmentStart * VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat); | 461 | GLsizeiptr segmentSizeBytes = segmentLength * sizeof(VoxelShaderVBOData); |
| 403 | GLsizeiptr segmentSizeBytes = segmentLength * VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat); | 462 | void* readDataAt = &_readVoxelShaderData[segmentStart]; |
| 404 | GLfloat* readVerticesAt = _readVerticesArray + (segmentStart * VERTEX_POINTS_PER_VOXEL); | 463 | void* writeDataAt = &_writeVoxelShaderData[segmentStart]; |
| 405 | GLfloat* writeVerticesAt = _writeVerticesArray + (segmentStart * VERTEX_POINTS_PER_VOXEL); | 464 | memcpy(readDataAt, writeDataAt, segmentSizeBytes); |
| 406 | memcpy(readVerticesAt, writeVerticesAt, segmentSizeBytes); | 465 | } else { |
| 407 | 466 | GLintptr segmentStartAt = segmentStart * VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat); | |
| 408 | segmentStartAt = segmentStart * VERTEX_POINTS_PER_VOXEL * sizeof(GLubyte); | 467 | GLsizeiptr segmentSizeBytes = segmentLength * VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat); |
| 409 | segmentSizeBytes = segmentLength * VERTEX_POINTS_PER_VOXEL * sizeof(GLubyte); | 468 | GLfloat* readVerticesAt = _readVerticesArray + (segmentStart * VERTEX_POINTS_PER_VOXEL); |
| 410 | GLubyte* readColorsAt = _readColorsArray + (segmentStart * VERTEX_POINTS_PER_VOXEL); | 469 | GLfloat* writeVerticesAt = _writeVerticesArray + (segmentStart * VERTEX_POINTS_PER_VOXEL); |
| 411 | GLubyte* writeColorsAt = _writeColorsArray + (segmentStart * VERTEX_POINTS_PER_VOXEL); | 470 | memcpy(readVerticesAt, writeVerticesAt, segmentSizeBytes); |
| 412 | memcpy(readColorsAt, writeColorsAt, segmentSizeBytes); | 471 | |
| 472 | segmentStartAt = segmentStart * VERTEX_POINTS_PER_VOXEL * sizeof(GLubyte); | ||
| 473 | segmentSizeBytes = segmentLength * VERTEX_POINTS_PER_VOXEL * sizeof(GLubyte); | ||
| 474 | GLubyte* readColorsAt = _readColorsArray + (segmentStart * VERTEX_POINTS_PER_VOXEL); | ||
| 475 | GLubyte* writeColorsAt = _writeColorsArray + (segmentStart * VERTEX_POINTS_PER_VOXEL); | ||
| 476 | memcpy(readColorsAt, writeColorsAt, segmentSizeBytes); | ||
| 477 | } | ||
| 413 | } | 478 | } |
| 414 | 479 | ||
| 415 | void VoxelSystem::copyWrittenDataToReadArrays(bool fullVBOs) { | 480 | void VoxelSystem::copyWrittenDataToReadArrays(bool fullVBOs) { |
| @@ -532,11 +597,29 @@ int VoxelSystem::updateNodeInArraysAsPartialVBO(VoxelNode* node) { · 116 unmodified lines | |||
| 532 | 597 | ||
| 533 | void VoxelSystem::updateNodeInArrays(glBufferIndex nodeIndex, const glm::vec3& startVertex, | 598 | void VoxelSystem::updateNodeInArrays(glBufferIndex nodeIndex, const glm::vec3& startVertex, |
| 534 | float voxelScale, const nodeColor& color) { | 599 | float voxelScale, const nodeColor& color) { |
| 535 | for (int j = 0; j < VERTEX_POINTS_PER_VOXEL; j++ ) { | 600 | |
| 536 | GLfloat* writeVerticesAt = _writeVerticesArray + (nodeIndex * VERTEX_POINTS_PER_VOXEL); | 601 | if (getUseVoxelShader()) { |
| 537 | GLubyte* writeColorsAt = _writeColorsArray + (nodeIndex * VERTEX_POINTS_PER_VOXEL); | 602 | VoxelShaderVBOData* writeVerticesAt = &_writeVoxelShaderData[nodeIndex]; |
| 538 | *(writeVerticesAt+j) = startVertex[j % 3] + (identityVertices[j] * voxelScale); | 603 | writeVerticesAt->x = startVertex.x * TREE_SCALE; |
| 539 | *(writeColorsAt +j) = color[j % 3]; | 604 | writeVerticesAt->y = startVertex.y * TREE_SCALE; |
| 605 | writeVerticesAt->z = startVertex.z * TREE_SCALE; | ||
| 606 | writeVerticesAt->s = voxelScale * TREE_SCALE; | ||
| 607 | writeVerticesAt->r = color[RED_INDEX]; | ||
| 608 | writeVerticesAt->g = color[GREEN_INDEX]; | ||
| 609 | writeVerticesAt->b = color[BLUE_INDEX]; | ||
| 610 | |||
| 611 | /** | ||
| 612 | printf("updateNodeInArrays() nodeIndex=%lu writeVerticesAt=[%f,%f,%f,%f (%u,%u,%u)]\n", | ||
| 613 | nodeIndex, writeVerticesAt->x, writeVerticesAt->y, writeVerticesAt->z, writeVerticesAt->s, | ||
| 614 | writeVerticesAt->r, writeVerticesAt->g, writeVerticesAt->b ); | ||
| 615 | **/ | ||
| 616 | } else { | ||
| 617 | for (int j = 0; j < VERTEX_POINTS_PER_VOXEL; j++ ) { | ||
| 618 | GLfloat* writeVerticesAt = _writeVerticesArray + (nodeIndex * VERTEX_POINTS_PER_VOXEL); | ||
| 619 | GLubyte* writeColorsAt = _writeColorsArray + (nodeIndex * VERTEX_POINTS_PER_VOXEL); | ||
| 620 | *(writeVerticesAt+j) = startVertex[j % 3] + (identityVertices[j] * voxelScale); | ||
| 621 | *(writeColorsAt +j) = color[j % 3]; | ||
| 622 | } | ||
| 540 | } | 623 | } |
| 541 | } | 624 | } |
| 542 | 625 | ||
| @@ -563,85 +646,89 @@ void VoxelSystem::init() { · 20 unmodified lines | |||
| 563 | _voxelsInWriteArrays = 0; | 646 | _voxelsInWriteArrays = 0; |
| 564 | _voxelsInReadArrays = 0; | 647 | _voxelsInReadArrays = 0; |
| 565 | 648 | ||
| 566 | GLuint* indicesArray = new GLuint[INDICES_PER_VOXEL * _maxVoxels]; | 649 | // VBO for the verticesArray |
| 567 | 650 | //initVoxelShader(); | |
| 568 | // populate the indicesArray | 651 | |
| 569 | // this will not change given new voxels, so we can set it all up now | 652 | if (true) { |
| 570 | for (int n = 0; n < _maxVoxels; n++) { | 653 | GLuint* indicesArray = new GLuint[INDICES_PER_VOXEL * _maxVoxels]; |
| 571 | // fill the indices array | 654 | |
| 572 | int voxelIndexOffset = n * INDICES_PER_VOXEL; | 655 | // populate the indicesArray |
| 573 | GLuint* currentIndicesPos = indicesArray + voxelIndexOffset; | 656 | // this will not change given new voxels, so we can set it all up now |
| 574 | int startIndex = (n * VERTICES_PER_VOXEL); | 657 | for (int n = 0; n < _maxVoxels; n++) { |
| 575 | 658 | // fill the indices array | |
| 576 | for (int i = 0; i < INDICES_PER_VOXEL; i++) { | 659 | int voxelIndexOffset = n * INDICES_PER_VOXEL; |
| 577 | // add indices for this side of the cube | 660 | GLuint* currentIndicesPos = indicesArray + voxelIndexOffset; |
| 578 | currentIndicesPos[i] = startIndex + identityIndices[i]; | 661 | int startIndex = (n * VERTICES_PER_VOXEL); |
| 662 | |||
| 663 | for (int i = 0; i < INDICES_PER_VOXEL; i++) { | ||
| 664 | // add indices for this side of the cube | ||
| 665 | currentIndicesPos[i] = startIndex + identityIndices[i]; | ||
| 666 | } | ||
| 579 | } | 667 | } |
| 580 | } | ||
| 581 | 668 | ||
| 582 | GLfloat* normalsArray = new GLfloat[VERTEX_POINTS_PER_VOXEL * _maxVoxels]; | 669 | GLfloat* normalsArray = new GLfloat[VERTEX_POINTS_PER_VOXEL * _maxVoxels]; |
| 583 | GLfloat* normalsArrayEndPointer = normalsArray; | 670 | GLfloat* normalsArrayEndPointer = normalsArray; |
| 584 | 671 | ||
| 585 | // populate the normalsArray | 672 | // populate the normalsArray |
| 586 | for (int n = 0; n < _maxVoxels; n++) { | 673 | for (int n = 0; n < _maxVoxels; n++) { |
| 587 | for (int i = 0; i < VERTEX_POINTS_PER_VOXEL; i++) { | 674 | for (int i = 0; i < VERTEX_POINTS_PER_VOXEL; i++) { |
| 588 | *(normalsArrayEndPointer++) = identityNormals[i]; | 675 | *(normalsArrayEndPointer++) = identityNormals[i]; |
| 676 | } | ||
| 589 | } | 677 | } |
| 590 | } | ||
| 591 | |||
| 592 | // VBO for the verticesArray | ||
| 593 | glGenBuffers(1, &_vboVerticesID); | ||
| 594 | glBindBuffer(GL_ARRAY_BUFFER, _vboVerticesID); | ||
| 595 | glBufferData(GL_ARRAY_BUFFER, VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat) * _maxVoxels, NULL, GL_DYNAMIC_DRAW); | ||
| 596 | |||
| 597 | // VBO for the normalsArray | ||
| 598 | glGenBuffers(1, &_vboNormalsID); | ||
| 599 | glBindBuffer(GL_ARRAY_BUFFER, _vboNormalsID); | ||
| 600 | glBufferData(GL_ARRAY_BUFFER, | ||
| 601 | VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat) * _maxVoxels, | ||
| 602 | normalsArray, GL_STATIC_DRAW); | ||
| 603 | |||
| 604 | // VBO for colorsArray | ||
| 605 | glGenBuffers(1, &_vboColorsID); | ||
| 606 | glBindBuffer(GL_ARRAY_BUFFER, _vboColorsID); | ||
| 607 | glBufferData(GL_ARRAY_BUFFER, VERTEX_POINTS_PER_VOXEL * sizeof(GLubyte) * _maxVoxels, NULL, GL_DYNAMIC_DRAW); | ||
| 608 | |||
| 609 | // VBO for the indicesArray | ||
| 610 | glGenBuffers(1, &_vboIndicesID); | ||
| 611 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vboIndicesID); | ||
| 612 | glBufferData(GL_ELEMENT_ARRAY_BUFFER, | ||
| 613 | INDICES_PER_VOXEL * sizeof(GLuint) * _maxVoxels, | ||
| 614 | indicesArray, GL_STATIC_DRAW); | ||
| 615 | |||
| 616 | // delete the indices and normals arrays that are no longer needed | ||
| 617 | delete[] indicesArray; | ||
| 618 | delete[] normalsArray; | ||
| 619 | |||
| 620 | |||
| 621 | // we will track individual dirty sections with these arrays of bools | ||
| 622 | _writeVoxelDirtyArray = new bool[_maxVoxels]; | ||
| 623 | memset(_writeVoxelDirtyArray, false, _maxVoxels * sizeof(bool)); | ||
| 624 | _readVoxelDirtyArray = new bool[_maxVoxels]; | ||
| 625 | memset(_readVoxelDirtyArray, false, _maxVoxels * sizeof(bool)); | ||
| 626 | |||
| 627 | // prep the data structures for incoming voxel data | ||
| 628 | _writeVerticesArray = new GLfloat[VERTEX_POINTS_PER_VOXEL * _maxVoxels]; | ||
| 629 | _readVerticesArray = new GLfloat[VERTEX_POINTS_PER_VOXEL * _maxVoxels]; | ||
| 630 | 678 | ||
| 631 | _writeColorsArray = new GLubyte[VERTEX_POINTS_PER_VOXEL * _maxVoxels]; | 679 | glGenBuffers(1, &_vboVerticesID); |
| 632 | _readColorsArray = new GLubyte[VERTEX_POINTS_PER_VOXEL * _maxVoxels]; | 680 | glBindBuffer(GL_ARRAY_BUFFER, _vboVerticesID); |
| 633 | 681 | glBufferData(GL_ARRAY_BUFFER, VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat) * _maxVoxels, NULL, GL_DYNAMIC_DRAW); | |
| 634 | 682 | ||
| 635 | // create our simple fragment shader if we're the first system to init | 683 | // VBO for the normalsArray |
| 636 | if (!_perlinModulateProgram.isLinked()) { | 684 | glGenBuffers(1, &_vboNormalsID); |
| 637 | switchToResourcesParentIfRequired(); | 685 | glBindBuffer(GL_ARRAY_BUFFER, _vboNormalsID); |
| 638 | _perlinModulateProgram.addShaderFromSourceFile(QGLShader::Vertex, "resources/shaders/perlin_modulate.vert"); | 686 | glBufferData(GL_ARRAY_BUFFER, |
| 639 | _perlinModulateProgram.addShaderFromSourceFile(QGLShader::Fragment, "resources/shaders/perlin_modulate.frag"); | 687 | VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat) * _maxVoxels, |
| 640 | _perlinModulateProgram.link(); | 688 | normalsArray, GL_STATIC_DRAW); |
| 641 | 689 | ||
| 642 | _perlinModulateProgram.bind(); | 690 | // VBO for colorsArray |
| 643 | _perlinModulateProgram.setUniformValue("permutationNormalTexture", 0); | 691 | glGenBuffers(1, &_vboColorsID); |
| 644 | _perlinModulateProgram.release(); | 692 | glBindBuffer(GL_ARRAY_BUFFER, _vboColorsID); |
| 693 | glBufferData(GL_ARRAY_BUFFER, VERTEX_POINTS_PER_VOXEL * sizeof(GLubyte) * _maxVoxels, NULL, GL_DYNAMIC_DRAW); | ||
| 694 | |||
| 695 | // VBO for the indicesArray | ||
| 696 | glGenBuffers(1, &_vboIndicesID); | ||
| 697 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vboIndicesID); | ||
| 698 | glBufferData(GL_ELEMENT_ARRAY_BUFFER, | ||
| 699 | INDICES_PER_VOXEL * sizeof(GLuint) * _maxVoxels, | ||
| 700 | indicesArray, GL_STATIC_DRAW); | ||
| 701 | |||
| 702 | // delete the indices and normals arrays that are no longer needed | ||
| 703 | delete[] indicesArray; | ||
| 704 | delete[] normalsArray; | ||
| 705 | |||
| 706 | |||
| 707 | // we will track individual dirty sections with these arrays of bools | ||
| 708 | _writeVoxelDirtyArray = new bool[_maxVoxels]; | ||
| 709 | memset(_writeVoxelDirtyArray, false, _maxVoxels * sizeof(bool)); | ||
| 710 | _readVoxelDirtyArray = new bool[_maxVoxels]; | ||
| 711 | memset(_readVoxelDirtyArray, false, _maxVoxels * sizeof(bool)); | ||
| 712 | |||
| 713 | // prep the data structures for incoming voxel data | ||
| 714 | _writeVerticesArray = new GLfloat[VERTEX_POINTS_PER_VOXEL * _maxVoxels]; | ||
| 715 | _readVerticesArray = new GLfloat[VERTEX_POINTS_PER_VOXEL * _maxVoxels]; | ||
| 716 | |||
| 717 | _writeColorsArray = new GLubyte[VERTEX_POINTS_PER_VOXEL * _maxVoxels]; | ||
| 718 | _readColorsArray = new GLubyte[VERTEX_POINTS_PER_VOXEL * _maxVoxels]; | ||
| 719 | |||
| 720 | |||
| 721 | // create our simple fragment shader if we're the first system to init | ||
| 722 | if (!_perlinModulateProgram.isLinked()) { | ||
| 723 | switchToResourcesParentIfRequired(); | ||
| 724 | _perlinModulateProgram.addShaderFromSourceFile(QGLShader::Vertex, "resources/shaders/perlin_modulate.vert"); | ||
| 725 | _perlinModulateProgram.addShaderFromSourceFile(QGLShader::Fragment, "resources/shaders/perlin_modulate.frag"); | ||
| 726 | _perlinModulateProgram.link(); | ||
| 727 | |||
| 728 | _perlinModulateProgram.bind(); | ||
| 729 | _perlinModulateProgram.setUniformValue("permutationNormalTexture", 0); | ||
| 730 | _perlinModulateProgram.release(); | ||
| 731 | } | ||
| 645 | } | 732 | } |
| 646 | _initialized = true; | 733 | _initialized = true; |
| 647 | } | 734 | } |
| @@ -714,17 +801,34 @@ void VoxelSystem::updateVBOs() { · 66 unmodified lines | |||
| 714 | } | 801 | } |
| 715 | 802 | ||
| 716 | void VoxelSystem::updateVBOSegment(glBufferIndex segmentStart, glBufferIndex segmentEnd) { | 803 | void VoxelSystem::updateVBOSegment(glBufferIndex segmentStart, glBufferIndex segmentEnd) { |
| 717 | int segmentLength = (segmentEnd - segmentStart) + 1; | 804 | if (getUseVoxelShader()) { |
| 718 | GLintptr segmentStartAt = segmentStart * VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat); | 805 | int segmentLength = (segmentEnd - segmentStart) + 1; |
| 719 | GLsizeiptr segmentSizeBytes = segmentLength * VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat); | 806 | GLintptr segmentStartAt = segmentStart * sizeof(VoxelShaderVBOData); |
| 720 | GLfloat* readVerticesFrom = _readVerticesArray + (segmentStart * VERTEX_POINTS_PER_VOXEL); | 807 | GLsizeiptr segmentSizeBytes = segmentLength * sizeof(VoxelShaderVBOData); |
| 721 | glBindBuffer(GL_ARRAY_BUFFER, _vboVerticesID); | 808 | void* readVerticesFrom = &_readVoxelShaderData[segmentStart]; |
| 722 | glBufferSubData(GL_ARRAY_BUFFER, segmentStartAt, segmentSizeBytes, readVerticesFrom); | 809 | |
| 723 | segmentStartAt = segmentStart * VERTEX_POINTS_PER_VOXEL * sizeof(GLubyte); | 810 | glBindBuffer(GL_ARRAY_BUFFER, _vboVoxelsID); |
| 724 | segmentSizeBytes = segmentLength * VERTEX_POINTS_PER_VOXEL * sizeof(GLubyte); | 811 | glBufferSubData(GL_ARRAY_BUFFER, segmentStartAt, segmentSizeBytes, readVerticesFrom); |
| 725 | GLubyte* readColorsFrom = _readColorsArray + (segmentStart * VERTEX_POINTS_PER_VOXEL); | 812 | } else { |
| 726 | glBindBuffer(GL_ARRAY_BUFFER, _vboColorsID); | 813 | int segmentLength = (segmentEnd - segmentStart) + 1; |
| 727 | glBufferSubData(GL_ARRAY_BUFFER, segmentStartAt, segmentSizeBytes, readColorsFrom); | 814 | GLintptr segmentStartAt = segmentStart * VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat); |
| 815 | GLsizeiptr segmentSizeBytes = segmentLength * VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat); | ||
| 816 | GLfloat* readVerticesFrom = _readVerticesArray + (segmentStart * VERTEX_POINTS_PER_VOXEL); | ||
| 817 | glBindBuffer(GL_ARRAY_BUFFER, _vboVerticesID); | ||
| 818 | glBufferSubData(GL_ARRAY_BUFFER, segmentStartAt, segmentSizeBytes, readVerticesFrom); | ||
| 819 | //printf("VoxelSystem::updateVBOSegment() segmentStart=%lu, segmentEnd=%lu, segmentSizeBytes=%lu \n", | ||
| 820 | // segmentStart, segmentEnd, segmentSizeBytes); | ||
| 821 | |||
| 822 | |||
| 823 | segmentStartAt = segmentStart * VERTEX_POINTS_PER_VOXEL * sizeof(GLubyte); | ||
| 824 | segmentSizeBytes = segmentLength * VERTEX_POINTS_PER_VOXEL * sizeof(GLubyte); | ||
| 825 | GLubyte* readColorsFrom = _readColorsArray + (segmentStart * VERTEX_POINTS_PER_VOXEL); | ||
| 826 | glBindBuffer(GL_ARRAY_BUFFER, _vboColorsID); | ||
| 827 | glBufferSubData(GL_ARRAY_BUFFER, segmentStartAt, segmentSizeBytes, readColorsFrom); | ||
| 828 | |||
| 829 | //printf("VoxelSystem::updateVBOSegment() segmentStart=%lu, segmentEnd=%lu, segmentSizeBytes=%lu \n", | ||
| 830 | // segmentStart, segmentEnd, segmentSizeBytes); | ||
| 831 | } | ||
| 728 | } | 832 | } |
| 729 | 833 | ||
| 730 | void VoxelSystem::render(bool texture) { | 834 | void VoxelSystem::render(bool texture) { |
| @@ -734,43 +838,85 @@ void VoxelSystem::render(bool texture) { · 3 unmodified lines | |||
| 734 | pthread_mutex_lock(&_bufferWriteLock); | 838 | pthread_mutex_lock(&_bufferWriteLock); |
| 735 | 839 | ||
| 736 | updateVBOs(); | 840 | updateVBOs(); |
| 841 | |||
| 842 | //printf("render() _voxelsInReadArrays=%lu voxel shader=%s\n", _voxelsInReadArrays, debug::valueOf(getUseVoxelShader())); | ||
| 737 | 843 | ||
| 738 | // tell OpenGL where to find vertex and color information | 844 | if (getUseVoxelShader()) { |
| 739 | glEnableClientState(GL_VERTEX_ARRAY); | 845 | |
| 740 | glEnableClientState(GL_NORMAL_ARRAY); | 846 | Application::getInstance()->getVoxelShader().begin(); |
| 741 | glEnableClientState(GL_COLOR_ARRAY); | 847 | |
| 848 | //Define this somewhere in your header file | ||
| 849 | #define BUFFER_OFFSET(i) ((void*)(i)) | ||
| 850 | |||
| 851 | glBindBuffer(GL_ARRAY_BUFFER, _vboVoxelsID); | ||
| 852 | glEnableClientState(GL_VERTEX_ARRAY); | ||
| 853 | glVertexPointer(3, GL_FLOAT, sizeof(VoxelShaderVBOData), BUFFER_OFFSET(0)); //The starting point of the VBO, for the vertices | ||
| 854 | int loc = Application::getInstance()->getVoxelShader().attributeLocation("voxelSizeIn"); | ||
| 855 | glEnableVertexAttribArray(loc); | ||
| 856 | glVertexAttribPointer(loc, 1, GL_FLOAT, false, sizeof(VoxelShaderVBOData), BUFFER_OFFSET(3*sizeof(float))); | ||
| 857 | glEnableClientState(GL_COLOR_ARRAY); | ||
| 858 | glColorPointer(3, GL_UNSIGNED_BYTE, sizeof(VoxelShaderVBOData), BUFFER_OFFSET(4*sizeof(float)));//The starting point of colors | ||
| 859 | |||
| 860 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vboVoxelsIndicesID); | ||
| 861 | glDrawElements(GL_POINTS, _voxelsInReadArrays, GL_UNSIGNED_INT, BUFFER_OFFSET(0)); //The starting point of the IBO | ||
| 862 | |||
| 863 | /** | ||
| 864 | for (int i=0; i < _voxelsInReadArrays; i++) { | ||
| 865 | VoxelShaderVBOData* readDataAt = &_readVoxelShaderData[i]; | ||
| 866 | printf("render() _voxelsInReadArrays=%lu i=%d readDataAt=[%f,%f,%f,%f (%u,%u,%u)]\n", | ||
| 867 | _voxelsInReadArrays, i, readDataAt->x, readDataAt->y, readDataAt->z, readDataAt->s, | ||
| 868 | readDataAt->r, readDataAt->g, readDataAt->b ); | ||
| 869 | } | ||
| 870 | **/ | ||
| 871 | |||
| 872 | // deactivate vertex and color arrays after drawing | ||
| 873 | glDisableClientState(GL_VERTEX_ARRAY); | ||
| 874 | glDisableClientState(GL_COLOR_ARRAY); | ||
| 875 | |||
| 876 | // bind with 0 to switch back to normal operation | ||
| 877 | glBindBuffer(GL_ARRAY_BUFFER, 0); | ||
| 878 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); | ||
| 879 | |||
| 880 | Application::getInstance()->getVoxelShader().end(); | ||
| 881 | |||
| 882 | } else { | ||
| 883 | // tell OpenGL where to find vertex and color information | ||
| 884 | glEnableClientState(GL_VERTEX_ARRAY); | ||
| 885 | glEnableClientState(GL_NORMAL_ARRAY); | ||
| 886 | glEnableClientState(GL_COLOR_ARRAY); | ||
| 742 | 887 | ||
| 743 | glBindBuffer(GL_ARRAY_BUFFER, _vboVerticesID); | 888 | glBindBuffer(GL_ARRAY_BUFFER, _vboVerticesID); |
| 744 | glVertexPointer(3, GL_FLOAT, 0, 0); | 889 | glVertexPointer(3, GL_FLOAT, 0, 0); |
| 745 | 890 | ||
| 746 | glBindBuffer(GL_ARRAY_BUFFER, _vboNormalsID); | 891 | glBindBuffer(GL_ARRAY_BUFFER, _vboNormalsID); |
| 747 | glNormalPointer(GL_FLOAT, 0, 0); | 892 | glNormalPointer(GL_FLOAT, 0, 0); |
| 748 | 893 | ||
| 749 | glBindBuffer(GL_ARRAY_BUFFER, _vboColorsID); | 894 | glBindBuffer(GL_ARRAY_BUFFER, _vboColorsID); |
| 750 | glColorPointer(3, GL_UNSIGNED_BYTE, 0, 0); | 895 | glColorPointer(3, GL_UNSIGNED_BYTE, 0, 0); |
| 751 | 896 | ||
| 752 | applyScaleAndBindProgram(texture); | 897 | applyScaleAndBindProgram(texture); |
| 753 | 898 | ||
| 754 | // for performance, enable backface culling | 899 | // for performance, enable backface culling |
| 755 | glEnable(GL_CULL_FACE); | 900 | glEnable(GL_CULL_FACE); |
| 756 | 901 | ||
| 757 | // draw the number of voxels we have | 902 | // draw the number of voxels we have |
| 758 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vboIndicesID); | 903 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vboIndicesID); |
| 759 | glDrawRangeElementsEXT(GL_TRIANGLES, 0, VERTICES_PER_VOXEL * _voxelsInReadArrays - 1, | 904 | glDrawRangeElementsEXT(GL_TRIANGLES, 0, VERTICES_PER_VOXEL * _voxelsInReadArrays - 1, |
| 760 | 36 * _voxelsInReadArrays, GL_UNSIGNED_INT, 0); | 905 | 36 * _voxelsInReadArrays, GL_UNSIGNED_INT, 0); |
| 761 | 906 | ||
| 762 | glDisable(GL_CULL_FACE); | 907 | glDisable(GL_CULL_FACE); |
| 763 | 908 | ||
| 764 | removeScaleAndReleaseProgram(texture); | 909 | removeScaleAndReleaseProgram(texture); |
| 765 | 910 | ||
| 766 | // deactivate vertex and color arrays after drawing | 911 | // deactivate vertex and color arrays after drawing |
| 767 | glDisableClientState(GL_VERTEX_ARRAY); | 912 | glDisableClientState(GL_VERTEX_ARRAY); |
| 768 | glDisableClientState(GL_NORMAL_ARRAY); | 913 | glDisableClientState(GL_NORMAL_ARRAY); |
| 769 | glDisableClientState(GL_COLOR_ARRAY); | 914 | glDisableClientState(GL_COLOR_ARRAY); |
| 770 | 915 | ||
| 771 | // bind with 0 to switch back to normal operation | 916 | // bind with 0 to switch back to normal operation |
| 772 | glBindBuffer(GL_ARRAY_BUFFER, 0); | 917 | glBindBuffer(GL_ARRAY_BUFFER, 0); |
| 773 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); | 918 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); |
| 919 | } | ||
| 774 | 920 | ||
| 775 | pthread_mutex_unlock(&_bufferWriteLock); | 921 | pthread_mutex_unlock(&_bufferWriteLock); |
| 776 | } | 922 | } |