Skip to content

Commit

Permalink
Tech ENABLE_GLBEGIN_GLEND_REMOVAL - Removed Slic3r::GUI::GeometryBuff…
Browse files Browse the repository at this point in the history
…er from 3DBed.hpp and replaced with GLModel
  • Loading branch information
enricoturri1966 committed Feb 7, 2022
1 parent 1d7f4a0 commit 6b04142
Show file tree
Hide file tree
Showing 6 changed files with 239 additions and 13 deletions.
2 changes: 1 addition & 1 deletion resources/shaders/printbed.fs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#version 110

const vec3 back_color_dark = vec3(0.235, 0.235, 0.235);
const vec3 back_color_dark = vec3(0.235, 0.235, 0.235);
const vec3 back_color_light = vec3(0.365, 0.365, 0.365);

uniform sampler2D texture;
Expand Down
9 changes: 2 additions & 7 deletions resources/shaders/printbed.vs
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
#version 110

attribute vec3 v_position;
attribute vec2 v_tex_coords;

varying vec2 tex_coords;

void main()
{
gl_Position = gl_ModelViewProjectionMatrix * vec4(v_position.x, v_position.y, v_position.z, 1.0);
// the following line leads to crash on some Intel graphics card
//gl_Position = gl_ModelViewProjectionMatrix * vec4(v_position, 1.0);
tex_coords = v_tex_coords;
gl_Position = ftransform();
tex_coords = gl_MultiTexCoord0.xy;
}
186 changes: 185 additions & 1 deletion src/slic3r/GUI/3DBed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ static const Slic3r::ColorRGBA DEFAULT_TRANSPARENT_GRID_COLOR = { 0.9f, 0.9f, 0
namespace Slic3r {
namespace GUI {

#if !ENABLE_GLBEGIN_GLEND_REMOVAL
bool GeometryBuffer::set_from_triangles(const std::vector<Vec2f> &triangles, float z)
{
if (triangles.empty()) {
Expand Down Expand Up @@ -95,6 +96,7 @@ const float* GeometryBuffer::get_vertices_data() const
{
return (m_vertices.size() > 0) ? (const float*)m_vertices.data() : nullptr;
}
#endif // !ENABLE_GLBEGIN_GLEND_REMOVAL

const float Bed3D::Axes::DefaultStemRadius = 0.5f;
const float Bed3D::Axes::DefaultStemLength = 25.0f;
Expand Down Expand Up @@ -198,16 +200,24 @@ bool Bed3D::set_shape(const Pointfs& bed_shape, const double max_print_height, c
m_model_filename = model_filename;
m_extended_bounding_box = this->calc_extended_bounding_box();

#if ENABLE_GLBEGIN_GLEND_REMOVAL
m_contour = ExPolygon(Polygon::new_scale(bed_shape));
m_polygon = offset(m_contour.contour, (float)m_contour.contour.bounding_box().radius() * 1.7f, jtRound, scale_(0.5)).front();

m_triangles.reset();
m_gridlines.reset();
#else
ExPolygon poly{ Polygon::new_scale(bed_shape) };

calc_triangles(poly);

const BoundingBox& bed_bbox = poly.contour.bounding_box();
calc_gridlines(poly, bed_bbox);

m_polygon = offset(poly.contour, (float)bed_bbox.radius() * 1.7f, jtRound, scale_(0.5))[0];
m_polygon = offset(poly.contour, (float)bed_bbox.radius() * 1.7f, jtRound, scale_(0.5)).front();

this->release_VBOs();
#endif // ENABLE_GLBEGIN_GLEND_REMOVAL
m_texture.reset();
m_model.reset();

Expand Down Expand Up @@ -288,6 +298,105 @@ BoundingBoxf3 Bed3D::calc_extended_bounding_box() const
return out;
}

#if ENABLE_GLBEGIN_GLEND_REMOVAL
void Bed3D::init_triangles()
{
if (m_triangles.is_initialized())
return;

if (m_contour.empty())
return;

const std::vector<Vec2f> triangles = triangulate_expolygon_2f(m_contour, NORMALS_UP);
if (triangles.empty() || triangles.size() % 3 != 0)
return;

const GLModel::Geometry::EIndexType index_type = (triangles.size() < 65536) ? GLModel::Geometry::EIndexType::USHORT : GLModel::Geometry::EIndexType::UINT;

GLModel::Geometry init_data;
init_data.format = { GLModel::Geometry::EPrimitiveType::Triangles, GLModel::Geometry::EVertexLayout::P3T2, index_type };

Vec2f min = triangles.front();
Vec2f max = min;
for (const Vec2f v : triangles) {
const Vec3f p = { v.x(), v.y(), GROUND_Z };
min = min.cwiseMin(v).eval();
max = max.cwiseMax(v).eval();
}

const Vec2f size = max - min;
if (size.x() <= 0.0f || size.y() <= 0.0f)
return;

Vec2f inv_size = size.cwiseInverse();
inv_size.y() *= -1.0f;

unsigned int vertices_counter = 0;
for (const Vec2f v : triangles) {
const Vec3f p = { v.x(), v.y(), GROUND_Z };
init_data.add_vertex(p, (Vec2f)v.cwiseProduct(inv_size).eval());
++vertices_counter;
if (vertices_counter % 3 == 0) {
if (index_type == GLModel::Geometry::EIndexType::USHORT)
init_data.add_ushort_triangle((unsigned short)vertices_counter - 3, (unsigned short)vertices_counter - 2, (unsigned short)vertices_counter - 1);
else
init_data.add_uint_triangle(vertices_counter - 3, vertices_counter - 2, vertices_counter - 1);
}
}

m_triangles.init_from(std::move(init_data));
}

void Bed3D::init_gridlines()
{
if (m_gridlines.is_initialized())
return;

if (m_contour.empty())
return;

const BoundingBox& bed_bbox = m_contour.contour.bounding_box();
const coord_t step = scale_(10.0);

Polylines axes_lines;
for (coord_t x = bed_bbox.min.x(); x <= bed_bbox.max.x(); x += step) {
Polyline line;
line.append(Point(x, bed_bbox.min.y()));
line.append(Point(x, bed_bbox.max.y()));
axes_lines.push_back(line);
}
for (coord_t y = bed_bbox.min.y(); y <= bed_bbox.max.y(); y += step) {
Polyline line;
line.append(Point(bed_bbox.min.x(), y));
line.append(Point(bed_bbox.max.x(), y));
axes_lines.push_back(line);
}

// clip with a slightly grown expolygon because our lines lay on the contours and may get erroneously clipped
Lines gridlines = to_lines(intersection_pl(axes_lines, offset(m_contour, float(SCALED_EPSILON))));

// append bed contours
Lines contour_lines = to_lines(m_contour);
std::copy(contour_lines.begin(), contour_lines.end(), std::back_inserter(gridlines));

const GLModel::Geometry::EIndexType index_type = (gridlines.size() < 65536 / 2) ? GLModel::Geometry::EIndexType::USHORT : GLModel::Geometry::EIndexType::UINT;

GLModel::Geometry init_data;
init_data.format = { GLModel::Geometry::EPrimitiveType::Lines, GLModel::Geometry::EVertexLayout::P3, index_type };

for (const Line& l : gridlines) {
init_data.add_vertex(Vec3f(unscale<float>(l.a.x()), unscale<float>(l.a.y()), GROUND_Z));
init_data.add_vertex(Vec3f(unscale<float>(l.b.x()), unscale<float>(l.b.y()), GROUND_Z));
const unsigned int vertices_counter = (unsigned int)init_data.vertices_count();
if (index_type == GLModel::Geometry::EIndexType::USHORT)
init_data.add_ushort_line((unsigned short)vertices_counter - 2, (unsigned short)vertices_counter - 1);
else
init_data.add_uint_line(vertices_counter - 2, vertices_counter - 1);
}

m_gridlines.init_from(std::move(init_data));
}
#else
void Bed3D::calc_triangles(const ExPolygon& poly)
{
if (! m_triangles.set_from_triangles(triangulate_expolygon_2f(poly, NORMALS_UP), GROUND_Z))
Expand Down Expand Up @@ -320,6 +429,7 @@ void Bed3D::calc_gridlines(const ExPolygon& poly, const BoundingBox& bed_bbox)
if (!m_gridlines.set_from_lines(gridlines, GROUND_Z))
BOOST_LOG_TRIVIAL(error) << "Unable to create bed grid lines\n";
}
#endif // ENABLE_GLBEGIN_GLEND_REMOVAL

// Try to match the print bed shape with the shape of an active profile. If such a match exists,
// return the print bed model.
Expand Down Expand Up @@ -421,6 +531,44 @@ void Bed3D::render_texture(bool bottom, GLCanvas3D& canvas)
canvas.request_extra_frame();
}

#if ENABLE_GLBEGIN_GLEND_REMOVAL
init_triangles();

GLShaderProgram* shader = wxGetApp().get_shader("printbed");
if (shader != nullptr) {
shader->start_using();
shader->set_uniform("transparent_background", bottom);
shader->set_uniform("svg_source", boost::algorithm::iends_with(m_texture.get_source(), ".svg"));

glsafe(::glEnable(GL_DEPTH_TEST));
if (bottom)
glsafe(::glDepthMask(GL_FALSE));

glsafe(::glEnable(GL_BLEND));
glsafe(::glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA));

if (bottom)
glsafe(::glFrontFace(GL_CW));

// show the temporary texture while no compressed data is available
GLuint tex_id = (GLuint)m_temp_texture.get_id();
if (tex_id == 0)
tex_id = (GLuint)m_texture.get_id();

glsafe(::glBindTexture(GL_TEXTURE_2D, tex_id));
m_triangles.render();
glsafe(::glBindTexture(GL_TEXTURE_2D, 0));

if (bottom)
glsafe(::glFrontFace(GL_CCW));

glsafe(::glDisable(GL_BLEND));
if (bottom)
glsafe(::glDepthMask(GL_TRUE));

shader->stop_using();
}
#else
if (m_triangles.get_vertices_count() > 0) {
GLShaderProgram* shader = wxGetApp().get_shader("printbed");
if (shader != nullptr) {
Expand Down Expand Up @@ -488,6 +636,7 @@ void Bed3D::render_texture(bool bottom, GLCanvas3D& canvas)
shader->stop_using();
}
}
#endif // ENABLE_GLBEGIN_GLEND_REMOVAL
}

void Bed3D::render_model()
Expand Down Expand Up @@ -541,6 +690,38 @@ void Bed3D::render_default(bool bottom, bool picking)
{
m_texture.reset();

#if ENABLE_GLBEGIN_GLEND_REMOVAL
init_gridlines();
init_triangles();

GLShaderProgram* shader = wxGetApp().get_shader("flat");
if (shader != nullptr) {
shader->start_using();

glsafe(::glEnable(GL_DEPTH_TEST));
glsafe(::glEnable(GL_BLEND));
glsafe(::glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA));

if (m_model.get_filename().empty() && !bottom) {
// draw background
glsafe(::glDepthMask(GL_FALSE));
m_triangles.set_color(picking ? PICKING_MODEL_COLOR : DEFAULT_MODEL_COLOR);
m_triangles.render();
glsafe(::glDepthMask(GL_TRUE));
}

if (!picking) {
// draw grid
glsafe(::glLineWidth(1.5f * m_scale_factor));
m_gridlines.set_color(picking ? DEFAULT_SOLID_GRID_COLOR : DEFAULT_TRANSPARENT_GRID_COLOR);

This comment has been minimized.

Copy link
@n8bot

n8bot Feb 7, 2022

Contributor

I think this should be something like !bottom ? instead of picking ?

This comment has been minimized.

Copy link
@enricoturri1966

enricoturri1966 Feb 8, 2022

Author Collaborator

You are right, thank you, there is a missing edit after copy and paste

m_gridlines.render();
}

glsafe(::glDisable(GL_BLEND));

shader->stop_using();
}
#else
const unsigned int triangles_vcount = m_triangles.get_vertices_count();
if (triangles_vcount > 0) {
const bool has_model = !m_model.get_filename().empty();
Expand Down Expand Up @@ -573,15 +754,18 @@ void Bed3D::render_default(bool bottom, bool picking)

glsafe(::glDisable(GL_BLEND));
}
#endif // ENABLE_GLBEGIN_GLEND_REMOVAL
}

#if !ENABLE_GLBEGIN_GLEND_REMOVAL
void Bed3D::release_VBOs()
{
if (m_vbo_id > 0) {
glsafe(::glDeleteBuffers(1, &m_vbo_id));
m_vbo_id = 0;
}
}
#endif // !ENABLE_GLBEGIN_GLEND_REMOVAL

} // GUI
} // Slic3r
29 changes: 28 additions & 1 deletion src/slic3r/GUI/3DBed.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
#include "3DScene.hpp"
#include "GLModel.hpp"

