Tech ENABLE_SINKING_CONTOURS -> Sinking contours rendered using triangles

This commit is contained in:
enricoturri1966 2021-07-28 11:21:59 +02:00
parent b24488ce51
commit d99ea7c20f
4 changed files with 42 additions and 16 deletions

View file

@ -24,6 +24,9 @@
#include "libslic3r/Utils.hpp" #include "libslic3r/Utils.hpp"
#include "libslic3r/AppConfig.hpp" #include "libslic3r/AppConfig.hpp"
#include "libslic3r/PresetBundle.hpp" #include "libslic3r/PresetBundle.hpp"
#if ENABLE_SINKING_CONTOURS
#include "libslic3r/Tesselate.hpp"
#endif // ENABLE_SINKING_CONTOURS
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -285,6 +288,8 @@ void GLIndexedVertexArray::render(
} }
#if ENABLE_SINKING_CONTOURS #if ENABLE_SINKING_CONTOURS
const float GLVolume::SinkingContours::HalfWidth = 0.25f;
void GLVolume::SinkingContours::update() void GLVolume::SinkingContours::update()
{ {
if (m_parent.is_sinking() && !m_parent.is_below_printbed()) { if (m_parent.is_sinking() && !m_parent.is_below_printbed()) {
@ -301,14 +306,35 @@ void GLVolume::SinkingContours::update()
Polygons polygons = slice_mesh(mesh.its, 0.0f, slicing_params); Polygons polygons = slice_mesh(mesh.its, 0.0f, slicing_params);
m_model.reset(); m_model.reset();
m_model.init_from(polygons, 0.0f); GUI::GLModel::InitializationData init_data;
std::array<float, 4> color = { for (const Polygon& polygon : polygons) {
1.0f - m_parent.render_color[0], const Polygons outer_polys = offset(polygon, float(scale_(HalfWidth)));
1.0f - m_parent.render_color[1], const Polygons inner_polys = offset(polygon, -float(scale_(HalfWidth)));
1.0f - m_parent.render_color[2],
m_parent.render_color[3] if (outer_polys.empty())
}; // no outer contour, skip
m_model.set_color(-1, color); continue;
const ExPolygons diff_polys_ex = diff_ex(outer_polys, inner_polys);
for (const ExPolygon& poly : diff_polys_ex) {
GUI::GLModel::InitializationData::Entity entity;
entity.type = GUI::GLModel::PrimitiveType::Triangles;
const std::vector<Vec3d> triangulation = triangulate_expolygon_3d(poly);
for (const Vec3d& v : triangulation) {
entity.positions.emplace_back(v.cast<float>() + Vec3f(0.0f, 0.0f, 0.015f)); // add a small positive z to avoid z-fighting
entity.normals.emplace_back(Vec3f::UnitZ());
const size_t positions_count = entity.positions.size();
if (positions_count % 3 == 0) {
entity.indices.emplace_back(positions_count - 3);
entity.indices.emplace_back(positions_count - 2);
entity.indices.emplace_back(positions_count - 1);
}
}
init_data.entities.emplace_back(entity);
}
}
m_model.init_from(init_data);
} }
else else
m_shift = box.center() - m_old_box.center(); m_shift = box.center() - m_old_box.center();
@ -831,7 +857,6 @@ void GLVolumeCollection::render(GLVolumeCollection::ERenderType type, bool disab
if (volume.first->is_sinking() && !volume.first->is_below_printbed() && if (volume.first->is_sinking() && !volume.first->is_below_printbed() &&
volume.first->hover == GLVolume::HS_None && !volume.first->force_sinking_contours) { volume.first->hover == GLVolume::HS_None && !volume.first->force_sinking_contours) {
shader->stop_using(); shader->stop_using();
glsafe(::glLineWidth(5.0f));
volume.first->update_sinking_contours_color(); volume.first->update_sinking_contours_color();
volume.first->render_sinking_contours(); volume.first->render_sinking_contours();
shader->start_using(); shader->start_using();
@ -879,11 +904,10 @@ void GLVolumeCollection::render(GLVolumeCollection::ERenderType type, bool disab
if (volume.first->is_sinking() && !volume.first->is_below_printbed() && if (volume.first->is_sinking() && !volume.first->is_below_printbed() &&
(volume.first->hover != GLVolume::HS_None || volume.first->force_sinking_contours)) { (volume.first->hover != GLVolume::HS_None || volume.first->force_sinking_contours)) {
shader->stop_using(); shader->stop_using();
glsafe(::glLineWidth(5.0f)); glsafe(::glDepthFunc(GL_ALWAYS));
glsafe(::glDisable(GL_DEPTH_TEST));
volume.first->update_sinking_contours_color(); volume.first->update_sinking_contours_color();
volume.first->render_sinking_contours(); volume.first->render_sinking_contours();
glsafe(::glEnable(GL_DEPTH_TEST)); glsafe(::glDepthFunc(GL_LESS));
shader->start_using(); shader->start_using();
} }
} }

View file

@ -284,6 +284,7 @@ private:
#if ENABLE_SINKING_CONTOURS #if ENABLE_SINKING_CONTOURS
class SinkingContours class SinkingContours
{ {
static const float HalfWidth;
GLVolume& m_parent; GLVolume& m_parent;
GUI::GLModel m_model; GUI::GLModel m_model;
BoundingBoxf3 m_old_box; BoundingBoxf3 m_old_box;

View file

@ -792,7 +792,7 @@ void GLCanvas3D::SequentialPrintClearance::set_polygons(const Polygons& polygons
for (const Polygon& poly : polygons) { for (const Polygon& poly : polygons) {
triangles_count += poly.points.size() - 2; triangles_count += poly.points.size() - 2;
} }
size_t vertices_count = 3 * triangles_count; const size_t vertices_count = 3 * triangles_count;
if (m_render_fill) { if (m_render_fill) {
GLModel::InitializationData fill_data; GLModel::InitializationData fill_data;
@ -803,13 +803,13 @@ void GLCanvas3D::SequentialPrintClearance::set_polygons(const Polygons& polygons
entity.normals.reserve(vertices_count); entity.normals.reserve(vertices_count);
entity.indices.reserve(vertices_count); entity.indices.reserve(vertices_count);
ExPolygons polygons_union = union_ex(polygons); const ExPolygons polygons_union = union_ex(polygons);
for (const ExPolygon& poly : polygons_union) { for (const ExPolygon& poly : polygons_union) {
std::vector<Vec3d> triangulation = triangulate_expolygon_3d(poly, false); const std::vector<Vec3d> triangulation = triangulate_expolygon_3d(poly);
for (const Vec3d& v : triangulation) { for (const Vec3d& v : triangulation) {
entity.positions.emplace_back(v.cast<float>() + Vec3f(0.0f, 0.0f, 0.0125f)); // add a small positive z to avoid z-fighting entity.positions.emplace_back(v.cast<float>() + Vec3f(0.0f, 0.0f, 0.0125f)); // add a small positive z to avoid z-fighting
entity.normals.emplace_back(Vec3f::UnitZ()); entity.normals.emplace_back(Vec3f::UnitZ());
size_t positions_count = entity.positions.size(); const size_t positions_count = entity.positions.size();
if (positions_count % 3 == 0) { if (positions_count % 3 == 0) {
entity.indices.emplace_back(positions_count - 3); entity.indices.emplace_back(positions_count - 3);
entity.indices.emplace_back(positions_count - 2); entity.indices.emplace_back(positions_count - 2);

View file

@ -142,6 +142,7 @@ void GLGizmoCut::on_render()
#if ENABLE_SINKING_CONTOURS #if ENABLE_SINKING_CONTOURS
glsafe(::glPushMatrix()); glsafe(::glPushMatrix());
glsafe(::glTranslated(m_cut_contours.shift.x(), m_cut_contours.shift.y(), m_cut_contours.shift.z())); glsafe(::glTranslated(m_cut_contours.shift.x(), m_cut_contours.shift.y(), m_cut_contours.shift.z()));
glsafe(::glLineWidth(2.0f));
m_cut_contours.contours.render(); m_cut_contours.contours.render();
glsafe(::glPopMatrix()); glsafe(::glPopMatrix());
#endif // ENABLE_SINKING_CONTOURS #endif // ENABLE_SINKING_CONTOURS