Tech ENABLE_GLBEGIN_GLEND_REMOVAL - 1st installment - Selection bounding box

(cherry picked from commit prusa3d/PrusaSlicer@22f38235ea)
This commit is contained in:
enricoturri1966 2023-10-20 20:28:27 +08:00 committed by Noisyfox
parent 28d0147d09
commit 7f0c095446
8 changed files with 148 additions and 79 deletions

View file

@ -0,0 +1,8 @@
#version 110
uniform vec4 uniform_color;
void main()
{
gl_FragColor = uniform_color;
}

View file

@ -0,0 +1,6 @@
#version 110
void main()
{
gl_Position = ftransform();
}

View file

@ -6935,7 +6935,7 @@ void GLCanvas3D::_render_gcode(int canvas_width, int canvas_height)
} }
} }
void GLCanvas3D::_render_selection() const void GLCanvas3D::_render_selection()
{ {
float scale_factor = 1.0; float scale_factor = 1.0;
#if ENABLE_RETINA_GL #if ENABLE_RETINA_GL
@ -6966,7 +6966,7 @@ void GLCanvas3D::_render_sequential_clearance()
} }
#if ENABLE_RENDER_SELECTION_CENTER #if ENABLE_RENDER_SELECTION_CENTER
void GLCanvas3D::_render_selection_center() const void GLCanvas3D::_render_selection_center()
{ {
m_selection.render_center(m_gizmos.is_dragging()); m_selection.render_center(m_gizmos.is_dragging());
} }

View file

@ -1115,10 +1115,10 @@ private:
void _render_gcode(int canvas_width, int canvas_height); void _render_gcode(int canvas_width, int canvas_height);
//BBS: render a plane for assemble //BBS: render a plane for assemble
void _render_plane() const; void _render_plane() const;
void _render_selection() const; void _render_selection();
void _render_sequential_clearance(); void _render_sequential_clearance();
#if ENABLE_RENDER_SELECTION_CENTER #if ENABLE_RENDER_SELECTION_CENTER
void _render_selection_center() const; void _render_selection_center();
#endif // ENABLE_RENDER_SELECTION_CENTER #endif // ENABLE_RENDER_SELECTION_CENTER
void _check_and_update_toolbar_icon_scale(); void _check_and_update_toolbar_icon_scale();
void _render_overlays(); void _render_overlays();

View file

@ -33,6 +33,8 @@ std::pair<bool, std::string> GLShadersManager::init()
bool valid = true; bool valid = true;
// basic shader, used to render selection bbox
valid &= append_shader("flat", { "flat.vs", "flat.fs" });
// used to render bed axes and model, selection hints, gcode sequential view marker model, preview shells, options in gcode preview // used to render bed axes and model, selection hints, gcode sequential view marker model, preview shells, options in gcode preview
valid &= append_shader("gouraud_light", { "gouraud_light.vs", "gouraud_light.fs" }); valid &= append_shader("gouraud_light", { "gouraud_light.vs", "gouraud_light.fs" });
//used to render thumbnail //used to render thumbnail

View file

@ -131,8 +131,8 @@ void GLGizmoMeshBoolean::on_render()
} }
} }
float src_color[3] = { 1.0f, 1.0f, 1.0f }; ColorRGB src_color = { 1.0f, 1.0f, 1.0f };
float tool_color[3] = { 0.0f, 150.0f / 255.0f, 136.0f / 255.0f }; ColorRGB tool_color = {0.0f, 150.0f / 255.0f, 136.0f / 255.0f};
m_parent.get_selection().render_bounding_box(src_bb, src_color, m_parent.get_scale()); m_parent.get_selection().render_bounding_box(src_bb, src_color, m_parent.get_scale());
m_parent.get_selection().render_bounding_box(tool_bb, tool_color, m_parent.get_scale()); m_parent.get_selection().render_bounding_box(tool_bb, tool_color, m_parent.get_scale());
} }

View file

