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 | 75 | connect(_tree, SIGNAL(importSize(float,float,float)), SIGNAL(importSize(float,float,float))); |
| 76 | 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 | 92 | void VoxelSystem::nodeDeleted(VoxelNode* node) { |
| @@ -125,13 +138,20 @@ VoxelSystem::~VoxelSystem() { · 45 unmodified lines | ||
| 125 | 138 | glDeleteBuffers(1, &_vboNormalsID); |
| 126 | 139 | glDeleteBuffers(1, &_vboColorsID); |
| 127 | 140 | glDeleteBuffers(1, &_vboIndicesID); |
| 128 | − | |
| 141 | + | |
| 129 | 142 | delete[] _readVerticesArray; |
| 130 | 143 | delete[] _writeVerticesArray; |
| 131 | 144 | delete[] _readColorsArray; |
| 132 | 145 | delete[] _writeColorsArray; |
| 133 | 146 | delete[] _writeVoxelDirtyArray; |
| 134 | 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 | 157 | delete _tree; |
| @@ -141,6 +161,45 @@ VoxelSystem::~VoxelSystem() { · 3 unmodified lines | ||
| 141 | 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 | 203 | void VoxelSystem::loadVoxelsFile(const char* fileName, bool wantColorRandomizer) { |
| 145 | 204 | _tree->loadVoxelsFile(fileName, wantColorRandomizer); |
| 146 | 205 | setupNewVoxelsForDrawing(); |
| @@ -398,18 +457,24 @@ void VoxelSystem::copyWrittenDataToReadArraysPartialVBOs() { · 251 unmodified lines | ||
| 398 | 457 | |
| 399 | 458 | void VoxelSystem::copyWrittenDataSegmentToReadArrays(glBufferIndex segmentStart, glBufferIndex segmentEnd) { |
| 400 | 459 | int segmentLength = (segmentEnd - segmentStart) + 1; |
| 401 | − | |
| 402 | − GLintptr segmentStartAt = segmentStart * VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat); | |
| 403 | − GLsizeiptr segmentSizeBytes = segmentLength * VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat); | |
| 404 | − GLfloat* readVerticesAt = _readVerticesArray + (segmentStart * VERTEX_POINTS_PER_VOXEL); | |
| 405 | − GLfloat* writeVerticesAt = _writeVerticesArray + (segmentStart * VERTEX_POINTS_PER_VOXEL); | |
| 406 | − memcpy(readVerticesAt, writeVerticesAt, segmentSizeBytes); | |
| 407 | − | |
| 408 | − segmentStartAt = segmentStart * VERTEX_POINTS_PER_VOXEL * sizeof(GLubyte); | |
| 409 | − segmentSizeBytes = segmentLength * VERTEX_POINTS_PER_VOXEL * sizeof(GLubyte); | |
| 410 | − GLubyte* readColorsAt = _readColorsArray + (segmentStart * VERTEX_POINTS_PER_VOXEL); | |
| 411 | − GLubyte* writeColorsAt = _writeColorsArray + (segmentStart * VERTEX_POINTS_PER_VOXEL); | |
| 412 | − memcpy(readColorsAt, writeColorsAt, segmentSizeBytes); | |
| 460 | + if (getUseVoxelShader()) { | |
| 461 | + GLsizeiptr segmentSizeBytes = segmentLength * sizeof(VoxelShaderVBOData); | |
| 462 | + void* readDataAt = &_readVoxelShaderData[segmentStart]; | |
| 463 | + void* writeDataAt = &_writeVoxelShaderData[segmentStart]; | |
| 464 | + memcpy(readDataAt, writeDataAt, segmentSizeBytes); | |
| 465 | + } else { | |
| 466 | + GLintptr segmentStartAt = segmentStart * VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat); | |
| 467 | + GLsizeiptr segmentSizeBytes = segmentLength * VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat); | |
| 468 | + GLfloat* readVerticesAt = _readVerticesArray + (segmentStart * VERTEX_POINTS_PER_VOXEL); | |
| 469 | + GLfloat* writeVerticesAt = _writeVerticesArray + (segmentStart * VERTEX_POINTS_PER_VOXEL); | |
| 470 | + memcpy(readVerticesAt, writeVerticesAt, 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 | 480 | void VoxelSystem::copyWrittenDataToReadArrays(bool fullVBOs) { |
| @@ -532,11 +597,29 @@ int VoxelSystem::updateNodeInArraysAsPartialVBO(VoxelNode* node) { · 116 unmodified lines | ||
| 532 | 597 | |
| 533 | 598 | void VoxelSystem::updateNodeInArrays(glBufferIndex nodeIndex, const glm::vec3& startVertex, |
| 534 | 599 | float voxelScale, const nodeColor& color) { |
| 535 | − for (int j = 0; j < VERTEX_POINTS_PER_VOXEL; j++ ) { | |
| 536 | − GLfloat* writeVerticesAt = _writeVerticesArray + (nodeIndex * VERTEX_POINTS_PER_VOXEL); | |
| 537 | − GLubyte* writeColorsAt = _writeColorsArray + (nodeIndex * VERTEX_POINTS_PER_VOXEL); | |
| 538 | − *(writeVerticesAt+j) = startVertex[j % 3] + (identityVertices[j] * voxelScale); | |
| 539 | − *(writeColorsAt +j) = color[j % 3]; | |
| 600 | + | |
| 601 | + if (getUseVoxelShader()) { | |
| 602 | + VoxelShaderVBOData* writeVerticesAt = &_writeVoxelShaderData[nodeIndex]; | |
| 603 | + writeVerticesAt->x = startVertex.x * TREE_SCALE; | |
| 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 | 646 | _voxelsInWriteArrays = 0; |
| 564 | 647 | _voxelsInReadArrays = 0; |
| 565 | 648 | |
| 566 | − GLuint* indicesArray = new GLuint[INDICES_PER_VOXEL * _maxVoxels]; | |
| 567 | − | |
| 568 | − // populate the indicesArray | |
| 569 | − // this will not change given new voxels, so we can set it all up now | |
| 570 | − for (int n = 0; n < _maxVoxels; n++) { | |
| 571 | − // fill the indices array | |
| 572 | − int voxelIndexOffset = n * INDICES_PER_VOXEL; | |
| 573 | − GLuint* currentIndicesPos = indicesArray + voxelIndexOffset; | |
| 574 | − int startIndex = (n * VERTICES_PER_VOXEL); | |
| 575 | − | |
| 576 | − for (int i = 0; i < INDICES_PER_VOXEL; i++) { | |
| 577 | − // add indices for this side of the cube | |
| 578 | − currentIndicesPos[i] = startIndex + identityIndices[i]; | |
| 649 | + // VBO for the verticesArray | |
| 650 | + //initVoxelShader(); | |
| 651 | + | |
| 652 | + if (true) { | |
| 653 | + GLuint* indicesArray = new GLuint[INDICES_PER_VOXEL * _maxVoxels]; | |
| 654 | + | |
| 655 | + // populate the indicesArray | |
| 656 | + // this will not change given new voxels, so we can set it all up now | |
| 657 | + for (int n = 0; n < _maxVoxels; n++) { | |
| 658 | + // fill the indices array | |
| 659 | + int voxelIndexOffset = n * INDICES_PER_VOXEL; | |
| 660 | + GLuint* currentIndicesPos = indicesArray + voxelIndexOffset; | |
| 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]; | |
| 583 | − GLfloat* normalsArrayEndPointer = normalsArray; | |
| 669 | + GLfloat* normalsArray = new GLfloat[VERTEX_POINTS_PER_VOXEL * _maxVoxels]; | |
| 670 | + GLfloat* normalsArrayEndPointer = normalsArray; | |
| 584 | 671 | |
| 585 | − // populate the normalsArray | |
| 586 | − for (int n = 0; n < _maxVoxels; n++) { | |
| 587 | − for (int i = 0; i < VERTEX_POINTS_PER_VOXEL; i++) { | |
| 588 | − *(normalsArrayEndPointer++) = identityNormals[i]; | |
| 672 | + // populate the normalsArray | |
| 673 | + for (int n = 0; n < _maxVoxels; n++) { | |
| 674 | + for (int i = 0; i < VERTEX_POINTS_PER_VOXEL; 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]; | |
| 632 | − _readColorsArray = new GLubyte[VERTEX_POINTS_PER_VOXEL * _maxVoxels]; | |
| 633 | − | |
| 634 | − | |
| 635 | − // create our simple fragment shader if we're the first system to init | |
| 636 | − if (!_perlinModulateProgram.isLinked()) { | |
| 637 | − switchToResourcesParentIfRequired(); | |
| 638 | − _perlinModulateProgram.addShaderFromSourceFile(QGLShader::Vertex, "resources/shaders/perlin_modulate.vert"); | |
| 639 | − _perlinModulateProgram.addShaderFromSourceFile(QGLShader::Fragment, "resources/shaders/perlin_modulate.frag"); | |
| 640 | − _perlinModulateProgram.link(); | |
| 641 | − | |
| 642 | − _perlinModulateProgram.bind(); | |
| 643 | − _perlinModulateProgram.setUniformValue("permutationNormalTexture", 0); | |
| 644 | − _perlinModulateProgram.release(); | |
| 679 | + glGenBuffers(1, &_vboVerticesID); | |
| 680 | + glBindBuffer(GL_ARRAY_BUFFER, _vboVerticesID); | |
| 681 | + glBufferData(GL_ARRAY_BUFFER, VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat) * _maxVoxels, NULL, GL_DYNAMIC_DRAW); | |
| 682 | + | |
| 683 | + // VBO for the normalsArray | |
| 684 | + glGenBuffers(1, &_vboNormalsID); | |
| 685 | + glBindBuffer(GL_ARRAY_BUFFER, _vboNormalsID); | |
| 686 | + glBufferData(GL_ARRAY_BUFFER, | |
| 687 | + VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat) * _maxVoxels, | |
| 688 | + normalsArray, GL_STATIC_DRAW); | |
| 689 | + | |
| 690 | + // VBO for colorsArray | |
| 691 | + glGenBuffers(1, &_vboColorsID); | |
| 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 | 733 | _initialized = true; |
| 647 | 734 | } |
| @@ -714,17 +801,34 @@ void VoxelSystem::updateVBOs() { · 66 unmodified lines | ||
| 714 | 801 | } |
| 715 | 802 | |
| 716 | 803 | void VoxelSystem::updateVBOSegment(glBufferIndex segmentStart, glBufferIndex segmentEnd) { |
| 717 | − int segmentLength = (segmentEnd - segmentStart) + 1; | |
| 718 | − GLintptr segmentStartAt = segmentStart * VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat); | |
| 719 | − GLsizeiptr segmentSizeBytes = segmentLength * VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat); | |
| 720 | − GLfloat* readVerticesFrom = _readVerticesArray + (segmentStart * VERTEX_POINTS_PER_VOXEL); | |
| 721 | − glBindBuffer(GL_ARRAY_BUFFER, _vboVerticesID); | |
| 722 | − glBufferSubData(GL_ARRAY_BUFFER, segmentStartAt, segmentSizeBytes, readVerticesFrom); | |
| 723 | − segmentStartAt = segmentStart * VERTEX_POINTS_PER_VOXEL * sizeof(GLubyte); | |
| 724 | − segmentSizeBytes = segmentLength * VERTEX_POINTS_PER_VOXEL * sizeof(GLubyte); | |
| 725 | − GLubyte* readColorsFrom = _readColorsArray + (segmentStart * VERTEX_POINTS_PER_VOXEL); | |
| 726 | − glBindBuffer(GL_ARRAY_BUFFER, _vboColorsID); | |
| 727 | − glBufferSubData(GL_ARRAY_BUFFER, segmentStartAt, segmentSizeBytes, readColorsFrom); | |
| 804 | + if (getUseVoxelShader()) { | |
| 805 | + int segmentLength = (segmentEnd - segmentStart) + 1; | |
| 806 | + GLintptr segmentStartAt = segmentStart * sizeof(VoxelShaderVBOData); | |
| 807 | + GLsizeiptr segmentSizeBytes = segmentLength * sizeof(VoxelShaderVBOData); | |
| 808 | + void* readVerticesFrom = &_readVoxelShaderData[segmentStart]; | |
| 809 | + | |
| 810 | + glBindBuffer(GL_ARRAY_BUFFER, _vboVoxelsID); | |
| 811 | + glBufferSubData(GL_ARRAY_BUFFER, segmentStartAt, segmentSizeBytes, readVerticesFrom); | |
| 812 | + } else { | |
| 813 | + int segmentLength = (segmentEnd - segmentStart) + 1; | |
| 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 | 834 | void VoxelSystem::render(bool texture) { |
| @@ -734,43 +838,85 @@ void VoxelSystem::render(bool texture) { · 3 unmodified lines | ||
| 734 | 838 | pthread_mutex_lock(&_bufferWriteLock); |
| 735 | 839 | |
| 736 | 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 | |
| 739 | − glEnableClientState(GL_VERTEX_ARRAY); | |
| 740 | − glEnableClientState(GL_NORMAL_ARRAY); | |
| 741 | − glEnableClientState(GL_COLOR_ARRAY); | |
| 844 | + if (getUseVoxelShader()) { | |
| 845 | + | |
| 846 | + Application::getInstance()->getVoxelShader().begin(); | |
| 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); | |
| 744 | − glVertexPointer(3, GL_FLOAT, 0, 0); | |
| 888 | + glBindBuffer(GL_ARRAY_BUFFER, _vboVerticesID); | |
| 889 | + glVertexPointer(3, GL_FLOAT, 0, 0); | |
| 745 | 890 | |
| 746 | − glBindBuffer(GL_ARRAY_BUFFER, _vboNormalsID); | |
| 747 | − glNormalPointer(GL_FLOAT, 0, 0); | |
| 891 | + glBindBuffer(GL_ARRAY_BUFFER, _vboNormalsID); | |
| 892 | + glNormalPointer(GL_FLOAT, 0, 0); | |
| 748 | 893 | |
| 749 | − glBindBuffer(GL_ARRAY_BUFFER, _vboColorsID); | |
| 750 | − glColorPointer(3, GL_UNSIGNED_BYTE, 0, 0); | |
| 894 | + glBindBuffer(GL_ARRAY_BUFFER, _vboColorsID); | |
| 895 | + glColorPointer(3, GL_UNSIGNED_BYTE, 0, 0); | |
| 751 | 896 | |
| 752 | − applyScaleAndBindProgram(texture); | |
| 897 | + applyScaleAndBindProgram(texture); | |
| 753 | 898 | |
| 754 | − // for performance, enable backface culling | |
| 755 | − glEnable(GL_CULL_FACE); | |
| 899 | + // for performance, enable backface culling | |
| 900 | + glEnable(GL_CULL_FACE); | |
| 756 | 901 | |
| 757 | − // draw the number of voxels we have | |
| 758 | − glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vboIndicesID); | |
| 759 | − glDrawRangeElementsEXT(GL_TRIANGLES, 0, VERTICES_PER_VOXEL * _voxelsInReadArrays - 1, | |
| 760 | − 36 * _voxelsInReadArrays, GL_UNSIGNED_INT, 0); | |
| 902 | + // draw the number of voxels we have | |
| 903 | + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vboIndicesID); | |
| 904 | + glDrawRangeElementsEXT(GL_TRIANGLES, 0, VERTICES_PER_VOXEL * _voxelsInReadArrays - 1, | |
| 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 | |
| 767 | − glDisableClientState(GL_VERTEX_ARRAY); | |
| 768 | − glDisableClientState(GL_NORMAL_ARRAY); | |
| 769 | − glDisableClientState(GL_COLOR_ARRAY); | |
| 770 | − | |
| 771 | − // bind with 0 to switch back to normal operation | |
| 772 | − glBindBuffer(GL_ARRAY_BUFFER, 0); | |
| 773 | − glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); | |
| 911 | + // deactivate vertex and color arrays after drawing | |
| 912 | + glDisableClientState(GL_VERTEX_ARRAY); | |
| 913 | + glDisableClientState(GL_NORMAL_ARRAY); | |
| 914 | + glDisableClientState(GL_COLOR_ARRAY); | |
| 915 | + | |
| 916 | + // bind with 0 to switch back to normal operation | |
| 917 | + glBindBuffer(GL_ARRAY_BUFFER, 0); | |
| 918 | + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); | |
| 919 | + } | |
| 774 | 920 | |
| 775 | 921 | pthread_mutex_unlock(&_bufferWriteLock); |
| 776 | 922 | } |