Tech ENABLE_GIZMO_GRABBER_REFACTOR - Refactoring of GLGizmoBase::Grabber to have a single static instance of the cube and cone models to be shared by all grabbers

This commit is contained in:
enricoturri1966 2022-04-14 15:15:14 +02:00
parent 09512c086b
commit f504236734
7 changed files with 2203 additions and 2019 deletions

View File

@ -79,6 +79,8 @@
#define ENABLE_PROCESS_G2_G3_LINES (1 && ENABLE_2_5_0_ALPHA1)
// Enable fix of used filament data exported to gcode file
#define ENABLE_USED_FILAMENT_POST_PROCESS (1 && ENABLE_2_5_0_ALPHA1)
// Enable gizmo grabbers to share common models
#define ENABLE_GIZMO_GRABBER_REFACTOR (1 && ENABLE_2_5_0_ALPHA1)
#endif // _prusaslicer_technologies_h_

View File

@ -18,10 +18,19 @@ const float GLGizmoBase::Grabber::SizeFactor = 0.05f;
const float GLGizmoBase::Grabber::MinHalfSize = 1.5f;
const float GLGizmoBase::Grabber::DraggingScaleFactor = 1.25f;
void GLGizmoBase::Grabber::render(bool hover, float size)
#if ENABLE_GIZMO_GRABBER_REFACTOR
GLModel GLGizmoBase::Grabber::s_cube;
GLModel GLGizmoBase::Grabber::s_cone;
GLGizmoBase::Grabber::~Grabber()
{
render(size, hover ? complementary(color) : color, false);
if (s_cube.is_initialized())
s_cube.reset();
if (s_cone.is_initialized())
s_cone.reset();
}
#endif // ENABLE_GIZMO_GRABBER_REFACTOR
float GLGizmoBase::Grabber::get_half_size(float size) const
{
@ -41,29 +50,64 @@ void GLGizmoBase::Grabber::render(float size, const ColorRGBA& render_color, boo
return;
#endif // ENABLE_GL_SHADERS_ATTRIBUTES
#if ENABLE_GIZMO_GRABBER_REFACTOR
if (!s_cube.is_initialized()) {
#else
if (!m_cube.is_initialized()) {
#endif // ENABLE_GIZMO_GRABBER_REFACTOR
// This cannot be done in constructor, OpenGL is not yet
// initialized at that point (on Linux at least).
indexed_triangle_set its = its_make_cube(1., 1., 1.);
indexed_triangle_set its = its_make_cube(1.0, 1.0, 1.0);
its_translate(its, -0.5f * Vec3f::Ones());
#if ENABLE_LEGACY_OPENGL_REMOVAL
#if ENABLE_GIZMO_GRABBER_REFACTOR
s_cube.init_from(its);
#else
m_cube.init_from(its);
#endif // ENABLE_GIZMO_GRABBER_REFACTOR
#else
#if ENABLE_GIZMO_GRABBER_REFACTOR
s_cube.init_from(its, BoundingBoxf3{ { -0.5, -0.5, -0.5 }, { 0.5, 0.5, 0.5 } });
#else
m_cube.init_from(its, BoundingBoxf3{ { -0.5, -0.5, -0.5 }, { 0.5, 0.5, 0.5 } });
#endif // ENABLE_GIZMO_GRABBER_REFACTOR
#endif // ENABLE_LEGACY_OPENGL_REMOVAL
}
#if ENABLE_GIZMO_GRABBER_REFACTOR
if (!s_cone.is_initialized())
s_cone.init_from(its_make_cone(0.375, 1.5, double(PI) / 18.0));
#endif // ENABLE_GIZMO_GRABBER_REFACTOR
#if ENABLE_GIZMO_GRABBER_REFACTOR
const float half_size = dragging ? get_dragging_half_size(size) : get_half_size(size);
#else
const float fullsize = 2.0f * (dragging ? get_dragging_half_size(size) : get_half_size(size));
#endif // ENABLE_GIZMO_GRABBER_REFACTOR
#if ENABLE_LEGACY_OPENGL_REMOVAL
#if ENABLE_GIZMO_GRABBER_REFACTOR
s_cube.set_color(render_color);
s_cone.set_color(render_color);
#else
m_cube.set_color(render_color);
#endif // ENABLE_GIZMO_GRABBER_REFACTOR
#else
#if ENABLE_GIZMO_GRABBER_REFACTOR
s_cube.set_color(-1, render_color);
s_cone.set_color(-1, render_color);
#else
m_cube.set_color(-1, render_color);
#endif // ENABLE_GIZMO_GRABBER_REFACTOR
#endif // ENABLE_LEGACY_OPENGL_REMOVAL
#if ENABLE_GL_SHADERS_ATTRIBUTES
const Camera& camera = wxGetApp().plater()->get_camera();
#if ENABLE_GIZMO_GRABBER_REFACTOR
const Transform3d view_model_matrix = camera.get_view_matrix() * matrix * Geometry::assemble_transform(center, angles, 2.0 * half_size * Vec3d::Ones());
#else
const Transform3d view_model_matrix = camera.get_view_matrix() * matrix * Geometry::assemble_transform(center, angles, fullsize * Vec3d::Ones());
#endif // ENABLE_GIZMO_GRABBER_REFACTOR
const Transform3d& projection_matrix = camera.get_projection_matrix();
shader->set_uniform("view_model_matrix", view_model_matrix);
@ -75,9 +119,86 @@ void GLGizmoBase::Grabber::render(float size, const ColorRGBA& render_color, boo
glsafe(::glRotated(Geometry::rad2deg(angles.z()), 0.0, 0.0, 1.0));
glsafe(::glRotated(Geometry::rad2deg(angles.y()), 0.0, 1.0, 0.0));
glsafe(::glRotated(Geometry::rad2deg(angles.x()), 1.0, 0.0, 0.0));
#if ENABLE_GIZMO_GRABBER_REFACTOR
glsafe(::glScaled(2.0 * half_size, 2.0 * half_size, 2.0 * half_size));
#else
glsafe(::glScaled(fullsize, fullsize, fullsize));
#endif // ENABLE_GIZMO_GRABBER_REFACTOR
#endif // ENABLE_GL_SHADERS_ATTRIBUTES
#if ENABLE_GIZMO_GRABBER_REFACTOR
s_cube.render();
#if ENABLE_GL_SHADERS_ATTRIBUTES
if ((int(extensions) & int(GLGizmoBase::EGrabberExtension::PosX)) != 0) {
shader->set_uniform("view_model_matrix", view_model_matrix * Geometry::assemble_transform(Vec3d::UnitX(), Vec3d(0.0, 0.5 * double(PI), 0.0)));
s_cone.render();
}
if ((int(extensions) & int(GLGizmoBase::EGrabberExtension::NegX)) != 0) {
shader->set_uniform("view_model_matrix", view_model_matrix * Geometry::assemble_transform(-Vec3d::UnitX(), Vec3d(0.0, -0.5 * double(PI), 0.0)));
s_cone.render();
}
if ((int(extensions) & int(GLGizmoBase::EGrabberExtension::PosY)) != 0) {
shader->set_uniform("view_model_matrix", view_model_matrix * Geometry::assemble_transform(Vec3d::UnitY(), Vec3d(-0.5 * double(PI), 0.0, 0.0)));
s_cone.render();
}
if ((int(extensions) & int(GLGizmoBase::EGrabberExtension::NegY)) != 0) {
shader->set_uniform("view_model_matrix", view_model_matrix * Geometry::assemble_transform(-Vec3d::UnitY(), Vec3d(0.5 * double(PI), 0.0, 0.0)));
s_cone.render();
}
if ((int(extensions) & int(GLGizmoBase::EGrabberExtension::PosZ)) != 0) {
shader->set_uniform("view_model_matrix", view_model_matrix * Geometry::assemble_transform(Vec3d::UnitZ()));
s_cone.render();
}
if ((int(extensions) & int(GLGizmoBase::EGrabberExtension::NegZ)) != 0) {
shader->set_uniform("view_model_matrix", view_model_matrix * Geometry::assemble_transform(-Vec3d::UnitZ(), Vec3d(double(PI), 0.0, 0.0)));
s_cone.render();
}
#else
if ((int(extensions) & int(GLGizmoBase::EGrabberExtension::PosX)) != 0) {
glsafe(::glPushMatrix());
glsafe(::glTranslated(1.0, 0.0, 0.0));
glsafe(::glRotated(0.5 * Geometry::rad2deg(double(PI)), 0.0, 1.0, 0.0));
s_cone.render();
glsafe(::glPopMatrix());
}
if ((int(extensions) & int(GLGizmoBase::EGrabberExtension::NegX)) != 0) {
glsafe(::glPushMatrix());
glsafe(::glTranslated(-1.0, 0.0, 0.0));
glsafe(::glRotated(-0.5 * Geometry::rad2deg(double(PI)), 0.0, 1.0, 0.0));
s_cone.render();
glsafe(::glPopMatrix());
}
if ((int(extensions) & int(GLGizmoBase::EGrabberExtension::PosY)) != 0) {
glsafe(::glPushMatrix());
glsafe(::glTranslated(0.0, 1.0, 0.0));
glsafe(::glRotated(-0.5 * Geometry::rad2deg(double(PI)), 1.0, 0.0, 0.0));
s_cone.render();
glsafe(::glPopMatrix());
}
if ((int(extensions) & int(GLGizmoBase::EGrabberExtension::NegY)) != 0) {
glsafe(::glPushMatrix());
glsafe(::glTranslated(0.0, -1.0, 0.0));
glsafe(::glRotated(0.5 * Geometry::rad2deg(double(PI)), 1.0, 0.0, 0.0));
s_cone.render();
glsafe(::glPopMatrix());
}
if ((int(extensions) & int(GLGizmoBase::EGrabberExtension::PosZ)) != 0) {
glsafe(::glPushMatrix());
glsafe(::glTranslated(0.0, 0.0, 1.0));
s_cone.render();
glsafe(::glPopMatrix());
}
if ((int(extensions) & int(GLGizmoBase::EGrabberExtension::NegZ)) != 0) {
glsafe(::glPushMatrix());
glsafe(::glTranslated(0.0, 0.0, -1.0));
glsafe(::glRotated(Geometry::rad2deg(double(PI)), 1.0, 0.0, 0.0));
s_cone.render();
glsafe(::glPopMatrix());
}
#endif // ENABLE_GL_SHADERS_ATTRIBUTES
#else
m_cube.render();
#endif // ENABLE_GIZMO_GRABBER_REFACTOR
#if !ENABLE_GL_SHADERS_ATTRIBUTES
glsafe(::glPopMatrix());
#endif // !ENABLE_GL_SHADERS_ATTRIBUTES

View File

@ -38,6 +38,19 @@ public:
// (254 is choosen to leave some space for forward compatibility)
static const unsigned int BASE_ID = 255 * 255 * 254;
#if ENABLE_GIZMO_GRABBER_REFACTOR
enum class EGrabberExtension
{
None = 0,
PosX = 1 << 0,
NegX = 1 << 1,
PosY = 1 << 2,
NegY = 1 << 3,
PosZ = 1 << 4,
NegZ = 1 << 5,
};
#endif // ENABLE_GIZMO_GRABBER_REFACTOR
protected:
struct Grabber
{
@ -53,10 +66,16 @@ protected:
Transform3d matrix{ Transform3d::Identity() };
#endif // ENABLE_GL_SHADERS_ATTRIBUTES
ColorRGBA color{ ColorRGBA::WHITE() };
#if ENABLE_GIZMO_GRABBER_REFACTOR
EGrabberExtension extensions{ EGrabberExtension::None };
#endif // ENABLE_GIZMO_GRABBER_REFACTOR
Grabber() = default;
#if ENABLE_GIZMO_GRABBER_REFACTOR
~Grabber();
#endif // ENABLE_GIZMO_GRABBER_REFACTOR
void render(bool hover, float size);
void render(bool hover, float size) { render(size, hover ? complementary(color) : color, false); }
void render_for_picking(float size) { render(size, color, true); }
float get_half_size(float size) const;
@ -65,7 +84,12 @@ protected:
private:
void render(float size, const ColorRGBA& render_color, bool picking);
#if ENABLE_GIZMO_GRABBER_REFACTOR
static GLModel s_cube;
static GLModel s_cone;
#else
GLModel m_cube;
#endif // ENABLE_GIZMO_GRABBER_REFACTOR
};
public:

View File

@ -49,8 +49,16 @@ bool GLGizmoMove3D::on_init()
{
for (int i = 0; i < 3; ++i) {
m_grabbers.push_back(Grabber());
#if ENABLE_GIZMO_GRABBER_REFACTOR
m_grabbers.back().extensions = GLGizmoBase::EGrabberExtension::PosZ;
#endif // ENABLE_GIZMO_GRABBER_REFACTOR
}
#if ENABLE_GIZMO_GRABBER_REFACTOR
m_grabbers[0].angles = { 0.0, 0.5 * double(PI), 0.0 };
m_grabbers[1].angles = { -0.5 * double(PI), 0.0, 0.0 };
#endif // ENABLE_GIZMO_GRABBER_REFACTOR
m_shortcut_key = WXK_CONTROL_M;
return true;
@ -99,8 +107,10 @@ void GLGizmoMove3D::on_dragging(const UpdateData& data)
void GLGizmoMove3D::on_render()
{
#if !ENABLE_GIZMO_GRABBER_REFACTOR
if (!m_cone.is_initialized())
m_cone.init_from(its_make_cone(1.0, 1.0, double(PI) / 18.0));
#endif // !ENABLE_GIZMO_GRABBER_REFACTOR
const Selection& selection = m_parent.get_selection();
@ -187,10 +197,12 @@ void GLGizmoMove3D::on_render()
// draw grabbers
render_grabbers(box);
#if !ENABLE_GIZMO_GRABBER_REFACTOR
for (unsigned int i = 0; i < 3; ++i) {
if (m_grabbers[i].enabled)
render_grabber_extension((Axis)i, box, false);
}
#endif // !ENABLE_GIZMO_GRABBER_REFACTOR
}
else {
// draw axis
@ -227,7 +239,9 @@ void GLGizmoMove3D::on_render()
m_grabbers[m_hover_id].render(true, mean_size);
shader->stop_using();
}
#if !ENABLE_GIZMO_GRABBER_REFACTOR
render_grabber_extension((Axis)m_hover_id, box, false);
#endif // !ENABLE_GIZMO_GRABBER_REFACTOR
}
}
@ -237,9 +251,11 @@ void GLGizmoMove3D::on_render_for_picking()
const BoundingBoxf3& box = m_parent.get_selection().get_bounding_box();
render_grabbers_for_picking(box);
#if !ENABLE_GIZMO_GRABBER_REFACTOR
render_grabber_extension(X, box, true);
render_grabber_extension(Y, box, true);
render_grabber_extension(Z, box, true);
#endif // !ENABLE_GIZMO_GRABBER_REFACTOR
}
double GLGizmoMove3D::calc_projection(const UpdateData& data) const
@ -268,6 +284,7 @@ double GLGizmoMove3D::calc_projection(const UpdateData& data) const
return projection;
}
#if !ENABLE_GIZMO_GRABBER_REFACTOR
void GLGizmoMove3D::render_grabber_extension(Axis axis, const BoundingBoxf3& box, bool picking)
{
const float mean_size = float((box.size().x() + box.size().y() + box.size().z()) / 3.0);
@ -326,6 +343,7 @@ void GLGizmoMove3D::render_grabber_extension(Axis axis, const BoundingBoxf3& box
#endif // !ENABLE_LEGACY_OPENGL_REMOVAL
shader->stop_using();
}
#endif // !ENABLE_GIZMO_GRABBER_REFACTOR
} // namespace GUI
} // namespace Slic3r

