From 237958819644c62e5e4e09b48a49d15f2e77cf3d Mon Sep 17 00:00:00 2001
From: enricoturri1966 <enricoturri@seznam.cz>
Date: Mon, 28 Feb 2022 10:21:03 +0100
Subject: [PATCH] Tech ENABLE_GLBEGIN_GLEND_REMOVAL - Fix in
 GLGizmoRotate::render_angle_arc():

Tech ENABLE_GLINDEXEDVERTEXARRAY_REMOVAL - Fix in GLModel::render()
---
 src/slic3r/GUI/GLModel.cpp              |  2 +-
 src/slic3r/GUI/Gizmos/GLGizmoRotate.cpp | 28 ++++++++++++++-----------
 src/slic3r/GUI/Gizmos/GLGizmoRotate.hpp |  1 +
 3 files changed, 18 insertions(+), 13 deletions(-)

diff --git a/src/slic3r/GUI/GLModel.cpp b/src/slic3r/GUI/GLModel.cpp
index 8620c7eaf..f0eac9a24 100644
--- a/src/slic3r/GUI/GLModel.cpp
+++ b/src/slic3r/GUI/GLModel.cpp
@@ -998,7 +998,7 @@ void GLModel::render(const std::pair<size_t, size_t>& range)
     shader->set_uniform("uniform_color", data.color);
 
     glsafe(::glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_render_data.ibo_id));
-    glsafe(::glDrawElements(mode, range.second - range.first + 1, index_type, (const void*)(range.first * Geometry::index_stride_bytes(data.format))));
+    glsafe(::glDrawElements(mode, range.second - range.first, index_type, (const void*)(range.first * Geometry::index_stride_bytes(data.format))));
     glsafe(::glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0));
 
     if (tex_coord)
diff --git a/src/slic3r/GUI/Gizmos/GLGizmoRotate.cpp b/src/slic3r/GUI/Gizmos/GLGizmoRotate.cpp
index c97a4a788..a09ac8057 100644
--- a/src/slic3r/GUI/Gizmos/GLGizmoRotate.cpp
+++ b/src/slic3r/GUI/Gizmos/GLGizmoRotate.cpp
@@ -461,22 +461,26 @@ void GLGizmoRotate::render_angle() const
     const float ex_radius = m_radius * (1.0f + GrabberOffset);
 
 #if ENABLE_GLBEGIN_GLEND_REMOVAL
-    if (!m_angle_arc.is_initialized() || radius_changed) {
+    const bool angle_changed = std::abs(m_old_angle - m_angle) > EPSILON;
+    m_old_angle = m_angle;
+
+    if (!m_angle_arc.is_initialized() || radius_changed || angle_changed) {
         m_angle_arc.reset();
+        if (m_angle > 0.0f) {
+            GLModel::Geometry init_data;
+            init_data.format = { GLModel::Geometry::EPrimitiveType::LineStrip, GLModel::Geometry::EVertexLayout::P3, GLModel::Geometry::EIndexType::USHORT };
+            init_data.reserve_vertices(1 + AngleResolution);
+            init_data.reserve_indices(1 + AngleResolution);
 
-        GLModel::Geometry init_data;
-        init_data.format = { GLModel::Geometry::EPrimitiveType::LineStrip, GLModel::Geometry::EVertexLayout::P3, GLModel::Geometry::EIndexType::USHORT };
-        init_data.reserve_vertices(1 + AngleResolution);
-        init_data.reserve_indices(1 + AngleResolution);
+            // vertices + indices
+            for (unsigned short i = 0; i <= AngleResolution; ++i) {
+                const float angle = float(i) * step_angle;
+                init_data.add_vertex(Vec3f(::cos(angle) * ex_radius, ::sin(angle) * ex_radius, 0.0f));
+                init_data.add_ushort_index(i);
+            }
 
-        // vertices + indices
-        for (unsigned short i = 0; i <= AngleResolution; ++i) {
-            const float angle = float(i) * step_angle;
-            init_data.add_vertex(Vec3f(::cos(angle) * ex_radius, ::sin(angle) * ex_radius, 0.0f));
-            init_data.add_ushort_index(i);
+            m_angle_arc.init_from(std::move(init_data));
         }
-
-        m_angle_arc.init_from(std::move(init_data));
     }
 
     m_angle_arc.set_color(color);
diff --git a/src/slic3r/GUI/Gizmos/GLGizmoRotate.hpp b/src/slic3r/GUI/Gizmos/GLGizmoRotate.hpp
index 02dd84e68..d0474df0e 100644
--- a/src/slic3r/GUI/Gizmos/GLGizmoRotate.hpp
+++ b/src/slic3r/GUI/Gizmos/GLGizmoRotate.hpp
@@ -50,6 +50,7 @@ private:
     GrabberConnection m_grabber_connection;
     float m_old_radius{ 0.0f };
     float m_old_hover_radius{ 0.0f };
+    float m_old_angle{ 0.0f };
 #endif // ENABLE_GLBEGIN_GLEND_REMOVAL
 
     ColorRGBA m_drag_color;