Showing the changes to interface/src/MetavoxelSystem.cpp (1 of 204 files). Show all
Unified · Split
| @@ -21,6 +21,7 @@ · 20 unmodified lines | |||
| 21 | 21 | ||
| 22 | #include <glm/gtx/transform.hpp> | 22 | #include <glm/gtx/transform.hpp> |
| 23 | 23 | ||
| 24 | #include <GeometryUtil.h> | ||
| 24 | #include <SharedUtil.h> | 25 | #include <SharedUtil.h> |
| 25 | 26 | ||
| 26 | #include <MetavoxelMessages.h> | 27 | #include <MetavoxelMessages.h> |
| @@ -461,6 +462,34 @@ void MetavoxelSystem::render() { · 434 unmodified lines | |||
| 461 | _voxelBaseBatches.clear(); | 462 | _voxelBaseBatches.clear(); |
| 462 | } | 463 | } |
| 463 | 464 | ||
| 465 | if (!_hermiteBatches.isEmpty() && Menu::getInstance()->isOptionChecked(MenuOption::DisplayHermiteData)) { | ||
| 466 | Application::getInstance()->getTextureCache()->setPrimaryDrawBuffers(true, true); | ||
| 467 | |||
| 468 | glEnableClientState(GL_VERTEX_ARRAY); | ||
| 469 | |||
| 470 | glColor4f(1.0f, 1.0f, 1.0f, 1.0f); | ||
| 471 | glNormal3f(0.0f, 1.0f, 0.0f); | ||
| 472 | |||
| 473 | Application::getInstance()->getDeferredLightingEffect()->bindSimpleProgram(); | ||
| 474 | |||
| 475 | foreach (const HermiteBatch& batch, _hermiteBatches) { | ||
| 476 | batch.vertexBuffer->bind(); | ||
| 477 | |||
| 478 | glVertexPointer(3, GL_FLOAT, 0, 0); | ||
| 479 | |||
| 480 | glDrawArrays(GL_LINES, 0, batch.vertexCount); | ||
| 481 | |||
| 482 | batch.vertexBuffer->release(); | ||
| 483 | } | ||
| 484 | |||
| 485 | Application::getInstance()->getDeferredLightingEffect()->releaseSimpleProgram(); | ||
| 486 | |||
| 487 | glDisableClientState(GL_VERTEX_ARRAY); | ||
| 488 | |||
| 489 | Application::getInstance()->getTextureCache()->setPrimaryDrawBuffers(true, false); | ||
| 490 | } | ||
| 491 | _hermiteBatches.clear(); | ||
| 492 | |||
| 464 | // give external parties a chance to join in | 493 | // give external parties a chance to join in |
| 465 | emit rendering(); | 494 | emit rendering(); |
| 466 | } | 495 | } |
| @@ -550,10 +579,53 @@ void MetavoxelSystem::setVoxelMaterial(const SharedObjectPointer& spanner, const · 83 unmodified lines | |||
| 550 | applyMaterialEdit(edit, true); | 579 | applyMaterialEdit(edit, true); |
| 551 | } | 580 | } |
| 552 | 581 | ||
| 553 | class SpannerCursorRenderVisitor : public SpannerVisitor { | 582 | void MetavoxelSystem::deleteTextures(int heightTextureID, int colorTextureID, int materialTextureID) const { |
| 583 | glDeleteTextures(1, (const GLuint*)&heightTextureID); | ||
| 584 | glDeleteTextures(1, (const GLuint*)&colorTextureID); | ||
| 585 | glDeleteTextures(1, (const GLuint*)&materialTextureID); | ||
| 586 | } | ||
| 587 | |||
| 588 | class SpannerRenderVisitor : public SpannerVisitor { | ||
| 589 | public: | ||
| 590 | |||
| 591 | SpannerRenderVisitor(const MetavoxelLOD& lod); | ||
| 592 | |||
| 593 | virtual int visit(MetavoxelInfo& info); | ||
| 594 | virtual bool visit(Spanner* spanner); | ||
| 595 | |||
| 596 | protected: | ||
| 597 | |||
| 598 | int _containmentDepth; | ||
| 599 | }; | ||
| 600 | |||
| 601 | SpannerRenderVisitor::SpannerRenderVisitor(const MetavoxelLOD& lod) : | ||
| 602 | SpannerVisitor(QVector<AttributePointer>() << AttributeRegistry::getInstance()->getSpannersAttribute(), | ||
| 603 | QVector<AttributePointer>(), QVector<AttributePointer>(), lod, | ||
| 604 | encodeOrder(Application::getInstance()->getViewFrustum()->getDirection())), | ||
| 605 | _containmentDepth(INT_MAX) { | ||
| 606 | } | ||
| 607 | |||
| 608 | int SpannerRenderVisitor::visit(MetavoxelInfo& info) { | ||
| 609 | if (_containmentDepth >= _depth) { | ||
| 610 | Frustum::IntersectionType intersection = Application::getInstance()->getMetavoxels()->getFrustum().getIntersectionType( | ||
| 611 | info.getBounds()); | ||
| 612 | if (intersection == Frustum::NO_INTERSECTION) { | ||
| 613 | return STOP_RECURSION; | ||
| 614 | } | ||
| 615 | _containmentDepth = (intersection == Frustum::CONTAINS_INTERSECTION) ? _depth : INT_MAX; | ||
| 616 | } | ||
| 617 | return SpannerVisitor::visit(info); | ||
| 618 | } | ||
| 619 | |||
| 620 | bool SpannerRenderVisitor::visit(Spanner* spanner) { | ||
| 621 | spanner->getRenderer()->render(_lod, _containmentDepth <= _depth); | ||
| 622 | return true; | ||
| 623 | } | ||
| 624 | |||
| 625 | class SpannerCursorRenderVisitor : public SpannerRenderVisitor { | ||
| 554 | public: | 626 | public: |
| 555 | 627 | ||
| 556 | SpannerCursorRenderVisitor(const Box& bounds); | 628 | SpannerCursorRenderVisitor(const MetavoxelLOD& lod, const Box& bounds); |
| 557 | 629 | ||
| 558 | virtual bool visit(Spanner* spanner); | 630 | virtual bool visit(Spanner* spanner); |
| 559 | 631 | ||
| @@ -564,20 +636,20 @@ private: · 4 unmodified lines | |||
| 564 | Box _bounds; | 636 | Box _bounds; |
| 565 | }; | 637 | }; |
| 566 | 638 | ||
| 567 | SpannerCursorRenderVisitor::SpannerCursorRenderVisitor(const Box& bounds) : | 639 | SpannerCursorRenderVisitor::SpannerCursorRenderVisitor(const MetavoxelLOD& lod, const Box& bounds) : |
| 568 | SpannerVisitor(QVector<AttributePointer>() << AttributeRegistry::getInstance()->getSpannersAttribute()), | 640 | SpannerRenderVisitor(lod), |
| 569 | _bounds(bounds) { | 641 | _bounds(bounds) { |
| 570 | } | 642 | } |
| 571 | 643 | ||
| 572 | bool SpannerCursorRenderVisitor::visit(Spanner* spanner) { | 644 | bool SpannerCursorRenderVisitor::visit(Spanner* spanner) { |
| 573 | if (spanner->isHeightfield()) { | 645 | if (spanner->isHeightfield()) { |
| 574 | spanner->getRenderer()->render(true); | 646 | spanner->getRenderer()->render(_lod, _containmentDepth <= _depth, true); |
| 575 | } | 647 | } |
| 576 | return true; | 648 | return true; |
| 577 | } | 649 | } |
| 578 | 650 | ||
| 579 | int SpannerCursorRenderVisitor::visit(MetavoxelInfo& info) { | 651 | int SpannerCursorRenderVisitor::visit(MetavoxelInfo& info) { |
| 580 | return info.getBounds().intersects(_bounds) ? SpannerVisitor::visit(info) : STOP_RECURSION; | 652 | return info.getBounds().intersects(_bounds) ? SpannerRenderVisitor::visit(info) : STOP_RECURSION; |
| 581 | } | 653 | } |
| 582 | 654 | ||
| 583 | void MetavoxelSystem::renderHeightfieldCursor(const glm::vec3& position, float radius) { | 655 | void MetavoxelSystem::renderHeightfieldCursor(const glm::vec3& position, float radius) { |
| @@ -604,7 +676,7 @@ void MetavoxelSystem::renderHeightfieldCursor(const glm::vec3& position, float r · 20 unmodified lines | |||
| 604 | glActiveTexture(GL_TEXTURE0); | 676 | glActiveTexture(GL_TEXTURE0); |
| 605 | 677 | ||
| 606 | glm::vec3 extents(radius, radius, radius); | 678 | glm::vec3 extents(radius, radius, radius); |
| 607 | SpannerCursorRenderVisitor visitor(Box(position - extents, position + extents)); | 679 | SpannerCursorRenderVisitor visitor(getLOD(), Box(position - extents, position + extents)); |
| 608 | guide(visitor); | 680 | guide(visitor); |
| 609 | 681 | ||
| 610 | _heightfieldCursorProgram.release(); | 682 | _heightfieldCursorProgram.release(); |
| @@ -678,7 +750,7 @@ void MetavoxelSystem::renderVoxelCursor(const glm::vec3& position, float radius) · 67 unmodified lines | |||
| 678 | 750 | ||
| 679 | _heightfieldCursorProgram.bind(); | 751 | _heightfieldCursorProgram.bind(); |
| 680 | 752 | ||
| 681 | SpannerCursorRenderVisitor spannerVisitor(bounds); | 753 | SpannerCursorRenderVisitor spannerVisitor(getLOD(), bounds); |
| 682 | guide(spannerVisitor); | 754 | guide(spannerVisitor); |
| 683 | 755 | ||
| 684 | _heightfieldCursorProgram.release(); | 756 | _heightfieldCursorProgram.release(); |
| @@ -1024,30 +1096,6 @@ _indexBuffer(QOpenGLBuffer::IndexBuffer), · 339 unmodified lines | |||
| 1024 | _materials(materials) { | 1096 | _materials(materials) { |
| 1025 | } | 1097 | } |
| 1026 | 1098 | ||
| 1027 | static bool findRayTriangleIntersection(const glm::vec3& origin, const glm::vec3& direction, | ||
| 1028 | const glm::vec3& v0, const glm::vec3& v1, const glm::vec3& v2, float& distance) { | ||
| 1029 | glm::vec3 firstSide = v0 - v1; | ||
| 1030 | glm::vec3 secondSide = v2 - v1; | ||
| 1031 | glm::vec3 normal = glm::cross(secondSide, firstSide); | ||
| 1032 | float dividend = glm::dot(normal, v1) - glm::dot(origin, normal); | ||
| 1033 | if (dividend > 0.0f) { | ||
| 1034 | return false; // origin below plane | ||
| 1035 | } | ||
| 1036 | float divisor = glm::dot(normal, direction); | ||
| 1037 | if (divisor > -EPSILON) { | ||
| 1038 | return false; | ||
| 1039 | } | ||
| 1040 | float t = dividend / divisor; | ||
| 1041 | glm::vec3 point = origin + direction * t; | ||
| 1042 | if (glm::dot(normal, glm::cross(point - v1, firstSide)) > 0.0f && | ||
| 1043 | glm::dot(normal, glm::cross(secondSide, point - v1)) > 0.0f && | ||
| 1044 | glm::dot(normal, glm::cross(point - v0, v2 - v0)) > 0.0f) { | ||
| 1045 | distance = t; | ||
| 1046 | return true; | ||
| 1047 | } | ||
| 1048 | return false; | ||
| 1049 | } | ||
| 1050 | |||
| 1051 | bool VoxelBuffer::findFirstRayIntersection(const glm::vec3& entry, const glm::vec3& origin, | 1099 | bool VoxelBuffer::findFirstRayIntersection(const glm::vec3& entry, const glm::vec3& origin, |
| 1052 | const glm::vec3& direction, float& distance) const { | 1100 | const glm::vec3& direction, float& distance) const { |
| 1053 | float highest = _size - 1.0f; | 1101 | float highest = _size - 1.0f; |
| @@ -1192,31 +1240,18 @@ void VoxelBuffer::render(bool cursor) { · 138 unmodified lines | |||
| 1192 | } | 1240 | } |
| 1193 | } | 1241 | } |
| 1194 | 1242 | ||
| 1195 | if (_hermiteCount > 0 && Menu::getInstance()->isOptionChecked(MenuOption::DisplayHermiteData)) { | 1243 | if (_hermiteCount > 0) { |
| 1196 | if (!_hermiteBuffer.isCreated()) { | 1244 | if (!_hermiteBuffer.isCreated()) { |
| 1197 | _hermiteBuffer.create(); | 1245 | _hermiteBuffer.create(); |
| 1198 | _hermiteBuffer.bind(); | 1246 | _hermiteBuffer.bind(); |
| 1199 | _hermiteBuffer.allocate(_hermite.constData(), _hermite.size() * sizeof(glm::vec3)); | 1247 | _hermiteBuffer.allocate(_hermite.constData(), _hermite.size() * sizeof(glm::vec3)); |
| 1248 | _hermiteBuffer.release(); | ||
| 1200 | _hermite.clear(); | 1249 | _hermite.clear(); |
| 1201 | |||
| 1202 | } else { | ||
| 1203 | _hermiteBuffer.bind(); | ||
| 1204 | } | 1250 | } |
| 1205 | 1251 | HermiteBatch hermiteBatch; | |
| 1206 | glVertexPointer(3, GL_FLOAT, 0, 0); | 1252 | hermiteBatch.vertexBuffer = &_hermiteBuffer; |
| 1207 | 1253 | hermiteBatch.vertexCount = _hermiteCount; | |
| 1208 | Application::getInstance()->getDeferredLightingEffect()->getSimpleProgram().bind(); | 1254 | Application::getInstance()->getMetavoxels()->addHermiteBatch(hermiteBatch); |
| 1209 | |||
| 1210 | glColor4f(1.0f, 1.0f, 1.0f, 1.0f); | ||
| 1211 | glNormal3f(0.0f, 1.0f, 0.0f); | ||
| 1212 | |||
| 1213 | glLineWidth(1.0f); | ||
| 1214 | |||
| 1215 | glDrawArrays(GL_LINES, 0, _hermiteCount); | ||
| 1216 | |||
| 1217 | Application::getInstance()->getDeferredLightingEffect()->getSimpleProgram().release(); | ||
| 1218 | |||
| 1219 | _hermiteBuffer.release(); | ||
| 1220 | } | 1255 | } |
| 1221 | } | 1256 | } |
| 1222 | 1257 | ||
| @@ -1880,43 +1915,6 @@ void DefaultMetavoxelRendererImplementation::simulate(MetavoxelData& data, float · 657 unmodified lines | |||
| 1880 | data.guide(spannerSimulateVisitor); | 1915 | data.guide(spannerSimulateVisitor); |
| 1881 | } | 1916 | } |
| 1882 | 1917 | ||
| 1883 | class SpannerRenderVisitor : public SpannerVisitor { | ||
| 1884 | public: | ||
| 1885 | |||
| 1886 | SpannerRenderVisitor(const MetavoxelLOD& lod); | ||
| 1887 | |||
| 1888 | virtual int visit(MetavoxelInfo& info); | ||
| 1889 | virtual bool visit(Spanner* spanner); | ||
| 1890 | |||
| 1891 | private: | ||
| 1892 | |||
| 1893 | int _containmentDepth; | ||
| 1894 | }; | ||
| 1895 | |||
| 1896 | SpannerRenderVisitor::SpannerRenderVisitor(const MetavoxelLOD& lod) : | ||
| 1897 | SpannerVisitor(QVector<AttributePointer>() << AttributeRegistry::getInstance()->getSpannersAttribute(), | ||
| 1898 | QVector<AttributePointer>(), QVector<AttributePointer>(), lod, | ||
| 1899 | encodeOrder(Application::getInstance()->getDisplayViewFrustum()->getDirection())), | ||
| 1900 | _containmentDepth(INT_MAX) { | ||
| 1901 | } | ||
| 1902 | |||
| 1903 | int SpannerRenderVisitor::visit(MetavoxelInfo& info) { | ||
| 1904 | if (_containmentDepth >= _depth) { | ||
| 1905 | Frustum::IntersectionType intersection = Application::getInstance()->getMetavoxels()->getFrustum().getIntersectionType( | ||
| 1906 | info.getBounds()); | ||
| 1907 | if (intersection == Frustum::NO_INTERSECTION) { | ||
| 1908 | return STOP_RECURSION; | ||
| 1909 | } | ||
| 1910 | _containmentDepth = (intersection == Frustum::CONTAINS_INTERSECTION) ? _depth : INT_MAX; | ||
| 1911 | } | ||
| 1912 | return SpannerVisitor::visit(info); | ||
| 1913 | } | ||
| 1914 | |||
| 1915 | bool SpannerRenderVisitor::visit(Spanner* spanner) { | ||
| 1916 | spanner->getRenderer()->render(); | ||
| 1917 | return true; | ||
| 1918 | } | ||
| 1919 | |||
| 1920 | class BufferRenderVisitor : public MetavoxelVisitor { | 1918 | class BufferRenderVisitor : public MetavoxelVisitor { |
| 1921 | public: | 1919 | public: |
| 1922 | 1920 | ||
| @@ -1970,7 +1968,7 @@ SphereRenderer::SphereRenderer() { · 47 unmodified lines | |||
| 1970 | } | 1968 | } |
| 1971 | 1969 | ||
| 1972 | 1970 | ||
| 1973 | void SphereRenderer::render(bool cursor) { | 1971 | void SphereRenderer::render(const MetavoxelLOD& lod, bool contained, bool cursor) { |
| 1974 | Sphere* sphere = static_cast<Sphere*>(_spanner); | 1972 | Sphere* sphere = static_cast<Sphere*>(_spanner); |
| 1975 | const QColor& color = sphere->getColor(); | 1973 | const QColor& color = sphere->getColor(); |
| 1976 | glColor4f(color.redF(), color.greenF(), color.blueF(), color.alphaF()); | 1974 | glColor4f(color.redF(), color.greenF(), color.blueF(), color.alphaF()); |
| @@ -1990,7 +1988,7 @@ void SphereRenderer::render(bool cursor) { · 13 unmodified lines | |||
| 1990 | CuboidRenderer::CuboidRenderer() { | 1988 | CuboidRenderer::CuboidRenderer() { |
| 1991 | } | 1989 | } |
| 1992 | 1990 | ||
| 1993 | void CuboidRenderer::render(bool cursor) { | 1991 | void CuboidRenderer::render(const MetavoxelLOD& lod, bool contained, bool cursor) { |
| 1994 | Cuboid* cuboid = static_cast<Cuboid*>(_spanner); | 1992 | Cuboid* cuboid = static_cast<Cuboid*>(_spanner); |
| 1995 | const QColor& color = cuboid->getColor(); | 1993 | const QColor& color = cuboid->getColor(); |
| 1996 | glColor4f(color.redF(), color.greenF(), color.blueF(), color.alphaF()); | 1994 | glColor4f(color.redF(), color.greenF(), color.blueF(), color.alphaF()); |
| @@ -2041,7 +2039,7 @@ void StaticModelRenderer::simulate(float deltaTime) { · 44 unmodified lines | |||
| 2041 | _model->simulate(deltaTime); | 2039 | _model->simulate(deltaTime); |
| 2042 | } | 2040 | } |
| 2043 | 2041 | ||
| 2044 | void StaticModelRenderer::render(bool cursor) { | 2042 | void StaticModelRenderer::render(const MetavoxelLOD& lod, bool contained, bool cursor) { |
| 2045 | _model->render(); | 2043 | _model->render(); |
| 2046 | } | 2044 | } |
| 2047 | 2045 | ||
| @@ -2073,57 +2071,66 @@ void StaticModelRenderer::applyURL(const QUrl& url) { · 25 unmodified lines | |||
| 2073 | } | 2071 | } |
| 2074 | 2072 | ||
| 2075 | HeightfieldRenderer::HeightfieldRenderer() { | 2073 | HeightfieldRenderer::HeightfieldRenderer() { |
| 2076 | glGenTextures(1, &_heightTextureID); | ||
| 2077 | glBindTexture(GL_TEXTURE_2D, _heightTextureID); | ||
| 2078 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); | ||
| 2079 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); | ||
| 2080 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | ||
| 2081 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | ||
| 2082 | |||
| 2083 | glGenTextures(1, &_colorTextureID); | ||
| 2084 | glBindTexture(GL_TEXTURE_2D, _colorTextureID); | ||
| 2085 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | ||
| 2086 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | ||
| 2087 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | ||
| 2088 | |||
| 2089 | glGenTextures(1, &_materialTextureID); | ||
| 2090 | glBindTexture(GL_TEXTURE_2D, _materialTextureID); | ||
| 2091 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); | ||
| 2092 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); | ||
| 2093 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | ||
| 2094 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | ||
| 2095 | |||
| 2096 | glBindTexture(GL_TEXTURE_2D, 0); | ||
| 2097 | } | 2074 | } |
| 2098 | 2075 | ||
| 2099 | HeightfieldRenderer::~HeightfieldRenderer() { | 2076 | const int X_MAXIMUM_FLAG = 1; |
| 2100 | glDeleteTextures(1, &_heightTextureID); | 2077 | const int Y_MAXIMUM_FLAG = 2; |
| 2101 | glDeleteTextures(1, &_colorTextureID); | 2078 | |
| 2102 | glDeleteTextures(1, &_materialTextureID); | 2079 | static void renderNode(const HeightfieldNodePointer& node, Heightfield* heightfield, const MetavoxelLOD& lod, |
| 2080 | const glm::vec2& minimum, float size, bool contained, bool cursor) { | ||
| 2081 | const glm::quat& rotation = heightfield->getRotation(); | ||
| 2082 | glm::vec3 scale(heightfield->getScale() * size, heightfield->getScale() * heightfield->getAspectY(), | ||
| 2083 | heightfield->getScale() * heightfield->getAspectZ() * size); | ||
| 2084 | glm::vec3 translation = heightfield->getTranslation() + rotation * glm::vec3(minimum.x * heightfield->getScale(), | ||
| 2085 | 0.0f, minimum.y * heightfield->getScale() * heightfield->getAspectZ()); | ||
| 2086 | if (!contained) { | ||
| 2087 | Frustum::IntersectionType type = Application::getInstance()->getMetavoxels()->getFrustum().getIntersectionType( | ||
| 2088 | glm::translate(translation) * glm::mat4_cast(rotation) * Box(glm::vec3(), scale)); | ||
| 2089 | if (type == Frustum::NO_INTERSECTION) { | ||
| 2090 | return; | ||
| 2091 | } | ||
| 2092 | if (type == Frustum::CONTAINS_INTERSECTION) { | ||
| 2093 | contained = true; | ||
| 2094 | } | ||
| 2095 | } | ||
| 2096 | if (!node->isLeaf() && lod.shouldSubdivide(minimum, size)) { | ||
| 2097 | float nextSize = size * 0.5f; | ||
| 2098 | for (int i = 0; i < HeightfieldNode::CHILD_COUNT; i++) { | ||
| 2099 | renderNode(node->getChild(i), heightfield, lod, minimum + glm::vec2(i & X_MAXIMUM_FLAG ? nextSize : 0.0f, | ||
| 2100 | i & Y_MAXIMUM_FLAG ? nextSize : 0.0f), nextSize, contained, cursor); | ||
| 2101 | } | ||
| 2102 | return; | ||
| 2103 | } | ||
| 2104 | HeightfieldNodeRenderer* renderer = static_cast<HeightfieldNodeRenderer*>(node->getRenderer()); | ||
| 2105 | if (!renderer) { | ||
| 2106 | node->setRenderer(renderer = new HeightfieldNodeRenderer()); | ||
| 2107 | } | ||
| 2108 | renderer->render(node, translation, rotation, scale, cursor); | ||
| 2103 | } | 2109 | } |
| 2104 | 2110 | ||
| 2105 | void HeightfieldRenderer::init(Spanner* spanner) { | 2111 | void HeightfieldRenderer::render(const MetavoxelLOD& lod, bool contained, bool cursor) { |
| 2106 | SpannerRenderer::init(spanner); | 2112 | Heightfield* heightfield = static_cast<Heightfield*>(_spanner); |
| 2107 | 2113 | renderNode(heightfield->getRoot(), heightfield, heightfield->transformLOD(lod), glm::vec2(), 1.0f, contained, cursor); | |
| 2108 | Heightfield* heightfield = static_cast<Heightfield*>(spanner); | ||
| 2109 | applyHeight(heightfield->getHeight()); | ||
| 2110 | applyColor(heightfield->getColor()); | ||
| 2111 | applyMaterial(heightfield->getMaterial()); | ||
| 2112 | |||
| 2113 | connect(heightfield, &Heightfield::heightChanged, this, &HeightfieldRenderer::applyHeight); | ||
| 2114 | connect(heightfield, &Heightfield::colorChanged, this, &HeightfieldRenderer::applyColor); | ||
| 2115 | connect(heightfield, &Heightfield::materialChanged, this, &HeightfieldRenderer::applyMaterial); | ||
| 2116 | } | 2114 | } |
| 2117 | 2115 | ||
| 2118 | void HeightfieldRenderer::render(bool cursor) { | 2116 | HeightfieldNodeRenderer::HeightfieldNodeRenderer() : |
| 2119 | // create the buffer objects lazily | 2117 | _heightTextureID(0), |
| 2120 | Heightfield* heightfield = static_cast<Heightfield*>(_spanner); | 2118 | _colorTextureID(0), |
| 2121 | if (!heightfield->getHeight()) { | 2119 | _materialTextureID(0) { |
| 2120 | } | ||
| 2121 | |||
| 2122 | HeightfieldNodeRenderer::~HeightfieldNodeRenderer() { | ||
| 2123 | QMetaObject::invokeMethod(Application::getInstance()->getMetavoxels(), "deleteTextures", Q_ARG(int, _heightTextureID), | ||
| 2124 | Q_ARG(int, _colorTextureID), Q_ARG(int, _materialTextureID)); | ||
| 2125 | } | ||
| 2126 | |||
| 2127 | void HeightfieldNodeRenderer::render(const HeightfieldNodePointer& node, const glm::vec3& translation, | ||
| 2128 | const glm::quat& rotation, const glm::vec3& scale, bool cursor) { | ||
| 2129 | if (!node->getHeight()) { | ||
| 2122 | return; | 2130 | return; |
| 2123 | } | 2131 | } |
| 2124 | int width = heightfield->getHeight()->getWidth(); | 2132 | int width = node->getHeight()->getWidth(); |
| 2125 | int height = heightfield->getHeight()->getContents().size() / width; | 2133 | int height = node->getHeight()->getContents().size() / width; |
| 2126 | |||
| 2127 | int innerWidth = width - 2 * HeightfieldHeight::HEIGHT_BORDER; | 2134 | int innerWidth = width - 2 * HeightfieldHeight::HEIGHT_BORDER; |
| 2128 | int innerHeight = height - 2 * HeightfieldHeight::HEIGHT_BORDER; | 2135 | int innerHeight = height - 2 * HeightfieldHeight::HEIGHT_BORDER; |
| 2129 | int vertexCount = width * height; | 2136 | int vertexCount = width * height; |
| @@ -2180,17 +2187,76 @@ void HeightfieldRenderer::render(bool cursor) { · 50 unmodified lines | |||
| 2180 | bufferPair.second.allocate(indices.constData(), indexCount * sizeof(int)); | 2187 | bufferPair.second.allocate(indices.constData(), indexCount * sizeof(int)); |
| 2181 | bufferPair.second.release(); | 2188 | bufferPair.second.release(); |
| 2182 | } | 2189 | } |
| 2190 | if (_heightTextureID == 0) { | ||
| 2191 | // we use non-aligned data for the various layers | ||
| 2192 | glPixelStorei(GL_UNPACK_ALIGNMENT, 1); | ||
| 2193 | |||
| 2194 | glGenTextures(1, &_heightTextureID); | ||
| 2195 | glBindTexture(GL_TEXTURE_2D, _heightTextureID); | ||
| 2196 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); | ||
| 2197 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); | ||
| 2198 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | ||
| 2199 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | ||
| 2200 | const QVector<quint16>& heightContents = node->getHeight()->getContents(); | ||
| 2201 | glTexImage2D(GL_TEXTURE_2D, 0, GL_R16, width, height, 0, | ||
| 2202 | GL_RED, GL_UNSIGNED_SHORT, heightContents.constData()); | ||
| 2203 | |||
| 2204 | glGenTextures(1, &_colorTextureID); | ||
| 2205 | glBindTexture(GL_TEXTURE_2D, _colorTextureID); | ||
| 2206 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | ||
| 2207 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | ||
| 2208 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | ||
| 2209 | if (node->getColor()) { | ||
| 2210 | const QByteArray& contents = node->getColor()->getContents(); | ||
| 2211 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, node->getColor()->getWidth(), | ||
| 2212 | contents.size() / (node->getColor()->getWidth() * DataBlock::COLOR_BYTES), | ||
| 2213 | 0, GL_RGB, GL_UNSIGNED_BYTE, contents.constData()); | ||
| 2214 | |||
| 2215 | } else { | ||
| 2216 | const quint8 WHITE_COLOR[] = { 255, 255, 255 }; | ||
| 2217 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, 1, 1, 0, GL_RGB, GL_UNSIGNED_BYTE, WHITE_COLOR); | ||
| 2218 | } | ||
| 2219 | |||
| 2220 | glGenTextures(1, &_materialTextureID); | ||
| 2221 | glBindTexture(GL_TEXTURE_2D, _materialTextureID); | ||
| 2222 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); | ||
| 2223 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); | ||
| 2224 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | ||
| 2225 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | ||
| 2226 | if (node->getMaterial()) { | ||
| 2227 | const QByteArray& contents = node->getMaterial()->getContents(); | ||
| 2228 | glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, node->getMaterial()->getWidth(), | ||
| 2229 | contents.size() / node->getMaterial()->getWidth(), | ||
| 2230 | 0, GL_RED, GL_UNSIGNED_BYTE, contents.constData()); | ||
| 2231 | |||
| 2232 | const QVector<SharedObjectPointer>& materials = node->getMaterial()->getMaterials(); | ||
| 2233 | _networkTextures.resize(materials.size()); | ||
| 2234 | for (int i = 0; i < materials.size(); i++) { | ||
| 2235 | const SharedObjectPointer& material = materials.at(i); | ||
| 2236 | if (material) { | ||
| 2237 | _networkTextures[i] = Application::getInstance()->getTextureCache()->getTexture( | ||
| 2238 | static_cast<MaterialObject*>(material.data())->getDiffuse(), SPLAT_TEXTURE); | ||
| 2239 | } | ||
| 2240 | } | ||
| 2241 | } else { | ||
| 2242 | const quint8 ZERO_VALUE = 0; | ||
| 2243 | glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, 1, 1, 0, GL_RED, GL_UNSIGNED_BYTE, &ZERO_VALUE); | ||
| 2244 | } | ||
| 2245 | glBindTexture(GL_TEXTURE_2D, 0); | ||
| 2246 | |||
| 2247 | // restore the default alignment; it's what Qt uses for image storage | ||
| 2248 | glPixelStorei(GL_UNPACK_ALIGNMENT, 4); | ||
| 2249 | } | ||
| 2183 | 2250 | ||
| 2184 | float xScale = heightfield->getScale(), zScale = xScale * heightfield->getAspectZ(); | ||
| 2185 | if (cursor) { | 2251 | if (cursor) { |
| 2186 | bufferPair.first.bind(); | 2252 | bufferPair.first.bind(); |
| 2187 | bufferPair.second.bind(); | 2253 | bufferPair.second.bind(); |
| 2188 | 2254 | ||
| 2189 | glPushMatrix(); | 2255 | glPushMatrix(); |
| 2190 | glTranslatef(heightfield->getTranslation().x, heightfield->getTranslation().y, heightfield->getTranslation().z); | 2256 | glTranslatef(translation.x, translation.y, translation.z); |
| 2191 | glm::vec3 axis = glm::axis(heightfield->getRotation()); | 2257 | glm::vec3 axis = glm::axis(rotation); |
| 2192 | glRotatef(glm::degrees(glm::angle(heightfield->getRotation())), axis.x, axis.y, axis.z); | 2258 | glRotatef(glm::degrees(glm::angle(rotation)), axis.x, axis.y, axis.z); |
| 2193 | glScalef(xScale, xScale * heightfield->getAspectY(), zScale); | 2259 | glScalef(scale.x, scale.y, scale.z); |
| 2194 | 2260 | ||
| 2195 | HeightfieldPoint* point = 0; | 2261 | HeightfieldPoint* point = 0; |
| 2196 | glVertexPointer(3, GL_FLOAT, sizeof(HeightfieldPoint), &point->vertex); | 2262 | glVertexPointer(3, GL_FLOAT, sizeof(HeightfieldPoint), &point->vertex); |
| @@ -2208,13 +2274,12 @@ void HeightfieldRenderer::render(bool cursor) { · 11 unmodified lines | |||
| 2208 | bufferPair.second.release(); | 2274 | bufferPair.second.release(); |
| 2209 | return; | 2275 | return; |
| 2210 | } | 2276 | } |
| 2211 | |||
| 2212 | HeightfieldBaseLayerBatch baseBatch; | 2277 | HeightfieldBaseLayerBatch baseBatch; |
| 2213 | baseBatch.vertexBuffer = &bufferPair.first; | 2278 | baseBatch.vertexBuffer = &bufferPair.first; |
| 2214 | baseBatch.indexBuffer = &bufferPair.second; | 2279 | baseBatch.indexBuffer = &bufferPair.second; |
| 2215 | baseBatch.translation = heightfield->getTranslation(); | 2280 | baseBatch.translation = translation; |
| 2216 | baseBatch.rotation = heightfield->getRotation(); | 2281 | baseBatch.rotation = rotation; |
| 2217 | baseBatch.scale = glm::vec3(xScale, xScale * heightfield->getAspectY(), zScale); | 2282 | baseBatch.scale = scale; |
| 2218 | baseBatch.vertexCount = vertexCount; | 2283 | baseBatch.vertexCount = vertexCount; |
| 2219 | baseBatch.indexCount = indexCount; | 2284 | baseBatch.indexCount = indexCount; |
| 2220 | baseBatch.heightTextureID = _heightTextureID; | 2285 | baseBatch.heightTextureID = _heightTextureID; |
| @@ -2223,13 +2288,13 @@ void HeightfieldRenderer::render(bool cursor) { · 2 unmodified lines | |||
| 2223 | baseBatch.colorScale = glm::vec2((float)width / innerWidth, (float)height / innerHeight); | 2288 | baseBatch.colorScale = glm::vec2((float)width / innerWidth, (float)height / innerHeight); |
| 2224 | Application::getInstance()->getMetavoxels()->addHeightfieldBaseBatch(baseBatch); | 2289 | Application::getInstance()->getMetavoxels()->addHeightfieldBaseBatch(baseBatch); |
| 2225 | 2290 | ||
| 2226 | if (heightfield->getMaterial() && !_networkTextures.isEmpty()) { | 2291 | if (!_networkTextures.isEmpty()) { |
| 2227 | HeightfieldSplatBatch splatBatch; | 2292 | HeightfieldSplatBatch splatBatch; |
| 2228 | splatBatch.vertexBuffer = &bufferPair.first; | 2293 | splatBatch.vertexBuffer = &bufferPair.first; |
| 2229 | splatBatch.indexBuffer = &bufferPair.second; | 2294 | splatBatch.indexBuffer = &bufferPair.second; |
| 2230 | splatBatch.translation = heightfield->getTranslation(); | 2295 | splatBatch.translation = translation; |
| 2231 | splatBatch.rotation = heightfield->getRotation(); | 2296 | splatBatch.rotation = rotation; |
| 2232 | splatBatch.scale = glm::vec3(xScale, xScale * heightfield->getAspectY(), zScale); | 2297 | splatBatch.scale = scale; |
| 2233 | splatBatch.vertexCount = vertexCount; | 2298 | splatBatch.vertexCount = vertexCount; |
| 2234 | splatBatch.indexCount = indexCount; | 2299 | splatBatch.indexCount = indexCount; |
| 2235 | splatBatch.heightTextureID = _heightTextureID; | 2300 | splatBatch.heightTextureID = _heightTextureID; |
| @@ -2237,10 +2302,10 @@ void HeightfieldRenderer::render(bool cursor) { · 1 unmodified line | |||
| 2237 | splatBatch.materialTextureID = _materialTextureID; | 2302 | splatBatch.materialTextureID = _materialTextureID; |
| 2238 | splatBatch.textureScale = glm::vec2((float)width / innerWidth, (float)height / innerHeight); | 2303 | splatBatch.textureScale = glm::vec2((float)width / innerWidth, (float)height / innerHeight); |
| 2239 | splatBatch.splatTextureOffset = glm::vec2( | 2304 | splatBatch.splatTextureOffset = glm::vec2( |
| 2240 | glm::dot(heightfield->getTranslation(), heightfield->getRotation() * glm::vec3(1.0f, 0.0f, 0.0f)) / xScale, | 2305 | glm::dot(translation, rotation * glm::vec3(1.0f, 0.0f, 0.0f)) / scale.x, |
| 2241 | glm::dot(heightfield->getTranslation(), heightfield->getRotation() * glm::vec3(0.0f, 0.0f, 1.0f)) / zScale); | 2306 | glm::dot(translation, rotation * glm::vec3(0.0f, 0.0f, 1.0f)) / scale.z); |
| 2242 | 2307 | ||
| 2243 | const QVector<SharedObjectPointer>& materials = heightfield->getMaterial()->getMaterials(); | 2308 | const QVector<SharedObjectPointer>& materials = node->getMaterial()->getMaterials(); |
| 2244 | for (int i = 0; i < materials.size(); i += SPLAT_COUNT) { | 2309 | for (int i = 0; i < materials.size(); i += SPLAT_COUNT) { |
| 2245 | for (int j = 0; j < SPLAT_COUNT; j++) { | 2310 | for (int j = 0; j < SPLAT_COUNT; j++) { |
| 2246 | int index = i + j; | 2311 | int index = i + j; |
| @@ -2248,8 +2313,8 @@ void HeightfieldRenderer::render(bool cursor) { · 1 unmodified line | |||
| 2248 | const NetworkTexturePointer& texture = _networkTextures.at(index); | 2313 | const NetworkTexturePointer& texture = _networkTextures.at(index); |
| 2249 | if (texture) { | 2314 | if (texture) { |
| 2250 | MaterialObject* material = static_cast<MaterialObject*>(materials.at(index).data()); | 2315 | MaterialObject* material = static_cast<MaterialObject*>(materials.at(index).data()); |
| 2251 | splatBatch.splatTextureScalesS[j] = xScale / material->getScaleS(); | 2316 | splatBatch.splatTextureScalesS[j] = scale.x / material->getScaleS(); |
| 2252 | splatBatch.splatTextureScalesT[j] = zScale / material->getScaleT(); | 2317 | splatBatch.splatTextureScalesT[j] = scale.z / material->getScaleT(); |
| 2253 | splatBatch.splatTextureIDs[j] = texture->getID(); | 2318 | splatBatch.splatTextureIDs[j] = texture->getID(); |
| 2254 | 2319 | ||
| 2255 | } else { | 2320 | } else { |
| @@ -2265,61 +2330,4 @@ void HeightfieldRenderer::render(bool cursor) { · 9 unmodified lines | |||
| 2265 | } | 2330 | } |
| 2266 | } | 2331 | } |
| 2267 | 2332 | ||
| 2268 | void HeightfieldRenderer::applyHeight(const HeightfieldHeightPointer& height) { | 2333 | QHash<HeightfieldNodeRenderer::IntPair, HeightfieldNodeRenderer::BufferPair> HeightfieldNodeRenderer::_bufferPairs; |
| 2269 | glPixelStorei(GL_UNPACK_ALIGNMENT, 1); | ||
| 2270 | glBindTexture(GL_TEXTURE_2D, _heightTextureID); | ||
| 2271 | if (height) { | ||
| 2272 | const QVector<quint16>& contents = height->getContents(); | ||
| 2273 | glTexImage2D(GL_TEXTURE_2D, 0, GL_R16, height->getWidth(), contents.size() / height->getWidth(), 0, | ||
| 2274 | GL_RED, GL_UNSIGNED_SHORT, contents.constData()); | ||
| 2275 | |||
| 2276 | } else { | ||
| 2277 | const quint16 ZERO_VALUE = 0; | ||
| 2278 | glTexImage2D(GL_TEXTURE_2D, 0, GL_R16, 1, 1, 0, GL_RED, GL_UNSIGNED_SHORT, &ZERO_VALUE); | ||
| 2279 | } | ||
| 2280 | glBindTexture(GL_TEXTURE_2D, 0); | ||
| 2281 | } | ||
| 2282 | |||
| 2283 | void HeightfieldRenderer::applyColor(const HeightfieldColorPointer& color) { | ||
| 2284 | glPixelStorei(GL_UNPACK_ALIGNMENT, 1); | ||
| 2285 | glBindTexture(GL_TEXTURE_2D, _colorTextureID); | ||
| 2286 | if (color) { | ||
| 2287 | const QByteArray& contents = color->getContents(); | ||
| 2288 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, color->getWidth(), | ||
| 2289 | contents.size() / (color->getWidth() * DataBlock::COLOR_BYTES), 0, GL_RGB, GL_UNSIGNED_BYTE, contents.constData()); | ||
| 2290 | |||
| 2291 | } else { | ||
| 2292 | const quint8 WHITE_COLOR[] = { 255, 255, 255 }; | ||
| 2293 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, 1, 1, 0, GL_RGB, GL_UNSIGNED_BYTE, WHITE_COLOR); | ||
| 2294 | } | ||
| 2295 | glBindTexture(GL_TEXTURE_2D, 0); | ||
| 2296 | } | ||
| 2297 | |||
| 2298 | void HeightfieldRenderer::applyMaterial(const HeightfieldMaterialPointer& material) { | ||
| 2299 | glPixelStorei(GL_UNPACK_ALIGNMENT, 1); | ||
| 2300 | glBindTexture(GL_TEXTURE_2D, _materialTextureID); | ||
| 2301 | if (material) { | ||
| 2302 | const QByteArray& contents = material->getContents(); | ||
| 2303 | glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, material->getWidth(), contents.size() / material->getWidth(), 0, | ||
| 2304 | GL_RED, GL_UNSIGNED_BYTE, contents.constData()); | ||
| 2305 | |||
| 2306 | const QVector<SharedObjectPointer>& materials = material->getMaterials(); | ||
| 2307 | _networkTextures.resize(materials.size()); | ||
| 2308 | for (int i = 0; i < materials.size(); i++) { | ||
| 2309 | const SharedObjectPointer& material = materials.at(i); | ||
| 2310 | if (material) { | ||
| 2311 | _networkTextures[i] = Application::getInstance()->getTextureCache()->getTexture( | ||
| 2312 | static_cast<MaterialObject*>(material.data())->getDiffuse(), SPLAT_TEXTURE); | ||
| 2313 | } else { | ||
| 2314 | _networkTextures[i].clear(); | ||
| 2315 | } | ||
| 2316 | } | ||
| 2317 | } else { | ||
| 2318 | const quint8 ZERO_VALUE = 0; | ||
| 2319 | glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, 1, 1, 0, GL_RED, GL_UNSIGNED_BYTE, &ZERO_VALUE); | ||
| 2320 | _networkTextures.clear(); | ||
| 2321 | } | ||
| 2322 | glBindTexture(GL_TEXTURE_2D, 0); | ||
| 2323 | } | ||
| 2324 | |||
| 2325 | QHash<HeightfieldRenderer::IntPair, HeightfieldRenderer::BufferPair> HeightfieldRenderer::_bufferPairs; | ||