From 9c365da8285ffdfeee22290e8222d2060702af09 Mon Sep 17 00:00:00 2001 From: Lukas Matena Date: Thu, 21 May 2020 09:45:10 +0200 Subject: [PATCH] Removed immediate OpenGL mode calls in place on face gizmo --- src/slic3r/GUI/Gizmos/GLGizmoFlatten.cpp | 45 +++++++++++++----------- src/slic3r/GUI/Gizmos/GLGizmoFlatten.hpp | 3 +- 2 files changed, 27 insertions(+), 21 deletions(-) diff --git a/src/slic3r/GUI/Gizmos/GLGizmoFlatten.cpp b/src/slic3r/GUI/Gizmos/GLGizmoFlatten.cpp index 7f33916cd..3f2c0ea46 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmoFlatten.cpp +++ b/src/slic3r/GUI/Gizmos/GLGizmoFlatten.cpp @@ -1,7 +1,6 @@ // Include GLGizmoBase.hpp before I18N.hpp as it includes some libigl code, which overrides our localization "L" macro. #include "GLGizmoFlatten.hpp" #include "slic3r/GUI/GLCanvas3D.hpp" -#include "slic3r/GUI/GUI_App.hpp" #include "slic3r/GUI/Gizmos/GLGizmosCommon.hpp" #include @@ -80,12 +79,7 @@ void GLGizmoFlatten::on_render() const else glsafe(::glColor4f(0.9f, 0.9f, 0.9f, 0.5f)); - ::glBegin(GL_POLYGON); - for (const Vec3d& vertex : m_planes[i].vertices) - { - ::glVertex3dv(vertex.data()); - } - glsafe(::glEnd()); + m_planes[i].vbo.render(); } glsafe(::glPopMatrix()); } @@ -112,12 +106,7 @@ void GLGizmoFlatten::on_render_for_picking() const for (int i = 0; i < (int)m_planes.size(); ++i) { glsafe(::glColor4fv(picking_color_component(i).data())); - ::glBegin(GL_POLYGON); - for (const Vec3d& vertex : m_planes[i].vertices) - { - ::glVertex3dv(vertex.data()); - } - glsafe(::glEnd()); + m_planes[i].vbo.render(); } glsafe(::glPopMatrix()); } @@ -181,7 +170,7 @@ void GLGizmoFlatten::update_planes() if (std::abs(this_normal(0) - (*normal_ptr)(0)) < 0.001 && std::abs(this_normal(1) - (*normal_ptr)(1)) < 0.001 && std::abs(this_normal(2) - (*normal_ptr)(2)) < 0.001) { stl_vertex* first_vertex = ch.stl.facet_start[facet_idx].vertex; for (int j=0; j<3; ++j) - m_planes.back().vertices.emplace_back((double)first_vertex[j](0), (double)first_vertex[j](1), (double)first_vertex[j](2)); + m_planes.back().vertices.emplace_back(first_vertex[j].cast()); facet_visited[facet_idx] = true; for (int j = 0; j < 3; ++ j) { @@ -193,15 +182,16 @@ void GLGizmoFlatten::update_planes() } m_planes.back().normal = normal_ptr->cast(); + Pointf3s& verts = m_planes.back().vertices; // Now we'll transform all the points into world coordinates, so that the areas, angles and distances // make real sense. - m_planes.back().vertices = transform(m_planes.back().vertices, inst_matrix); + verts = transform(verts, inst_matrix); // if this is a just a very small triangle, remove it to speed up further calculations (it would be rejected later anyway): - if (m_planes.back().vertices.size() == 3 && - ((m_planes.back().vertices[0] - m_planes.back().vertices[1]).norm() < minimal_side - || (m_planes.back().vertices[0] - m_planes.back().vertices[2]).norm() < minimal_side - || (m_planes.back().vertices[1] - m_planes.back().vertices[2]).norm() < minimal_side)) + if (verts.size() == 3 && + ((verts[0] - verts[1]).norm() < minimal_side + || (verts[0] - verts[2]).norm() < minimal_side + || (verts[1] - verts[2]).norm() < minimal_side)) m_planes.pop_back(); } @@ -246,7 +236,7 @@ void GLGizmoFlatten::update_planes() discard = true; else { // We also check the inner angles and discard polygons with angles smaller than the following threshold - const double angle_threshold = ::cos(10.0 * (double)PI / 180.0); + constexpr double angle_threshold = ::cos(10.0 * (double)PI / 180.0); for (unsigned int i = 0; i < polygon.size(); ++i) { const Vec3d& prec = polygon[(i == 0) ? polygon.size() - 1 : i - 1]; @@ -334,6 +324,21 @@ void GLGizmoFlatten::update_planes() m_first_instance_mirror = mo->instances.front()->get_mirror(); m_old_model_object = mo; + // And finally create respective VBOs. The polygon is convex with + // the vertices in order, so triangulation is trivial. + for (auto& plane : m_planes) { + plane.vbo.reserve(plane.vertices.size()); + for (const auto& vert : plane.vertices) + plane.vbo.push_geometry(vert, plane.normal); + for (size_t i=1; i vertices; + std::vector vertices; // should be in fact local in update_planes() + GLIndexedVertexArray vbo; Vec3d normal; float area; };