diff --git a/resources/shaders/110/gouraud.fs b/resources/shaders/110/gouraud.fs index 0742f6cc88..cf3163fa37 100644 --- a/resources/shaders/110/gouraud.fs +++ b/resources/shaders/110/gouraud.fs @@ -26,8 +26,6 @@ struct SlopeDetection uniform vec4 uniform_color; uniform SlopeDetection slope; -uniform bool offset_depth_buffer; - #ifdef ENABLE_ENVIRONMENT_MAP uniform sampler2D environment_tex; uniform bool use_environment_tex; @@ -78,9 +76,4 @@ void main() else #endif gl_FragColor = vec4(vec3(intensity.y) + color * intensity.x, alpha); - - // In the support painting gizmo and the seam painting gizmo are painted triangles rendered over the already - // rendered object. To resolved z-fighting between previously rendered object and painted triangles, values - // inside the depth buffer are offset by small epsilon for painted triangles inside those gizmos. - gl_FragDepth = gl_FragCoord.z - (offset_depth_buffer ? EPSILON : 0.0); } diff --git a/resources/shaders/110/mm_contour.fs b/resources/shaders/110/mm_contour.fs index 14477a59e6..ab656998df 100644 --- a/resources/shaders/110/mm_contour.fs +++ b/resources/shaders/110/mm_contour.fs @@ -1,13 +1,8 @@ #version 110 -const float EPSILON = 0.0001; - uniform vec4 uniform_color; void main() { gl_FragColor = uniform_color; - // 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/110/mm_contour.vs b/resources/shaders/110/mm_contour.vs index d9063f0c70..f75ff1077d 100644 --- a/resources/shaders/110/mm_contour.vs +++ b/resources/shaders/110/mm_contour.vs @@ -7,5 +7,8 @@ attribute vec3 v_position; void main() { - gl_Position = projection_matrix * view_model_matrix * vec4(v_position, 1.0); + // Add small epsilon to z to solve z-fighting between painted triangles and contour lines. + vec4 clip_position = projection_matrix * view_model_matrix * vec4(v_position, 1.0); + clip_position.z -= 0.00001 * abs(clip_position.w); + gl_Position = clip_position; } diff --git a/resources/shaders/140/gouraud.fs b/resources/shaders/140/gouraud.fs index 225b11dbc0..d87e336321 100644 --- a/resources/shaders/140/gouraud.fs +++ b/resources/shaders/140/gouraud.fs @@ -26,8 +26,6 @@ struct SlopeDetection uniform vec4 uniform_color; uniform SlopeDetection slope; -uniform bool offset_depth_buffer; - #ifdef ENABLE_ENVIRONMENT_MAP uniform sampler2D environment_tex; uniform bool use_environment_tex; @@ -80,9 +78,4 @@ void main() else #endif out_color = vec4(vec3(intensity.y) + color * intensity.x, alpha); - - // In the support painting gizmo and the seam painting gizmo are painted triangles rendered over the already - // rendered object. To resolved z-fighting between previously rendered object and painted triangles, values - // inside the depth buffer are offset by small epsilon for painted triangles inside those gizmos. - gl_FragDepth = gl_FragCoord.z - (offset_depth_buffer ? EPSILON : 0.0); } diff --git a/resources/shaders/140/mm_contour.fs b/resources/shaders/140/mm_contour.fs index 7797023fa5..28642d53fe 100644 --- a/resources/shaders/140/mm_contour.fs +++ b/resources/shaders/140/mm_contour.fs @@ -1,7 +1,5 @@ #version 140 -const float EPSILON = 0.0001; - uniform vec4 uniform_color; out vec4 out_color; @@ -9,7 +7,4 @@ out vec4 out_color; void main() { out_color = uniform_color; - // 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/140/mm_contour.vs b/resources/shaders/140/mm_contour.vs index 7042671de2..2f6419b8d5 100644 --- a/resources/shaders/140/mm_contour.vs +++ b/resources/shaders/140/mm_contour.vs @@ -7,5 +7,8 @@ in vec3 v_position; void main() { - gl_Position = projection_matrix * view_model_matrix * vec4(v_position, 1.0); + // Add small epsilon to z to solve z-fighting between painted triangles and contour lines. + vec4 clip_position = projection_matrix * view_model_matrix * vec4(v_position, 1.0); + clip_position.z -= 0.00001 * abs(clip_position.w); + gl_Position = clip_position; } diff --git a/resources/shaders/gouraud.fs b/resources/shaders/gouraud.fs index 511faf4c00..1657fc0513 100644 --- a/resources/shaders/gouraud.fs +++ b/resources/shaders/gouraud.fs @@ -26,8 +26,6 @@ struct SlopeDetection uniform vec4 uniform_color; uniform SlopeDetection slope; -uniform bool offset_depth_buffer; - #ifdef ENABLE_ENVIRONMENT_MAP uniform sampler2D environment_tex; uniform bool use_environment_tex; @@ -78,9 +76,4 @@ void main() else #endif gl_FragColor = vec4(vec3(intensity.y) + color * intensity.x, alpha); - - // In the support painting gizmo and the seam painting gizmo are painted triangles rendered over the already - // rendered object. To resolved z-fighting between previously rendered object and painted triangles, values - // inside the depth buffer are offset by small epsilon for painted triangles inside those gizmos. - gl_FragDepth = gl_FragCoord.z - (offset_depth_buffer ? EPSILON : 0.0); } diff --git a/resources/shaders/mm_contour.fs b/resources/shaders/mm_contour.fs index 8ccf5b832c..14c18dcf16 100644 --- a/resources/shaders/mm_contour.fs +++ b/resources/shaders/mm_contour.fs @@ -1,11 +1,6 @@ #version 110 -const float EPSILON = 0.0001; - void main() { 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.vs b/resources/shaders/mm_contour.vs index d0d3ee42a9..f85fefb80e 100644 --- a/resources/shaders/mm_contour.vs +++ b/resources/shaders/mm_contour.vs @@ -2,5 +2,8 @@ void main() { - gl_Position = ftransform(); + // Add small epsilon to z to solve z-fighting between painted triangles and contour lines. + vec4 clip_position = gl_ModelViewProjectionMatrix * gl_Vertex; + clip_position.z -= 0.00001 * abs(clip_position.w); + gl_Position = clip_position; } diff --git a/src/slic3r/GUI/GLCanvas3D.cpp b/src/slic3r/GUI/GLCanvas3D.cpp index f53fd51e66..4af648aa0f 100644 --- a/src/slic3r/GUI/GLCanvas3D.cpp +++ b/src/slic3r/GUI/GLCanvas3D.cpp @@ -5333,7 +5333,7 @@ void GLCanvas3D::_picking_pass() #if !ENABLE_LEGACY_OPENGL_REMOVAL m_camera_clipping_plane = m_gizmos.get_clipping_plane(); if (m_camera_clipping_plane.is_active()) { - ::glClipPlane(GL_CLIP_PLANE0, (GLdouble*)m_camera_clipping_plane.get_data()); + ::glClipPlane(GL_CLIP_PLANE0, (GLdouble*)m_camera_clipping_plane.get_data().data()); ::glEnable(GL_CLIP_PLANE0); } #endif // !ENABLE_LEGACY_OPENGL_REMOVAL diff --git a/src/slic3r/GUI/Gizmos/GLGizmoMmuSegmentation.cpp b/src/slic3r/GUI/Gizmos/GLGizmoMmuSegmentation.cpp index 64451fed04..723ce46d59 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmoMmuSegmentation.cpp +++ b/src/slic3r/GUI/Gizmos/GLGizmoMmuSegmentation.cpp @@ -621,11 +621,7 @@ void TriangleSelectorMmGui::render(ImGuiWrapper *imgui) auto *contour_shader = wxGetApp().get_shader("mm_contour"); contour_shader->start_using(); - - glsafe(::glDepthFunc(GL_LEQUAL)); m_paint_contour.render(); - glsafe(::glDepthFunc(GL_LESS)); - contour_shader->stop_using(); } #endif // ENABLE_LEGACY_OPENGL_REMOVAL diff --git a/src/slic3r/GUI/Gizmos/GLGizmoPainterBase.cpp b/src/slic3r/GUI/Gizmos/GLGizmoPainterBase.cpp index a3128b9620..ea818b6f74 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmoPainterBase.cpp +++ b/src/slic3r/GUI/Gizmos/GLGizmoPainterBase.cpp @@ -316,9 +316,11 @@ void GLGizmoPainterBase::render_cursor_sphere(const Transform3d& trafo) const #endif // ENABLE_LEGACY_OPENGL_REMOVAL } +#if ENABLE_LEGACY_OPENGL_REMOVAL GLShaderProgram* shader = wxGetApp().get_shader("flat"); if (shader == nullptr) return; +#endif // ENABLE_LEGACY_OPENGL_REMOVAL const Transform3d complete_scaling_matrix_inverse = Geometry::Transformation(trafo).get_matrix(true, true, false, true).inverse(); const bool is_left_handed = Geometry::Transformation(trafo).is_left_handed(); @@ -948,8 +950,6 @@ void TriangleSelectorGUI::render(ImGuiWrapper* imgui) assert(shader->get_name() == "gouraud"); - ScopeGuard guard([shader]() { if (shader) shader->set_uniform("offset_depth_buffer", false);}); - shader->set_uniform("offset_depth_buffer", true); for (auto iva : {std::make_pair(&m_iva_enforcers, enforcers_color), std::make_pair(&m_iva_blockers, blockers_color)}) { #if ENABLE_LEGACY_OPENGL_REMOVAL @@ -993,11 +993,7 @@ void TriangleSelectorGUI::render(ImGuiWrapper* imgui) auto *contour_shader = wxGetApp().get_shader("mm_contour"); contour_shader->start_using(); - - glsafe(::glDepthFunc(GL_LEQUAL)); m_paint_contour.render(); - glsafe(::glDepthFunc(GL_LESS)); - contour_shader->stop_using(); } #endif // ENABLE_LEGACY_OPENGL_REMOVAL @@ -1040,6 +1036,9 @@ void TriangleSelectorGUI::update_render_data() iva.release_geometry(); #endif // ENABLE_LEGACY_OPENGL_REMOVAL + // small value used to offset triangles along their normal to avoid z-fighting + static const float offset = 0.001f; + for (const Triangle &tr : m_triangles) { if (!tr.valid() || tr.is_split() || (tr.get_state() == EnforcerBlockerType::NONE && !tr.is_selected_by_seed_fill())) continue; @@ -1063,15 +1062,17 @@ void TriangleSelectorGUI::update_render_data() //FIXME the normal may likely be pulled from m_triangle_selectors, but it may not be worth the effort // or the current implementation may be more cache friendly. const Vec3f n = (v1 - v0).cross(v2 - v1).normalized(); + // small value used to offset triangles along their normal to avoid z-fighting + const Vec3f offset_n = offset * n; #if ENABLE_LEGACY_OPENGL_REMOVAL - iva.add_vertex(v0, n); - iva.add_vertex(v1, n); - iva.add_vertex(v2, n); + iva.add_vertex(v0 + offset_n, n); + iva.add_vertex(v1 + offset_n, n); + iva.add_vertex(v2 + offset_n, n); iva.add_triangle((unsigned int)cnt, (unsigned int)cnt + 1, (unsigned int)cnt + 2); #else - iva.push_geometry(v0, n); - iva.push_geometry(v1, n); - iva.push_geometry(v2, n); + iva.push_geometry(v0 + offset_n, n); + iva.push_geometry(v1 + offset_n, n); + iva.push_geometry(v2 + offset_n, n); iva.push_triangle(cnt, cnt + 1, cnt + 2); #endif // ENABLE_LEGACY_OPENGL_REMOVAL cnt += 3; @@ -1383,10 +1384,7 @@ void TriangleSelectorGUI::render_paint_contour(const Transform3d& matrix) contour_shader->set_uniform("view_model_matrix", camera.get_view_matrix() * matrix); contour_shader->set_uniform("projection_matrix", camera.get_projection_matrix()); - glsafe(::glDepthFunc(GL_LEQUAL)); m_paint_contour.render(); - glsafe(::glDepthFunc(GL_LESS)); - contour_shader->stop_using(); }