From dd309c9dfc69af15344a8607cdb93e5e927dfe1c Mon Sep 17 00:00:00 2001 From: Enrico Turri Date: Fri, 15 Mar 2019 10:05:14 +0100 Subject: [PATCH 1/4] GLGizmoBase::picking_color_component modified to return all the three components of the picking color --- src/slic3r/GUI/GLGizmo.cpp | 34 ++++++++++++++++++++++------------ src/slic3r/GUI/GLGizmo.hpp | 2 +- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/src/slic3r/GUI/GLGizmo.cpp b/src/slic3r/GUI/GLGizmo.cpp index a87cb68fb..e8e48b63c 100644 --- a/src/slic3r/GUI/GLGizmo.cpp +++ b/src/slic3r/GUI/GLGizmo.cpp @@ -244,13 +244,21 @@ void GLGizmoBase::update(const UpdateData& data, const GLCanvas3D::Selection& se on_update(data, selection); } -float GLGizmoBase::picking_color_component(unsigned int id) const +std::array GLGizmoBase::picking_color_component(unsigned int id) const { - int color = 254 - (int)id; - if (m_group_id > -1) - color -= m_group_id; + // Starting value for id to avoid clashing with id used by GLVolumes + static const unsigned int BASE = 254 * 255 * 255; + static const float INV_255 = 1.0f / 255.0f; - return (float)color / 255.0f; + id = BASE - id; + + std::array color; + + color[0] = (float)((id >> 16) & 0xff) * INV_255; // red + color[1] = (float)((id >> 8) & 0xff) * INV_255; // green + color[2] = (float)(id & 0xff) * INV_255; // blue + + return color; } void GLGizmoBase::render_grabbers(const BoundingBoxf3& box) const @@ -281,9 +289,10 @@ void GLGizmoBase::render_grabbers_for_picking(const BoundingBoxf3& box) const { if (m_grabbers[i].enabled) { - m_grabbers[i].color[0] = 1.0f; - m_grabbers[i].color[1] = 1.0f; - m_grabbers[i].color[2] = picking_color_component(i); + std::array color = picking_color_component(i); + m_grabbers[i].color[0] = color[0]; + m_grabbers[i].color[1] = color[1]; + m_grabbers[i].color[2] = color[2]; m_grabbers[i].render_for_picking(size); } } @@ -1478,7 +1487,7 @@ void GLGizmoFlatten::on_render_for_picking(const GLCanvas3D::Selection& selectio const_cast(this)->update_planes(); for (int i = 0; i < (int)m_planes.size(); ++i) { - ::glColor3f(1.0f, 1.0f, picking_color_component(i)); + ::glColor3fv(picking_color_component(i).data()); ::glBegin(GL_POLYGON); for (const Vec3d& vertex : m_planes[i].vertices) { @@ -1878,9 +1887,10 @@ void GLGizmoSlaSupports::render_points(const GLCanvas3D::Selection& selection, b // First decide about the color of the point. if (picking) { - render_color[0] = 1.0f; - render_color[1] = 1.0f; - render_color[2] = picking_color_component(i); + std::array color = picking_color_component(i); + render_color[0] = color[0]; + render_color[1] = color[1]; + render_color[2] = color[2]; } else { if ((m_hover_id == i && m_editing_mode)) { // ignore hover state unless editing mode is active diff --git a/src/slic3r/GUI/GLGizmo.hpp b/src/slic3r/GUI/GLGizmo.hpp index a872a161e..83a062a2c 100644 --- a/src/slic3r/GUI/GLGizmo.hpp +++ b/src/slic3r/GUI/GLGizmo.hpp @@ -175,7 +175,7 @@ protected: virtual void on_render_input_window(float x, float y, float bottom_limit, const GLCanvas3D::Selection& selection) {} #endif // ENABLE_IMGUI - float picking_color_component(unsigned int id) const; + std::array picking_color_component(unsigned int id) const; void render_grabbers(const BoundingBoxf3& box) const; void render_grabbers(float size) const; void render_grabbers_for_picking(const BoundingBoxf3& box) const; From 32c9e8b168277e0d916c2f1eb1b69d1dc0c34139 Mon Sep 17 00:00:00 2001 From: Lukas Matena Date: Fri, 15 Mar 2019 10:15:23 +0100 Subject: [PATCH 2/4] A small fix of the gizmo grabbers picking --- src/slic3r/GUI/GLGizmo.cpp | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/slic3r/GUI/GLGizmo.cpp b/src/slic3r/GUI/GLGizmo.cpp index e8e48b63c..359deed3a 100644 --- a/src/slic3r/GUI/GLGizmo.cpp +++ b/src/slic3r/GUI/GLGizmo.cpp @@ -252,13 +252,12 @@ std::array GLGizmoBase::picking_color_component(unsigned int id) const id = BASE - id; - std::array color; + if (m_group_id > -1) + id -= m_group_id; - color[0] = (float)((id >> 16) & 0xff) * INV_255; // red - color[1] = (float)((id >> 8) & 0xff) * INV_255; // green - color[2] = (float)(id & 0xff) * INV_255; // blue - - return color; + return std::array { (float)((id >> 16) & 0xff) * INV_255, // red + (float)((id >> 8) & 0xff) * INV_255, // green + (float)(id & 0xff) * INV_255}; // blue } void GLGizmoBase::render_grabbers(const BoundingBoxf3& box) const From ef939905b198460dc1e8c8ab0661dbf2518ff4c4 Mon Sep 17 00:00:00 2001 From: Lukas Matena Date: Fri, 15 Mar 2019 11:04:08 +0100 Subject: [PATCH 3/4] Another fix of the gizmo grabber color picking --- src/slic3r/GUI/GLCanvas3D.cpp | 3 +-- src/slic3r/GUI/GLGizmo.cpp | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/slic3r/GUI/GLCanvas3D.cpp b/src/slic3r/GUI/GLCanvas3D.cpp index 33f460802..3100a024b 100644 --- a/src/slic3r/GUI/GLCanvas3D.cpp +++ b/src/slic3r/GUI/GLCanvas3D.cpp @@ -6286,7 +6286,6 @@ void GLCanvas3D::_picking_pass() const ::glReadPixels(pos(0), cnv_size.get_height() - pos(1) - 1, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, (void*)color); volume_id = color[0] + color[1] * 256 + color[2] * 256 * 256; } - if ((0 <= volume_id) && (volume_id < (int)m_volumes.volumes.size())) { m_hover_volume_id = volume_id; @@ -6295,7 +6294,7 @@ void GLCanvas3D::_picking_pass() const else { m_hover_volume_id = -1; - m_gizmos.set_hover_id(inside ? (254 - (int)color[2]) : -1); + m_gizmos.set_hover_id(inside && volume_id <= 254 * 255 * 255 ? (254 * 255 * 255 - volume_id) : -1); } _update_volumes_hover_state(); diff --git a/src/slic3r/GUI/GLGizmo.cpp b/src/slic3r/GUI/GLGizmo.cpp index 359deed3a..19d50e2e0 100644 --- a/src/slic3r/GUI/GLGizmo.cpp +++ b/src/slic3r/GUI/GLGizmo.cpp @@ -255,9 +255,9 @@ std::array GLGizmoBase::picking_color_component(unsigned int id) const if (m_group_id > -1) id -= m_group_id; - return std::array { (float)((id >> 16) & 0xff) * INV_255, // red + return std::array { (float)((id >> 0) & 0xff) * INV_255, // red (float)((id >> 8) & 0xff) * INV_255, // green - (float)(id & 0xff) * INV_255}; // blue + (float)((id >> 16)& 0xff) * INV_255}; // blue } void GLGizmoBase::render_grabbers(const BoundingBoxf3& box) const From bc3036d777fb6ca7c937c28d8aa9b9131a376c20 Mon Sep 17 00:00:00 2001 From: Enrico Turri Date: Fri, 15 Mar 2019 12:07:25 +0100 Subject: [PATCH 4/4] Follow-up to previous commits on gizmo grabbers picking (use of centralized static constant for ids and added comments) --- src/slic3r/GUI/GLCanvas3D.cpp | 2 +- src/slic3r/GUI/GLGizmo.cpp | 7 +++---- src/slic3r/GUI/GLGizmo.hpp | 7 +++++++ 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/slic3r/GUI/GLCanvas3D.cpp b/src/slic3r/GUI/GLCanvas3D.cpp index 3100a024b..dac370b54 100644 --- a/src/slic3r/GUI/GLCanvas3D.cpp +++ b/src/slic3r/GUI/GLCanvas3D.cpp @@ -6294,7 +6294,7 @@ void GLCanvas3D::_picking_pass() const else { m_hover_volume_id = -1; - m_gizmos.set_hover_id(inside && volume_id <= 254 * 255 * 255 ? (254 * 255 * 255 - volume_id) : -1); + m_gizmos.set_hover_id(inside && volume_id <= GLGizmoBase::BASE_ID ? (GLGizmoBase::BASE_ID - volume_id) : -1); } _update_volumes_hover_state(); diff --git a/src/slic3r/GUI/GLGizmo.cpp b/src/slic3r/GUI/GLGizmo.cpp index 19d50e2e0..6ab2f46ab 100644 --- a/src/slic3r/GUI/GLGizmo.cpp +++ b/src/slic3r/GUI/GLGizmo.cpp @@ -246,18 +246,17 @@ void GLGizmoBase::update(const UpdateData& data, const GLCanvas3D::Selection& se std::array GLGizmoBase::picking_color_component(unsigned int id) const { - // Starting value for id to avoid clashing with id used by GLVolumes - static const unsigned int BASE = 254 * 255 * 255; static const float INV_255 = 1.0f / 255.0f; - id = BASE - id; + id = BASE_ID - id; if (m_group_id > -1) id -= m_group_id; + // color components are encoded to match the calculation of volume_id made into GLCanvas3D::_picking_pass() return std::array { (float)((id >> 0) & 0xff) * INV_255, // red (float)((id >> 8) & 0xff) * INV_255, // green - (float)((id >> 16)& 0xff) * INV_255}; // blue + (float)((id >> 16) & 0xff) * INV_255 }; // blue } void GLGizmoBase::render_grabbers(const BoundingBoxf3& box) const diff --git a/src/slic3r/GUI/GLGizmo.hpp b/src/slic3r/GUI/GLGizmo.hpp index 83a062a2c..4e44da540 100644 --- a/src/slic3r/GUI/GLGizmo.hpp +++ b/src/slic3r/GUI/GLGizmo.hpp @@ -35,6 +35,11 @@ class ImGuiWrapper; class GLGizmoBase { +public: + // Starting value for ids to avoid clashing with ids used by GLVolumes + // (254 is choosen to leave some space for forward compatibility) + static const unsigned int BASE_ID = 255 * 255 * 254; + protected: struct Grabber { @@ -175,6 +180,8 @@ protected: virtual void on_render_input_window(float x, float y, float bottom_limit, const GLCanvas3D::Selection& selection) {} #endif // ENABLE_IMGUI + // Returns the picking color for the given id, based on the BASE_ID constant + // No check is made for clashing with other picking color (i.e. GLVolumes) std::array picking_color_component(unsigned int id) const; void render_grabbers(const BoundingBoxf3& box) const; void render_grabbers(float size) const;