Skip to content

Commit

Permalink
Tech ENABLE_GLBEGIN_GLEND_REMOVAL - Selection layers hints
Browse files Browse the repository at this point in the history
  • Loading branch information
enricoturri1966 committed Jan 18, 2022
1 parent 960158b commit bebb550
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 25 deletions.
9 changes: 8 additions & 1 deletion src/slic3r/GUI/GLCanvas3D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3058,7 +3058,11 @@ void GLCanvas3D::on_mouse(wxMouseEvent& evt)

#if ENABLE_OBJECT_MANIPULATOR_FOCUS
if (evt.ButtonDown()) {
std::string curr_sidebar_field = m_sidebar_field;
handle_sidebar_focus_event("", false);
if (boost::algorithm::istarts_with(curr_sidebar_field, "layer"))
// restore visibility of layers hints after left clicking on the 3D scene
m_sidebar_field = curr_sidebar_field;
if (wxWindow::FindFocus() != m_canvas)
// Grab keyboard focus on any mouse click event.
m_canvas->SetFocus();
Expand Down Expand Up @@ -3740,7 +3744,6 @@ void GLCanvas3D::update_gizmos_on_off_state()
void GLCanvas3D::handle_sidebar_focus_event(const std::string& opt_key, bool focus_on)
{
m_sidebar_field = focus_on ? opt_key : "";

if (!m_sidebar_field.empty())
m_gizmos.reset_all_states();

Expand Down Expand Up @@ -5662,7 +5665,11 @@ void GLCanvas3D::_render_sla_slices()
}
}

#if ENABLE_GLBEGIN_GLEND_REMOVAL
void GLCanvas3D::_render_selection_sidebar_hints()
#else
void GLCanvas3D::_render_selection_sidebar_hints() const
#endif // ENABLE_GLBEGIN_GLEND_REMOVAL
{
m_selection.render_sidebar_hints(m_sidebar_field);
}
Expand Down
4 changes: 4 additions & 0 deletions src/slic3r/GUI/GLCanvas3D.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -937,7 +937,11 @@ class GLCanvas3D
void _render_camera_target() const;
#endif // ENABLE_SHOW_CAMERA_TARGET
void _render_sla_slices();
#if ENABLE_GLBEGIN_GLEND_REMOVAL
void _render_selection_sidebar_hints();
#else
void _render_selection_sidebar_hints() const;
#endif // ENABLE_GLBEGIN_GLEND_REMOVAL
bool _render_undo_redo_stack(const bool is_undo, float pos_x);
bool _render_search_list(float pos_x);
bool _render_arrange_menu(float pos_x);
Expand Down
184 changes: 162 additions & 22 deletions src/slic3r/GUI/Selection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1306,7 +1306,11 @@ void Selection::render_center(bool gizmo_is_dragging)
}
#endif // ENABLE_RENDER_SELECTION_CENTER

