diff --git a/src/modules/render/GridRenderer.cpp b/src/modules/render/GridRenderer.cpp index 1790c5992d..b667bb3c4c 100644 --- a/src/modules/render/GridRenderer.cpp +++ b/src/modules/render/GridRenderer.cpp @@ -31,6 +31,9 @@ bool GridRenderer::setGridResolution(int resolution) { if (resolution < 1) { return false; } + if (_resolution == resolution) { + return false; + } _resolution = resolution; _dirty = true; return true; @@ -41,10 +44,32 @@ int GridRenderer::gridResolution() const { } void GridRenderer::setColor(const glm::vec4 &color) { - _shapeBuilder.setColor(color); + if (_shapeBuilder.setColor(color)) { + _dirty = true; + } +} + +void GridRenderer::createForwardArrow(const math::AABB &aabb) { + if (!aabb.isValid() || aabb.isEmpty()) { + return; + } + _shapeBuilder.clear(); + const float arrowSize = 10.0f; + const float forward = glm::forward().z * arrowSize; + const float left = glm::left().x * arrowSize; + const float right = glm::right().x * arrowSize; + const float arrowX = aabb.getCenterX(); + const float arrowY = aabb.getLowerY(); + const float arrowZ = aabb.getLowerZ(); + const glm::vec3 point1{arrowX + left, arrowY, arrowZ + forward}; + const glm::vec3 point2{arrowX, arrowY, arrowZ + 2.0f * forward}; + const glm::vec3 point3{arrowX + right, arrowY, arrowZ + forward}; + _shapeBuilder.arrow(point1, point2, point3); + _shapeRenderer.createOrUpdate(_array, _shapeBuilder); + _shapeRenderer.hide(_array, true); } -void GridRenderer::update(const math::AABB& aabb) { +void GridRenderer::update(const math::AABB &aabb) { if (!_dirty && _aabb == aabb) { return; } @@ -78,21 +103,7 @@ void GridRenderer::update(const math::AABB& aabb) { _shapeBuilder.aabbGridYZ(aabb, true, (float)_resolution, thickness); _shapeRenderer.createOrUpdate(_gridMeshIndexYZNear, _shapeBuilder); - _shapeBuilder.clear(); - - const float arrowSize = 10.0f; - const float forward = glm::forward().z * arrowSize; - const float left = glm::left().x * arrowSize; - const float right = glm::right().x * arrowSize; - const float arrowX = aabb.getCenterX(); - const float arrowY = aabb.getLowerY(); - const float arrowZ = aabb.getLowerZ(); - const glm::vec3 point1{arrowX + left, arrowY, arrowZ + forward}; - const glm::vec3 point2{arrowX, arrowY, arrowZ + 2.0f * forward}; - const glm::vec3 point3{arrowX + right, arrowY, arrowZ + forward}; - _shapeBuilder.arrow(point1, point2, point3); - _shapeRenderer.createOrUpdate(_array, _shapeBuilder); - _shapeRenderer.hide(_array, true); + createForwardArrow(aabb); _dirty = false; } diff --git a/src/modules/render/GridRenderer.h b/src/modules/render/GridRenderer.h index 7a95b8a254..2adda111df 100644 --- a/src/modules/render/GridRenderer.h +++ b/src/modules/render/GridRenderer.h @@ -35,8 +35,9 @@ class GridRenderer { int32_t _gridMeshIndexYZNear = -1; int32_t _gridMeshIndexYZFar = -1; int32_t _array = -1; + void createForwardArrow(const math::AABB &aabb); - int _resolution = 1; + int _resolution = -1; bool _renderAABB; bool _renderGrid; bool _dirty = false; @@ -62,7 +63,7 @@ class GridRenderer { * @brief Update the internal render buffers for the new region. * @param region The region to render the grid for */ - void update(const math::AABB& region); + void update(const math::AABB ®ion); void clear(); void setColor(const glm::vec4 &color); @@ -83,11 +84,19 @@ inline bool GridRenderer::renderGrid() const { } inline void GridRenderer::setRenderAABB(bool renderAABB) { + if (_renderAABB == renderAABB) { + return; + } _renderAABB = renderAABB; + _dirty = true; } inline void GridRenderer::setRenderGrid(bool renderGrid) { + if (_renderGrid == renderGrid) { + return; + } _renderGrid = renderGrid; + _dirty = true; } } diff --git a/src/modules/video/ShapeBuilder.cpp b/src/modules/video/ShapeBuilder.cpp index 7ea76f85d7..f0a07f72ef 100644 --- a/src/modules/video/ShapeBuilder.cpp +++ b/src/modules/video/ShapeBuilder.cpp @@ -13,6 +13,8 @@ #include "core/ArrayLength.h" #include "video/Types.h" #include +#include +#include #ifndef GLM_ENABLE_EXPERIMENTAL #define GLM_ENABLE_EXPERIMENTAL #endif @@ -104,6 +106,14 @@ void ShapeBuilder::arrow(const glm::vec3 &left, const glm::vec3 ¢er, const g line(center, right, thickness); } +bool ShapeBuilder::setColor(const glm::vec4& color) { + if (glm::all(glm::epsilonEqual(_color, color, glm::epsilon()))) { + return false; + } + _color = color; + return true; +} + void ShapeBuilder::line(const glm::vec3& start, const glm::vec3& end, float thickness) { if (thickness <= 1.0f) { setPrimitive(Primitive::Lines); diff --git a/src/modules/video/ShapeBuilder.h b/src/modules/video/ShapeBuilder.h index c5f616d5c2..e6e6251e37 100644 --- a/src/modules/video/ShapeBuilder.h +++ b/src/modules/video/ShapeBuilder.h @@ -203,7 +203,7 @@ class ShapeBuilder { const Colors& getColors() const; const Texcoords& getTexcoords() const; - void setColor(const glm::vec4& color); + bool setColor(const glm::vec4& color); void setPosition(const glm::vec3& position); void setRotation(const glm::mat3& rotation); void resetRotation(); @@ -218,10 +218,6 @@ inline void ShapeBuilder::aabb(const math::AABB& aabb, bool renderGrid, flo this->aabb(converted, renderGrid, stepWidth, thickness); } -inline void ShapeBuilder::setColor(const glm::vec4& color) { - _color = color; -} - inline void ShapeBuilder::setPosition(const glm::vec3& position) { _position = position; }