Skip to content

Commit

Permalink
Tech ENABLE_GLINDEXEDVERTEXARRAY_REMOVAL - Replace GLIndexedVertexArr…
Browse files Browse the repository at this point in the history
…ay with GLModel - GLGizmoFlatten::PlaneData::vbo
  • Loading branch information
enricoturri1966 committed Feb 9, 2022
1 parent a74bea5 commit ffa3a1d
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 14 deletions.
2 changes: 2 additions & 0 deletions src/libslic3r/Technologies.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@
#define ENABLE_WIPETOWER_OBJECTID_1000_REMOVAL (1 && ENABLE_2_5_0_ALPHA1)
// Enable removal of old OpenGL render calls
#define ENABLE_GLBEGIN_GLEND_REMOVAL (1 && ENABLE_2_5_0_ALPHA1)
// Enable replace GLIndexedVertexArray with GLModel
#define ENABLE_GLINDEXEDVERTEXARRAY_REMOVAL (1 && ENABLE_GLBEGIN_GLEND_REMOVAL)
// Enable show non-manifold edges
#define ENABLE_SHOW_NON_MANIFOLD_EDGES (1 && ENABLE_2_5_0_ALPHA1)
// Enable rework of Reload from disk command
Expand Down
42 changes: 28 additions & 14 deletions src/slic3r/GUI/GLModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ namespace Slic3r {
namespace GUI {

#if ENABLE_GLBEGIN_GLEND_REMOVAL
void GLModel::Geometry::reserve_vertices(size_t vertices_count)
{
vertices.reserve(vertices_count * vertex_stride_floats(format));
}

void GLModel::Geometry::reserve_indices(size_t indices_count)
{
indices.reserve(indices_count * index_stride_bytes(format));
}

void GLModel::Geometry::add_vertex(const Vec2f& position)
{
assert(format.vertex_layout == EVertexLayout::P2);
Expand Down Expand Up @@ -470,8 +480,8 @@ void GLModel::init_from(const indexed_triangle_set& its, const BoundingBoxf3 &bb

Geometry& data = m_render_data.geometry;
data.format = { Geometry::EPrimitiveType::Triangles, Geometry::EVertexLayout::P3N3, Geometry::EIndexType::UINT };
data.vertices.reserve(3 * its.indices.size() * Geometry::vertex_stride_floats(data.format));
data.indices.reserve(3 * its.indices.size() * Geometry::index_stride_bytes(data.format));
data.reserve_vertices(3 * its.indices.size());
data.reserve_indices(3 * its.indices.size());

// vertices + indices
unsigned int vertices_counter = 0;
Expand Down Expand Up @@ -552,8 +562,8 @@ void GLModel::init_from(const Polygons& polygons, float z)
segments_count += polygon.points.size();
}

data.vertices.reserve(2 * segments_count * Geometry::vertex_stride_floats(data.format));
data.indices.reserve(2 * segments_count * Geometry::index_stride_bytes(data.format));
data.reserve_vertices(2 * segments_count);
data.reserve_indices(2 * segments_count);

// vertices + indices
unsigned int vertices_counter = 0;
Expand Down Expand Up @@ -1034,8 +1044,8 @@ GLModel::Geometry stilized_arrow(unsigned short resolution, float tip_radius, fl
GLModel::Geometry data;
#if ENABLE_GLBEGIN_GLEND_REMOVAL
data.format = { GLModel::Geometry::EPrimitiveType::Triangles, GLModel::Geometry::EVertexLayout::P3N3, GLModel::Geometry::EIndexType::USHORT };
data.vertices.reserve((6 * resolution + 2) * GLModel::Geometry::vertex_stride_floats(data.format));
data.indices.reserve((6 * resolution * 3) * GLModel::Geometry::index_stride_bytes(data.format));
data.reserve_vertices(6 * resolution + 2);
data.reserve_indices(6 * resolution * 3);
#else
GLModel::Geometry::Entity entity;
entity.type = GLModel::EPrimitiveType::Triangles;
Expand Down Expand Up @@ -1175,6 +1185,7 @@ GLModel::Geometry stilized_arrow(unsigned short resolution, float tip_radius, fl

data.entities.emplace_back(entity);
#endif // ENABLE_GLBEGIN_GLEND_REMOVAL

return data;
}

Expand All @@ -1200,8 +1211,8 @@ GLModel::Geometry circular_arrow(unsigned short resolution, float radius, float
GLModel::Geometry data;
#if ENABLE_GLBEGIN_GLEND_REMOVAL
data.format = { GLModel::Geometry::EPrimitiveType::Triangles, GLModel::Geometry::EVertexLayout::P3N3, GLModel::Geometry::EIndexType::USHORT };
data.vertices.reserve((8 * (resolution + 1) + 30) * GLModel::Geometry::vertex_stride_floats(data.format));
data.indices.reserve(((8 * resolution + 16) * 3) * GLModel::Geometry::index_stride_bytes(data.format));
data.reserve_vertices(8 * (resolution + 1) + 30);
data.reserve_indices((8 * resolution + 16) * 3);
#else
GLModel::Geometry::Entity entity;
entity.type = GLModel::EPrimitiveType::Triangles;
Expand Down Expand Up @@ -1506,6 +1517,7 @@ GLModel::Geometry circular_arrow(unsigned short resolution, float radius, float

data.entities.emplace_back(entity);
#endif // ENABLE_GLBEGIN_GLEND_REMOVAL

return data;
}

Expand All @@ -1526,8 +1538,8 @@ GLModel::Geometry straight_arrow(float tip_width, float tip_height, float stem_w
GLModel::Geometry data;
#if ENABLE_GLBEGIN_GLEND_REMOVAL
data.format = { GLModel::Geometry::EPrimitiveType::Triangles, GLModel::Geometry::EVertexLayout::P3N3, GLModel::Geometry::EIndexType::USHORT };
data.vertices.reserve(42 * GLModel::Geometry::vertex_stride_floats(data.format));
data.indices.reserve((24 * 3) * GLModel::Geometry::index_stride_bytes(data.format));
data.reserve_vertices(42);
data.reserve_indices(72);
#else
GLModel::Geometry::Entity entity;
entity.type = GLModel::EPrimitiveType::Triangles;
Expand Down Expand Up @@ -1699,6 +1711,7 @@ GLModel::Geometry straight_arrow(float tip_width, float tip_height, float stem_w

data.entities.emplace_back(entity);
#endif // ENABLE_GLBEGIN_GLEND_REMOVAL

return data;
}

Expand All @@ -1712,8 +1725,8 @@ GLModel::Geometry diamond(unsigned short resolution)
GLModel::Geometry data;
#if ENABLE_GLBEGIN_GLEND_REMOVAL
data.format = { GLModel::Geometry::EPrimitiveType::Triangles, GLModel::Geometry::EVertexLayout::P3N3, GLModel::Geometry::EIndexType::USHORT };
data.vertices.reserve((resolution + 2) * GLModel::Geometry::vertex_stride_floats(data.format));
data.indices.reserve(((2 * (resolution + 1)) * 3) * GLModel::Geometry::index_stride_bytes(data.format));
data.reserve_vertices(resolution + 2);
data.reserve_indices((2 * (resolution + 1)) * 3);
#else
GLModel::Geometry::Entity entity;
entity.type = GLModel::EPrimitiveType::Triangles;
Expand Down Expand Up @@ -1782,6 +1795,7 @@ GLModel::Geometry diamond(unsigned short resolution)

data.entities.emplace_back(entity);
#endif // ENABLE_GLBEGIN_GLEND_REMOVAL

return data;
}

Expand All @@ -1800,8 +1814,8 @@ GLModel::Geometry smooth_sphere(unsigned short resolution, float radius)

GLModel::Geometry data;
data.format = { GLModel::Geometry::EPrimitiveType::Triangles, GLModel::Geometry::EVertexLayout::P3N3, GLModel::Geometry::EIndexType::USHORT };
data.vertices.reserve(((stackCount - 1) * sectorCount + 2) * GLModel::Geometry::vertex_stride_floats(data.format));
data.indices.reserve(((2 * (stackCount - 1) * sectorCount) * 3) * GLModel::Geometry::index_stride_bytes(data.format));
data.reserve_vertices((stackCount - 1) * sectorCount + 2);
data.reserve_indices((2 * (stackCount - 1) * sectorCount) * 3);

// vertices
for (unsigned short i = 0; i <= stackCount; ++i) {
Expand Down
3 changes: 3 additions & 0 deletions src/slic3r/GUI/GLModel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ namespace GUI {
std::vector<unsigned char> indices;
ColorRGBA color{ ColorRGBA::BLACK() };

void reserve_vertices(size_t vertices_count);
void reserve_indices(size_t indices_count);

void add_vertex(const Vec2f& position);
void add_vertex(const Vec2f& position, const Vec2f& tex_coord);
void add_vertex(const Vec3f& position);
Expand Down
53 changes: 53 additions & 0 deletions src/slic3r/GUI/Gizmos/GLGizmoFlatten.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// 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"
#if ENABLE_GLINDEXEDVERTEXARRAY_REMOVAL
#include "slic3r/GUI/GUI_App.hpp"
#endif // ENABLE_GLINDEXEDVERTEXARRAY_REMOVAL
#include "slic3r/GUI/Gizmos/GLGizmosCommon.hpp"

#include "libslic3r/Geometry/ConvexHull.hpp"
Expand Down Expand Up @@ -63,6 +66,14 @@ void GLGizmoFlatten::on_render()
{
const Selection& selection = m_parent.get_selection();

#if ENABLE_GLINDEXEDVERTEXARRAY_REMOVAL
GLShaderProgram* shader = wxGetApp().get_shader("flat");
if (shader == nullptr)
return;

shader->start_using();
#endif // ENABLE_GLINDEXEDVERTEXARRAY_REMOVAL

glsafe(::glClear(GL_DEPTH_BUFFER_BIT));

glsafe(::glEnable(GL_DEPTH_TEST));
Expand All @@ -76,21 +87,38 @@ void GLGizmoFlatten::on_render()
if (this->is_plane_update_necessary())
update_planes();
for (int i = 0; i < (int)m_planes.size(); ++i) {
#if ENABLE_GLINDEXEDVERTEXARRAY_REMOVAL
m_planes[i].vbo.set_color(i == m_hover_id ? DEFAULT_HOVER_PLANE_COLOR : DEFAULT_PLANE_COLOR);
m_planes[i].vbo.render();
#else
glsafe(::glColor4fv(i == m_hover_id ? DEFAULT_HOVER_PLANE_COLOR.data() : DEFAULT_PLANE_COLOR.data()));
if (m_planes[i].vbo.has_VBOs())
m_planes[i].vbo.render();
#endif // ENABLE_GLINDEXEDVERTEXARRAY_REMOVAL
}
glsafe(::glPopMatrix());
}

glsafe(::glEnable(GL_CULL_FACE));
glsafe(::glDisable(GL_BLEND));

#if ENABLE_GLINDEXEDVERTEXARRAY_REMOVAL
shader->stop_using();
#endif // ENABLE_GLINDEXEDVERTEXARRAY_REMOVAL
}

void GLGizmoFlatten::on_render_for_picking()
{
const Selection& selection = m_parent.get_selection();

#if ENABLE_GLINDEXEDVERTEXARRAY_REMOVAL
GLShaderProgram* shader = wxGetApp().get_shader("flat");
if (shader == nullptr)
return;

shader->start_using();
#endif // ENABLE_GLINDEXEDVERTEXARRAY_REMOVAL

glsafe(::glDisable(GL_DEPTH_TEST));
glsafe(::glDisable(GL_BLEND));

Expand All @@ -102,13 +130,21 @@ void GLGizmoFlatten::on_render_for_picking()
if (this->is_plane_update_necessary())
update_planes();
for (int i = 0; i < (int)m_planes.size(); ++i) {
#if ENABLE_GLINDEXEDVERTEXARRAY_REMOVAL
m_planes[i].vbo.set_color(picking_color_component(i));
#else
glsafe(::glColor4fv(picking_color_component(i).data()));
#endif // ENABLE_GLINDEXEDVERTEXARRAY_REMOVAL
m_planes[i].vbo.render();
}
glsafe(::glPopMatrix());
}

glsafe(::glEnable(GL_CULL_FACE));

#if ENABLE_GLINDEXEDVERTEXARRAY_REMOVAL
shader->stop_using();
#endif // ENABLE_GLINDEXEDVERTEXARRAY_REMOVAL
}

void GLGizmoFlatten::set_flattening_data(const ModelObject* model_object)
Expand Down Expand Up @@ -324,12 +360,29 @@ void GLGizmoFlatten::update_planes()
// And finally create respective VBOs. The polygon is convex with
// the vertices in order, so triangulation is trivial.
for (auto& plane : m_planes) {
#if ENABLE_GLINDEXEDVERTEXARRAY_REMOVAL
GLModel::Geometry init_data;
const GLModel::Geometry::EIndexType index_type = (plane.vertices.size() < 65536) ? GLModel::Geometry::EIndexType::USHORT : GLModel::Geometry::EIndexType::UINT;
init_data.format = { GLModel::Geometry::EPrimitiveType::TriangleFan, GLModel::Geometry::EVertexLayout::P3N3, index_type };
init_data.reserve_vertices(plane.vertices.size());
init_data.reserve_indices(plane.vertices.size());
// vertices + indices
for (size_t i = 0; i < plane.vertices.size(); ++i) {
init_data.add_vertex((Vec3f)plane.vertices[i].cast<float>(), (Vec3f)plane.normal.cast<float>());
if (index_type == GLModel::Geometry::EIndexType::USHORT)
init_data.add_ushort_index(i);
else
init_data.add_uint_index(i);
}
plane.vbo.init_from(std::move(init_data));
#else
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);
#endif // ENABLE_GLINDEXEDVERTEXARRAY_REMOVAL
// FIXME: vertices should really be local, they need not
// persist now when we use VBOs
plane.vertices.clear();
Expand Down
8 changes: 8 additions & 0 deletions src/slic3r/GUI/Gizmos/GLGizmoFlatten.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
#define slic3r_GLGizmoFlatten_hpp_

#include "GLGizmoBase.hpp"
#if ENABLE_GLINDEXEDVERTEXARRAY_REMOVAL
#include "slic3r/GUI/GLModel.hpp"
#else
#include "slic3r/GUI/3DScene.hpp"
#endif // ENABLE_GLINDEXEDVERTEXARRAY_REMOVAL


namespace Slic3r {
Expand All @@ -22,7 +26,11 @@ class GLGizmoFlatten : public GLGizmoBase

struct PlaneData {
std::vector<Vec3d> vertices; // should be in fact local in update_planes()
#if ENABLE_GLINDEXEDVERTEXARRAY_REMOVAL
GLModel vbo;
#else
GLIndexedVertexArray vbo;
#endif // ENABLE_GLINDEXEDVERTEXARRAY_REMOVAL
Vec3d normal;
float area;
};
Expand Down

0 comments on commit ffa3a1d

Please sign in to comment.