GCodeViewer -> Modified shape of printbed for the unknown size case

This commit is contained in:
enricoturri1966 2020-08-19 15:19:07 +02:00
parent db77f80681
commit 992d7065b2
2 changed files with 52 additions and 76 deletions

View file

@ -45,10 +45,8 @@ bool GeometryBuffer::set_from_triangles(const Polygons& triangles, float z, bool
float max_y = min_y; float max_y = min_y;
unsigned int v_count = 0; unsigned int v_count = 0;
for (const Polygon& t : triangles) for (const Polygon& t : triangles) {
{ for (unsigned int i = 0; i < 3; ++i) {
for (unsigned int i = 0; i < 3; ++i)
{
Vertex& v = m_vertices[v_count]; Vertex& v = m_vertices[v_count];
const Point& p = t.points[i]; const Point& p = t.points[i];
@ -59,8 +57,7 @@ bool GeometryBuffer::set_from_triangles(const Polygons& triangles, float z, bool
v.position[1] = y; v.position[1] = y;
v.position[2] = z; v.position[2] = z;
if (generate_tex_coords) if (generate_tex_coords) {
{
v.tex_coords[0] = x; v.tex_coords[0] = x;
v.tex_coords[1] = y; v.tex_coords[1] = y;
@ -74,17 +71,14 @@ bool GeometryBuffer::set_from_triangles(const Polygons& triangles, float z, bool
} }
} }
if (generate_tex_coords) if (generate_tex_coords) {
{
float size_x = max_x - min_x; float size_x = max_x - min_x;
float size_y = max_y - min_y; float size_y = max_y - min_y;
if ((size_x != 0.0f) && (size_y != 0.0f)) if ((size_x != 0.0f) && (size_y != 0.0f)) {
{
float inv_size_x = 1.0f / size_x; float inv_size_x = 1.0f / size_x;
float inv_size_y = -1.0f / size_y; float inv_size_y = -1.0f / size_y;
for (Vertex& v : m_vertices) for (Vertex& v : m_vertices) {
{
v.tex_coords[0] = (v.tex_coords[0] - min_x) * inv_size_x; v.tex_coords[0] = (v.tex_coords[0] - min_x) * inv_size_x;
v.tex_coords[1] = (v.tex_coords[1] - min_y) * inv_size_y; v.tex_coords[1] = (v.tex_coords[1] - min_y) * inv_size_y;
} }
@ -105,8 +99,7 @@ bool GeometryBuffer::set_from_lines(const Lines& lines, float z)
m_vertices = std::vector<Vertex>(v_size, Vertex()); m_vertices = std::vector<Vertex>(v_size, Vertex());
unsigned int v_count = 0; unsigned int v_count = 0;
for (const Line& l : lines) for (const Line& l : lines) {
{
Vertex& v1 = m_vertices[v_count]; Vertex& v1 = m_vertices[v_count];
v1.position[0] = unscale<float>(l.a(0)); v1.position[0] = unscale<float>(l.a(0));
v1.position[1] = unscale<float>(l.a(1)); v1.position[1] = unscale<float>(l.a(1));
@ -360,8 +353,7 @@ void Bed3D::render(GLCanvas3D& canvas, bool bottom, float scale_factor,
void Bed3D::calc_bounding_boxes() const void Bed3D::calc_bounding_boxes() const
{ {
m_bounding_box = BoundingBoxf3(); m_bounding_box = BoundingBoxf3();
for (const Vec2d& p : m_shape) for (const Vec2d& p : m_shape) {
{
m_bounding_box.merge(Vec3d(p(0), p(1), 0.0)); m_bounding_box.merge(Vec3d(p(0), p(1), 0.0));
} }
@ -374,8 +366,7 @@ void Bed3D::calc_bounding_boxes() const
// extend to contain model, if any // extend to contain model, if any
BoundingBoxf3 model_bb = m_model.get_bounding_box(); BoundingBoxf3 model_bb = m_model.get_bounding_box();
if (model_bb.defined) if (model_bb.defined) {
{
model_bb.translate(m_model_offset); model_bb.translate(m_model_offset);
m_extended_bounding_box.merge(model_bb); m_extended_bounding_box.merge(model_bb);
} }
@ -390,7 +381,7 @@ void Bed3D::calc_bounding_boxes() const
void Bed3D::calc_triangles(const ExPolygon& poly) void Bed3D::calc_triangles(const ExPolygon& poly)
{ {
Polygons triangles; Polygons triangles;
poly.triangulate(&triangles); poly.triangulate_p2t(&triangles);
if (!m_triangles.set_from_triangles(triangles, GROUND_Z, true)) if (!m_triangles.set_from_triangles(triangles, GROUND_Z, true))
printf("Unable to create bed triangles\n"); printf("Unable to create bed triangles\n");
@ -399,15 +390,13 @@ void Bed3D::calc_triangles(const ExPolygon& poly)
void Bed3D::calc_gridlines(const ExPolygon& poly, const BoundingBox& bed_bbox) void Bed3D::calc_gridlines(const ExPolygon& poly, const BoundingBox& bed_bbox)
{ {
Polylines axes_lines; Polylines axes_lines;
for (coord_t x = bed_bbox.min(0); x <= bed_bbox.max(0); x += scale_(10.0)) for (coord_t x = bed_bbox.min(0); x <= bed_bbox.max(0); x += scale_(10.0)) {
{
Polyline line; Polyline line;
line.append(Point(x, bed_bbox.min(1))); line.append(Point(x, bed_bbox.min(1)));
line.append(Point(x, bed_bbox.max(1))); line.append(Point(x, bed_bbox.max(1)));
axes_lines.push_back(line); axes_lines.push_back(line);
} }
for (coord_t y = bed_bbox.min(1); y <= bed_bbox.max(1); y += scale_(10.0)) for (coord_t y = bed_bbox.min(1); y <= bed_bbox.max(1); y += scale_(10.0)) {
{
Polyline line; Polyline line;
line.append(Point(bed_bbox.min(0), y)); line.append(Point(bed_bbox.min(0), y));
line.append(Point(bed_bbox.max(0), y)); line.append(Point(bed_bbox.max(0), y));
@ -482,26 +471,21 @@ void Bed3D::render_system(GLCanvas3D& canvas, bool bottom, bool show_texture) co
void Bed3D::render_texture(bool bottom, GLCanvas3D& canvas) const void Bed3D::render_texture(bool bottom, GLCanvas3D& canvas) const
{ {
if (m_texture_filename.empty()) if (m_texture_filename.empty()) {
{
m_texture.reset(); m_texture.reset();
render_default(bottom); render_default(bottom);
return; return;
} }
if ((m_texture.get_id() == 0) || (m_texture.get_source() != m_texture_filename)) if ((m_texture.get_id() == 0) || (m_texture.get_source() != m_texture_filename)) {
{
m_texture.reset(); m_texture.reset();
if (boost::algorithm::iends_with(m_texture_filename, ".svg")) if (boost::algorithm::iends_with(m_texture_filename, ".svg")) {
{
// use higher resolution images if graphic card and opengl version allow // use higher resolution images if graphic card and opengl version allow
GLint max_tex_size = OpenGLManager::get_gl_info().get_max_tex_size(); GLint max_tex_size = OpenGLManager::get_gl_info().get_max_tex_size();
if ((m_temp_texture.get_id() == 0) || (m_temp_texture.get_source() != m_texture_filename)) if ((m_temp_texture.get_id() == 0) || (m_temp_texture.get_source() != m_texture_filename)) {
{
// generate a temporary lower resolution texture to show while no main texture levels have been compressed // generate a temporary lower resolution texture to show while no main texture levels have been compressed
if (!m_temp_texture.load_from_svg_file(m_texture_filename, false, false, false, max_tex_size / 8)) if (!m_temp_texture.load_from_svg_file(m_texture_filename, false, false, false, max_tex_size / 8)) {
{
render_default(bottom); render_default(bottom);
return; return;
} }
@ -509,19 +493,15 @@ void Bed3D::render_texture(bool bottom, GLCanvas3D& canvas) const
} }
// starts generating the main texture, compression will run asynchronously // starts generating the main texture, compression will run asynchronously
if (!m_texture.load_from_svg_file(m_texture_filename, true, true, true, max_tex_size)) if (!m_texture.load_from_svg_file(m_texture_filename, true, true, true, max_tex_size)) {
{
render_default(bottom); render_default(bottom);
return; return;
} }
} }
else if (boost::algorithm::iends_with(m_texture_filename, ".png")) else if (boost::algorithm::iends_with(m_texture_filename, ".png")) {
{
// generate a temporary lower resolution texture to show while no main texture levels have been compressed // generate a temporary lower resolution texture to show while no main texture levels have been compressed
if ((m_temp_texture.get_id() == 0) || (m_temp_texture.get_source() != m_texture_filename)) if ((m_temp_texture.get_id() == 0) || (m_temp_texture.get_source() != m_texture_filename)) {
{ if (!m_temp_texture.load_from_file(m_texture_filename, false, GLTexture::None, false)) {
if (!m_temp_texture.load_from_file(m_texture_filename, false, GLTexture::None, false))
{
render_default(bottom); render_default(bottom);
return; return;
} }
@ -529,20 +509,17 @@ void Bed3D::render_texture(bool bottom, GLCanvas3D& canvas) const
} }
// starts generating the main texture, compression will run asynchronously // starts generating the main texture, compression will run asynchronously
if (!m_texture.load_from_file(m_texture_filename, true, GLTexture::MultiThreaded, true)) if (!m_texture.load_from_file(m_texture_filename, true, GLTexture::MultiThreaded, true)) {
{
render_default(bottom); render_default(bottom);
return; return;
} }
} }
else else {
{
render_default(bottom); render_default(bottom);
return; return;
} }
} }
else if (m_texture.unsent_compressed_data_available()) else if (m_texture.unsent_compressed_data_available()) {
{
// sends to gpu the already available compressed levels of the main texture // sends to gpu the already available compressed levels of the main texture
m_texture.send_compressed_data_to_gpu(); m_texture.send_compressed_data_to_gpu();
@ -554,17 +531,14 @@ void Bed3D::render_texture(bool bottom, GLCanvas3D& canvas) const
} }
if (m_triangles.get_vertices_count() > 0) if (m_triangles.get_vertices_count() > 0) {
{
GLShaderProgram* shader = wxGetApp().get_shader("printbed"); GLShaderProgram* shader = wxGetApp().get_shader("printbed");
if (shader != nullptr) if (shader != nullptr) {
{
shader->start_using(); shader->start_using();
shader->set_uniform("transparent_background", bottom); shader->set_uniform("transparent_background", bottom);
shader->set_uniform("svg_source", boost::algorithm::iends_with(m_texture.get_source(), ".svg")); shader->set_uniform("svg_source", boost::algorithm::iends_with(m_texture.get_source(), ".svg"));
if (m_vbo_id == 0) if (m_vbo_id == 0) {
{
glsafe(::glGenBuffers(1, &m_vbo_id)); glsafe(::glGenBuffers(1, &m_vbo_id));
glsafe(::glBindBuffer(GL_ARRAY_BUFFER, m_vbo_id)); glsafe(::glBindBuffer(GL_ARRAY_BUFFER, m_vbo_id));
glsafe(::glBufferData(GL_ARRAY_BUFFER, (GLsizeiptr)m_triangles.get_vertices_data_size(), (const GLvoid*)m_triangles.get_vertices_data(), GL_STATIC_DRAW)); glsafe(::glBufferData(GL_ARRAY_BUFFER, (GLsizeiptr)m_triangles.get_vertices_data_size(), (const GLvoid*)m_triangles.get_vertices_data(), GL_STATIC_DRAW));
@ -593,13 +567,11 @@ void Bed3D::render_texture(bool bottom, GLCanvas3D& canvas) const
glsafe(::glBindTexture(GL_TEXTURE_2D, tex_id)); glsafe(::glBindTexture(GL_TEXTURE_2D, tex_id));
glsafe(::glBindBuffer(GL_ARRAY_BUFFER, m_vbo_id)); glsafe(::glBindBuffer(GL_ARRAY_BUFFER, m_vbo_id));
if (position_id != -1) if (position_id != -1) {
{
glsafe(::glEnableVertexAttribArray(position_id)); glsafe(::glEnableVertexAttribArray(position_id));
glsafe(::glVertexAttribPointer(position_id, 3, GL_FLOAT, GL_FALSE, stride, (GLvoid*)(intptr_t)m_triangles.get_position_offset())); glsafe(::glVertexAttribPointer(position_id, 3, GL_FLOAT, GL_FALSE, stride, (GLvoid*)(intptr_t)m_triangles.get_position_offset()));
} }
if (tex_coords_id != -1) if (tex_coords_id != -1) {
{
glsafe(::glEnableVertexAttribArray(tex_coords_id)); glsafe(::glEnableVertexAttribArray(tex_coords_id));
glsafe(::glVertexAttribPointer(tex_coords_id, 2, GL_FLOAT, GL_FALSE, stride, (GLvoid*)(intptr_t)m_triangles.get_tex_coords_offset())); glsafe(::glVertexAttribPointer(tex_coords_id, 2, GL_FLOAT, GL_FALSE, stride, (GLvoid*)(intptr_t)m_triangles.get_tex_coords_offset()));
} }
@ -631,8 +603,7 @@ void Bed3D::render_model() const
if (m_model_filename.empty()) if (m_model_filename.empty())
return; return;
if ((m_model.get_filename() != m_model_filename) && m_model.init_from_file(m_model_filename)) if ((m_model.get_filename() != m_model_filename) && m_model.init_from_file(m_model_filename)) {
{
// move the model so that its origin (0.0, 0.0, 0.0) goes into the bed shape center and a bit down to avoid z-fighting with the texture quad // move the model so that its origin (0.0, 0.0, 0.0) goes into the bed shape center and a bit down to avoid z-fighting with the texture quad
Vec3d shift = m_bounding_box.center(); Vec3d shift = m_bounding_box.center();
shift(2) = -0.03; shift(2) = -0.03;
@ -646,11 +617,9 @@ void Bed3D::render_model() const
calc_bounding_boxes(); calc_bounding_boxes();
} }
if (!m_model.get_filename().empty()) if (!m_model.get_filename().empty()) {
{
GLShaderProgram* shader = wxGetApp().get_shader("gouraud_light"); GLShaderProgram* shader = wxGetApp().get_shader("gouraud_light");
if (shader != nullptr) if (shader != nullptr) {
{
shader->start_using(); shader->start_using();
#if ENABLE_GCODE_VIEWER #if ENABLE_GCODE_VIEWER
shader->set_uniform("uniform_color", m_model_color); shader->set_uniform("uniform_color", m_model_color);
@ -668,8 +637,7 @@ void Bed3D::render_model() const
void Bed3D::render_custom(GLCanvas3D& canvas, bool bottom, bool show_texture) const void Bed3D::render_custom(GLCanvas3D& canvas, bool bottom, bool show_texture) const
{ {
if (m_texture_filename.empty() && m_model_filename.empty()) if (m_texture_filename.empty() && m_model_filename.empty()) {
{
render_default(bottom); render_default(bottom);
return; return;
} }
@ -686,8 +654,7 @@ void Bed3D::render_default(bool bottom) const
m_texture.reset(); m_texture.reset();
unsigned int triangles_vcount = m_triangles.get_vertices_count(); unsigned int triangles_vcount = m_triangles.get_vertices_count();
if (triangles_vcount > 0) if (triangles_vcount > 0) {
{
bool has_model = !m_model.get_filename().empty(); bool has_model = !m_model.get_filename().empty();
glsafe(::glEnable(GL_DEPTH_TEST)); glsafe(::glEnable(GL_DEPTH_TEST));
@ -696,8 +663,7 @@ void Bed3D::render_default(bool bottom) const
glsafe(::glEnableClientState(GL_VERTEX_ARRAY)); glsafe(::glEnableClientState(GL_VERTEX_ARRAY));
if (!has_model && !bottom) if (!has_model && !bottom) {
{
// draw background // draw background
glsafe(::glDepthMask(GL_FALSE)); glsafe(::glDepthMask(GL_FALSE));
#if ENABLE_GCODE_VIEWER #if ENABLE_GCODE_VIEWER
@ -728,8 +694,7 @@ void Bed3D::render_default(bool bottom) const
void Bed3D::reset() void Bed3D::reset()
{ {
if (m_vbo_id > 0) if (m_vbo_id > 0) {
{
glsafe(::glDeleteBuffers(1, &m_vbo_id)); glsafe(::glDeleteBuffers(1, &m_vbo_id));
m_vbo_id = 0; m_vbo_id = 0;
} }

View file

@ -343,10 +343,21 @@ void GCodeViewer::load(const GCodeProcessor::Result& gcode_result, const Print&
const double margin = 10.0; const double margin = 10.0;
Vec2d min(m_paths_bounding_box.min(0) - margin, m_paths_bounding_box.min(1) - margin); Vec2d min(m_paths_bounding_box.min(0) - margin, m_paths_bounding_box.min(1) - margin);
Vec2d max(m_paths_bounding_box.max(0) + margin, m_paths_bounding_box.max(1) + margin); Vec2d max(m_paths_bounding_box.max(0) + margin, m_paths_bounding_box.max(1) + margin);
bed_shape = { { min(0), min(1) },
{ max(0), min(1) }, Vec2d size = max - min;
{ max(0), max(1) }, bed_shape = {
{ min(0), max(1) } }; { min(0), min(1) },
{ max(0), min(1) },
{ max(0), min(1) + 0.442265 * size[1]},
{ max(0) - 10.0, min(1) + 0.4711325 * size[1]},
{ max(0) + 10.0, min(1) + 0.5288675 * size[1]},
{ max(0), min(1) + 0.557735 * size[1]},
{ max(0), max(1) },
{ min(0) + 0.557735 * size[0], max(1)},
{ min(0) + 0.5288675 * size[0], max(1) - 10.0},
{ min(0) + 0.4711325 * size[0], max(1) + 10.0},
{ min(0) + 0.442265 * size[0], max(1)},
{ min(0), max(1) } };
} }
wxGetApp().plater()->set_bed_shape(bed_shape, "", "", true); wxGetApp().plater()->set_bed_shape(bed_shape, "", "", true);
} }