From 66fa2b1e2685b9b01232b89488a97d6d413ea240 Mon Sep 17 00:00:00 2001 From: enricoturri1966 Date: Tue, 8 Mar 2022 10:57:43 +0100 Subject: [PATCH] Tech ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES - Use vertex attributes and matrices in shaders. Shader: mm_contour --- resources/shaders/mm_contour_attr.fs | 18 +++++++++++ resources/shaders/mm_contour_attr.vs | 16 ++++++++++ src/slic3r/GUI/GLShadersManager.cpp | 4 +++ .../GUI/Gizmos/GLGizmoMmuSegmentation.cpp | 14 +++++++- .../GUI/Gizmos/GLGizmoMmuSegmentation.hpp | 4 +++ src/slic3r/GUI/Gizmos/GLGizmoPainterBase.cpp | 32 +++++++++++++++++-- src/slic3r/GUI/Gizmos/GLGizmoPainterBase.hpp | 9 ++++++ src/slic3r/GUI/Gizmos/GLGizmoSimplify.cpp | 21 ++++++++++-- src/slic3r/GUI/Gizmos/GLGizmosCommon.cpp | 21 +++++++++--- 9 files changed, 129 insertions(+), 10 deletions(-) create mode 100644 resources/shaders/mm_contour_attr.fs create mode 100644 resources/shaders/mm_contour_attr.vs diff --git a/resources/shaders/mm_contour_attr.fs b/resources/shaders/mm_contour_attr.fs new file mode 100644 index 000000000..a8f0a0554 --- /dev/null +++ b/resources/shaders/mm_contour_attr.fs @@ -0,0 +1,18 @@ +#version 110 + +//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +uniform vec4 uniform_color; +//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ + +const float EPSILON = 0.0001; + +void main() +{ +//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ + gl_FragColor = uniform_color; +// gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ + // Values inside depth buffer for fragments of the contour of a selected area are offset + // by small epsilon to solve z-fighting between painted triangles and contour lines. + gl_FragDepth = gl_FragCoord.z - EPSILON; +} diff --git a/resources/shaders/mm_contour_attr.vs b/resources/shaders/mm_contour_attr.vs new file mode 100644 index 000000000..871fbb14b --- /dev/null +++ b/resources/shaders/mm_contour_attr.vs @@ -0,0 +1,16 @@ +#version 110 + +//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +attribute vec3 v_position; + +uniform mat4 view_model_matrix; +uniform mat4 projection_matrix; +//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ + +void main() +{ +//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ + gl_Position = projection_matrix * view_model_matrix * vec4(v_position, 1.0); +// gl_Position = ftransform(); +//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +} diff --git a/src/slic3r/GUI/GLShadersManager.cpp b/src/slic3r/GUI/GLShadersManager.cpp index 091e59818..c565957a9 100644 --- a/src/slic3r/GUI/GLShadersManager.cpp +++ b/src/slic3r/GUI/GLShadersManager.cpp @@ -98,7 +98,11 @@ std::pair GLShadersManager::init() // used to render variable layers heights in 3d editor valid &= append_shader("variable_layer_height", { "variable_layer_height.vs", "variable_layer_height.fs" }); // used to render highlight contour around selected triangles inside the multi-material gizmo +#if ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES + valid &= append_shader("mm_contour_attr", { "mm_contour_attr.vs", "mm_contour_attr.fs" }); +#else valid &= append_shader("mm_contour", { "mm_contour.vs", "mm_contour.fs" }); +#endif // ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES // Used to render painted triangles inside the multi-material gizmo. Triangle normals are computed inside fragment shader. // For Apple's on Arm CPU computed triangle normals inside fragment shader using dFdx and dFdy has the opposite direction. // Because of this, objects had darker colors inside the multi-material gizmo. diff --git a/src/slic3r/GUI/Gizmos/GLGizmoMmuSegmentation.cpp b/src/slic3r/GUI/Gizmos/GLGizmoMmuSegmentation.cpp index 24e90fceb..ec5eb94e5 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmoMmuSegmentation.cpp +++ b/src/slic3r/GUI/Gizmos/GLGizmoMmuSegmentation.cpp @@ -188,7 +188,7 @@ void GLGizmoMmuSegmentation::render_triangles(const Selection &selection) const const Transform3d trafo_matrix = mo->instances[selection.get_instance_idx()]->get_transformation().get_matrix() * mv->get_matrix(); - bool is_left_handed = trafo_matrix.matrix().determinant() < 0.; + const bool is_left_handed = trafo_matrix.matrix().determinant() < 0.0; if (is_left_handed) glsafe(::glFrontFace(GL_CW)); @@ -197,7 +197,11 @@ void GLGizmoMmuSegmentation::render_triangles(const Selection &selection) const shader->set_uniform("volume_world_matrix", trafo_matrix); shader->set_uniform("volume_mirrored", is_left_handed); +#if ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES + m_triangle_selectors[mesh_id]->render(m_imgui, trafo_matrix); +#else m_triangle_selectors[mesh_id]->render(m_imgui); +#endif // ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES glsafe(::glPopMatrix()); if (is_left_handed) @@ -568,7 +572,11 @@ ColorRGBA GLGizmoMmuSegmentation::get_cursor_sphere_right_button_color() const return color; } +#if ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES +void TriangleSelectorMmGui::render(ImGuiWrapper* imgui, const Transform3d& matrix) +#else void TriangleSelectorMmGui::render(ImGuiWrapper *imgui) +#endif // ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES { if (m_update_render_data) update_render_data(); @@ -589,7 +597,11 @@ void TriangleSelectorMmGui::render(ImGuiWrapper *imgui) } #if ENABLE_GLBEGIN_GLEND_REMOVAL +#if ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES + render_paint_contour(matrix); +#else render_paint_contour(); +#endif // ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES #else if (m_paint_contour.has_VBO()) { ScopeGuard guard_mm_gouraud([shader]() { shader->start_using(); }); diff --git a/src/slic3r/GUI/Gizmos/GLGizmoMmuSegmentation.hpp b/src/slic3r/GUI/Gizmos/GLGizmoMmuSegmentation.hpp index 6f357bb6f..21835f902 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmoMmuSegmentation.hpp +++ b/src/slic3r/GUI/Gizmos/GLGizmoMmuSegmentation.hpp @@ -66,9 +66,13 @@ public: : TriangleSelectorGUI(mesh), m_colors(colors), m_default_volume_color(default_volume_color), m_gizmo_scene(2 * (colors.size() + 1)) {} ~TriangleSelectorMmGui() override = default; +#if ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES + void render(ImGuiWrapper* imgui, const Transform3d& matrix) override; +#else // Render current selection. Transformation matrices are supposed // to be already set. void render(ImGuiWrapper* imgui) override; +#endif // ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES private: void update_render_data(); diff --git a/src/slic3r/GUI/Gizmos/GLGizmoPainterBase.cpp b/src/slic3r/GUI/Gizmos/GLGizmoPainterBase.cpp index 7910c6eb1..a9e869601 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmoPainterBase.cpp +++ b/src/slic3r/GUI/Gizmos/GLGizmoPainterBase.cpp @@ -126,11 +126,13 @@ void GLGizmoPainterBase::render_triangles(const Selection& selection) const // wrong transformation matrix is used for "Clipping of view". shader->set_uniform("volume_world_matrix", trafo_matrix); +#if ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES + m_triangle_selectors[mesh_id]->render(m_imgui, trafo_matrix); +#else m_triangle_selectors[mesh_id]->render(m_imgui); -#if !ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES glsafe(::glPopMatrix()); -#endif // !ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES +#endif // ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES if (is_left_handed) glsafe(::glFrontFace(GL_CCW)); } @@ -908,7 +910,11 @@ ColorRGBA TriangleSelectorGUI::get_seed_fill_color(const ColorRGBA& base_color) return saturate(base_color, 0.75f); } +#if ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES +void TriangleSelectorGUI::render(ImGuiWrapper* imgui, const Transform3d& matrix) +#else void TriangleSelectorGUI::render(ImGuiWrapper* imgui) +#endif // ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES { static const ColorRGBA enforcers_color = { 0.47f, 0.47f, 1.0f, 1.0f }; static const ColorRGBA blockers_color = { 1.0f, 0.44f, 0.44f, 1.0f }; @@ -963,7 +969,11 @@ void TriangleSelectorGUI::render(ImGuiWrapper* imgui) #endif // ENABLE_GLINDEXEDVERTEXARRAY_REMOVAL #if ENABLE_GLBEGIN_GLEND_REMOVAL +#if ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES + render_paint_contour(matrix); +#else render_paint_contour(); +#endif // ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES #else if (m_paint_contour.has_VBO()) { ScopeGuard guard_gouraud([shader]() { shader->start_using(); }); @@ -1340,6 +1350,10 @@ void TriangleSelectorGUI::update_paint_contour() init_data.format = { GLModel::Geometry::EPrimitiveType::Lines, GLModel::Geometry::EVertexLayout::P3, GLModel::Geometry::index_type(2 * contour_edges.size()) }; init_data.reserve_vertices(2 * contour_edges.size()); init_data.reserve_indices(2 * contour_edges.size()); +#if ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES + init_data.color = ColorRGBA::WHITE(); +#endif // ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES +// // vertices + indices unsigned int vertices_count = 0; for (const Vec2i& edge : contour_edges) { @@ -1356,16 +1370,30 @@ void TriangleSelectorGUI::update_paint_contour() m_paint_contour.init_from(std::move(init_data)); } +#if ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES +void TriangleSelectorGUI::render_paint_contour(const Transform3d& matrix) +#else void TriangleSelectorGUI::render_paint_contour() +#endif // ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES { auto* curr_shader = wxGetApp().get_current_shader(); if (curr_shader != nullptr) curr_shader->stop_using(); +#if ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES + auto* contour_shader = wxGetApp().get_shader("mm_contour_attr"); +#else auto* contour_shader = wxGetApp().get_shader("mm_contour"); +#endif // ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES if (contour_shader != nullptr) { contour_shader->start_using(); +#if ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES + const Camera& camera = wxGetApp().plater()->get_camera(); + contour_shader->set_uniform("view_model_matrix", camera.get_view_matrix() * matrix); + contour_shader->set_uniform("projection_matrix", camera.get_projection_matrix()); +#endif // ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES + glsafe(::glDepthFunc(GL_LEQUAL)); m_paint_contour.render(); glsafe(::glDepthFunc(GL_LESS)); diff --git a/src/slic3r/GUI/Gizmos/GLGizmoPainterBase.hpp b/src/slic3r/GUI/Gizmos/GLGizmoPainterBase.hpp index 7949e2657..e3b5d0ea7 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmoPainterBase.hpp +++ b/src/slic3r/GUI/Gizmos/GLGizmoPainterBase.hpp @@ -77,10 +77,15 @@ public: : TriangleSelector(mesh) {} virtual ~TriangleSelectorGUI() = default; +#if ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES + virtual void render(ImGuiWrapper* imgui, const Transform3d& matrix); + void render(const Transform3d& matrix) { this->render(nullptr, matrix); } +#else // Render current selection. Transformation matrices are supposed // to be already set. virtual void render(ImGuiWrapper *imgui); void render() { this->render(nullptr); } +#endif // ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES void request_update_render_data() { m_update_render_data = true; } @@ -117,7 +122,11 @@ protected: GLModel m_paint_contour; void update_paint_contour(); +#if ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES + void render_paint_contour(const Transform3d& matrix); +#else void render_paint_contour(); +#endif // ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES #else GLPaintContour m_paint_contour; #endif // ENABLE_GLBEGIN_GLEND_REMOVAL diff --git a/src/slic3r/GUI/Gizmos/GLGizmoSimplify.cpp b/src/slic3r/GUI/Gizmos/GLGizmoSimplify.cpp index e67a9b7a7..252f5a185 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmoSimplify.cpp +++ b/src/slic3r/GUI/Gizmos/GLGizmoSimplify.cpp @@ -737,12 +737,12 @@ void GLGizmoSimplify::on_render() GLModel &glmodel = it->second; const Transform3d trafo_matrix = selected_volume->world_matrix(); - glsafe(::glPushMatrix()); - glsafe(::glMultMatrixd(trafo_matrix.data())); - #if ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES auto* gouraud_shader = wxGetApp().get_shader("gouraud_light_attr"); #else + glsafe(::glPushMatrix()); + glsafe(::glMultMatrixd(trafo_matrix.data())); + auto *gouraud_shader = wxGetApp().get_shader("gouraud_light"); #endif // ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES glsafe(::glPushAttrib(GL_DEPTH_TEST)); @@ -759,16 +759,31 @@ void GLGizmoSimplify::on_render() gouraud_shader->stop_using(); if (m_show_wireframe) { +#if ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES + auto* contour_shader = wxGetApp().get_shader("mm_contour_attr"); +#else auto *contour_shader = wxGetApp().get_shader("mm_contour"); +#endif // ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES contour_shader->start_using(); +#if ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES + contour_shader->set_uniform("view_model_matrix", view_model_matrix); + contour_shader->set_uniform("projection_matrix", camera.get_projection_matrix()); + const ColorRGBA color = glmodel.get_color(); + glmodel.set_color(ColorRGBA::WHITE()); +#endif // ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES glsafe(::glLineWidth(1.0f)); glsafe(::glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)); glmodel.render(); glsafe(::glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)); +#if ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES + glmodel.set_color(color); +#endif // ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES contour_shader->stop_using(); } glsafe(::glPopAttrib()); +#if !ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES glsafe(::glPopMatrix()); +#endif // !ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES } } diff --git a/src/slic3r/GUI/Gizmos/GLGizmosCommon.cpp b/src/slic3r/GUI/Gizmos/GLGizmosCommon.cpp index 1e49ebc8c..705b541ad 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmosCommon.cpp +++ b/src/slic3r/GUI/Gizmos/GLGizmosCommon.cpp @@ -209,10 +209,13 @@ void InstancesHider::render_cut() const ClippingPlane clp = *get_pool()->object_clipper()->get_clipping_plane(); clp.set_normal(-clp.get_normal()); clipper->set_limiting_plane(clp); - } else + } + else clipper->set_limiting_plane(ClippingPlane::ClipsNothing()); +#if !ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES glsafe(::glPushMatrix()); +#endif // !ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES #if !ENABLE_GLINDEXEDVERTEXARRAY_REMOVAL if (mv->is_model_part()) glsafe(::glColor3f(0.8f, 0.3f, 0.0f)); @@ -229,7 +232,9 @@ void InstancesHider::render_cut() const clipper->render_cut(); #endif // ENABLE_GLINDEXEDVERTEXARRAY_REMOVAL glsafe(::glPopAttrib()); +#if !ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES glsafe(::glPopMatrix()); +#endif // !ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES ++clipper_id; } @@ -410,11 +415,11 @@ void ObjectClipper::render_cut() const return; const SelectionInfo* sel_info = get_pool()->selection_info(); const ModelObject* mo = sel_info->model_object(); - Geometry::Transformation inst_trafo = mo->instances[sel_info->get_active_instance()]->get_transformation(); + const Geometry::Transformation inst_trafo = mo->instances[sel_info->get_active_instance()]->get_transformation(); size_t clipper_id = 0; for (const ModelVolume* mv : mo->volumes) { - Geometry::Transformation vol_trafo = mv->get_transformation(); + const Geometry::Transformation vol_trafo = mv->get_transformation(); Geometry::Transformation trafo = inst_trafo * vol_trafo; trafo.set_offset(trafo.get_offset() + Vec3d(0., 0., sel_info->get_sla_shift())); @@ -422,14 +427,18 @@ void ObjectClipper::render_cut() const clipper->set_plane(*m_clp); clipper->set_transformation(trafo); clipper->set_limiting_plane(ClippingPlane(Vec3d::UnitZ(), -SINKING_Z_THRESHOLD)); +#if !ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES glsafe(::glPushMatrix()); +#endif // !ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES #if ENABLE_GLINDEXEDVERTEXARRAY_REMOVAL clipper->render_cut({ 1.0f, 0.37f, 0.0f, 1.0f }); #else glsafe(::glColor3f(1.0f, 0.37f, 0.0f)); clipper->render_cut(); #endif // ENABLE_GLINDEXEDVERTEXARRAY_REMOVAL +#if !ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES glsafe(::glPopMatrix()); +#endif // !ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES ++clipper_id; } @@ -520,7 +529,7 @@ void SupportsClipper::render_cut() const const SelectionInfo* sel_info = get_pool()->selection_info(); const ModelObject* mo = sel_info->model_object(); - Geometry::Transformation inst_trafo = mo->instances[sel_info->get_active_instance()]->get_transformation(); + const Geometry::Transformation inst_trafo = mo->instances[sel_info->get_active_instance()]->get_transformation(); //Geometry::Transformation vol_trafo = mo->volumes.front()->get_transformation(); Geometry::Transformation trafo = inst_trafo;// * vol_trafo; trafo.set_offset(trafo.get_offset() + Vec3d(0., 0., sel_info->get_sla_shift())); @@ -539,14 +548,18 @@ void SupportsClipper::render_cut() const m_clipper->set_plane(*ocl->get_clipping_plane()); m_clipper->set_transformation(supports_trafo); +#if !ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES glsafe(::glPushMatrix()); +#endif // !ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES #if ENABLE_GLINDEXEDVERTEXARRAY_REMOVAL m_clipper->render_cut({ 1.0f, 0.f, 0.37f, 1.0f }); #else glsafe(::glColor3f(1.0f, 0.f, 0.37f)); m_clipper->render_cut(); #endif // ENABLE_GLINDEXEDVERTEXARRAY_REMOVAL +#if !ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES glsafe(::glPopMatrix()); +#endif // !ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES }