Skip to content

Commit

Permalink
RENDER: optimized GridRenderer - the grid is not re-created each frame
Browse files Browse the repository at this point in the history
  • Loading branch information
mgerhardy committed Nov 14, 2024
1 parent d4e9221 commit 180c557
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 24 deletions.
45 changes: 28 additions & 17 deletions src/modules/render/GridRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<float> &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<float>& aabb) {
void GridRenderer::update(const math::AABB<float> &aabb) {
if (!_dirty && _aabb == aabb) {
return;
}
Expand Down Expand Up @@ -78,21 +103,7 @@ void GridRenderer::update(const math::AABB<float>& 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;
}
Expand Down
13 changes: 11 additions & 2 deletions src/modules/render/GridRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ class GridRenderer {
int32_t _gridMeshIndexYZNear = -1;
int32_t _gridMeshIndexYZFar = -1;
int32_t _array = -1;
void createForwardArrow(const math::AABB<float> &aabb);

int _resolution = 1;
int _resolution = -1;
bool _renderAABB;
bool _renderGrid;
bool _dirty = false;
Expand All @@ -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<float>& region);
void update(const math::AABB<float> &region);
void clear();
void setColor(const glm::vec4 &color);

Expand All @@ -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;
}

}
10 changes: 10 additions & 0 deletions src/modules/video/ShapeBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#include "core/ArrayLength.h"
#include "video/Types.h"
#include <glm/geometric.hpp>
#include <glm/gtc/epsilon.hpp>
#include <glm/vector_relational.hpp>
#ifndef GLM_ENABLE_EXPERIMENTAL
#define GLM_ENABLE_EXPERIMENTAL
#endif
Expand Down Expand Up @@ -104,6 +106,14 @@ void ShapeBuilder::arrow(const glm::vec3 &left, const glm::vec3 &center, const g
line(center, right, thickness);
}

bool ShapeBuilder::setColor(const glm::vec4& color) {
if (glm::all(glm::epsilonEqual(_color, color, glm::epsilon<float>()))) {
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);
Expand Down
6 changes: 1 addition & 5 deletions src/modules/video/ShapeBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -218,10 +218,6 @@ inline void ShapeBuilder::aabb(const math::AABB<int>& 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;
}
Expand Down

0 comments on commit 180c557

Please sign in to comment.