@ -134,9 +134,8 @@ bool Selection::init()
{ {
m_arrow.init_from(straight_arrow(10.0f, 5.0f, 5.0f, 10.0f, 1.0f)); m_arrow.init_from(straight_arrow(10.0f, 5.0f, 5.0f, 10.0f, 1.0f));
m_curved_arrow.init_from(circular_arrow(16, 10.0f, 5.0f, 10.0f, 5.0f, 1.0f)); m_curved_arrow.init_from(circular_arrow(16, 10.0f, 5.0f, 10.0f, 5.0f, 1.0f));
#if ENABLE_RENDER_SELECTION_CENTER #if ENABLE_RENDER_SELECTION_CENTER
m_vbo_sphere.init_from(make_sphere(0.75, 2*PI/24)); m_vbo_sphere.init_from(its_make_sphere(0.75, PI / 12.0));
#endif // ENABLE_RENDER_SELECTION_CENTER #endif // ENABLE_RENDER_SELECTION_CENTER
return true; return true;
@ -1554,20 +1553,18 @@ void Selection::erase()
} }
} }
void Selection::render(float scale_factor) const void Selection::render(float scale_factor)
{ {
if (!m_valid || is_empty()) if (!m_valid || is_empty())
return; return;
*const_cast<float*>(&m_scale_factor) = scale_factor; m_scale_factor = scale_factor;
render_bounding_box(get_bounding_box(), ColorRGB::WHITE());
// render cumulative bounding box of selected volumes
render_selected_volumes();
render_synchronized_volumes(); render_synchronized_volumes();
} }
#if ENABLE_RENDER_SELECTION_CENTER #if ENABLE_RENDER_SELECTION_CENTER
void Selection::render_center(bool gizmo_is_dragging) const void Selection::render_center(bool gizmo_is_dragging)
{ {
if (!m_valid || is_empty()) if (!m_valid || is_empty())
return; return;
@ -1576,10 +1573,17 @@ void Selection::render_center(bool gizmo_is_dragging) const
glsafe(::glDisable(GL_DEPTH_TEST)); glsafe(::glDisable(GL_DEPTH_TEST));
glsafe(::glColor3f(1.0f, 1.0f, 1.0f));
glsafe(::glPushMatrix()); glsafe(::glPushMatrix());
glsafe(::glTranslated(center(0), center(1), center(2))); glsafe(::glTranslated(center.x(), center.y(), center.z()));
GLShaderProgram* shader = wxGetApp().get_shader("flat");
if (shader == nullptr)
return;
shader->start_using();
m_vbo_sphere.set_color(-1, ColorRGBA::WHITE());
m_vbo_sphere.render(); m_vbo_sphere.render();
shader->stop_using();
glsafe(::glPopMatrix()); glsafe(::glPopMatrix());
} }
#endif // ENABLE_RENDER_SELECTION_CENTER #endif // ENABLE_RENDER_SELECTION_CENTER
@ -2133,91 +2137,140 @@ void Selection::do_remove_object(unsigned int object_idx)
} }
} }
void Selection::render_selected_volumes() const void Selection::render_synchronized_volumes()
{
float color[3] = { 1.0f, 1.0f, 1.0f };
render_bounding_box(get_bounding_box(), color);
}
void Selection::render_synchronized_volumes() const
{ {
if (m_mode == Instance) if (m_mode == Instance)
return; return;
float color[3] = { 1.0f, 1.0f, 0.0f };
for (unsigned int i : m_list) { for (unsigned int i : m_list) {
const GLVolume* volume = (*m_volumes)[i]; const GLVolume& volume = *(*m_volumes)[i];
int object_idx = volume->object_idx(); int object_idx = volume.object_idx();
int volume_idx = volume->volume_idx(); int volume_idx = volume.volume_idx();
for (unsigned int j = 0; j < (unsigned int)m_volumes->size(); ++j) { for (unsigned int j = 0; j < (unsigned int)m_volumes->size(); ++j) {
if (i == j) if (i == j)
continue; continue;
const GLVolume* v = (*m_volumes)[j]; const GLVolume& v = *(*m_volumes)[j];
if (v->object_idx() != object_idx || v->volume_idx() != volume_idx) if (v.object_idx() != object_idx || v.volume_idx() != volume_idx)
continue; continue;
render_bounding_box(v->transformed_convex_hull_bounding_box(), color); render_bounding_box(v.transformed_convex_hull_bounding_box(), ColorRGB::YELLOW());
} }
} }
} }
void Selection::render_bounding_box(const BoundingBoxf3& box, float* color) const void Selection::render_bounding_box(const BoundingBoxf3& box, const ColorRGB& color)
{ {
if (color == nullptr)
return;
Vec3f b_min = box.min.cast<float>(); auto is_approx = [](const Vec3d& v1, const Vec3d& v2) {
Vec3f b_max = box.max.cast<float>(); for (int i = 0; i < 3; ++i) {
Vec3f size = 0.2f * box.size().cast<float>(); if (std::abs(v1[i] - v2[i]) > EPSILON)
return false;
}
return true;
};
const BoundingBoxf3& curr_box = m_box.get_bounding_box();
if (!m_box.is_initialized() || !is_approx(box.min, curr_box.min) || !is_approx(box.max, curr_box.max)) {
m_box.reset();
const Vec3f b_min = box.min.cast<float>();
const Vec3f b_max = box.max.cast<float>();
const Vec3f size = 0.2f * box.size().cast<float>();
GLModel::InitializationData init_data;
GUI::GLModel::InitializationData::Entity entity;
entity.type = GUI::GLModel::PrimitiveType::Lines;
entity.positions.reserve(48);
entity.positions.emplace_back(Vec3f(b_min.x(), b_min.y(), b_min.z()));
entity.positions.emplace_back(Vec3f(b_min.x() + size.x(), b_min.y(), b_min.z()));
entity.positions.emplace_back(Vec3f(b_min.x(), b_min.y(), b_min.z()));
entity.positions.emplace_back(Vec3f(b_min.x(), b_min.y() + size.y(), b_min.z()));
entity.positions.emplace_back(Vec3f(b_min.x(), b_min.y(), b_min.z()));
entity.positions.emplace_back(Vec3f(b_min.x(), b_min.y(), b_min.z() + size.z()));
entity.positions.emplace_back(Vec3f(b_max.x(), b_min.y(), b_min.z()));
entity.positions.emplace_back(Vec3f(b_max.x() - size.x(), b_min.y(), b_min.z()));
entity.positions.emplace_back(Vec3f(b_max.x(), b_min.y(), b_min.z()));
entity.positions.emplace_back(Vec3f(b_max.x(), b_min.y() + size.y(), b_min.z()));
entity.positions.emplace_back(Vec3f(b_max.x(), b_min.y(), b_min.z()));
entity.positions.emplace_back(Vec3f(b_max.x(), b_min.y(), b_min.z() + size.z()));
entity.positions.emplace_back(Vec3f(b_max.x(), b_max.y(), b_min.z()));
entity.positions.emplace_back(Vec3f(b_max.x() - size.x(), b_max.y(), b_min.z()));
entity.positions.emplace_back(Vec3f(b_max.x(), b_max.y(), b_min.z()));
entity.positions.emplace_back(Vec3f(b_max.x(), b_max.y() - size.y(), b_min.z()));
entity.positions.emplace_back(Vec3f(b_max.x(), b_max.y(), b_min.z()));
entity.positions.emplace_back(Vec3f(b_max.x(), b_max.y(), b_min.z() + size.z()));
entity.positions.emplace_back(Vec3f(b_min.x(), b_max.y(), b_min.z()));
entity.positions.emplace_back(Vec3f(b_min.x() + size.x(), b_max.y(), b_min.z()));
entity.positions.emplace_back(Vec3f(b_min.x(), b_max.y(), b_min.z()));
entity.positions.emplace_back(Vec3f(b_min.x(), b_max.y() - size.y(), b_min.z()));
entity.positions.emplace_back(Vec3f(b_min.x(), b_max.y(), b_min.z()));
entity.positions.emplace_back(Vec3f(b_min.x(), b_max.y(), b_min.z() + size.z()));
entity.positions.emplace_back(Vec3f(b_min.x(), b_min.y(), b_max.z()));
entity.positions.emplace_back(Vec3f(b_min.x() + size.x(), b_min.y(), b_max.z()));
entity.positions.emplace_back(Vec3f(b_min.x(), b_min.y(), b_max.z()));
entity.positions.emplace_back(Vec3f(b_min.x(), b_min.y() + size.y(), b_max.z()));
entity.positions.emplace_back(Vec3f(b_min.x(), b_min.y(), b_max.z()));
entity.positions.emplace_back(Vec3f(b_min.x(), b_min.y(), b_max.z() - size.z()));
entity.positions.emplace_back(Vec3f(b_max.x(), b_min.y(), b_max.z()));
entity.positions.emplace_back(Vec3f(b_max.x() - size.x(), b_min.y(), b_max.z()));
entity.positions.emplace_back(Vec3f(b_max.x(), b_min.y(), b_max.z()));
entity.positions.emplace_back(Vec3f(b_max.x(), b_min.y() + size.y(), b_max.z()));
entity.positions.emplace_back(Vec3f(b_max.x(), b_min.y(), b_max.z()));
entity.positions.emplace_back(Vec3f(b_max.x(), b_min.y(), b_max.z() - size.z()));
entity.positions.emplace_back(Vec3f(b_max.x(), b_max.y(), b_max.z()));
entity.positions.emplace_back(Vec3f(b_max.x() - size.x(), b_max.y(), b_max.z()));
entity.positions.emplace_back(Vec3f(b_max.x(), b_max.y(), b_max.z()));
entity.positions.emplace_back(Vec3f(b_max.x(), b_max.y() - size.y(), b_max.z()));
entity.positions.emplace_back(Vec3f(b_max.x(), b_max.y(), b_max.z()));
entity.positions.emplace_back(Vec3f(b_max.x(), b_max.y(), b_max.z() - size.z()));
entity.positions.emplace_back(Vec3f(b_min.x(), b_max.y(), b_max.z()));
entity.positions.emplace_back(Vec3f(b_min.x() + size.x(), b_max.y(), b_max.z()));
entity.positions.emplace_back(Vec3f(b_min.x(), b_max.y(), b_max.z()));
entity.positions.emplace_back(Vec3f(b_min.x(), b_max.y() - size.y(), b_max.z()));
entity.positions.emplace_back(Vec3f(b_min.x(), b_max.y(), b_max.z()));
entity.positions.emplace_back(Vec3f(b_min.x(), b_max.y(), b_max.z() - size.z()));
entity.normals.reserve(48);
for (size_t i = 0; i < 48; ++i) {
entity.normals.emplace_back(Vec3f::UnitZ());
}
entity.indices.reserve(48);
for (size_t i = 0; i < 48; ++i) {
entity.indices.emplace_back(i);
}
init_data.entities.emplace_back(entity);
m_box.init_from(init_data);
}
glsafe(::glEnable(GL_DEPTH_TEST)); glsafe(::glEnable(GL_DEPTH_TEST));
glsafe(::glColor3fv(color));
glsafe(::glLineWidth(2.0f * m_scale_factor)); glsafe(::glLineWidth(2.0f * m_scale_factor));
::glBegin(GL_LINES); GLShaderProgram* shader = wxGetApp().get_shader("flat");
if (shader == nullptr)
return;
::glVertex3f(b_min(0), b_min(1), b_min(2)); ::glVertex3f(b_min(0) + size(0), b_min(1), b_min(2)); shader->start_using();
::glVertex3f(b_min(0), b_min(1), b_min(2)); ::glVertex3f(b_min(0), b_min(1) + size(1), b_min(2)); m_box.set_color(-1, to_rgba(color));
::glVertex3f(b_min(0), b_min(1), b_min(2)); ::glVertex3f(b_min(0), b_min(1), b_min(2) + size(2)); m_box.render();
shader->stop_using();
::glVertex3f(b_max(0), b_min(1), b_min(2)); ::glVertex3f(b_max(0) - size(0), b_min(1), b_min(2));
::glVertex3f(b_max(0), b_min(1), b_min(2)); ::glVertex3f(b_max(0), b_min(1) + size(1), b_min(2));
::glVertex3f(b_max(0), b_min(1), b_min(2)); ::glVertex3f(b_max(0), b_min(1), b_min(2) + size(2));
::glVertex3f(b_max(0), b_max(1), b_min(2)); ::glVertex3f(b_max(0) - size(0), b_max(1), b_min(2));
::glVertex3f(b_max(0), b_max(1), b_min(2)); ::glVertex3f(b_max(0), b_max(1) - size(1), b_min(2));
::glVertex3f(b_max(0), b_max(1), b_min(2)); ::glVertex3f(b_max(0), b_max(1), b_min(2) + size(2));
::glVertex3f(b_min(0), b_max(1), b_min(2)); ::glVertex3f(b_min(0) + size(0), b_max(1), b_min(2));
::glVertex3f(b_min(0), b_max(1), b_min(2)); ::glVertex3f(b_min(0), b_max(1) - size(1), b_min(2));
::glVertex3f(b_min(0), b_max(1), b_min(2)); ::glVertex3f(b_min(0), b_max(1), b_min(2) + size(2));
::glVertex3f(b_min(0), b_min(1), b_max(2)); ::glVertex3f(b_min(0) + size(0), b_min(1), b_max(2));
::glVertex3f(b_min(0), b_min(1), b_max(2)); ::glVertex3f(b_min(0), b_min(1) + size(1), b_max(2));
::glVertex3f(b_min(0), b_min(1), b_max(2)); ::glVertex3f(b_min(0), b_min(1), b_max(2) - size(2));
::glVertex3f(b_max(0), b_min(1), b_max(2)); ::glVertex3f(b_max(0) - size(0), b_min(1), b_max(2));
::glVertex3f(b_max(0), b_min(1), b_max(2)); ::glVertex3f(b_max(0), b_min(1) + size(1), b_max(2));
::glVertex3f(b_max(0), b_min(1), b_max(2)); ::glVertex3f(b_max(0), b_min(1), b_max(2) - size(2));
::glVertex3f(b_max(0), b_max(1), b_max(2)); ::glVertex3f(b_max(0) - size(0), b_max(1), b_max(2));
::glVertex3f(b_max(0), b_max(1), b_max(2)); ::glVertex3f(b_max(0), b_max(1) - size(1), b_max(2));
::glVertex3f(b_max(0), b_max(1), b_max(2)); ::glVertex3f(b_max(0), b_max(1), b_max(2) - size(2));
::glVertex3f(b_min(0), b_max(1), b_max(2)); ::glVertex3f(b_min(0) + size(0), b_max(1), b_max(2));
::glVertex3f(b_min(0), b_max(1), b_max(2)); ::glVertex3f(b_min(0), b_max(1) - size(1), b_max(2));
::glVertex3f(b_min(0), b_max(1), b_max(2)); ::glVertex3f(b_min(0), b_max(1), b_max(2) - size(2));
glsafe(::glEnd());
} }
static ColorRGBA get_color(Axis axis) static ColorRGBA get_color(Axis axis)
{ {
return GLGizmoBase::AXES_COLOR[axis]; return GLGizmoBase::AXES_COLOR[axis];
}; }
void Selection::render_sidebar_position_hints(const std::string& sidebar_field) const void Selection::render_sidebar_position_hints(const std::string& sidebar_field) const
{ {

View file

@ -220,6 +220,7 @@ private:
GLModel m_arrow; GLModel m_arrow;
GLModel m_curved_arrow; GLModel m_curved_arrow;
GLModel m_box;
float m_scale_factor; float m_scale_factor;
bool m_dragging; bool m_dragging;
@ -353,16 +354,16 @@ public:
void erase(); void erase();
void render(float scale_factor = 1.0) const; void render(float scale_factor = 1.0);
#if ENABLE_RENDER_SELECTION_CENTER #if ENABLE_RENDER_SELECTION_CENTER
void render_center(bool gizmo_is_dragging) const; void render_center(bool gizmo_is_dragging);
#endif // ENABLE_RENDER_SELECTION_CENTER #endif // ENABLE_RENDER_SELECTION_CENTER
//BBS: GUI refactor: add uniform scale from gizmo //BBS: GUI refactor: add uniform scale from gizmo
void render_sidebar_hints(const std::string& sidebar_field, bool uniform_scale) const; void render_sidebar_hints(const std::string& sidebar_field, bool uniform_scale) const;
bool requires_local_axes() const; bool requires_local_axes() const;
void render_bounding_box(const BoundingBoxf3& box, float* color, float scale) { void render_bounding_box(const BoundingBoxf3& box, const ColorRGB& color, float scale) {
m_scale_factor = scale; m_scale_factor = scale;
render_bounding_box(box, color); render_bounding_box(box, color);
} }
@ -399,9 +400,8 @@ private:
void do_remove_instance(unsigned int object_idx, unsigned int instance_idx); void do_remove_instance(unsigned int object_idx, unsigned int instance_idx);
void do_remove_object(unsigned int object_idx); void do_remove_object(unsigned int object_idx);
void set_bounding_boxes_dirty() { m_bounding_box.reset(); m_unscaled_instance_bounding_box.reset(); m_scaled_instance_bounding_box.reset(); } void set_bounding_boxes_dirty() { m_bounding_box.reset(); m_unscaled_instance_bounding_box.reset(); m_scaled_instance_bounding_box.reset(); }
void render_selected_volumes() const; void render_synchronized_volumes();
void render_synchronized_volumes() const; void render_bounding_box(const BoundingBoxf3& box, const ColorRGB& color);
void render_bounding_box(const BoundingBoxf3& box, float* color) const;
void render_sidebar_position_hints(const std::string& sidebar_field) const; void render_sidebar_position_hints(const std::string& sidebar_field) const;
void render_sidebar_rotation_hints(const std::string& sidebar_field) const; void render_sidebar_rotation_hints(const std::string& sidebar_field) const;
//BBS: GUI refactor: add uniform_scale from gizmo //BBS: GUI refactor: add uniform_scale from gizmo