#if ENABLE_GLBEGIN_GLEND_REMOVAL
void Selection::render_sidebar_hints(const std::string& sidebar_field)
#else
void Selection::render_sidebar_hints(const std::string& sidebar_field) const
#endif // ENABLE_GLBEGIN_GLEND_REMOVAL
{
if (sidebar_field.empty())
return;
Expand All @@ -1330,7 +1334,7 @@ void Selection::render_sidebar_hints(const std::string& sidebar_field) const
const Vec3d& center = get_bounding_box().center();

if (is_single_full_instance() && !wxGetApp().obj_manipul()->get_world_coordinates()) {
glsafe(::glTranslated(center(0), center(1), center(2)));
glsafe(::glTranslated(center.x(), center.y(), center.z()));
if (!boost::starts_with(sidebar_field, "position")) {
Transform3d orient_matrix = Transform3d::Identity();
if (boost::starts_with(sidebar_field, "scale"))
Expand All @@ -1350,7 +1354,7 @@ void Selection::render_sidebar_hints(const std::string& sidebar_field) const
glsafe(::glMultMatrixd(orient_matrix.data()));
}
} else if (is_single_volume() || is_single_modifier()) {
glsafe(::glTranslated(center(0), center(1), center(2)));
glsafe(::glTranslated(center.x(), center.y(), center.z()));
Transform3d orient_matrix = (*m_volumes)[*m_list.begin()]->get_instance_transformation().get_matrix(true, false, true, true);
if (!boost::starts_with(sidebar_field, "position"))
orient_matrix = orient_matrix * (*m_volumes)[*m_list.begin()]->get_volume_transformation().get_matrix(true, false, true, true);
Expand Down Expand Up @@ -1861,6 +1865,15 @@ void Selection::render_synchronized_volumes() const
}

#if ENABLE_GLBEGIN_GLEND_REMOVAL
static bool is_approx(const Vec3d& v1, const Vec3d& v2)
{
for (int i = 0; i < 3; ++i) {
if (std::abs(v1[i] - v2[i]) > EPSILON)
return false;
}
return true;
}

void Selection::render_bounding_box(const BoundingBoxf3& box, const ColorRGB& color)
{
#else
Expand All @@ -1879,14 +1892,6 @@ void Selection::render_bounding_box(const BoundingBoxf3 & box, float* color) con
#endif // ENABLE_GLBEGIN_GLEND_REMOVAL

#if ENABLE_GLBEGIN_GLEND_REMOVAL
auto is_approx = [](const Vec3d& v1, const Vec3d& v2) {
for (int i = 0; i < 3; ++i) {
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();
Expand Down Expand Up @@ -2026,6 +2031,25 @@ static ColorRGBA get_color(Axis axis)
return AXES_COLOR[axis];
}

#if ENABLE_GLBEGIN_GLEND_REMOVAL
void Selection::render_sidebar_position_hints(const std::string& sidebar_field)
{
if (boost::ends_with(sidebar_field, "x")) {
glsafe(::glRotated(-90.0, 0.0, 0.0, 1.0));
m_arrow.set_color(-1, get_color(X));
m_arrow.render();
}
else if (boost::ends_with(sidebar_field, "y")) {
m_arrow.set_color(-1, get_color(Y));
m_arrow.render();
}
else if (boost::ends_with(sidebar_field, "z")) {
glsafe(::glRotated(90.0, 1.0, 0.0, 0.0));
m_arrow.set_color(-1, get_color(Z));
m_arrow.render();
}
}
#else
void Selection::render_sidebar_position_hints(const std::string& sidebar_field) const
{
if (boost::ends_with(sidebar_field, "x")) {
Expand All @@ -2043,7 +2067,33 @@ void Selection::render_sidebar_position_hints(const std::string& sidebar_field)
m_arrow.render();
}
}
#endif // ENABLE_GLBEGIN_GLEND_REMOVAL

#if ENABLE_GLBEGIN_GLEND_REMOVAL
void Selection::render_sidebar_rotation_hints(const std::string& sidebar_field)
{
auto render_sidebar_rotation_hint = [this]() {
m_curved_arrow.render();
glsafe(::glRotated(180.0, 0.0, 0.0, 1.0));
m_curved_arrow.render();
};

if (boost::ends_with(sidebar_field, "x")) {
glsafe(::glRotated(90.0, 0.0, 1.0, 0.0));
m_curved_arrow.set_color(-1, get_color(X));
render_sidebar_rotation_hint();
}
else if (boost::ends_with(sidebar_field, "y")) {
glsafe(::glRotated(-90.0, 1.0, 0.0, 0.0));
m_curved_arrow.set_color(-1, get_color(Y));
render_sidebar_rotation_hint();
}
else if (boost::ends_with(sidebar_field, "z")) {
m_curved_arrow.set_color(-1, get_color(Z));
render_sidebar_rotation_hint();
}
}
#else
void Selection::render_sidebar_rotation_hints(const std::string& sidebar_field) const
{
auto render_sidebar_rotation_hint = [this]() {
Expand All @@ -2067,13 +2117,22 @@ void Selection::render_sidebar_rotation_hints(const std::string& sidebar_field)
render_sidebar_rotation_hint();
}
}
#endif // ENABLE_GLBEGIN_GLEND_REMOVAL

#if ENABLE_GLBEGIN_GLEND_REMOVAL
void Selection::render_sidebar_scale_hints(const std::string& sidebar_field)
#else
void Selection::render_sidebar_scale_hints(const std::string& sidebar_field) const
#endif // ENABLE_GLBEGIN_GLEND_REMOVAL
{
bool uniform_scale = requires_uniform_scale() || wxGetApp().obj_manipul()->get_uniform_scaling();

auto render_sidebar_scale_hint = [this, uniform_scale](Axis axis) {
#if ENABLE_GLBEGIN_GLEND_REMOVAL
m_arrow.set_color(-1, uniform_scale ? UNIFORM_SCALE_COLOR : get_color(axis));
#else
const_cast<GLModel*>(&m_arrow)->set_color(-1, uniform_scale ? UNIFORM_SCALE_COLOR : get_color(axis));
#endif // ENABLE_GLBEGIN_GLEND_REMOVAL
GLShaderProgram* shader = wxGetApp().get_current_shader();
if (shader != nullptr)
shader->set_uniform("emission_factor", 0.0f);
Expand Down Expand Up @@ -2107,9 +2166,13 @@ void Selection::render_sidebar_scale_hints(const std::string& sidebar_field) con
}
}

#if ENABLE_GLBEGIN_GLEND_REMOVAL
void Selection::render_sidebar_layers_hints(const std::string& sidebar_field)
#else
void Selection::render_sidebar_layers_hints(const std::string& sidebar_field) const
#endif // ENABLE_GLBEGIN_GLEND_REMOVAL
{
static const double Margin = 10.0;
static const float Margin = 10.0f;

std::string field = sidebar_field;

Expand All @@ -2118,15 +2181,15 @@ void Selection::render_sidebar_layers_hints(const std::string& sidebar_field) co
if (pos == std::string::npos)
return;

double max_z = string_to_double_decimal_point(field.substr(pos + 1));
const float max_z = float(string_to_double_decimal_point(field.substr(pos + 1)));

// extract min_z
field = field.substr(0, pos);
pos = field.rfind("_");
if (pos == std::string::npos)
return;

const double min_z = string_to_double_decimal_point(field.substr(pos + 1));
const float min_z = float(string_to_double_decimal_point(field.substr(pos + 1)));

// extract type
field = field.substr(0, pos);
Expand All @@ -2138,38 +2201,115 @@ void Selection::render_sidebar_layers_hints(const std::string& sidebar_field) co

const BoundingBoxf3& box = get_bounding_box();

const float min_x = box.min(0) - Margin;
const float max_x = box.max(0) + Margin;
const float min_y = box.min(1) - Margin;
const float max_y = box.max(1) + Margin;
#if !ENABLE_GLBEGIN_GLEND_REMOVAL
const float min_x = float(box.min.x()) - Margin;
const float max_x = float(box.max.x()) + Margin;
const float min_y = float(box.min.y()) - Margin;
const float max_y = float(box.max.y()) + Margin;
#endif // !ENABLE_GLBEGIN_GLEND_REMOVAL

// view dependend order of rendering to keep correct transparency
bool camera_on_top = wxGetApp().plater()->get_camera().is_looking_downward();
const bool camera_on_top = wxGetApp().plater()->get_camera().is_looking_downward();
const float z1 = camera_on_top ? min_z : max_z;
const float z2 = camera_on_top ? max_z : min_z;

#if ENABLE_GLBEGIN_GLEND_REMOVAL
const Vec3f p1 = { float(box.min.x()) - Margin, float(box.min.y()) - Margin, z1 };
const Vec3f p2 = { float(box.max.x()) + Margin, float(box.max.y()) + Margin, z2 };
#else
const float min_x = float(box.min.x()) - Margin;
const float max_x = float(box.max.x()) + Margin;
const float min_y = float(box.min.y()) - Margin;
const float max_y = float(box.max.y()) + Margin;
#endif // ENABLE_GLBEGIN_GLEND_REMOVAL

glsafe(::glEnable(GL_DEPTH_TEST));
glsafe(::glDisable(GL_CULL_FACE));
glsafe(::glEnable(GL_BLEND));
glsafe(::glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA));

#if ENABLE_GLBEGIN_GLEND_REMOVAL
if (!m_planes.models[0].is_initialized() || !is_approx(m_planes.check_points[0].cast<double>(), p1.cast<double>())) {
m_planes.check_points[0] = p1;
m_planes.models[0].reset();

GLModel::InitializationData init_data;
GUI::GLModel::InitializationData::Entity entity;
entity.type = GUI::GLModel::PrimitiveType::Triangles;
entity.positions.reserve(4);
entity.positions.emplace_back(Vec3f(p1.x(), p1.y(), z1));
entity.positions.emplace_back(Vec3f(p2.x(), p1.y(), z1));
entity.positions.emplace_back(Vec3f(p2.x(), p2.y(), z1));
entity.positions.emplace_back(Vec3f(p1.x(), p2.y(), z1));

entity.normals.reserve(4);
for (size_t i = 0; i < 4; ++i) {
entity.normals.emplace_back(Vec3f::UnitZ());
}

entity.indices.reserve(6);
entity.indices.emplace_back(0);
entity.indices.emplace_back(1);
entity.indices.emplace_back(2);
entity.indices.emplace_back(2);
entity.indices.emplace_back(3);
entity.indices.emplace_back(0);

init_data.entities.emplace_back(entity);
m_planes.models[0].init_from(init_data);
}

if (!m_planes.models[1].is_initialized() || !is_approx(m_planes.check_points[1].cast<double>(), p2.cast<double>())) {
m_planes.check_points[1] = p2;
m_planes.models[1].reset();

GLModel::InitializationData init_data;
GUI::GLModel::InitializationData::Entity entity;
entity.type = GUI::GLModel::PrimitiveType::Triangles;
entity.positions.reserve(4);
entity.positions.emplace_back(Vec3f(p1.x(), p1.y(), z2));
entity.positions.emplace_back(Vec3f(p2.x(), p1.y(), z2));
entity.positions.emplace_back(Vec3f(p2.x(), p2.y(), z2));
entity.positions.emplace_back(Vec3f(p1.x(), p2.y(), z2));

entity.normals.reserve(4);
for (size_t i = 0; i < 4; ++i) {
entity.normals.emplace_back(Vec3f::UnitZ());
}

entity.indices.reserve(6);
entity.indices.emplace_back(0);
entity.indices.emplace_back(1);
entity.indices.emplace_back(2);
entity.indices.emplace_back(2);
entity.indices.emplace_back(3);
entity.indices.emplace_back(0);

init_data.entities.emplace_back(entity);
m_planes.models[1].init_from(init_data);
}

m_planes.models[0].set_color(-1, (camera_on_top && type == 1) || (!camera_on_top && type == 2) ? SOLID_PLANE_COLOR : TRANSPARENT_PLANE_COLOR);
m_planes.models[0].render();
m_planes.models[1].set_color(-1, (camera_on_top && type == 2) || (!camera_on_top && type == 1) ? SOLID_PLANE_COLOR : TRANSPARENT_PLANE_COLOR);
m_planes.models[1].render();
#else
::glBegin(GL_QUADS);
::glColor4fv((camera_on_top && type == 1) || (!camera_on_top && type == 2) ?
SOLID_PLANE_COLOR.data() : TRANSPARENT_PLANE_COLOR.data());
::glColor4fv((camera_on_top && type == 1) || (!camera_on_top && type == 2) ? SOLID_PLANE_COLOR.data() : TRANSPARENT_PLANE_COLOR.data());
::glVertex3f(min_x, min_y, z1);
::glVertex3f(max_x, min_y, z1);
::glVertex3f(max_x, max_y, z1);
::glVertex3f(min_x, max_y, z1);
glsafe(::glEnd());

::glBegin(GL_QUADS);
::glColor4fv((camera_on_top && type == 2) || (!camera_on_top && type == 1) ?
SOLID_PLANE_COLOR.data() : TRANSPARENT_PLANE_COLOR.data());
::glColor4fv((camera_on_top && type == 2) || (!camera_on_top && type == 1) ? SOLID_PLANE_COLOR.data() : TRANSPARENT_PLANE_COLOR.data());
::glVertex3f(min_x, min_y, z2);
::glVertex3f(max_x, min_y, z2);
::glVertex3f(max_x, max_y, z2);
::glVertex3f(min_x, max_y, z2);
glsafe(::glEnd());
#endif // ENABLE_GLBEGIN_GLEND_REMOVAL

glsafe(::glEnable(GL_CULL_FACE));
glsafe(::glDisable(GL_BLEND));
Expand Down
15 changes: 13 additions & 2 deletions src/slic3r/GUI/Selection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,12 @@ class Selection
GLModel m_curved_arrow;
#if ENABLE_GLBEGIN_GLEND_REMOVAL
GLModel m_box;
struct Planes
{
std::array<Vec3f, 2> check_points{ Vec3f::Zero(), Vec3f::Zero() };
std::array<GLModel, 2> models;
};
Planes m_planes;
#endif // ENABLE_GLBEGIN_GLEND_REMOVAL

float m_scale_factor;
Expand Down Expand Up @@ -334,13 +340,14 @@ class Selection

#if ENABLE_GLBEGIN_GLEND_REMOVAL
void render(float scale_factor = 1.0);
void render_sidebar_hints(const std::string& sidebar_field);
#else
void render(float scale_factor = 1.0) const;
void render_sidebar_hints(const std::string& sidebar_field) const;
#endif // ENABLE_GLBEGIN_GLEND_REMOVAL
#if ENABLE_RENDER_SELECTION_CENTER
void render_center(bool gizmo_is_dragging);
#endif // ENABLE_RENDER_SELECTION_CENTER
void render_sidebar_hints(const std::string& sidebar_field) const;

bool requires_local_axes() const;

Expand Down Expand Up @@ -373,15 +380,19 @@ class Selection
#if ENABLE_GLBEGIN_GLEND_REMOVAL
void render_synchronized_volumes();
void render_bounding_box(const BoundingBoxf3& box, const ColorRGB& color);
void render_sidebar_position_hints(const std::string& sidebar_field);
void render_sidebar_rotation_hints(const std::string& sidebar_field);
void render_sidebar_scale_hints(const std::string& sidebar_field);
void render_sidebar_layers_hints(const std::string& sidebar_field);
#else
void render_selected_volumes() const;
void render_synchronized_volumes() const;
void render_bounding_box(const BoundingBoxf3& box, float* color) const;
#endif // ENABLE_GLBEGIN_GLEND_REMOVAL
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_scale_hints(const std::string& sidebar_field) const;
void render_sidebar_layers_hints(const std::string& sidebar_field) const;
#endif // ENABLE_GLBEGIN_GLEND_REMOVAL

public:
enum SyncRotationType {
Expand Down

0 comments on commit bebb550

Please sign in to comment.