View File

@ -17,7 +17,9 @@ class GLGizmoMove3D : public GLGizmoBase
Vec3d m_starting_box_center{ Vec3d::Zero() };
Vec3d m_starting_box_bottom_center{ Vec3d::Zero() };
#if !ENABLE_GIZMO_GRABBER_REFACTOR
GLModel m_cone;
#endif // !ENABLE_GIZMO_GRABBER_REFACTOR
#if ENABLE_LEGACY_OPENGL_REMOVAL
struct GrabberConnection
{
@ -47,6 +49,7 @@ public:
/// Detect reduction of move for wipetover on selection change
/// </summary>
void data_changed() override;
protected:
bool on_init() override;
std::string on_get_name() const override;
@ -59,11 +62,11 @@ protected:
private:
double calc_projection(const UpdateData& data) const;
#if !ENABLE_GIZMO_GRABBER_REFACTOR
void render_grabber_extension(Axis axis, const BoundingBoxf3& box, bool picking);
#endif // !ENABLE_GIZMO_GRABBER_REFACTOR
};
} // namespace GUI
} // namespace Slic3r

View File

@ -91,6 +91,9 @@ void GLGizmoRotate::disable_grabber() { m_grabbers[0].enabled = false; }
bool GLGizmoRotate::on_init()
{
m_grabbers.push_back(Grabber());
#if ENABLE_GIZMO_GRABBER_REFACTOR
m_grabbers.back().extensions = (GLGizmoBase::EGrabberExtension)(int(GLGizmoBase::EGrabberExtension::PosY) | int(GLGizmoBase::EGrabberExtension::NegY));
#endif // ENABLE_GIZMO_GRABBER_REFACTOR
return true;
}
@ -142,8 +145,10 @@ void GLGizmoRotate::on_render()
if (!m_grabbers.front().enabled)
return;
#if !ENABLE_GIZMO_GRABBER_REFACTOR
if (!m_cone.is_initialized())
m_cone.init_from(its_make_cone(1.0, 1.0, double(PI) / 12.0));
#endif // !ENABLE_GIZMO_GRABBER_REFACTOR
const Selection& selection = m_parent.get_selection();
const BoundingBoxf3& box = selection.get_bounding_box();
@ -219,7 +224,9 @@ void GLGizmoRotate::on_render()
#endif // ENABLE_LEGACY_OPENGL_REMOVAL
render_grabber(box);
#if !ENABLE_GIZMO_GRABBER_REFACTOR
render_grabber_extension(box, false);
#endif // !ENABLE_GIZMO_GRABBER_REFACTOR
#if !ENABLE_GL_SHADERS_ATTRIBUTES
glsafe(::glPopMatrix());
@ -241,7 +248,9 @@ void GLGizmoRotate::on_render_for_picking()
const BoundingBoxf3& box = selection.get_bounding_box();
render_grabbers_for_picking(box);
#if !ENABLE_GIZMO_GRABBER_REFACTOR
render_grabber_extension(box, true);
#endif // !ENABLE_GIZMO_GRABBER_REFACTOR
#if !ENABLE_GL_SHADERS_ATTRIBUTES
glsafe(::glPopMatrix());
@ -566,6 +575,7 @@ void GLGizmoRotate::render_grabber(const BoundingBoxf3& box)
render_grabbers(box);
}
#if !ENABLE_GIZMO_GRABBER_REFACTOR
void GLGizmoRotate::render_grabber_extension(const BoundingBoxf3& box, bool picking)
{
const float mean_size = float((box.size().x() + box.size().y() + box.size().z()) / 3.0);
@ -640,6 +650,7 @@ void GLGizmoRotate::render_grabber_extension(const BoundingBoxf3& box, bool pick
#endif // !ENABLE_LEGACY_OPENGL_REMOVAL
shader->stop_using();
}
#endif // !ENABLE_GIZMO_GRABBER_REFACTOR
#if ENABLE_GL_SHADERS_ATTRIBUTES
Transform3d GLGizmoRotate::local_transform(const Selection& selection) const

View File

@ -35,7 +35,9 @@ private:
float m_snap_fine_in_radius{ 0.0f };
float m_snap_fine_out_radius{ 0.0f };
#if !ENABLE_GIZMO_GRABBER_REFACTOR
GLModel m_cone;
#endif // !ENABLE_GIZMO_GRABBER_REFACTOR
#if ENABLE_LEGACY_OPENGL_REMOVAL
GLModel m_circle;
GLModel m_scale;
@ -55,6 +57,7 @@ private:
ColorRGBA m_drag_color;
ColorRGBA m_highlight_color;
public:
GLGizmoRotate(GLCanvas3D& parent, Axis axis);
virtual ~GLGizmoRotate() = default;
@ -104,7 +107,9 @@ private:
void render_angle() const;
#endif // ENABLE_LEGACY_OPENGL_REMOVAL
void render_grabber(const BoundingBoxf3& box);
#if !ENABLE_GIZMO_GRABBER_REFACTOR
void render_grabber_extension(const BoundingBoxf3& box, bool picking);
#endif // !ENABLE_GIZMO_GRABBER_REFACTOR
#if ENABLE_GL_SHADERS_ATTRIBUTES
Transform3d local_transform(const Selection& selection) const;