From 0995cfc658dfbfdbc61fde451d32d06a9195b9a7 Mon Sep 17 00:00:00 2001
From: enricoturri1966 <enricoturri@seznam.cz>
Date: Thu, 7 Apr 2022 10:40:33 +0200
Subject: [PATCH] Shader mm_contour - Correction to avoid z-fighting moved from
 fragment shader to vertex shader

---
 resources/shaders/110/mm_contour.fs              | 5 -----
 resources/shaders/110/mm_contour.vs              | 5 ++++-
 resources/shaders/140/mm_contour.fs              | 5 -----
 resources/shaders/140/mm_contour.vs              | 5 ++++-
 resources/shaders/mm_contour.fs                  | 5 -----
 resources/shaders/mm_contour.vs                  | 5 ++++-
 src/slic3r/GUI/Gizmos/GLGizmoMmuSegmentation.cpp | 4 ----
 src/slic3r/GUI/Gizmos/GLGizmoPainterBase.cpp     | 7 -------
 8 files changed, 12 insertions(+), 29 deletions(-)

diff --git a/resources/shaders/110/mm_contour.fs b/resources/shaders/110/mm_contour.fs
index 14477a59e..ab656998d 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 d9063f0c7..f75ff1077 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/mm_contour.fs b/resources/shaders/140/mm_contour.fs
index 3681d76c1..e74124dca 100644
--- a/resources/shaders/140/mm_contour.fs
+++ b/resources/shaders/140/mm_contour.fs
@@ -1,13 +1,8 @@
 #version 140
 
-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/140/mm_contour.vs b/resources/shaders/140/mm_contour.vs
index 7042671de..2f6419b8d 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/mm_contour.fs b/resources/shaders/mm_contour.fs
index 8ccf5b832..14c18dcf1 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 d0d3ee42a..f85fefb80 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/Gizmos/GLGizmoMmuSegmentation.cpp b/src/slic3r/GUI/Gizmos/GLGizmoMmuSegmentation.cpp
index 308100d7f..2c70e536a 100644
--- a/src/slic3r/GUI/Gizmos/GLGizmoMmuSegmentation.cpp
+++ b/src/slic3r/GUI/Gizmos/GLGizmoMmuSegmentation.cpp
@@ -627,11 +627,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 122619dd5..400dfcd8b 100644
--- a/src/slic3r/GUI/Gizmos/GLGizmoPainterBase.cpp
+++ b/src/slic3r/GUI/Gizmos/GLGizmoPainterBase.cpp
@@ -967,11 +967,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
@@ -1372,10 +1368,7 @@ void TriangleSelectorGUI::render_paint_contour()
         contour_shader->set_uniform("projection_matrix", camera.get_projection_matrix());
 #endif // ENABLE_GL_SHADERS_ATTRIBUTES
 
-        glsafe(::glDepthFunc(GL_LEQUAL));
         m_paint_contour.render();
-        glsafe(::glDepthFunc(GL_LESS));
-
         contour_shader->stop_using();
     }