Removed immediate OpenGL mode calls in place on face gizmo

This commit is contained in:
Lukas Matena 2020-05-21 09:45:10 +02:00
parent 6e597bb1ee
commit 9c365da828
2 changed files with 27 additions and 21 deletions

View file

@ -1,7 +1,6 @@
// Include GLGizmoBase.hpp before I18N.hpp as it includes some libigl code, which overrides our localization "L" macro. // Include GLGizmoBase.hpp before I18N.hpp as it includes some libigl code, which overrides our localization "L" macro.
#include "GLGizmoFlatten.hpp" #include "GLGizmoFlatten.hpp"
#include "slic3r/GUI/GLCanvas3D.hpp" #include "slic3r/GUI/GLCanvas3D.hpp"
#include "slic3r/GUI/GUI_App.hpp"
#include "slic3r/GUI/Gizmos/GLGizmosCommon.hpp" #include "slic3r/GUI/Gizmos/GLGizmosCommon.hpp"
#include <numeric> #include <numeric>
@ -80,12 +79,7 @@ void GLGizmoFlatten::on_render() const
else else
glsafe(::glColor4f(0.9f, 0.9f, 0.9f, 0.5f)); glsafe(::glColor4f(0.9f, 0.9f, 0.9f, 0.5f));
::glBegin(GL_POLYGON); m_planes[i].vbo.render();
for (const Vec3d& vertex : m_planes[i].vertices)
{
::glVertex3dv(vertex.data());
}
glsafe(::glEnd());
} }
glsafe(::glPopMatrix()); glsafe(::glPopMatrix());
} }
@ -112,12 +106,7 @@ void GLGizmoFlatten::on_render_for_picking() const
for (int i = 0; i < (int)m_planes.size(); ++i) for (int i = 0; i < (int)m_planes.size(); ++i)
{ {
glsafe(::glColor4fv(picking_color_component(i).data())); glsafe(::glColor4fv(picking_color_component(i).data()));
::glBegin(GL_POLYGON); m_planes[i].vbo.render();
for (const Vec3d& vertex : m_planes[i].vertices)
{
::glVertex3dv(vertex.data());
}
glsafe(::glEnd());
} }
glsafe(::glPopMatrix()); 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) { 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; stl_vertex* first_vertex = ch.stl.facet_start[facet_idx].vertex;
for (int j=0; j<3; ++j) 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<double>());
facet_visited[facet_idx] = true; facet_visited[facet_idx] = true;
for (int j = 0; j < 3; ++ j) { for (int j = 0; j < 3; ++ j) {
@ -193,15 +182,16 @@ void GLGizmoFlatten::update_planes()
} }
m_planes.back().normal = normal_ptr->cast<double>(); m_planes.back().normal = normal_ptr->cast<double>();
Pointf3s& verts = m_planes.back().vertices;
// Now we'll transform all the points into world coordinates, so that the areas, angles and distances // Now we'll transform all the points into world coordinates, so that the areas, angles and distances
// make real sense. // 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 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 && if (verts.size() == 3 &&
((m_planes.back().vertices[0] - m_planes.back().vertices[1]).norm() < minimal_side ((verts[0] - verts[1]).norm() < minimal_side
|| (m_planes.back().vertices[0] - m_planes.back().vertices[2]).norm() < minimal_side || (verts[0] - verts[2]).norm() < minimal_side
|| (m_planes.back().vertices[1] - m_planes.back().vertices[2]).norm() < minimal_side)) || (verts[1] - verts[2]).norm() < minimal_side))
m_planes.pop_back(); m_planes.pop_back();
} }
@ -246,7 +236,7 @@ void GLGizmoFlatten::update_planes()
discard = true; discard = true;
else { else {
// We also check the inner angles and discard polygons with angles smaller than the following threshold // 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) { for (unsigned int i = 0; i < polygon.size(); ++i) {
const Vec3d& prec = polygon[(i == 0) ? polygon.size() - 1 : i - 1]; 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_first_instance_mirror = mo->instances.front()->get_mirror();
m_old_model_object = mo; 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<plane.vertices.size()-1; ++i)
plane.vbo.push_triangle(0, i, i+1); // triangle fan
plane.vbo.finalize_geometry(true);
// FIXME: vertices should really be local, they need not
// persist now when we use VBOs
plane.vertices.clear();
plane.vertices.shrink_to_fit();
}
m_planes_valid = true; m_planes_valid = true;
} }

View file

@ -16,7 +16,8 @@ private:
mutable Vec3d m_normal; mutable Vec3d m_normal;
struct PlaneData { struct PlaneData {
std::vector<Vec3d> vertices; std::vector<Vec3d> vertices; // should be in fact local in update_planes()
GLIndexedVertexArray vbo;
Vec3d normal; Vec3d normal;
float area; float area;
}; };