diff --git a/src/slic3r/GUI/GLCanvas3D.cpp b/src/slic3r/GUI/GLCanvas3D.cpp index f76764c5f..14c1d4b8c 100644 --- a/src/slic3r/GUI/GLCanvas3D.cpp +++ b/src/slic3r/GUI/GLCanvas3D.cpp @@ -6504,7 +6504,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; @@ -6513,7 +6512,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 <= 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 a87cb68fb..6ab2f46ab 100644 --- a/src/slic3r/GUI/GLGizmo.cpp +++ b/src/slic3r/GUI/GLGizmo.cpp @@ -244,13 +244,19 @@ 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; + static const float INV_255 = 1.0f / 255.0f; - return (float)color / 255.0f; + 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 } void GLGizmoBase::render_grabbers(const BoundingBoxf3& box) const @@ -281,9 +287,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 +1485,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 +1885,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..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,7 +180,9 @@ 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; + // 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; void render_grabbers_for_picking(const BoundingBoxf3& box) const;