Tech ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES - Use vertex attributes and matrices in shaders.

Shader: flat - Picking for base gizmo grabbers and gizmo rotate grabber extensions
This commit is contained in:
enricoturri1966 2022-02-28 14:33:49 +01:00
parent 78a93acb5b
commit 8c8f0b32a7
4 changed files with 131 additions and 24 deletions

View File

@ -4,8 +4,10 @@
#include <GL/glew.h> #include <GL/glew.h>
#include "slic3r/GUI/GUI_App.hpp" #include "slic3r/GUI/GUI_App.hpp"
#include "slic3r/GUI/GUI_ObjectManipulation.hpp" #include "slic3r/GUI/GUI_ObjectManipulation.hpp"
#if ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES
#include "slic3r/GUI/Plater.hpp"
#endif // ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES
// TODO: Display tooltips quicker on Linux // TODO: Display tooltips quicker on Linux
@ -33,11 +35,19 @@ float GLGizmoBase::Grabber::get_dragging_half_size(float size) const
void GLGizmoBase::Grabber::render(float size, const ColorRGBA& render_color, bool picking) void GLGizmoBase::Grabber::render(float size, const ColorRGBA& render_color, bool picking)
{ {
#if ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES
GLShaderProgram* shader = wxGetApp().get_current_shader();
if (shader == nullptr)
return;
bool use_attributes = boost::algorithm::iends_with(shader->get_name(), "_attr");
#endif // ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES
if (!m_cube.is_initialized()) { if (!m_cube.is_initialized()) {
// This cannot be done in constructor, OpenGL is not yet // This cannot be done in constructor, OpenGL is not yet
// initialized at that point (on Linux at least). // 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., 1., 1.);
its_translate(its, Vec3f(-0.5, -0.5, -0.5)); its_translate(its, -0.5f * Vec3f::Ones());
#if ENABLE_GLBEGIN_GLEND_REMOVAL #if ENABLE_GLBEGIN_GLEND_REMOVAL
m_cube.init_from(its); m_cube.init_from(its);
#else #else
@ -53,14 +63,28 @@ void GLGizmoBase::Grabber::render(float size, const ColorRGBA& render_color, boo
m_cube.set_color(-1, render_color); m_cube.set_color(-1, render_color);
#endif // ENABLE_GLBEGIN_GLEND_REMOVAL #endif // ENABLE_GLBEGIN_GLEND_REMOVAL
glsafe(::glPushMatrix()); #if ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES
glsafe(::glTranslated(center.x(), center.y(), center.z())); if (use_attributes) {
glsafe(::glRotated(Geometry::rad2deg(angles.z()), 0.0, 0.0, 1.0)); const Transform3d trafo = wxGetApp().plater()->get_camera().get_projection_view_matrix() * matrix *
glsafe(::glRotated(Geometry::rad2deg(angles.y()), 0.0, 1.0, 0.0)); Geometry::assemble_transform(center, angles, fullsize * Vec3d::Ones());
glsafe(::glRotated(Geometry::rad2deg(angles.x()), 1.0, 0.0, 0.0)); shader->set_uniform("projection_view_model_matrix", trafo);
glsafe(::glScaled(fullsize, fullsize, fullsize)); }
else {
#endif // ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES
glsafe(::glPushMatrix());
glsafe(::glTranslated(center.x(), center.y(), center.z()));
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));
glsafe(::glScaled(fullsize, fullsize, fullsize));
#if ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES
}
#endif // ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES
m_cube.render(); m_cube.render();
glsafe(::glPopMatrix()); #if ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES
if (!use_attributes)
#endif // ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES
glsafe(::glPopMatrix());
} }
GLGizmoBase::GLGizmoBase(GLCanvas3D& parent, const std::string& icon_filename, unsigned int sprite_id) GLGizmoBase::GLGizmoBase(GLCanvas3D& parent, const std::string& icon_filename, unsigned int sprite_id)
@ -129,7 +153,11 @@ void GLGizmoBase::render_grabbers(float size) const
void GLGizmoBase::render_grabbers_for_picking(const BoundingBoxf3& box) const void GLGizmoBase::render_grabbers_for_picking(const BoundingBoxf3& box) const
{ {
#if ENABLE_GLBEGIN_GLEND_REMOVAL #if ENABLE_GLBEGIN_GLEND_REMOVAL
#if ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES
GLShaderProgram* shader = wxGetApp().get_shader("flat_attr");
#else
GLShaderProgram* shader = wxGetApp().get_shader("flat"); GLShaderProgram* shader = wxGetApp().get_shader("flat");
#endif // ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES
if (shader != nullptr) { if (shader != nullptr) {
shader->start_using(); shader->start_using();
#endif // ENABLE_GLBEGIN_GLEND_REMOVAL #endif // ENABLE_GLBEGIN_GLEND_REMOVAL

View File

@ -49,6 +49,9 @@ protected:
bool dragging{ false }; bool dragging{ false };
Vec3d center{ Vec3d::Zero() }; Vec3d center{ Vec3d::Zero() };
Vec3d angles{ Vec3d::Zero() }; Vec3d angles{ Vec3d::Zero() };
#if ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES
Transform3d matrix{ Transform3d::Identity() };
#endif // ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES
ColorRGBA color{ ColorRGBA::WHITE() }; ColorRGBA color{ ColorRGBA::WHITE() };
Grabber() = default; Grabber() = default;

View File

@ -219,15 +219,20 @@ void GLGizmoRotate::on_render_for_picking()
glsafe(::glDisable(GL_DEPTH_TEST)); glsafe(::glDisable(GL_DEPTH_TEST));
#if ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES
m_grabbers.front().matrix = local_transform(selection);
#else
glsafe(::glPushMatrix()); glsafe(::glPushMatrix());
transform_to_local(selection); transform_to_local(selection);
#endif // ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES
const BoundingBoxf3& box = selection.get_bounding_box(); const BoundingBoxf3& box = selection.get_bounding_box();
render_grabbers_for_picking(box); render_grabbers_for_picking(box);
render_grabber_extension(box, true); render_grabber_extension(box, true);
#if !ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES
glsafe(::glPopMatrix()); glsafe(::glPopMatrix());
#endif // !ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES
} }
void GLGizmoRotate3D::on_render_input_window(float x, float y, float bottom_limit) void GLGizmoRotate3D::on_render_input_window(float x, float y, float bottom_limit)
@ -550,10 +555,18 @@ void GLGizmoRotate::render_grabber_extension(const BoundingBoxf3& box, bool pick
const double size = m_dragging ? double(m_grabbers.front().get_dragging_half_size(mean_size)) : double(m_grabbers.front().get_half_size(mean_size)); const double size = m_dragging ? double(m_grabbers.front().get_dragging_half_size(mean_size)) : double(m_grabbers.front().get_half_size(mean_size));
#if ENABLE_GLBEGIN_GLEND_REMOVAL #if ENABLE_GLBEGIN_GLEND_REMOVAL
#if ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES
GLShaderProgram* shader = wxGetApp().get_shader(picking ? "flat_attr" : "gouraud_light");
#else
GLShaderProgram* shader = wxGetApp().get_shader(picking ? "flat" : "gouraud_light"); GLShaderProgram* shader = wxGetApp().get_shader(picking ? "flat" : "gouraud_light");
#endif // ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES
if (shader == nullptr) if (shader == nullptr)
return; return;
#if ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES
bool use_attributes = boost::algorithm::iends_with(shader->get_name(), "_attr");
#endif // ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES
m_cone.set_color((!picking && m_hover_id != -1) ? complementary(m_grabbers.front().color) : m_grabbers.front().color); m_cone.set_color((!picking && m_hover_id != -1) ? complementary(m_grabbers.front().color) : m_grabbers.front().color);
shader->start_using(); shader->start_using();
@ -572,22 +585,49 @@ void GLGizmoRotate::render_grabber_extension(const BoundingBoxf3& box, bool pick
const Vec3d& center = m_grabbers.front().center; const Vec3d& center = m_grabbers.front().center;
glsafe(::glPushMatrix()); #if ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES
glsafe(::glTranslated(center.x(), center.y(), center.z())); if (use_attributes) {
glsafe(::glRotated(Geometry::rad2deg(m_angle), 0.0, 0.0, 1.0)); const Transform3d trafo = wxGetApp().plater()->get_camera().get_projection_view_matrix() * m_grabbers.front().matrix *
glsafe(::glRotated(90.0, 1.0, 0.0, 0.0)); Geometry::assemble_transform(center, Vec3d(0.5 * PI, 0.0, m_angle)) *
glsafe(::glTranslated(0.0, 0.0, 2.0 * size)); Geometry::assemble_transform(Vec3d(0.0, 0.0, 2.0 * size), Vec3d::Zero(), Vec3d(0.75 * size, 0.75 * size, 3.0 * size));
glsafe(::glScaled(0.75 * size, 0.75 * size, 3.0 * size)); shader->set_uniform("projection_view_model_matrix", trafo);
}
else {
#endif // ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES
glsafe(::glPushMatrix());
glsafe(::glTranslated(center.x(), center.y(), center.z()));
glsafe(::glRotated(Geometry::rad2deg(m_angle), 0.0, 0.0, 1.0));
glsafe(::glRotated(90.0, 1.0, 0.0, 0.0));
glsafe(::glTranslated(0.0, 0.0, 2.0 * size));
glsafe(::glScaled(0.75 * size, 0.75 * size, 3.0 * size));
#if ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES
}
#endif // ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES
m_cone.render(); m_cone.render();
glsafe(::glPopMatrix()); #if ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES
glsafe(::glPushMatrix()); if (use_attributes) {
glsafe(::glTranslated(center.x(), center.y(), center.z())); const Transform3d trafo = wxGetApp().plater()->get_camera().get_projection_view_matrix() * m_grabbers.front().matrix *
glsafe(::glRotated(Geometry::rad2deg(m_angle), 0.0, 0.0, 1.0)); Geometry::assemble_transform(center, Vec3d(-0.5 * PI, 0.0, m_angle)) *
glsafe(::glRotated(-90.0, 1.0, 0.0, 0.0)); Geometry::assemble_transform(Vec3d(0.0, 0.0, 2.0 * size), Vec3d::Zero(), Vec3d(0.75 * size, 0.75 * size, 3.0 * size));
glsafe(::glTranslated(0.0, 0.0, 2.0 * size)); shader->set_uniform("projection_view_model_matrix", trafo);
glsafe(::glScaled(0.75 * size, 0.75 * size, 3.0 * size)); }
else {
#endif // ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES
glsafe(::glPopMatrix());
glsafe(::glPushMatrix());
glsafe(::glTranslated(center.x(), center.y(), center.z()));
glsafe(::glRotated(Geometry::rad2deg(m_angle), 0.0, 0.0, 1.0));
glsafe(::glRotated(-90.0, 1.0, 0.0, 0.0));
glsafe(::glTranslated(0.0, 0.0, 2.0 * size));
glsafe(::glScaled(0.75 * size, 0.75 * size, 3.0 * size));
#if ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES
}
#endif // ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES
m_cone.render(); m_cone.render();
glsafe(::glPopMatrix()); #if ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES
if (!use_attributes)
#endif // ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES
glsafe(::glPopMatrix());
#if !ENABLE_GLBEGIN_GLEND_REMOVAL #if !ENABLE_GLBEGIN_GLEND_REMOVAL
if (! picking) if (! picking)
@ -595,6 +635,38 @@ void GLGizmoRotate::render_grabber_extension(const BoundingBoxf3& box, bool pick
shader->stop_using(); shader->stop_using();
} }
#if ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES
Transform3d GLGizmoRotate::local_transform(const Selection& selection) const
{
Transform3d ret;
switch (m_axis)
{
case X:
{
ret = Geometry::assemble_transform(Vec3d::Zero(), Vec3d(0.0, 0.5 * PI, 0.0)) * Geometry::assemble_transform(Vec3d::Zero(), Vec3d(0.0, 0.0, -0.5 * PI));
break;
}
case Y:
{
ret = Geometry::assemble_transform(Vec3d::Zero(), Vec3d(0.0, 0.0, -0.5 * PI)) * Geometry::assemble_transform(Vec3d::Zero(), Vec3d(0.0, -0.5 * PI, 0.0));
break;
}
default:
case Z:
{
ret = Transform3d::Identity();
break;
}
}
if (selection.is_single_volume() || selection.is_single_modifier() || selection.requires_local_axes())
ret = selection.get_volume(*selection.get_volume_idxs().begin())->get_instance_transformation().get_matrix(true, false, true, true) * ret;
return Geometry::assemble_transform(m_center) * ret;
}
#endif // ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES
void GLGizmoRotate::transform_to_local(const Selection& selection) const void GLGizmoRotate::transform_to_local(const Selection& selection) const
{ {
glsafe(::glTranslated(m_center.x(), m_center.y(), m_center.z())); glsafe(::glTranslated(m_center.x(), m_center.y(), m_center.z()));

View File

@ -106,6 +106,10 @@ private:
void render_grabber(const BoundingBoxf3& box); void render_grabber(const BoundingBoxf3& box);
void render_grabber_extension(const BoundingBoxf3& box, bool picking); void render_grabber_extension(const BoundingBoxf3& box, bool picking);
#if ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES
Transform3d local_transform(const Selection& selection) const;
#endif // ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES
void transform_to_local(const Selection& selection) const; void transform_to_local(const Selection& selection) const;
// returns the intersection of the mouse ray with the plane perpendicular to the gizmo axis, in local coordinate // returns the intersection of the mouse ray with the plane perpendicular to the gizmo axis, in local coordinate
Vec3d mouse_position_in_local_plane(const Linef3& mouse_ray, const Selection& selection) const; Vec3d mouse_position_in_local_plane(const Linef3& mouse_ray, const Selection& selection) const;