#include <libslic3r/BuildVolume.hpp>
#include "libslic3r/BuildVolume.hpp"
#if ENABLE_GLBEGIN_GLEND_REMOVAL
#include "libslic3r/ExPolygon.hpp"
#endif // ENABLE_GLBEGIN_GLEND_REMOVAL

#include <tuple>
#include <array>
Expand All @@ -15,6 +18,7 @@ namespace GUI {

class GLCanvas3D;

#if !ENABLE_GLBEGIN_GLEND_REMOVAL
class GeometryBuffer
{
struct Vertex
Expand All @@ -36,6 +40,7 @@ class GeometryBuffer
size_t get_tex_coords_offset() const { return (size_t)(3 * sizeof(float)); }
unsigned int get_vertices_count() const { return (unsigned int)m_vertices.size(); }
};
#endif // !ENABLE_GLBEGIN_GLEND_REMOVAL

class Bed3D
{
Expand Down Expand Up @@ -79,23 +84,38 @@ class Bed3D
std::string m_model_filename;
// Print volume bounding box exteded with axes and model.
BoundingBoxf3 m_extended_bounding_box;
#if ENABLE_GLBEGIN_GLEND_REMOVAL
// Print bed polygon
ExPolygon m_contour;
#endif // ENABLE_GLBEGIN_GLEND_REMOVAL
// Slightly expanded print bed polygon, for collision detection.
Polygon m_polygon;
#if ENABLE_GLBEGIN_GLEND_REMOVAL
GLModel m_triangles;
GLModel m_gridlines;
#else
GeometryBuffer m_triangles;
GeometryBuffer m_gridlines;
#endif // ENABLE_GLBEGIN_GLEND_REMOVAL
GLTexture m_texture;
// temporary texture shown until the main texture has still no levels compressed
GLTexture m_temp_texture;
GLModel m_model;
Vec3d m_model_offset{ Vec3d::Zero() };
#if !ENABLE_GLBEGIN_GLEND_REMOVAL
unsigned int m_vbo_id{ 0 };
#endif // !ENABLE_GLBEGIN_GLEND_REMOVAL
Axes m_axes;

float m_scale_factor{ 1.0f };

public:
Bed3D() = default;
#if ENABLE_GLBEGIN_GLEND_REMOVAL
~Bed3D() = default;
#else
~Bed3D() { release_VBOs(); }
#endif // ENABLE_GLBEGIN_GLEND_REMOVAL

// Update print bed model from configuration.
// Return true if the bed shape changed, so the calee will update the UI.
Expand Down Expand Up @@ -125,8 +145,13 @@ class Bed3D
private:
// Calculate an extended bounding box from axes and current model for visualization purposes.
BoundingBoxf3 calc_extended_bounding_box() const;
#if ENABLE_GLBEGIN_GLEND_REMOVAL
void init_triangles();
void init_gridlines();
#else
void calc_triangles(const ExPolygon& poly);
void calc_gridlines(const ExPolygon& poly, const BoundingBox& bed_bbox);
#endif // ENABLE_GLBEGIN_GLEND_REMOVAL
static std::tuple<Type, std::string, std::string> detect_type(const Pointfs& shape);
void render_internal(GLCanvas3D& canvas, bool bottom, float scale_factor,
bool show_axes, bool show_texture, bool picking);
Expand All @@ -136,7 +161,9 @@ class Bed3D
void render_model();
void render_custom(GLCanvas3D& canvas, bool bottom, bool show_texture, bool picking);
void render_default(bool bottom, bool picking);
#if !ENABLE_GLBEGIN_GLEND_REMOVAL
void release_VBOs();
#endif // !ENABLE_GLBEGIN_GLEND_REMOVAL
};

} // GUI
Expand Down
Loading

0 comments on commit 6b04142

Please sign in to comment.