From 4ffdb46110774a4139fc00eb4f16adb683bcff7c Mon Sep 17 00:00:00 2001 From: Enrico Turri Date: Tue, 28 Aug 2018 16:08:43 +0200 Subject: [PATCH] Cached world matrix of GLVolume to speed up rendering --- xs/src/slic3r/GUI/3DScene.cpp | 25 +++++++++++++++++-------- xs/src/slic3r/GUI/3DScene.hpp | 10 +++++++--- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/xs/src/slic3r/GUI/3DScene.cpp b/xs/src/slic3r/GUI/3DScene.cpp index c9607ccf9..08a1b1457 100644 --- a/xs/src/slic3r/GUI/3DScene.cpp +++ b/xs/src/slic3r/GUI/3DScene.cpp @@ -198,6 +198,8 @@ GLVolume::GLVolume(float r, float g, float b, float a) : m_origin(0, 0, 0) , m_angle_z(0.0f) , m_scale_factor(1.0f) + , m_world_matrix(Transform3f::Identity()) + , m_world_matrix_dirty(true) , m_transformed_bounding_box_dirty(true) , m_transformed_convex_hull_bounding_box_dirty(true) , m_convex_hull(nullptr) @@ -268,6 +270,7 @@ void GLVolume::set_origin(const Vec3d& origin) if (m_origin != origin) { m_origin = origin; + m_world_matrix_dirty = true; m_transformed_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) { m_angle_z = angle_z; + m_world_matrix_dirty = true; m_transformed_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) { m_scale_factor = scale_factor; + m_world_matrix_dirty = true; m_transformed_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; } -Transform3f GLVolume::world_matrix() const +const Transform3f& GLVolume::world_matrix() const { - Transform3f matrix = Transform3f::Identity(); - matrix.translate(Vec3f((float)m_origin(0), (float)m_origin(1), (float)m_origin(2))); - matrix.rotate(Eigen::AngleAxisf(m_angle_z, Vec3f::UnitZ())); - matrix.scale(m_scale_factor); - return matrix; + if (m_world_matrix_dirty) + { + m_world_matrix = Transform3f::Identity(); + m_world_matrix.translate(Vec3f((float)m_origin(0), (float)m_origin(1), (float)m_origin(2))); + 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) { @@ -318,7 +327,7 @@ BoundingBoxf3 GLVolume::transformed_bounding_box() const 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) { diff --git a/xs/src/slic3r/GUI/3DScene.hpp b/xs/src/slic3r/GUI/3DScene.hpp index b4aa6ce95..69f1e1d35 100644 --- a/xs/src/slic3r/GUI/3DScene.hpp +++ b/xs/src/slic3r/GUI/3DScene.hpp @@ -260,6 +260,10 @@ private: float m_angle_z; // Scale factor of the volume to be rendered. 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. mutable BoundingBoxf3 m_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 instance_idx() const { return this->composite_id % 1000; } - Transform3f world_matrix() const; - BoundingBoxf3 transformed_bounding_box() const; - BoundingBoxf3 transformed_convex_hull_bounding_box() const; + const Transform3f& world_matrix() const; + const BoundingBoxf3& transformed_bounding_box() const; + const BoundingBoxf3& transformed_convex_hull_bounding_box() const; bool empty() const { return this->indexed_vertex_array.empty(); } bool indexed() const { return this->indexed_vertex_array.indexed(); }