Showing the changes to interface/src/MetavoxelSystem.cpp (1 of 5 files). Show all
Unified · Split
| @@ -366,6 +366,7 @@ void HeightfieldBuffer::render(bool cursor) { · 365 unmodified lines | ||
| 366 | 366 | if (_heightTextureID == 0) { |
| 367 | 367 | glGenTextures(1, &_heightTextureID); |
| 368 | 368 | glBindTexture(GL_TEXTURE_2D, _heightTextureID); |
| 369 | + glPixelStorei(GL_UNPACK_ALIGNMENT, 1); | |
| 369 | 370 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 370 | 371 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 371 | 372 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| @@ -385,7 +386,7 @@ void HeightfieldBuffer::render(bool cursor) { · 13 unmodified lines | ||
| 385 | 386 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0, GL_RGB, GL_UNSIGNED_BYTE, WHITE_COLOR); |
| 386 | 387 | |
| 387 | 388 | } else { |
| 388 | − int colorSize = glm::sqrt(_color.size() / 3); | |
| 389 | + int colorSize = glm::sqrt(_color.size() / 3); | |
| 389 | 390 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, colorSize, colorSize, 0, GL_RGB, GL_UNSIGNED_BYTE, _color.constData()); |
| 390 | 391 | if (_clearAfterLoading) { |
| 391 | 392 | _color.clear(); |
| @@ -393,24 +394,27 @@ void HeightfieldBuffer::render(bool cursor) { · 1 unmodified line | ||
| 393 | 394 | } |
| 394 | 395 | } |
| 395 | 396 | // create the buffer objects lazily |
| 396 | − int sizeWithSkirt = _heightSize + 2; | |
| 397 | − int vertexCount = sizeWithSkirt * sizeWithSkirt; | |
| 398 | − int rows = sizeWithSkirt - 1; | |
| 397 | + int innerSize = _heightSize - 2; | |
| 398 | + int vertexCount = _heightSize * _heightSize; | |
| 399 | + int rows = _heightSize - 1; | |
| 399 | 400 | int indexCount = rows * rows * 4; |
| 400 | 401 | BufferPair& bufferPair = _bufferPairs[_heightSize]; |
| 401 | 402 | if (!bufferPair.first.isCreated()) { |
| 402 | 403 | QVector<HeightfieldPoint> vertices(vertexCount); |
| 403 | 404 | HeightfieldPoint* point = vertices.data(); |
| 404 | 405 | |
| 405 | − float step = 1.0f / (_heightSize - 1); | |
| 406 | − float z = -step; | |
| 407 | − for (int i = 0; i < sizeWithSkirt; i++, z += step) { | |
| 408 | − float x = -step; | |
| 406 | + float vertexStep = 1.0f / (innerSize - 1); | |
| 407 | + float z = -vertexStep; | |
| 408 | + float textureStep = 1.0f / _heightSize; | |
| 409 | + float t = textureStep / 2.0f; | |
| 410 | + for (int i = 0; i < _heightSize; i++, z += vertexStep, t += textureStep) { | |
| 411 | + float x = -vertexStep; | |
| 412 | + float s = textureStep / 2.0f; | |
| 409 | 413 | const float SKIRT_LENGTH = 0.25f; |
| 410 | − float baseY = (i == 0 || i == sizeWithSkirt - 1) ? -SKIRT_LENGTH : 0.0f; | |
| 411 | − for (int j = 0; j < sizeWithSkirt; j++, point++, x += step) { | |
| 412 | − point->vertex = glm::vec3(x, (j == 0 || j == sizeWithSkirt - 1) ? -SKIRT_LENGTH : baseY, z); | |
| 413 | − point->textureCoord = glm::vec2(x, z); | |
| 414 | + float baseY = (i == 0 || i == _heightSize - 1) ? -SKIRT_LENGTH : 0.0f; | |
| 415 | + for (int j = 0; j < _heightSize; j++, point++, x += vertexStep, s += textureStep) { | |
| 416 | + point->vertex = glm::vec3(x, (j == 0 || j == _heightSize - 1) ? -SKIRT_LENGTH : baseY, z); | |
| 417 | + point->textureCoord = glm::vec2(s, t); | |
| 414 | 418 | } |
| 415 | 419 | } |
| 416 | 420 | |
| @@ -422,8 +426,8 @@ void HeightfieldBuffer::render(bool cursor) { · 5 unmodified lines | ||
| 422 | 426 | QVector<int> indices(indexCount); |
| 423 | 427 | int* index = indices.data(); |
| 424 | 428 | for (int i = 0; i < rows; i++) { |
| 425 | − int lineIndex = i * sizeWithSkirt; | |
| 426 | − int nextLineIndex = (i + 1) * sizeWithSkirt; | |
| 429 | + int lineIndex = i * _heightSize; | |
| 430 | + int nextLineIndex = (i + 1) * _heightSize; | |
| 427 | 431 | for (int j = 0; j < rows; j++) { |
| 428 | 432 | *index++ = lineIndex + j; |
| 429 | 433 | *index++ = nextLineIndex + j; |
| @@ -452,17 +456,15 @@ void HeightfieldBuffer::render(bool cursor) { · 22 unmodified lines | ||
| 452 | 456 | |
| 453 | 457 | glBindTexture(GL_TEXTURE_2D, _heightTextureID); |
| 454 | 458 | |
| 455 | − int heightScaleLocation; | |
| 456 | − if (cursor) { | |
| 457 | − heightScaleLocation = DefaultMetavoxelRendererImplementation::getCursorHeightScaleLocation(); | |
| 458 | − } else { | |
| 459 | − heightScaleLocation = DefaultMetavoxelRendererImplementation::getHeightScaleLocation(); | |
| 459 | + if (!cursor) { | |
| 460 | + DefaultMetavoxelRendererImplementation::getHeightfieldProgram().setUniformValue( | |
| 461 | + DefaultMetavoxelRendererImplementation::getHeightScaleLocation(), 1.0f / _heightSize); | |
| 462 | + DefaultMetavoxelRendererImplementation::getHeightfieldProgram().setUniformValue( | |
| 463 | + DefaultMetavoxelRendererImplementation::getColorScaleLocation(), (float)_heightSize / innerSize); | |
| 460 | 464 | glActiveTexture(GL_TEXTURE1); |
| 461 | 465 | glBindTexture(GL_TEXTURE_2D, _colorTextureID); |
| 462 | 466 | } |
| 463 | 467 | |
| 464 | − DefaultMetavoxelRendererImplementation::getHeightfieldProgram().setUniformValue(heightScaleLocation, 1.0f / _heightSize); | |
| 465 | − | |
| 466 | 468 | glDrawRangeElements(GL_QUADS, 0, vertexCount - 1, indexCount, GL_UNSIGNED_INT, 0); |
| 467 | 469 | |
| 468 | 470 | if (!cursor) { |
| @@ -548,6 +550,7 @@ void DefaultMetavoxelRendererImplementation::init() { · 79 unmodified lines | ||
| 548 | 550 | _heightfieldProgram.setUniformValue("heightMap", 0); |
| 549 | 551 | _heightfieldProgram.setUniformValue("diffuseMap", 1); |
| 550 | 552 | _heightScaleLocation = _heightfieldProgram.uniformLocation("heightScale"); |
| 553 | + _colorScaleLocation = _heightfieldProgram.uniformLocation("colorScale"); | |
| 551 | 554 | _heightfieldProgram.release(); |
| 552 | 555 | |
| 553 | 556 | _heightfieldCursorProgram.addShaderFromSourceFile(QGLShader::Vertex, Application::resourcesPath() + |
| @@ -558,7 +561,6 @@ void DefaultMetavoxelRendererImplementation::init() { · 4 unmodified lines | ||
| 558 | 561 | |
| 559 | 562 | _heightfieldCursorProgram.bind(); |
| 560 | 563 | _heightfieldCursorProgram.setUniformValue("heightMap", 0); |
| 561 | − _cursorHeightScaleLocation = _heightfieldCursorProgram.uniformLocation("heightScale"); | |
| 562 | 564 | _heightfieldCursorProgram.release(); |
| 563 | 565 | } |
| 564 | 566 | } |
| @@ -648,14 +650,62 @@ HeightfieldAugmentVisitor::HeightfieldAugmentVisitor(const MetavoxelLOD& lod) : · 83 unmodified lines | ||
| 648 | 650 | Application::getInstance()->getMetavoxels()->getHeightfieldBufferAttribute(), lod) { |
| 649 | 651 | } |
| 650 | 652 | |
| 653 | +class BorderFetchVisitor : public MetavoxelVisitor { | |
| 654 | +public: | |
| 655 | + | |
| 656 | + BorderFetchVisitor(const MetavoxelLOD& lod, QByteArray& height); | |
| 657 | + | |
| 658 | + virtual int visit(MetavoxelInfo& info); | |
| 659 | + | |
| 660 | +private: | |
| 661 | + | |
| 662 | + QByteArray& _height; | |
| 663 | +}; | |
| 664 | + | |
| 665 | +BorderFetchVisitor::BorderFetchVisitor(const MetavoxelLOD& lod, QByteArray& height) : | |
| 666 | + MetavoxelVisitor(QVector<AttributePointer>() << AttributeRegistry::getInstance()->getHeightfieldAttribute(), | |
| 667 | + QVector<AttributePointer>(), lod), | |
| 668 | + _height(height) { | |
| 669 | +} | |
| 670 | + | |
| 671 | +int BorderFetchVisitor::visit(MetavoxelInfo& info) { | |
| 672 | + if (!info.isLeaf) { | |
| 673 | + return DEFAULT_ORDER; | |
| 674 | + } | |
| 675 | + return STOP_RECURSION; | |
| 676 | +} | |
| 677 | + | |
| 651 | 678 | int HeightfieldAugmentVisitor::visit(MetavoxelInfo& info) { |
| 652 | 679 | if (info.isLeaf) { |
| 653 | 680 | HeightfieldBuffer* buffer = NULL; |
| 654 | 681 | HeightfieldDataPointer height = info.inputValues.at(0).getInlineValue<HeightfieldDataPointer>(); |
| 655 | 682 | if (height) { |
| 683 | + const QByteArray& heightContents = height->getContents(); | |
| 684 | + int size = glm::sqrt(heightContents.size()); | |
| 685 | + int extendedSize = size + 3; | |
| 686 | + QByteArray extendedHeightContents(extendedSize * extendedSize, 0); | |
| 687 | + char* dest = extendedHeightContents.data() + extendedSize + 1; | |
| 688 | + const char* src = heightContents.constData(); | |
| 689 | + for (int z = 0; z < size; z++, src += size, dest += extendedSize) { | |
| 690 | + memcpy(dest, src, size); | |
| 691 | + } | |
| 692 | + QByteArray extendedColorContents; | |
| 656 | 693 | HeightfieldDataPointer color = info.inputValues.at(1).getInlineValue<HeightfieldDataPointer>(); |
| 657 | − buffer = new HeightfieldBuffer(info.minimum, info.size, height->getContents(), | |
| 658 | − color ? color->getContents() : QByteArray()); | |
| 694 | + if (color) { | |
| 695 | + const QByteArray& colorContents = color->getContents(); | |
| 696 | + const int BYTES_PER_PIXEL = 3; | |
| 697 | + int colorSize = glm::sqrt(colorContents.size() / BYTES_PER_PIXEL); | |
| 698 | + int extendedColorSize = colorSize + 1; | |
| 699 | + extendedColorContents = QByteArray(extendedColorSize * extendedColorSize * BYTES_PER_PIXEL, 0); | |
| 700 | + char* dest = extendedColorContents.data(); | |
| 701 | + const char* src = colorContents.constData(); | |
| 702 | + int srcStride = colorSize * BYTES_PER_PIXEL; | |
| 703 | + int destStride = extendedColorSize * BYTES_PER_PIXEL; | |
| 704 | + for (int z = 0; z < colorSize; z++, src += srcStride, dest += destStride) { | |
| 705 | + memcpy(dest, src, srcStride); | |
| 706 | + } | |
| 707 | + } | |
| 708 | + buffer = new HeightfieldBuffer(info.minimum, info.size, extendedHeightContents, extendedColorContents); | |
| 659 | 709 | } |
| 660 | 710 | info.outputValues[0] = AttributeValue(_outputs.at(0), encodeInline(BufferDataPointer(buffer))); |
| 661 | 711 | return STOP_RECURSION; |
| @@ -856,8 +906,8 @@ ProgramObject DefaultMetavoxelRendererImplementation::_pointProgram; · 194 unmodified lines | ||
| 856 | 906 | int DefaultMetavoxelRendererImplementation::_pointScaleLocation; |
| 857 | 907 | ProgramObject DefaultMetavoxelRendererImplementation::_heightfieldProgram; |
| 858 | 908 | int DefaultMetavoxelRendererImplementation::_heightScaleLocation; |
| 909 | +int DefaultMetavoxelRendererImplementation::_colorScaleLocation; | |
| 859 | 910 | ProgramObject DefaultMetavoxelRendererImplementation::_heightfieldCursorProgram; |
| 860 | −int DefaultMetavoxelRendererImplementation::_cursorHeightScaleLocation; | |
| 861 | 911 | |
| 862 | 912 | static void enableClipPlane(GLenum plane, float x, float y, float z, float w) { |
| 863 | 913 | GLdouble coefficients[] = { x, y, z, w }; |