Cached world matrix of GLVolume to speed up rendering

This commit is contained in:
Enrico Turri 2018-08-28 16:08:43 +02:00
parent cfe49a20ad
commit 4ffdb46110
2 changed files with 24 additions and 11 deletions

View File

@ -198,6 +198,8 @@ GLVolume::GLVolume(float r, float g, float b, float a)
: m_origin(0, 0, 0) : m_origin(0, 0, 0)
, m_angle_z(0.0f) , m_angle_z(0.0f)
, m_scale_factor(1.0f) , m_scale_factor(1.0f)
, m_world_matrix(Transform3f::Identity())
, m_world_matrix_dirty(true)
, m_transformed_bounding_box_dirty(true) , m_transformed_bounding_box_dirty(true)
, m_transformed_convex_hull_bounding_box_dirty(true) , m_transformed_convex_hull_bounding_box_dirty(true)
, m_convex_hull(nullptr) , m_convex_hull(nullptr)
@ -268,6 +270,7 @@ void GLVolume::set_origin(const Vec3d& origin)
if (m_origin != origin) if (m_origin != origin)
{ {
m_origin = origin; m_origin = origin;
m_world_matrix_dirty = true;
m_transformed_bounding_box_dirty = true; m_transformed_bounding_box_dirty = true;
m_transformed_convex_hull_bounding_box_dirty = true; m_transformed_convex_hull_bounding_box_dirty = true;
} }
@ -278,6 +281,7 @@ void GLVolume::set_angle_z(float angle_z)
if (m_angle_z != angle_z) if (m_angle_z != angle_z)
{ {
m_angle_z = angle_z; m_angle_z = angle_z;
m_world_matrix_dirty = true;
m_transformed_bounding_box_dirty = true; m_transformed_bounding_box_dirty = true;
m_transformed_convex_hull_bounding_box_dirty = true; m_transformed_convex_hull_bounding_box_dirty = true;
} }
@ -288,6 +292,7 @@ void GLVolume::set_scale_factor(float scale_factor)
if (m_scale_factor != scale_factor) if (m_scale_factor != scale_factor)
{ {
m_scale_factor = scale_factor; m_scale_factor = scale_factor;
m_world_matrix_dirty = true;
m_transformed_bounding_box_dirty = true; m_transformed_bounding_box_dirty = true;
m_transformed_convex_hull_bounding_box_dirty = true; m_transformed_convex_hull_bounding_box_dirty = true;
} }
@ -298,16 +303,20 @@ void GLVolume::set_convex_hull(const TriangleMesh& convex_hull)
m_convex_hull = &convex_hull; m_convex_hull = &convex_hull;
} }
Transform3f GLVolume::world_matrix() const const Transform3f& GLVolume::world_matrix() const
{ {
Transform3f matrix = Transform3f::Identity(); if (m_world_matrix_dirty)
matrix.translate(Vec3f((float)m_origin(0), (float)m_origin(1), (float)m_origin(2))); {
matrix.rotate(Eigen::AngleAxisf(m_angle_z, Vec3f::UnitZ())); m_world_matrix = Transform3f::Identity();
matrix.scale(m_scale_factor); m_world_matrix.translate(Vec3f((float)m_origin(0), (float)m_origin(1), (float)m_origin(2)));
return matrix; m_world_matrix.rotate(Eigen::AngleAxisf(m_angle_z, Vec3f::UnitZ()));
m_world_matrix.scale(m_scale_factor);
m_world_matrix_dirty = false;
}
return m_world_matrix;
} }
BoundingBoxf3 GLVolume::transformed_bounding_box() const const BoundingBoxf3& GLVolume::transformed_bounding_box() const
{ {
if (m_transformed_bounding_box_dirty) if (m_transformed_bounding_box_dirty)
{ {
@ -318,7 +327,7 @@ BoundingBoxf3 GLVolume::transformed_bounding_box() const
return m_transformed_bounding_box; return m_transformed_bounding_box;
} }
BoundingBoxf3 GLVolume::transformed_convex_hull_bounding_box() const const BoundingBoxf3& GLVolume::transformed_convex_hull_bounding_box() const
{ {
if (m_transformed_convex_hull_bounding_box_dirty) if (m_transformed_convex_hull_bounding_box_dirty)
{ {

View File

@ -260,6 +260,10 @@ private:
float m_angle_z; float m_angle_z;
// Scale factor of the volume to be rendered. // Scale factor of the volume to be rendered.
float m_scale_factor; float m_scale_factor;
// World matrix of the volume to be rendered.
mutable Transform3f m_world_matrix;
// Whether or not is needed to recalculate the world matrix.
mutable bool m_world_matrix_dirty;
// Bounding box of this volume, in unscaled coordinates. // Bounding box of this volume, in unscaled coordinates.
mutable BoundingBoxf3 m_transformed_bounding_box; mutable BoundingBoxf3 m_transformed_bounding_box;
// Whether or not is needed to recalculate the transformed bounding box. // Whether or not is needed to recalculate the transformed bounding box.
@ -334,9 +338,9 @@ public:
int volume_idx() const { return (this->composite_id / 1000) % 1000; } int volume_idx() const { return (this->composite_id / 1000) % 1000; }
int instance_idx() const { return this->composite_id % 1000; } int instance_idx() const { return this->composite_id % 1000; }
Transform3f world_matrix() const; const Transform3f& world_matrix() const;
BoundingBoxf3 transformed_bounding_box() const; const BoundingBoxf3& transformed_bounding_box() const;
BoundingBoxf3 transformed_convex_hull_bounding_box() const; const BoundingBoxf3& transformed_convex_hull_bounding_box() const;
bool empty() const { return this->indexed_vertex_array.empty(); } bool empty() const { return this->indexed_vertex_array.empty(); }
bool indexed() const { return this->indexed_vertex_array.indexed(); } bool indexed() const { return this->indexed_vertex_array.indexed(); }