This commit is contained in:
bubnikv 2019-02-22 10:12:18 +01:00
commit 4fa2b9e601
23 changed files with 5815 additions and 919 deletions

File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 61 KiB

File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 61 KiB

File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 9.2 KiB

View File

@ -0,0 +1,20 @@
#version 110
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;
uniform bool transparent_background;
varying vec2 tex_coords;
void main()
{
// calculates radial gradient
vec3 back_color = vec3(mix(back_color_light, back_color_dark, smoothstep(0.0, 0.5, length(abs(tex_coords.xy) - vec2(0.5)))));
vec4 fore_color = texture2D(texture, tex_coords);
// blends foreground with background
gl_FragColor = vec4(mix(back_color, fore_color.rgb, fore_color.a), transparent_background ? fore_color.a : 1.0);
}

View File

@ -0,0 +1,11 @@
#version 110
attribute vec2 v_tex_coords;
varying vec2 tex_coords;
void main()
{
gl_Position = ftransform();
tex_coords = v_tex_coords;
}

View File

@ -27,15 +27,6 @@
#define ENABLE_NONCUSTOM_DATA_VIEW_RENDERING (0 && ENABLE_1_42_0_ALPHA1)
//====================
// 1.42.0.alpha2 techs
//====================
#define ENABLE_1_42_0_ALPHA2 1
// Adds print bed models to 3D scene
#define ENABLE_PRINT_BED_MODELS (1 && ENABLE_1_42_0_ALPHA2)
//====================
// 1.42.0.alpha4 techs
//====================
@ -47,10 +38,6 @@
#define ENABLE_MOVE_MIN_THRESHOLD (1 && ENABLE_1_42_0_ALPHA4)
// Modified initial default placement of generic subparts
#define ENABLE_GENERIC_SUBPARTS_PLACEMENT (1 && ENABLE_1_42_0_ALPHA4)
// Reworked management of bed shape changes
#define ENABLE_REWORKED_BED_SHAPE_CHANGE (1 && ENABLE_1_42_0_ALPHA4)
// Use anisotropic filtering on bed plate texture
#define ENABLE_ANISOTROPIC_FILTER_ON_BED_TEXTURES (1 && ENABLE_1_42_0_ALPHA4)
// Bunch of fixes related to volumes centering
#define ENABLE_VOLUMES_CENTERING_FIXES (1 && ENABLE_1_42_0_ALPHA4)
@ -63,4 +50,13 @@
// Toolbar items hidden/shown in dependence of the user mode
#define ENABLE_MODE_AWARE_TOOLBAR_ITEMS (1 && ENABLE_1_42_0_ALPHA5)
//====================
// 1.42.0.alpha7 techs
//====================
#define ENABLE_1_42_0_ALPHA7 1
// Printbed textures generated from svg files
#define ENABLE_TEXTURES_FROM_SVG (1 && ENABLE_1_42_0_ALPHA7)
#endif // _technologies_h_

2975
src/nanosvg/nanosvg.h Normal file

File diff suppressed because it is too large Load Diff

1452
src/nanosvg/nanosvgrast.h Normal file

File diff suppressed because it is too large Load Diff

View File

@ -74,6 +74,8 @@ set(SLIC3R_GUI_SOURCES
GUI/BedShapeDialog.hpp
GUI/2DBed.cpp
GUI/2DBed.hpp
GUI/3DBed.cpp
GUI/3DBed.hpp
GUI/wxExtensions.cpp
GUI/wxExtensions.hpp
GUI/WipeTowerDialog.cpp

796
src/slic3r/GUI/3DBed.cpp Normal file
View File

@ -0,0 +1,796 @@
#include "libslic3r/libslic3r.h"
#include "3DBed.hpp"
#include "libslic3r/Polygon.hpp"
#include "libslic3r/ClipperUtils.hpp"
#include "libslic3r/BoundingBox.hpp"
#include "GUI_App.hpp"
#include "PresetBundle.hpp"
#include <GL/glew.h>
#include <boost/algorithm/string/predicate.hpp>
static const float GROUND_Z = -0.02f;
namespace Slic3r {
namespace GUI {
bool GeometryBuffer::set_from_triangles(const Polygons& triangles, float z, bool generate_tex_coords)
{
#if ENABLE_TEXTURES_FROM_SVG
m_vertices.clear();
unsigned int v_size = 3 * (unsigned int)triangles.size();
if (v_size == 0)
return false;
m_vertices = std::vector<Vertex>(v_size, Vertex());
float min_x = unscale<float>(triangles[0].points[0](0));
float min_y = unscale<float>(triangles[0].points[0](1));
float max_x = min_x;
float max_y = min_y;
unsigned int v_count = 0;
for (const Polygon& t : triangles)
{
for (unsigned int i = 0; i < 3; ++i)
{
Vertex& v = m_vertices[v_count];
const Point& p = t.points[i];
float x = unscale<float>(p(0));
float y = unscale<float>(p(1));
v.position[0] = x;
v.position[1] = y;
v.position[2] = z;
if (generate_tex_coords)
{
v.tex_coords[0] = x;
v.tex_coords[1] = y;
min_x = std::min(min_x, x);
max_x = std::max(max_x, x);
min_y = std::min(min_y, y);
max_y = std::max(max_y, y);
}
++v_count;
}
}
if (generate_tex_coords)
{
float size_x = max_x - min_x;
float size_y = max_y - min_y;
if ((size_x != 0.0f) && (size_y != 0.0f))
{
float inv_size_x = 1.0f / size_x;
float inv_size_y = -1.0f / size_y;
for (Vertex& v : m_vertices)
{
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;
}
}
}
#else
m_vertices.clear();
m_tex_coords.clear();
unsigned int v_size = 9 * (unsigned int)triangles.size();
unsigned int t_size = 6 * (unsigned int)triangles.size();
if (v_size == 0)
return false;
m_vertices = std::vector<float>(v_size, 0.0f);
if (generate_tex_coords)
m_tex_coords = std::vector<float>(t_size, 0.0f);
float min_x = unscale<float>(triangles[0].points[0](0));
float min_y = unscale<float>(triangles[0].points[0](1));
float max_x = min_x;
float max_y = min_y;
unsigned int v_coord = 0;
unsigned int t_coord = 0;
for (const Polygon& t : triangles)
{
for (unsigned int v = 0; v < 3; ++v)
{
const Point& p = t.points[v];
float x = unscale<float>(p(0));
float y = unscale<float>(p(1));
m_vertices[v_coord++] = x;
m_vertices[v_coord++] = y;
m_vertices[v_coord++] = z;
if (generate_tex_coords)
{
m_tex_coords[t_coord++] = x;
m_tex_coords[t_coord++] = y;
min_x = std::min(min_x, x);
max_x = std::max(max_x, x);
min_y = std::min(min_y, y);
max_y = std::max(max_y, y);
}
}
}
if (generate_tex_coords)
{
float size_x = max_x - min_x;
float size_y = max_y - min_y;
if ((size_x != 0.0f) && (size_y != 0.0f))
{
float inv_size_x = 1.0f / size_x;
float inv_size_y = -1.0f / size_y;
for (unsigned int i = 0; i < m_tex_coords.size(); i += 2)
{
m_tex_coords[i] = (m_tex_coords[i] - min_x) * inv_size_x;
m_tex_coords[i + 1] = (m_tex_coords[i + 1] - min_y) * inv_size_y;
}
}
}
#endif // ENABLE_TEXTURES_FROM_SVG
return true;
}
bool GeometryBuffer::set_from_lines(const Lines& lines, float z)
{
#if ENABLE_TEXTURES_FROM_SVG
m_vertices.clear();
unsigned int v_size = 2 * (unsigned int)lines.size();
if (v_size == 0)
return false;
m_vertices = std::vector<Vertex>(v_size, Vertex());
unsigned int v_count = 0;
for (const Line& l : lines)
{
Vertex& v1 = m_vertices[v_count];
v1.position[0] = unscale<float>(l.a(0));
v1.position[1] = unscale<float>(l.a(1));
v1.position[2] = z;
++v_count;
Vertex& v2 = m_vertices[v_count];
v2.position[0] = unscale<float>(l.b(0));
v2.position[1] = unscale<float>(l.b(1));
v2.position[2] = z;
++v_count;
}
#else
m_vertices.clear();
m_tex_coords.clear();
unsigned int size = 6 * (unsigned int)lines.size();
if (size == 0)
return false;
m_vertices = std::vector<float>(size, 0.0f);
unsigned int coord = 0;
for (const Line& l : lines)
{
m_vertices[coord++] = unscale<float>(l.a(0));
m_vertices[coord++] = unscale<float>(l.a(1));
m_vertices[coord++] = z;
m_vertices[coord++] = unscale<float>(l.b(0));
m_vertices[coord++] = unscale<float>(l.b(1));
m_vertices[coord++] = z;
}
#endif // ENABLE_TEXTURES_FROM_SVG
return true;
}
#if ENABLE_TEXTURES_FROM_SVG
const float* GeometryBuffer::get_vertices_data() const
{
return (m_vertices.size() > 0) ? (const float*)m_vertices.data() : nullptr;
}
#endif // ENABLE_TEXTURES_FROM_SVG
const double Bed3D::Axes::Radius = 0.5;
const double Bed3D::Axes::ArrowBaseRadius = 2.5 * Bed3D::Axes::Radius;
const double Bed3D::Axes::ArrowLength = 5.0;
Bed3D::Axes::Axes()
: origin(Vec3d::Zero())
, length(Vec3d::Zero())
{
m_quadric = ::gluNewQuadric();
if (m_quadric != nullptr)
::gluQuadricDrawStyle(m_quadric, GLU_FILL);
}
Bed3D::Axes::~Axes()
{
if (m_quadric != nullptr)
::gluDeleteQuadric(m_quadric);
}
void Bed3D::Axes::render() const
{
if (m_quadric == nullptr)
return;
glsafe(::glEnable(GL_DEPTH_TEST));
glsafe(::glEnable(GL_LIGHTING));
// x axis
glsafe(::glColor3f(1.0f, 0.0f, 0.0f));
glsafe(::glPushMatrix());
glsafe(::glTranslated(origin(0), origin(1), origin(2)));
glsafe(::glRotated(90.0, 0.0, 1.0, 0.0));
render_axis(length(0));
glsafe(::glPopMatrix());
// y axis
glsafe(::glColor3f(0.0f, 1.0f, 0.0f));
glsafe(::glPushMatrix());
glsafe(::glTranslated(origin(0), origin(1), origin(2)));
glsafe(::glRotated(-90.0, 1.0, 0.0, 0.0));
render_axis(length(1));
glsafe(::glPopMatrix());
// z axis
glsafe(::glColor3f(0.0f, 0.0f, 1.0f));
glsafe(::glPushMatrix());
glsafe(::glTranslated(origin(0), origin(1), origin(2)));
render_axis(length(2));
glsafe(::glPopMatrix());
glsafe(::glDisable(GL_LIGHTING));
}
void Bed3D::Axes::render_axis(double length) const
{
::gluQuadricOrientation(m_quadric, GLU_OUTSIDE);
::gluCylinder(m_quadric, Radius, Radius, length, 32, 1);
::gluQuadricOrientation(m_quadric, GLU_INSIDE);
::gluDisk(m_quadric, 0.0, Radius, 32, 1);
glsafe(::glTranslated(0.0, 0.0, length));
::gluQuadricOrientation(m_quadric, GLU_OUTSIDE);
::gluCylinder(m_quadric, ArrowBaseRadius, 0.0, ArrowLength, 32, 1);
::gluQuadricOrientation(m_quadric, GLU_INSIDE);
::gluDisk(m_quadric, 0.0, ArrowBaseRadius, 32, 1);
}
Bed3D::Bed3D()
: m_type(Custom)
#if ENABLE_TEXTURES_FROM_SVG
, m_vbo_id(0)
#endif // ENABLE_TEXTURES_FROM_SVG
, m_scale_factor(1.0f)
{
}
bool Bed3D::set_shape(const Pointfs& shape)
{
EType new_type = detect_type(shape);
if (m_shape == shape && m_type == new_type)
// No change, no need to update the UI.
return false;
m_shape = shape;
m_type = new_type;
calc_bounding_box();
ExPolygon poly;
for (const Vec2d& p : m_shape)
{
poly.contour.append(Point(scale_(p(0)), scale_(p(1))));
}
calc_triangles(poly);
const BoundingBox& bed_bbox = poly.contour.bounding_box();
calc_gridlines(poly, bed_bbox);
m_polygon = offset_ex(poly.contour, (float)bed_bbox.radius() * 1.7f, jtRound, scale_(0.5))[0].contour;
#if ENABLE_TEXTURES_FROM_SVG
reset();
#endif // ENABLE_TEXTURES_FROM_SVG
// Set the origin and size for painting of the coordinate system axes.
m_axes.origin = Vec3d(0.0, 0.0, (double)GROUND_Z);
m_axes.length = 0.1 * get_bounding_box().max_size() * Vec3d::Ones();
// Let the calee to update the UI.
return true;
}
bool Bed3D::contains(const Point& point) const
{
return m_polygon.contains(point);
}
Point Bed3D::point_projection(const Point& point) const
{
return m_polygon.point_projection(point);
}
#if ENABLE_TEXTURES_FROM_SVG
void Bed3D::render(float theta, bool useVBOs, float scale_factor) const
{
m_scale_factor = scale_factor;
EType type = useVBOs ? m_type : Custom;
switch (type)
{
case MK2:
{
render_prusa("mk2", theta > 90.0f);
break;
}
case MK3:
{
render_prusa("mk3", theta > 90.0f);
break;
}
case SL1:
{
render_prusa("sl1", theta > 90.0f);
break;
}
default:
case Custom:
{
render_custom();
break;
}
}
}
#else
void Bed3D::render(float theta, bool useVBOs, float scale_factor) const
{
m_scale_factor = scale_factor;
if (m_shape.empty())
return;
switch (m_type)
{
case MK2:
{
render_prusa("mk2", theta, useVBOs);
break;
}
case MK3:
{
render_prusa("mk3", theta, useVBOs);
break;
}
case SL1:
{
render_prusa("sl1", theta, useVBOs);
break;
}
default:
case Custom:
{
render_custom();
break;
}
}
}
#endif // ENABLE_TEXTURES_FROM_SVG
void Bed3D::render_axes() const
{
if (!m_shape.empty())
m_axes.render();
}
void Bed3D::calc_bounding_box()
{
m_bounding_box = BoundingBoxf3();
for (const Vec2d& p : m_shape)
{
m_bounding_box.merge(Vec3d(p(0), p(1), 0.0));
}
}
void Bed3D::calc_triangles(const ExPolygon& poly)
{
Polygons triangles;
poly.triangulate(&triangles);
if (!m_triangles.set_from_triangles(triangles, GROUND_Z, m_type != Custom))
printf("Unable to create bed triangles\n");
}
void Bed3D::calc_gridlines(const ExPolygon& poly, const BoundingBox& bed_bbox)
{
Polylines axes_lines;
for (coord_t x = bed_bbox.min(0); x <= bed_bbox.max(0); x += scale_(10.0))
{
Polyline line;
line.append(Point(x, bed_bbox.min(1)));
line.append(Point(x, bed_bbox.max(1)));
axes_lines.push_back(line);
}
for (coord_t y = bed_bbox.min(1); y <= bed_bbox.max(1); y += scale_(10.0))
{
Polyline line;
line.append(Point(bed_bbox.min(0), y));
line.append(Point(bed_bbox.max(0), 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(poly, (float)SCALED_EPSILON)));
// append bed contours
Lines contour_lines = to_lines(poly);
std::copy(contour_lines.begin(), contour_lines.end(), std::back_inserter(gridlines));
if (!m_gridlines.set_from_lines(gridlines, GROUND_Z))
printf("Unable to create bed grid lines\n");
}
Bed3D::EType Bed3D::detect_type(const Pointfs& shape) const
{
EType type = Custom;
auto bundle = wxGetApp().preset_bundle;
if (bundle != nullptr)
{
const Preset* curr = &bundle->printers.get_selected_preset();
while (curr != nullptr)
{
if (curr->config.has("bed_shape"))
{
if ((curr->vendor != nullptr) && (curr->vendor->name == "Prusa Research") && (shape == dynamic_cast<const ConfigOptionPoints*>(curr->config.option("bed_shape"))->values))
{
if (boost::contains(curr->name, "SL1"))
{
type = SL1;
break;
}
else if (boost::contains(curr->name, "MK3") || boost::contains(curr->name, "MK2.5"))
{
type = MK3;
break;
}
else if (boost::contains(curr->name, "MK2"))
{
type = MK2;
break;
}
}
}
curr = bundle->printers.get_preset_parent(*curr);
}
}
return type;
}
#if ENABLE_TEXTURES_FROM_SVG
void Bed3D::render_prusa(const std::string &key, bool bottom) const
{
std::string tex_path = resources_dir() + "/icons/bed/" + key;
std::string model_path = resources_dir() + "/models/" + key;
// use anisotropic filter if graphic card allows
GLfloat max_anisotropy = 0.0f;
if (glewIsSupported("GL_EXT_texture_filter_anisotropic"))
::glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &max_anisotropy);
// use higher resolution images if graphic card allows
GLint max_tex_size;
::glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max_tex_size);
// clamp or the texture generation becomes too slow
max_tex_size = std::min(max_tex_size, 8192);
std::string filename = tex_path + ".svg";
if ((m_texture.get_id() == 0) || (m_texture.get_source() != filename))
{
if (!m_texture.load_from_svg_file(filename, true, max_tex_size))
{
render_custom();
return;
}
if (max_anisotropy > 0.0f)
{
::glBindTexture(GL_TEXTURE_2D, m_texture.get_id());
::glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, max_anisotropy);
::glBindTexture(GL_TEXTURE_2D, 0);
}
}
if (!bottom)
{
filename = model_path + "_bed.stl";
if ((m_model.get_filename() != filename) && m_model.init_from_file(filename, true)) {
Vec3d offset = m_bounding_box.center() - Vec3d(0.0, 0.0, 0.5 * m_model.get_bounding_box().size()(2));
if (key == "mk2")
// hardcoded value to match the stl model
offset += Vec3d(0.0, 7.5, -0.03);
else if (key == "mk3")
// hardcoded value to match the stl model
offset += Vec3d(0.0, 5.5, 2.43);
else if (key == "sl1")
// hardcoded value to match the stl model
offset += Vec3d(0.0, 0.0, -0.03);
m_model.center_around(offset);
}
if (!m_model.get_filename().empty())
{
::glEnable(GL_LIGHTING);
m_model.render();
::glDisable(GL_LIGHTING);
}
}
unsigned int triangles_vcount = m_triangles.get_vertices_count();
if (triangles_vcount > 0)
{
if (m_vbo_id == 0)
{
::glGenBuffers(1, &m_vbo_id);
::glBindBuffer(GL_ARRAY_BUFFER, m_vbo_id);
::glBufferData(GL_ARRAY_BUFFER, (GLsizeiptr)m_triangles.get_vertices_data_size(), (const GLvoid*)m_triangles.get_vertices_data(), GL_STATIC_DRAW);
::glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, m_triangles.get_vertex_data_size(), (GLvoid*)m_triangles.get_position_offset());
::glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, m_triangles.get_vertex_data_size(), (GLvoid*)m_triangles.get_tex_coords_offset());
::glBindBuffer(GL_ARRAY_BUFFER, 0);
}
::glEnable(GL_DEPTH_TEST);
::glDepthMask(GL_FALSE);
::glEnable(GL_BLEND);
::glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
::glEnable(GL_TEXTURE_2D);
::glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
if (bottom)
::glFrontFace(GL_CW);
render_prusa_shader(triangles_vcount, bottom);
if (bottom)
::glFrontFace(GL_CCW);
::glDisable(GL_TEXTURE_2D);
::glDisable(GL_BLEND);
::glDepthMask(GL_TRUE);
}
}
void Bed3D::render_prusa_shader(unsigned int vertices_count, bool transparent) const
{
if (m_shader.get_shader_program_id() == 0)
m_shader.init("printbed.vs", "printbed.fs");
if (m_shader.is_initialized())
{
m_shader.start_using();
m_shader.set_uniform("transparent_background", transparent);
::glBindTexture(GL_TEXTURE_2D, (GLuint)m_texture.get_id());
::glBindBuffer(GL_ARRAY_BUFFER, m_vbo_id);
::glEnableVertexAttribArray(0);
::glEnableVertexAttribArray(1);
::glDrawArrays(GL_TRIANGLES, 0, (GLsizei)vertices_count);
::glDisableVertexAttribArray(1);
::glDisableVertexAttribArray(0);
::glBindBuffer(GL_ARRAY_BUFFER, 0);
::glBindTexture(GL_TEXTURE_2D, 0);
m_shader.stop_using();
}
}
#else
void Bed3D::render_prusa(const std::string &key, float theta, bool useVBOs) const
{
std::string tex_path = resources_dir() + "/icons/bed/" + key;
// use higher resolution images if graphic card allows
GLint max_tex_size;
::glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max_tex_size);
// temporary set to lowest resolution
max_tex_size = 2048;
if (max_tex_size >= 8192)
tex_path += "_8192";
else if (max_tex_size >= 4096)
tex_path += "_4096";
std::string model_path = resources_dir() + "/models/" + key;
// use anisotropic filter if graphic card allows
GLfloat max_anisotropy = 0.0f;
if (glewIsSupported("GL_EXT_texture_filter_anisotropic"))
::glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &max_anisotropy);
std::string filename = tex_path + "_top.png";
if ((m_top_texture.get_id() == 0) || (m_top_texture.get_source() != filename))
{
if (!m_top_texture.load_from_file(filename, true))
{
render_custom();
return;
}
if (max_anisotropy > 0.0f)
{
glsafe(::glBindTexture(GL_TEXTURE_2D, m_top_texture.get_id()));
glsafe(::glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, max_anisotropy));
glsafe(::glBindTexture(GL_TEXTURE_2D, 0));
}
}
filename = tex_path + "_bottom.png";
if ((m_bottom_texture.get_id() == 0) || (m_bottom_texture.get_source() != filename))
{
if (!m_bottom_texture.load_from_file(filename, true))
{
render_custom();
return;
}
if (max_anisotropy > 0.0f)
{
glsafe(::glBindTexture(GL_TEXTURE_2D, m_bottom_texture.get_id()));
glsafe(::glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, max_anisotropy));
glsafe(::glBindTexture(GL_TEXTURE_2D, 0));
}
}
if (theta <= 90.0f)
{
filename = model_path + "_bed.stl";
if ((m_model.get_filename() != filename) && m_model.init_from_file(filename, useVBOs)) {
Vec3d offset = m_bounding_box.center() - Vec3d(0.0, 0.0, 0.5 * m_model.get_bounding_box().size()(2));
if (key == "mk2")
// hardcoded value to match the stl model
offset += Vec3d(0.0, 7.5, -0.03);
else if (key == "mk3")
// hardcoded value to match the stl model
offset += Vec3d(0.0, 5.5, 2.43);
else if (key == "sl1")
// hardcoded value to match the stl model
offset += Vec3d(0.0, 0.0, -0.03);
m_model.center_around(offset);
}
if (!m_model.get_filename().empty())
{
glsafe(::glEnable(GL_LIGHTING));
m_model.render();
glsafe(::glDisable(GL_LIGHTING));
}
}
unsigned int triangles_vcount = m_triangles.get_vertices_count();
if (triangles_vcount > 0)
{
glsafe(::glEnable(GL_DEPTH_TEST));
glsafe(::glDepthMask(GL_FALSE));
glsafe(::glEnable(GL_BLEND));
glsafe(::glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA));
glsafe(::glEnable(GL_TEXTURE_2D));
glsafe(::glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE));
glsafe(::glEnableClientState(GL_VERTEX_ARRAY));
glsafe(::glEnableClientState(GL_TEXTURE_COORD_ARRAY));
if (theta > 90.0f)
glsafe(::glFrontFace(GL_CW));
glsafe(::glBindTexture(GL_TEXTURE_2D, (theta <= 90.0f) ? (GLuint)m_top_texture.get_id() : (GLuint)m_bottom_texture.get_id()));
glsafe(::glVertexPointer(3, GL_FLOAT, 0, (GLvoid*)m_triangles.get_vertices()));
glsafe(::glTexCoordPointer(2, GL_FLOAT, 0, (GLvoid*)m_triangles.get_tex_coords()));
glsafe(::glDrawArrays(GL_TRIANGLES, 0, (GLsizei)triangles_vcount));
if (theta > 90.0f)
glsafe(::glFrontFace(GL_CCW));
glsafe(::glBindTexture(GL_TEXTURE_2D, 0));
glsafe(::glDisableClientState(GL_TEXTURE_COORD_ARRAY));
glsafe(::glDisableClientState(GL_VERTEX_ARRAY));
glsafe(::glDisable(GL_TEXTURE_2D));
glsafe(::glDisable(GL_BLEND));
glsafe(::glDepthMask(GL_TRUE));
}
}
#endif // ENABLE_TEXTURES_FROM_SVG
void Bed3D::render_custom() const
{
#if ENABLE_TEXTURES_FROM_SVG
m_texture.reset();
#else
m_top_texture.reset();
m_bottom_texture.reset();
#endif // ENABLE_TEXTURES_FROM_SVG
unsigned int triangles_vcount = m_triangles.get_vertices_count();
if (triangles_vcount > 0)
{
glsafe(::glEnable(GL_LIGHTING));
glsafe(::glDisable(GL_DEPTH_TEST));
glsafe(::glEnable(GL_BLEND));
glsafe(::glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA));
glsafe(::glEnableClientState(GL_VERTEX_ARRAY));
glsafe(::glColor4f(0.35f, 0.35f, 0.35f, 0.4f));
glsafe(::glNormal3d(0.0f, 0.0f, 1.0f));
#if ENABLE_TEXTURES_FROM_SVG
::glVertexPointer(3, GL_FLOAT, m_triangles.get_vertex_data_size(), (GLvoid*)m_triangles.get_vertices_data());
#else
glsafe(::glVertexPointer(3, GL_FLOAT, 0, (GLvoid*)m_triangles.get_vertices()));
#endif // ENABLE_TEXTURES_FROM_SVG
glsafe(::glDrawArrays(GL_TRIANGLES, 0, (GLsizei)triangles_vcount));
// draw grid
unsigned int gridlines_vcount = m_gridlines.get_vertices_count();
// we need depth test for grid, otherwise it would disappear when looking the object from below
glsafe(::glEnable(GL_DEPTH_TEST));
glsafe(::glLineWidth(3.0f * m_scale_factor));
glsafe(::glColor4f(0.2f, 0.2f, 0.2f, 0.4f));
#if ENABLE_TEXTURES_FROM_SVG
::glVertexPointer(3, GL_FLOAT, m_triangles.get_vertex_data_size(), (GLvoid*)m_gridlines.get_vertices_data());
#else
glsafe(::glVertexPointer(3, GL_FLOAT, 0, (GLvoid*)m_gridlines.get_vertices()));
#endif // ENABLE_TEXTURES_FROM_SVG
glsafe(::glDrawArrays(GL_LINES, 0, (GLsizei)gridlines_vcount));
glsafe(::glDisableClientState(GL_VERTEX_ARRAY));
glsafe(::glDisable(GL_BLEND));
glsafe(::glDisable(GL_LIGHTING));
}
}
#if ENABLE_TEXTURES_FROM_SVG
void Bed3D::reset()
{
if (m_vbo_id > 0)
{
::glDeleteBuffers(1, &m_vbo_id);
m_vbo_id = 0;
}
}
#endif // ENABLE_TEXTURES_FROM_SVG
} // GUI
} // Slic3r

147
src/slic3r/GUI/3DBed.hpp Normal file
View File

@ -0,0 +1,147 @@
#ifndef slic3r_3DBed_hpp_
#define slic3r_3DBed_hpp_
#include "GLTexture.hpp"
#include "3DScene.hpp"
#if ENABLE_TEXTURES_FROM_SVG
#include "GLShader.hpp"
#endif // ENABLE_TEXTURES_FROM_SVG
class GLUquadric;
typedef class GLUquadric GLUquadricObj;
namespace Slic3r {
namespace GUI {
class GeometryBuffer
{
#if ENABLE_TEXTURES_FROM_SVG
struct Vertex
{
float position[3];
float tex_coords[2];
Vertex()
{
position[0] = 0.0f; position[1] = 0.0f; position[2] = 0.0f;
tex_coords[0] = 0.0f; tex_coords[1] = 0.0f;
}
};
std::vector<Vertex> m_vertices;
#else
std::vector<float> m_vertices;
std::vector<float> m_tex_coords;
#endif // ENABLE_TEXTURES_FROM_SVG
public:
bool set_from_triangles(const Polygons& triangles, float z, bool generate_tex_coords);
bool set_from_lines(const Lines& lines, float z);
#if ENABLE_TEXTURES_FROM_SVG
const float* get_vertices_data() const;
unsigned int get_vertices_data_size() const { return (unsigned int)m_vertices.size() * get_vertex_data_size(); }
unsigned int get_vertex_data_size() const { return (unsigned int)(5 * sizeof(float)); }
unsigned int get_position_offset() const { return 0; }
unsigned int get_tex_coords_offset() const { return (unsigned int)(3 * sizeof(float)); }
unsigned int get_vertices_count() const { return (unsigned int)m_vertices.size(); }
#else
const float* get_vertices() const { return m_vertices.data(); }
const float* get_tex_coords() const { return m_tex_coords.data(); }
unsigned int get_vertices_count() const { return (unsigned int)m_vertices.size() / 3; }
#endif // ENABLE_TEXTURES_FROM_SVG
};
class Bed3D
{
struct Axes
{
static const double Radius;
static const double ArrowBaseRadius;
static const double ArrowLength;
Vec3d origin;
Vec3d length;
GLUquadricObj* m_quadric;
Axes();
~Axes();
void render() const;
private:
void render_axis(double length) const;
};
public:
enum EType : unsigned char
{
MK2,
MK3,
SL1,
Custom,
Num_Types
};
private:
EType m_type;
Pointfs m_shape;
BoundingBoxf3 m_bounding_box;
Polygon m_polygon;
GeometryBuffer m_triangles;
GeometryBuffer m_gridlines;
#if ENABLE_TEXTURES_FROM_SVG
mutable GLTexture m_texture;
mutable Shader m_shader;
mutable unsigned int m_vbo_id;
#else
mutable GLTexture m_top_texture;
mutable GLTexture m_bottom_texture;
#endif // ENABLE_TEXTURES_FROM_SVG
mutable GLBed m_model;
Axes m_axes;
mutable float m_scale_factor;
public:
Bed3D();
#if ENABLE_TEXTURES_FROM_SVG
~Bed3D() { reset(); }
#endif // ENABLE_TEXTURES_FROM_SVG
EType get_type() const { return m_type; }
bool is_prusa() const { return (m_type == MK2) || (m_type == MK3) || (m_type == SL1); }
bool is_custom() const { return m_type == Custom; }
const Pointfs& get_shape() const { return m_shape; }
// Return true if the bed shape changed, so the calee will update the UI.
bool set_shape(const Pointfs& shape);
const BoundingBoxf3& get_bounding_box() const { return m_bounding_box; }
bool contains(const Point& point) const;
Point point_projection(const Point& point) const;
void render(float theta, bool useVBOs, float scale_factor) const;
void render_axes() const;
private:
void calc_bounding_box();
void calc_triangles(const ExPolygon& poly);
void calc_gridlines(const ExPolygon& poly, const BoundingBox& bed_bbox);
EType detect_type(const Pointfs& shape) const;
#if ENABLE_TEXTURES_FROM_SVG
void render_prusa(const std::string& key, bool bottom) const;
void render_prusa_shader(unsigned int vertices_count, bool transparent) const;
#else
void render_prusa(const std::string &key, float theta, bool useVBOs) const;
#endif // ENABLE_TEXTURES_FROM_SVG
void render_custom() const;
#if ENABLE_TEXTURES_FROM_SVG
void reset();
#endif // ENABLE_TEXTURES_FROM_SVG
};
} // GUI
} // Slic3r
#endif // slic3r_3DBed_hpp_

View File

@ -11,9 +11,7 @@
#include "libslic3r/Slicing.hpp"
#include "libslic3r/GCode/Analyzer.hpp"
#include "slic3r/GUI/PresetBundle.hpp"
#if ENABLE_PRINT_BED_MODELS
#include "libslic3r/Format/STL.hpp"
#endif // ENABLE_PRINT_BED_MODELS
#include <stdio.h>
#include <stdlib.h>
@ -23,10 +21,8 @@
#include <boost/log/trivial.hpp>
#if ENABLE_PRINT_BED_MODELS
#include <boost/filesystem/operations.hpp>
#include <boost/algorithm/string.hpp>
#endif // ENABLE_PRINT_BED_MODELS
#include <tbb/parallel_for.h>
#include <tbb/spin_mutex.h>
@ -1671,27 +1667,19 @@ GUI::GLCanvas3DManager _3DScene::s_canvas_mgr;
GLModel::GLModel()
: m_useVBOs(false)
#if ENABLE_PRINT_BED_MODELS
, m_filename("")
#endif // ENABLE_PRINT_BED_MODELS
{
m_volume.shader_outside_printer_detection_enabled = false;
}
GLModel::~GLModel()
{
#if ENABLE_PRINT_BED_MODELS
reset();
#else
m_volume.release_geometry();
#endif // ENABLE_PRINT_BED_MODELS
}
void GLModel::set_color(const float* color, unsigned int size)
{
#if ENABLE_PRINT_BED_MODELS
::memcpy((void*)m_volume.color, (const void*)color, (size_t)(std::min((unsigned int)4, size) * sizeof(float)));
#endif // ENABLE_PRINT_BED_MODELS
m_volume.set_render_color(color, size);
}
@ -1725,13 +1713,11 @@ void GLModel::set_scale(const Vec3d& scale)
m_volume.set_volume_scaling_factor(scale);
}
#if ENABLE_PRINT_BED_MODELS
void GLModel::reset()
{
m_volume.release_geometry();
m_filename = "";
}
#endif // ENABLE_PRINT_BED_MODELS
void GLModel::render() const
{
@ -1968,7 +1954,6 @@ bool GLCurvedArrow::on_init(bool useVBOs)
return true;
}
#if ENABLE_PRINT_BED_MODELS
bool GLBed::on_init_from_file(const std::string& filename, bool useVBOs)
{
reset();
@ -2011,7 +1996,6 @@ bool GLBed::on_init_from_file(const std::string& filename, bool useVBOs)
return true;
}
#endif // ENABLE_PRINT_BED_MODELS
std::string _3DScene::get_gl_info(bool format_as_html, bool extensions)
{

View File

@ -498,20 +498,16 @@ class GLModel
protected:
GLVolume m_volume;
bool m_useVBOs;
#if ENABLE_PRINT_BED_MODELS
std::string m_filename;
#endif // ENABLE_PRINT_BED_MODELS
public:
GLModel();
virtual ~GLModel();
bool init(bool useVBOs) { return on_init(useVBOs); }
#if ENABLE_PRINT_BED_MODELS
bool init_from_file(const std::string& filename, bool useVBOs) { return on_init_from_file(filename, useVBOs); }
void center_around(const Vec3d& center) { m_volume.set_volume_offset(center - m_volume.bounding_box.center()); }
#endif // ENABLE_PRINT_BED_MODELS
void set_color(const float* color, unsigned int size);
const Vec3d& get_offset() const;
@ -521,22 +517,16 @@ public:
const Vec3d& get_scale() const;
void set_scale(const Vec3d& scale);
#if ENABLE_PRINT_BED_MODELS
const std::string& get_filename() const { return m_filename; }
const BoundingBoxf3& get_bounding_box() const { return m_volume.bounding_box; }
void reset();
#endif // ENABLE_PRINT_BED_MODELS
void render() const;
protected:
#if ENABLE_PRINT_BED_MODELS
virtual bool on_init(bool useVBOs) { return false; }
virtual bool on_init_from_file(const std::string& filename, bool useVBOs) { return false; }
#else
virtual bool on_init(bool useVBOs) = 0;
#endif // ENABLE_PRINT_BED_MODELS
private:
void render_VBOs() const;
@ -560,13 +550,11 @@ protected:
virtual bool on_init(bool useVBOs);
};
#if ENABLE_PRINT_BED_MODELS
class GLBed : public GLModel
{
protected:
virtual bool on_init_from_file(const std::string& filename, bool useVBOs);
};
#endif // ENABLE_PRINT_BED_MODELS
class _3DScene
{

View File

@ -84,112 +84,6 @@ static const float AXES_COLOR[3][3] = { { 1.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f
namespace Slic3r {
namespace GUI {
bool GeometryBuffer::set_from_triangles(const Polygons& triangles, float z, bool generate_tex_coords)
{
m_vertices.clear();
m_tex_coords.clear();
unsigned int v_size = 9 * (unsigned int)triangles.size();
unsigned int t_size = 6 * (unsigned int)triangles.size();
if (v_size == 0)
return false;
m_vertices = std::vector<float>(v_size, 0.0f);
if (generate_tex_coords)
m_tex_coords = std::vector<float>(t_size, 0.0f);
float min_x = unscale<float>(triangles[0].points[0](0));
float min_y = unscale<float>(triangles[0].points[0](1));
float max_x = min_x;
float max_y = min_y;
unsigned int v_coord = 0;
unsigned int t_coord = 0;
for (const Polygon& t : triangles)
{
for (unsigned int v = 0; v < 3; ++v)
{
const Point& p = t.points[v];
float x = unscale<float>(p(0));
float y = unscale<float>(p(1));
m_vertices[v_coord++] = x;
m_vertices[v_coord++] = y;
m_vertices[v_coord++] = z;
if (generate_tex_coords)
{
m_tex_coords[t_coord++] = x;
m_tex_coords[t_coord++] = y;
min_x = std::min(min_x, x);
max_x = std::max(max_x, x);
min_y = std::min(min_y, y);
max_y = std::max(max_y, y);
}
}
}
if (generate_tex_coords)
{
float size_x = max_x - min_x;
float size_y = max_y - min_y;
if ((size_x != 0.0f) && (size_y != 0.0f))
{
float inv_size_x = 1.0f / size_x;
float inv_size_y = -1.0f / size_y;
for (unsigned int i = 0; i < m_tex_coords.size(); i += 2)
{
m_tex_coords[i] = (m_tex_coords[i] - min_x) * inv_size_x;
m_tex_coords[i + 1] = (m_tex_coords[i + 1] - min_y) * inv_size_y;
}
}
}
return true;
}
bool GeometryBuffer::set_from_lines(const Lines& lines, float z)
{
m_vertices.clear();
m_tex_coords.clear();
unsigned int size = 6 * (unsigned int)lines.size();
if (size == 0)
return false;
m_vertices = std::vector<float>(size, 0.0f);
unsigned int coord = 0;
for (const Line& l : lines)
{
m_vertices[coord++] = unscale<float>(l.a(0));
m_vertices[coord++] = unscale<float>(l.a(1));
m_vertices[coord++] = z;
m_vertices[coord++] = unscale<float>(l.b(0));
m_vertices[coord++] = unscale<float>(l.b(1));
m_vertices[coord++] = z;
}
return true;
}
const float* GeometryBuffer::get_vertices() const
{
return m_vertices.data();
}
const float* GeometryBuffer::get_tex_coords() const
{
return m_tex_coords.data();
}
unsigned int GeometryBuffer::get_vertices_count() const
{
return (unsigned int)m_vertices.size() / 3;
}
Size::Size()
: m_width(0)
, m_height(0)
@ -344,502 +238,7 @@ void GLCanvas3D::Camera::set_scene_box(const BoundingBoxf3& box, GLCanvas3D& can
}
}
GLCanvas3D::Bed::Bed()
: m_type(Custom)
, m_scale_factor(1.0f)
{
}
bool GLCanvas3D::Bed::is_prusa() const
{
return (m_type == MK2) || (m_type == MK3) || (m_type == SL1);
}
bool GLCanvas3D::Bed::is_custom() const
{
return m_type == Custom;
}
const Pointfs& GLCanvas3D::Bed::get_shape() const
{
return m_shape;
}
bool GLCanvas3D::Bed::set_shape(const Pointfs& shape)
{
#if ENABLE_REWORKED_BED_SHAPE_CHANGE
EType new_type = _detect_type(shape);
#else
EType new_type = _detect_type();
#endif // ENABLE_REWORKED_BED_SHAPE_CHANGE
if (m_shape == shape && m_type == new_type)
// No change, no need to update the UI.
return false;
m_shape = shape;
m_type = new_type;
_calc_bounding_box();
ExPolygon poly;
for (const Vec2d& p : m_shape)
{
poly.contour.append(Point(scale_(p(0)), scale_(p(1))));
}
_calc_triangles(poly);
const BoundingBox& bed_bbox = poly.contour.bounding_box();
_calc_gridlines(poly, bed_bbox);
m_polygon = offset_ex(poly.contour, (float)bed_bbox.radius() * 1.7f, jtRound, scale_(0.5))[0].contour;
// Let the calee to update the UI.
return true;
}
const BoundingBoxf3& GLCanvas3D::Bed::get_bounding_box() const
{
return m_bounding_box;
}
bool GLCanvas3D::Bed::contains(const Point& point) const
{
return m_polygon.contains(point);
}
Point GLCanvas3D::Bed::point_projection(const Point& point) const
{
return m_polygon.point_projection(point);
}
#if ENABLE_PRINT_BED_MODELS
void GLCanvas3D::Bed::render(float theta, bool useVBOs, float scale_factor) const
{
m_scale_factor = scale_factor;
switch (m_type)
{
case MK2:
{
_render_prusa("mk2", theta, useVBOs);
break;
}
case MK3:
{
_render_prusa("mk3", theta, useVBOs);
break;
}
case SL1:
{
_render_prusa("sl1", theta, useVBOs);
break;
}
default:
case Custom:
{
_render_custom();
break;
}
}
}
#else
void GLCanvas3D::Bed::render(float theta, float scale_factor) const
{
m_scale_factor = scale_factor;
switch (m_type)
{
case MK2:
{
_render_prusa("mk2", theta);
break;
}
case MK3:
{
_render_prusa("mk3", theta);
break;
}
case SL1:
{
_render_prusa("sl1", theta);
break;
}
default:
case Custom:
{
_render_custom();
break;
}
}
}
#endif // ENABLE_PRINT_BED_MODELS
void GLCanvas3D::Bed::_calc_bounding_box()
{
m_bounding_box = BoundingBoxf3();
for (const Vec2d& p : m_shape)
{
m_bounding_box.merge(Vec3d(p(0), p(1), 0.0));
}
}
void GLCanvas3D::Bed::_calc_triangles(const ExPolygon& poly)
{
Polygons triangles;
poly.triangulate(&triangles);
if (!m_triangles.set_from_triangles(triangles, GROUND_Z, m_type != Custom))
printf("Unable to create bed triangles\n");
}
void GLCanvas3D::Bed::_calc_gridlines(const ExPolygon& poly, const BoundingBox& bed_bbox)
{
Polylines axes_lines;
for (coord_t x = bed_bbox.min(0); x <= bed_bbox.max(0); x += scale_(10.0))
{
Polyline line;
line.append(Point(x, bed_bbox.min(1)));
line.append(Point(x, bed_bbox.max(1)));
axes_lines.push_back(line);
}
for (coord_t y = bed_bbox.min(1); y <= bed_bbox.max(1); y += scale_(10.0))
{
Polyline line;
line.append(Point(bed_bbox.min(0), y));
line.append(Point(bed_bbox.max(0), 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(poly, (float)SCALED_EPSILON)));
// append bed contours
Lines contour_lines = to_lines(poly);
std::copy(contour_lines.begin(), contour_lines.end(), std::back_inserter(gridlines));
if (!m_gridlines.set_from_lines(gridlines, GROUND_Z))
printf("Unable to create bed grid lines\n");
}
#if ENABLE_REWORKED_BED_SHAPE_CHANGE
GLCanvas3D::Bed::EType GLCanvas3D::Bed::_detect_type(const Pointfs& shape) const
#else
GLCanvas3D::Bed::EType GLCanvas3D::Bed::_detect_type() const
#endif // ENABLE_REWORKED_BED_SHAPE_CHANGE
{
EType type = Custom;
auto bundle = wxGetApp().preset_bundle;
if (bundle != nullptr)
{
const Preset* curr = &bundle->printers.get_selected_preset();
while (curr != nullptr)
{
if (curr->config.has("bed_shape"))
{
#if ENABLE_REWORKED_BED_SHAPE_CHANGE
if ((curr->vendor != nullptr) && (curr->vendor->name == "Prusa Research") && (shape == dynamic_cast<const ConfigOptionPoints*>(curr->config.option("bed_shape"))->values))
{
if (boost::contains(curr->name, "SL1"))
{
type = SL1;
break;
}
else if (boost::contains(curr->name, "MK3") || boost::contains(curr->name, "MK2.5"))
{
type = MK3;
break;
}
else if (boost::contains(curr->name, "MK2"))
{
type = MK2;
break;
}
}
#else
if (boost::contains(curr->name, "SL1"))
{
//FIXME add a condition on the size of the print bed?
type = SL1;
break;
}
else if (_are_equal(m_shape, dynamic_cast<const ConfigOptionPoints*>(curr->config.option("bed_shape"))->values))
{
if ((curr->vendor != nullptr) && (curr->vendor->name == "Prusa Research"))
{
if (boost::contains(curr->name, "MK3") || boost::contains(curr->name, "MK2.5"))
{
type = MK3;
break;
} else if (boost::contains(curr->name, "MK2"))
{
type = MK2;
break;
}
}
}
#endif // ENABLE_REWORKED_BED_SHAPE_CHANGE
}
curr = bundle->printers.get_preset_parent(*curr);
}
}
return type;
}
#if ENABLE_PRINT_BED_MODELS
void GLCanvas3D::Bed::_render_prusa(const std::string &key, float theta, bool useVBOs) const
#else
void GLCanvas3D::Bed::_render_prusa(const std::string &key, float theta) const
#endif // ENABLE_PRINT_BED_MODELS
{
std::string tex_path = resources_dir() + "/icons/bed/" + key;
// use higher resolution images if graphic card allows
GLint max_tex_size;
::glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max_tex_size);
// temporary set to lowest resolution
max_tex_size = 2048;
if (max_tex_size >= 8192)
tex_path += "_8192";
else if (max_tex_size >= 4096)
tex_path += "_4096";
#if ENABLE_PRINT_BED_MODELS
std::string model_path = resources_dir() + "/models/" + key;
#endif // ENABLE_PRINT_BED_MODELS
#if ENABLE_ANISOTROPIC_FILTER_ON_BED_TEXTURES
// use anisotropic filter if graphic card allows
GLfloat max_anisotropy = 0.0f;
if (glewIsSupported("GL_EXT_texture_filter_anisotropic"))
::glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &max_anisotropy);
#endif // ENABLE_ANISOTROPIC_FILTER_ON_BED_TEXTURES
std::string filename = tex_path + "_top.png";
if ((m_top_texture.get_id() == 0) || (m_top_texture.get_source() != filename))
{
if (!m_top_texture.load_from_file(filename, true))
{
_render_custom();
return;
}
#if ENABLE_ANISOTROPIC_FILTER_ON_BED_TEXTURES
if (max_anisotropy > 0.0f)
{
::glBindTexture(GL_TEXTURE_2D, m_top_texture.get_id());
::glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, max_anisotropy);
::glBindTexture(GL_TEXTURE_2D, 0);
}
#endif // ENABLE_ANISOTROPIC_FILTER_ON_BED_TEXTURES
}
filename = tex_path + "_bottom.png";
if ((m_bottom_texture.get_id() == 0) || (m_bottom_texture.get_source() != filename))
{
if (!m_bottom_texture.load_from_file(filename, true))
{
_render_custom();
return;
}
#if ENABLE_ANISOTROPIC_FILTER_ON_BED_TEXTURES
if (max_anisotropy > 0.0f)
{
::glBindTexture(GL_TEXTURE_2D, m_bottom_texture.get_id());
::glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, max_anisotropy);
::glBindTexture(GL_TEXTURE_2D, 0);
}
#endif // ENABLE_ANISOTROPIC_FILTER_ON_BED_TEXTURES
}
#if ENABLE_PRINT_BED_MODELS
if (theta <= 90.0f)
{
filename = model_path + "_bed.stl";
if ((m_model.get_filename() != filename) && m_model.init_from_file(filename, useVBOs)) {
Vec3d offset = m_bounding_box.center() - Vec3d(0.0, 0.0, 0.5 * m_model.get_bounding_box().size()(2));
if (key == "mk2")
// hardcoded value to match the stl model
offset += Vec3d(0.0, 7.5, -0.03);
else if (key == "mk3")
// hardcoded value to match the stl model
offset += Vec3d(0.0, 5.5, 2.43);
else if (key == "sl1")
// hardcoded value to match the stl model
offset += Vec3d(0.0, 0.0, -0.03);
m_model.center_around(offset);
}
if (!m_model.get_filename().empty())
{
::glEnable(GL_LIGHTING);
m_model.render();
::glDisable(GL_LIGHTING);
}
}
#endif // ENABLE_PRINT_BED_MODELS
unsigned int triangles_vcount = m_triangles.get_vertices_count();
if (triangles_vcount > 0)
{
::glEnable(GL_DEPTH_TEST);
::glDepthMask(GL_FALSE);
::glEnable(GL_BLEND);
::glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
::glEnable(GL_TEXTURE_2D);
::glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
::glEnableClientState(GL_VERTEX_ARRAY);
::glEnableClientState(GL_TEXTURE_COORD_ARRAY);
if (theta > 90.0f)
::glFrontFace(GL_CW);
::glBindTexture(GL_TEXTURE_2D, (theta <= 90.0f) ? (GLuint)m_top_texture.get_id() : (GLuint)m_bottom_texture.get_id());
::glVertexPointer(3, GL_FLOAT, 0, (GLvoid*)m_triangles.get_vertices());
::glTexCoordPointer(2, GL_FLOAT, 0, (GLvoid*)m_triangles.get_tex_coords());
::glDrawArrays(GL_TRIANGLES, 0, (GLsizei)triangles_vcount);
if (theta > 90.0f)
::glFrontFace(GL_CCW);
::glBindTexture(GL_TEXTURE_2D, 0);
::glDisableClientState(GL_TEXTURE_COORD_ARRAY);
::glDisableClientState(GL_VERTEX_ARRAY);
::glDisable(GL_TEXTURE_2D);
::glDisable(GL_BLEND);
::glDepthMask(GL_TRUE);
}
}
void GLCanvas3D::Bed::_render_custom() const
{
m_top_texture.reset();
m_bottom_texture.reset();
unsigned int triangles_vcount = m_triangles.get_vertices_count();
if (triangles_vcount > 0)
{
::glEnable(GL_LIGHTING);
::glDisable(GL_DEPTH_TEST);
::glEnable(GL_BLEND);
::glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
::glEnableClientState(GL_VERTEX_ARRAY);
::glColor4f(0.35f, 0.35f, 0.35f, 0.4f);
::glNormal3d(0.0f, 0.0f, 1.0f);
::glVertexPointer(3, GL_FLOAT, 0, (GLvoid*)m_triangles.get_vertices());
::glDrawArrays(GL_TRIANGLES, 0, (GLsizei)triangles_vcount);
// draw grid
unsigned int gridlines_vcount = m_gridlines.get_vertices_count();
// we need depth test for grid, otherwise it would disappear when looking the object from below
::glEnable(GL_DEPTH_TEST);
::glLineWidth(3.0f * m_scale_factor);
::glColor4f(0.2f, 0.2f, 0.2f, 0.4f);
::glVertexPointer(3, GL_FLOAT, 0, (GLvoid*)m_gridlines.get_vertices());
::glDrawArrays(GL_LINES, 0, (GLsizei)gridlines_vcount);
::glDisableClientState(GL_VERTEX_ARRAY);
::glDisable(GL_BLEND);
::glDisable(GL_LIGHTING);
}
}
#if !ENABLE_REWORKED_BED_SHAPE_CHANGE
bool GLCanvas3D::Bed::_are_equal(const Pointfs& bed_1, const Pointfs& bed_2)
{
if (bed_1.size() != bed_2.size())
return false;
for (unsigned int i = 0; i < (unsigned int)bed_1.size(); ++i)
{
if (bed_1[i] != bed_2[i])
return false;
}
return true;
}
#endif // !ENABLE_REWORKED_BED_SHAPE_CHANGE
const double GLCanvas3D::Axes::Radius = 0.5;
const double GLCanvas3D::Axes::ArrowBaseRadius = 2.5 * GLCanvas3D::Axes::Radius;
const double GLCanvas3D::Axes::ArrowLength = 5.0;
GLCanvas3D::Axes::Axes()
: origin(Vec3d::Zero())
, length(Vec3d::Zero())
{
m_quadric = ::gluNewQuadric();
if (m_quadric != nullptr)
::gluQuadricDrawStyle(m_quadric, GLU_FILL);
}
GLCanvas3D::Axes::~Axes()
{
if (m_quadric != nullptr)
::gluDeleteQuadric(m_quadric);
}
void GLCanvas3D::Axes::render() const
{
if (m_quadric == nullptr)
return;
::glEnable(GL_DEPTH_TEST);
::glEnable(GL_LIGHTING);
// x axis
::glColor3f(1.0f, 0.0f, 0.0f);
::glPushMatrix();
::glTranslated(origin(0), origin(1), origin(2));
::glRotated(90.0, 0.0, 1.0, 0.0);
render_axis(length(0));
::glPopMatrix();
// y axis
::glColor3f(0.0f, 1.0f, 0.0f);
::glPushMatrix();
::glTranslated(origin(0), origin(1), origin(2));
::glRotated(-90.0, 1.0, 0.0, 0.0);
render_axis(length(1));
::glPopMatrix();
// z axis
::glColor3f(0.0f, 0.0f, 1.0f);
::glPushMatrix();
::glTranslated(origin(0), origin(1), origin(2));
render_axis(length(2));
::glPopMatrix();
::glDisable(GL_LIGHTING);
}
void GLCanvas3D::Axes::render_axis(double length) const
{
::gluQuadricOrientation(m_quadric, GLU_OUTSIDE);
::gluCylinder(m_quadric, Radius, Radius, length, 32, 1);
::gluQuadricOrientation(m_quadric, GLU_INSIDE);
::gluDisk(m_quadric, 0.0, Radius, 32, 1);
::glTranslated(0.0, 0.0, length);
::gluQuadricOrientation(m_quadric, GLU_OUTSIDE);
::gluCylinder(m_quadric, ArrowBaseRadius, 0.0, ArrowLength, 32, 1);
::gluQuadricOrientation(m_quadric, GLU_INSIDE);
::gluDisk(m_quadric, 0.0, ArrowBaseRadius, 32, 1);
}
#if !ENABLE_TEXTURES_FROM_SVG
GLCanvas3D::Shader::Shader()
: m_shader(nullptr)
{
@ -918,6 +317,7 @@ void GLCanvas3D::Shader::_reset()
m_shader = nullptr;
}
}
#endif // !ENABLE_TEXTURES_FROM_SVG
GLCanvas3D::LayersEditing::LayersEditing()
: m_use_legacy_opengl(false)
@ -4085,6 +3485,7 @@ wxDEFINE_EVENT(EVT_GLCANVAS_WIPETOWER_MOVED, Vec3dEvent);
wxDEFINE_EVENT(EVT_GLCANVAS_ENABLE_ACTION_BUTTONS, Event<bool>);
wxDEFINE_EVENT(EVT_GLCANVAS_UPDATE_GEOMETRY, Vec3dsEvent<2>);
wxDEFINE_EVENT(EVT_GLCANVAS_MOUSE_DRAGGING_FINISHED, SimpleEvent);
wxDEFINE_EVENT(EVT_GLCANVAS_UPDATE_BED_SHAPE, SimpleEvent);
wxDEFINE_EVENT(EVT_GLCANVAS_TAB, SimpleEvent);
GLCanvas3D::GLCanvas3D(wxGLCanvas* canvas)
@ -4094,6 +3495,7 @@ GLCanvas3D::GLCanvas3D(wxGLCanvas* canvas)
, m_retina_helper(nullptr)
#endif
, m_in_render(false)
, m_bed(nullptr)
, m_toolbar(GLToolbar::Normal)
, m_view_toolbar(nullptr)
, m_use_clipping_planes(false)
@ -4104,11 +3506,7 @@ GLCanvas3D::GLCanvas3D(wxGLCanvas* canvas)
, m_dirty(true)
, m_initialized(false)
, m_use_VBOs(false)
#if ENABLE_REWORKED_BED_SHAPE_CHANGE
, m_requires_zoom_to_bed(false)
#else
, m_force_zoom_to_bed_enabled(false)
#endif // ENABLE_REWORKED_BED_SHAPE_CHANGE
, m_apply_zoom_to_volumes_filter(false)
, m_hover_volume_id(-1)
, m_toolbar_action_running(false)
@ -4325,36 +3723,12 @@ void GLCanvas3D::set_model(Model* model)
m_selection.set_model(m_model);
}
void GLCanvas3D::set_bed_shape(const Pointfs& shape)
void GLCanvas3D::bed_shape_changed()
{
bool new_shape = m_bed.set_shape(shape);
#if ENABLE_REWORKED_BED_SHAPE_CHANGE
if (new_shape)
{
// Set the origin and size for painting of the coordinate system axes.
m_axes.origin = Vec3d(0.0, 0.0, (double)GROUND_Z);
set_bed_axes_length(0.1 * m_bed.get_bounding_box().max_size());
m_camera.set_scene_box(scene_bounding_box(), *this);
m_requires_zoom_to_bed = true;
m_dirty = true;
}
#else
// Set the origin and size for painting of the coordinate system axes.
m_axes.origin = Vec3d(0.0, 0.0, (double)GROUND_Z);
set_bed_axes_length(0.1 * m_bed.get_bounding_box().max_size());
if (new_shape)
zoom_to_bed();
m_camera.set_scene_box(scene_bounding_box(), *this);
m_requires_zoom_to_bed = true;
m_dirty = true;
#endif // ENABLE_REWORKED_BED_SHAPE_CHANGE
}
void GLCanvas3D::set_bed_axes_length(double length)
{
m_axes.length = length * Vec3d::Ones();
}
void GLCanvas3D::set_color_by(const std::string& value)
@ -4381,7 +3755,9 @@ BoundingBoxf3 GLCanvas3D::volumes_bounding_box() const
BoundingBoxf3 GLCanvas3D::scene_bounding_box() const
{
BoundingBoxf3 bb = volumes_bounding_box();
bb.merge(m_bed.get_bounding_box());
if (m_bed != nullptr)
bb.merge(m_bed->get_bounding_box());
if (m_config != nullptr)
{
double h = m_config->opt_float("max_print_height");
@ -4444,13 +3820,6 @@ void GLCanvas3D::enable_toolbar(bool enable)
m_toolbar.set_enabled(enable);
}
#if !ENABLE_REWORKED_BED_SHAPE_CHANGE
void GLCanvas3D::enable_force_zoom_to_bed(bool enable)
{
m_force_zoom_to_bed_enabled = enable;
}
#endif // !ENABLE_REWORKED_BED_SHAPE_CHANGE
void GLCanvas3D::enable_dynamic_background(bool enable)
{
m_dynamic_background_enabled = enable;
@ -4476,7 +3845,8 @@ bool GLCanvas3D::is_toolbar_item_pressed(const std::string& name) const
void GLCanvas3D::zoom_to_bed()
{
_zoom_to_bounding_box(m_bed.get_bounding_box());
if (m_bed != nullptr)
_zoom_to_bounding_box(m_bed->get_bounding_box());
}
void GLCanvas3D::zoom_to_volumes()
@ -4589,12 +3959,10 @@ void GLCanvas3D::render()
if (!_set_current() || !_3DScene::init(m_canvas))
return;
#if ENABLE_REWORKED_BED_SHAPE_CHANGE
if (m_bed.get_shape().empty())
if ((m_bed != nullptr) && m_bed->get_shape().empty())
{
// this happens at startup when no data is still saved under <>\AppData\Roaming\Slic3rPE
if (m_config != nullptr)
set_bed_shape(m_config->opt<ConfigOptionPoints>("bed_shape")->values);
post_event(SimpleEvent(EVT_GLCANVAS_UPDATE_BED_SHAPE));
}
if (m_requires_zoom_to_bed)
@ -4604,10 +3972,6 @@ void GLCanvas3D::render()
_resize((unsigned int)cnv_size.get_width(), (unsigned int)cnv_size.get_height());
m_requires_zoom_to_bed = false;
}
#else
if (m_force_zoom_to_bed_enabled)
_force_zoom_to_bed();
#endif // ENABLE_REWORKED_BED_SHAPE_CHANGE
_camera_tranform();
@ -4621,7 +3985,7 @@ void GLCanvas3D::render()
// absolute value of the rotation
theta = 360.f - theta;
bool is_custom_bed = m_bed.is_custom();
bool is_custom_bed = (m_bed == nullptr) || m_bed->is_custom();
#if ENABLE_IMGUI
wxGetApp().imgui()->new_frame();
@ -4903,10 +4267,6 @@ void GLCanvas3D::reload_scene(bool refresh_immediately, bool force_full_scene_re
if (m_reload_delayed)
return;
#if !ENABLE_REWORKED_BED_SHAPE_CHANGE
set_bed_shape(dynamic_cast<const ConfigOptionPoints*>(m_config->option("bed_shape"))->values);
#endif // !ENABLE_REWORKED_BED_SHAPE_CHANGE
if (m_regenerate_volumes)
{
m_volumes.volumes = std::move(glvolumes_new);
@ -6194,14 +5554,6 @@ bool GLCanvas3D::_is_shown_on_screen() const
return (m_canvas != nullptr) ? m_canvas->IsShownOnScreen() : false;
}
#if !ENABLE_REWORKED_BED_SHAPE_CHANGE
void GLCanvas3D::_force_zoom_to_bed()
{
zoom_to_bed();
m_force_zoom_to_bed_enabled = false;
}
#endif // !ENABLE_REWORKED_BED_SHAPE_CHANGE
bool GLCanvas3D::_init_toolbar()
{
if (!m_toolbar.is_enabled())
@ -6410,8 +5762,9 @@ void GLCanvas3D::_resize(unsigned int w, unsigned int h)
BoundingBoxf3 GLCanvas3D::_max_bounding_box() const
{
BoundingBoxf3 bb = m_bed.get_bounding_box();
bb.merge(volumes_bounding_box());
BoundingBoxf3 bb = volumes_bounding_box();
if (m_bed != nullptr)
bb.merge(m_bed->get_bounding_box());
return bb;
}
@ -6427,11 +5780,7 @@ void GLCanvas3D::_zoom_to_bounding_box(const BoundingBoxf3& bbox)
viewport_changed();
#if ENABLE_REWORKED_BED_SHAPE_CHANGE
m_dirty = true;
#else
_refresh_if_shown_on_screen();
#endif // ENABLE_REWORKED_BED_SHAPE_CHANGE
}
}
@ -6511,15 +5860,7 @@ void GLCanvas3D::_refresh_if_shown_on_screen()
// Because of performance problems on macOS, where PaintEvents are not delivered
// frequently enough, we call render() here directly when we can.
#if ENABLE_REWORKED_BED_SHAPE_CHANGE
render();
#else
// We can't do that when m_force_zoom_to_bed_enabled == true, because then render()
// ends up calling back here via _force_zoom_to_bed(), causing a stack overflow.
if (m_canvas != nullptr) {
m_force_zoom_to_bed_enabled ? m_canvas->Refresh() : render();
}
#endif // ENABLE_REWORKED_BED_SHAPE_CHANGE
}
}
@ -6628,16 +5969,14 @@ void GLCanvas3D::_render_bed(float theta) const
scale_factor = m_retina_helper->get_scale_factor();
#endif
#if ENABLE_PRINT_BED_MODELS
m_bed.render(theta, m_use_VBOs, scale_factor);
#else
m_bed.render(theta, scale_factor);
#endif // ENABLE_PRINT_BED_MODELS
if (m_bed != nullptr)
m_bed->render(theta, m_use_VBOs, scale_factor);
}
void GLCanvas3D::_render_axes() const
{
m_axes.render();
if (m_bed != nullptr)
m_bed->render_axes();
}
void GLCanvas3D::_render_objects() const
@ -6655,9 +5994,9 @@ void GLCanvas3D::_render_objects() const
// Update the layer editing selection to the first object selected, update the current object maximum Z.
const_cast<LayersEditing&>(m_layers_editing).select_object(*m_model, this->is_layers_editing_enabled() ? m_selection.get_object_idx() : -1);
if (m_config != nullptr)
if ((m_config != nullptr) && (m_bed != nullptr))
{
const BoundingBoxf3& bed_bb = m_bed.get_bounding_box();
const BoundingBoxf3& bed_bb = m_bed->get_bounding_box();
m_volumes.set_print_box((float)bed_bb.min(0), (float)bed_bb.min(1), 0.0f, (float)bed_bb.max(0), (float)bed_bb.max(1), (float)m_config->opt_float("max_print_height"));
m_volumes.check_outside_state(m_config, nullptr);
}

View File

@ -8,6 +8,7 @@
#include "3DScene.hpp"
#include "GLToolbar.hpp"
#include "Event.hpp"
#include "3DBed.hpp"
#include <float.h>
@ -25,9 +26,6 @@ class wxGLCanvas;
// Support for Retina OpenGL on Mac OS
#define ENABLE_RETINA_GL __APPLE__
class GLUquadric;
typedef class GLUquadric GLUquadricObj;
namespace Slic3r {
class GLShader;
@ -45,21 +43,6 @@ class GLGizmoBase;
class RetinaHelper;
#endif
class GeometryBuffer
{
std::vector<float> m_vertices;
std::vector<float> m_tex_coords;
public:
bool set_from_triangles(const Polygons& triangles, float z, bool generate_tex_coords);
bool set_from_lines(const Lines& lines, float z);
const float* get_vertices() const;
const float* get_tex_coords() const;
unsigned int get_vertices_count() const;
};
class Size
{
int m_width;
@ -131,6 +114,7 @@ wxDECLARE_EVENT(EVT_GLCANVAS_INSTANCE_SCALED, SimpleEvent);
wxDECLARE_EVENT(EVT_GLCANVAS_ENABLE_ACTION_BUTTONS, Event<bool>);
wxDECLARE_EVENT(EVT_GLCANVAS_UPDATE_GEOMETRY, Vec3dsEvent<2>);
wxDECLARE_EVENT(EVT_GLCANVAS_MOUSE_DRAGGING_FINISHED, SimpleEvent);
wxDECLARE_EVENT(EVT_GLCANVAS_UPDATE_BED_SHAPE, SimpleEvent);
wxDECLARE_EVENT(EVT_GLCANVAS_TAB, SimpleEvent);
// this describes events being passed from GLCanvas3D to SlaSupport gizmo
@ -209,95 +193,7 @@ class GLCanvas3D
void set_scene_box(const BoundingBoxf3& box, GLCanvas3D& canvas);
};
class Bed
{
public:
enum EType : unsigned char
{
MK2,
MK3,
SL1,
Custom,
Num_Types
};
private:
EType m_type;
Pointfs m_shape;
BoundingBoxf3 m_bounding_box;
Polygon m_polygon;
GeometryBuffer m_triangles;
GeometryBuffer m_gridlines;
mutable GLTexture m_top_texture;
mutable GLTexture m_bottom_texture;
#if ENABLE_PRINT_BED_MODELS
mutable GLBed m_model;
#endif // ENABLE_PRINT_BED_MODELS
mutable float m_scale_factor;
public:
Bed();
#if ENABLE_REWORKED_BED_SHAPE_CHANGE
EType get_type() const { return m_type; }
#endif // ENABLE_REWORKED_BED_SHAPE_CHANGE
bool is_prusa() const;
bool is_custom() const;
const Pointfs& get_shape() const;
// Return true if the bed shape changed, so the calee will update the UI.
bool set_shape(const Pointfs& shape);
const BoundingBoxf3& get_bounding_box() const;
bool contains(const Point& point) const;
Point point_projection(const Point& point) const;
#if ENABLE_PRINT_BED_MODELS
void render(float theta, bool useVBOs, float scale_factor) const;
#else
void render(float theta, float scale_factor) const;
#endif // ENABLE_PRINT_BED_MODELS
private:
void _calc_bounding_box();
void _calc_triangles(const ExPolygon& poly);
void _calc_gridlines(const ExPolygon& poly, const BoundingBox& bed_bbox);
#if ENABLE_REWORKED_BED_SHAPE_CHANGE
EType _detect_type(const Pointfs& shape) const;
#else
EType _detect_type() const;
#endif // ENABLE_REWORKED_BED_SHAPE_CHANGE
#if ENABLE_PRINT_BED_MODELS
void _render_prusa(const std::string &key, float theta, bool useVBOs) const;
#else
void _render_prusa(const std::string &key, float theta) const;
#endif // ENABLE_PRINT_BED_MODELS
void _render_custom() const;
#if !ENABLE_REWORKED_BED_SHAPE_CHANGE
static bool _are_equal(const Pointfs& bed_1, const Pointfs& bed_2);
#endif // !ENABLE_REWORKED_BED_SHAPE_CHANGE
};
struct Axes
{
static const double Radius;
static const double ArrowBaseRadius;
static const double ArrowLength;
Vec3d origin;
Vec3d length;
GLUquadricObj* m_quadric;
Axes();
~Axes();
void render() const;
private:
void render_axis(double length) const;
};
#if !ENABLE_TEXTURES_FROM_SVG
class Shader
{
GLShader* m_shader;
@ -321,6 +217,7 @@ class GLCanvas3D
private:
void _reset();
};
#endif // !ENABLE_TEXTURES_FROM_SVG
class LayersEditing
{
@ -907,8 +804,7 @@ private:
WarningTexture m_warning_texture;
wxTimer m_timer;
Camera m_camera;
Bed m_bed;
Axes m_axes;
Bed3D* m_bed;
LayersEditing m_layers_editing;
Shader m_shader;
Mouse m_mouse;
@ -930,11 +826,7 @@ private:
bool m_dirty;
bool m_initialized;
bool m_use_VBOs;
#if ENABLE_REWORKED_BED_SHAPE_CHANGE
bool m_requires_zoom_to_bed;
#else
bool m_force_zoom_to_bed_enabled;
#endif // ENABLE_REWORKED_BED_SHAPE_CHANGE
bool m_apply_zoom_to_volumes_filter;
mutable int m_hover_volume_id;
bool m_toolbar_action_running;
@ -968,6 +860,8 @@ public:
wxGLCanvas* get_wxglcanvas() { return m_canvas; }
const wxGLCanvas* get_wxglcanvas() const { return m_canvas; }
void set_bed(Bed3D* bed) { m_bed = bed; }
void set_view_toolbar(GLToolbar* toolbar) { m_view_toolbar = toolbar; }
bool init(bool useVBOs, bool use_legacy_opengl);
@ -989,12 +883,7 @@ public:
const Selection& get_selection() const { return m_selection; }
Selection& get_selection() { return m_selection; }
// Set the bed shape to a single closed 2D polygon(array of two element arrays),
// triangulate the bed and store the triangles into m_bed.m_triangles,
// fills the m_bed.m_grid_lines and sets m_bed.m_origin.
// Sets m_bed.m_polygon to limit the object placement.
void set_bed_shape(const Pointfs& shape);
void set_bed_axes_length(double length);
void bed_shape_changed();
void set_clipping_plane(unsigned int id, const ClippingPlane& plane)
{
@ -1024,9 +913,6 @@ public:
void enable_moving(bool enable);
void enable_gizmos(bool enable);
void enable_toolbar(bool enable);
#if !ENABLE_REWORKED_BED_SHAPE_CHANGE
void enable_force_zoom_to_bed(bool enable);
#endif // !ENABLE_REWORKED_BED_SHAPE_CHANGE
void enable_dynamic_background(bool enable);
void allow_multisample(bool allow);
@ -1112,9 +998,6 @@ public:
private:
bool _is_shown_on_screen() const;
#if !ENABLE_REWORKED_BED_SHAPE_CHANGE
void _force_zoom_to_bed();
#endif // !ENABLE_REWORKED_BED_SHAPE_CHANGE
bool _init_toolbar();

View File

@ -253,4 +253,91 @@ sub SetMatrix
}
*/
#if ENABLE_TEXTURES_FROM_SVG
Shader::Shader()
: m_shader(nullptr)
{
}
Shader::~Shader()
{
reset();
}
bool Shader::init(const std::string& vertex_shader_filename, const std::string& fragment_shader_filename)
{
if (is_initialized())
return true;
m_shader = new GLShader();
if (m_shader != nullptr)
{
if (!m_shader->load_from_file(fragment_shader_filename.c_str(), vertex_shader_filename.c_str()))
{
std::cout << "Compilaton of shader failed:" << std::endl;
std::cout << m_shader->last_error << std::endl;
reset();
return false;
}
}
return true;
}
bool Shader::is_initialized() const
{
return (m_shader != nullptr);
}
bool Shader::start_using() const
{
if (is_initialized())
{
m_shader->enable();
return true;
}
else
return false;
}
void Shader::stop_using() const
{
if (m_shader != nullptr)
m_shader->disable();
}
void Shader::set_uniform(const std::string& name, float value) const
{
if (m_shader != nullptr)
m_shader->set_uniform(name.c_str(), value);
}
void Shader::set_uniform(const std::string& name, const float* matrix) const
{
if (m_shader != nullptr)
m_shader->set_uniform(name.c_str(), matrix);
}
void Shader::set_uniform(const std::string& name, bool value) const
{
if (m_shader != nullptr)
m_shader->set_uniform(name.c_str(), value);
}
unsigned int Shader::get_shader_program_id() const
{
return (m_shader != nullptr) ? m_shader->shader_program_id : 0;
}
void Shader::reset()
{
if (m_shader != nullptr)
{
m_shader->release();
delete m_shader;
m_shader = nullptr;
}
}
#endif // ENABLE_TEXTURES_FROM_SVG
} // namespace Slic3r

View File

@ -36,6 +36,34 @@ public:
std::string last_error;
};
#if ENABLE_TEXTURES_FROM_SVG
class Shader
{
GLShader* m_shader;
public:
Shader();
~Shader();
bool init(const std::string& vertex_shader_filename, const std::string& fragment_shader_filename);
bool is_initialized() const;
bool start_using() const;
void stop_using() const;
void set_uniform(const std::string& name, float value) const;
void set_uniform(const std::string& name, const float* matrix) const;
void set_uniform(const std::string& name, bool value) const;
const GLShader* get_shader() const { return m_shader; }
unsigned int get_shader_program_id() const;
private:
void reset();
};
#endif // ENABLE_TEXTURES_FROM_SVG
}
#endif /* slic3r_GLShader_hpp_ */

View File

@ -1,3 +1,4 @@
#include "libslic3r/libslic3r.h"
#include "GLTexture.hpp"
#include <GL/glew.h>
@ -5,10 +6,20 @@
#include <wx/image.h>
#include <boost/filesystem.hpp>
#if ENABLE_TEXTURES_FROM_SVG
#include <boost/algorithm/string/predicate.hpp>
#endif // ENABLE_TEXTURES_FROM_SVG
#include <vector>
#include <algorithm>
#if ENABLE_TEXTURES_FROM_SVG
#define NANOSVG_IMPLEMENTATION
#include "nanosvg/nanosvg.h"
#define NANOSVGRAST_IMPLEMENTATION
#include "nanosvg/nanosvgrast.h"
#endif // ENABLE_TEXTURES_FROM_SVG
namespace Slic3r {
namespace GUI {
@ -27,7 +38,34 @@ GLTexture::~GLTexture()
reset();
}
bool GLTexture::load_from_file(const std::string& filename, bool generate_mipmaps)
#if ENABLE_TEXTURES_FROM_SVG
bool GLTexture::load_from_file(const std::string& filename, bool use_mipmaps)
{
reset();
if (!boost::filesystem::exists(filename))
return false;
if (boost::algorithm::iends_with(filename, ".png"))
return load_from_png(filename, use_mipmaps);
else
return false;
}
bool GLTexture::load_from_svg_file(const std::string& filename, bool use_mipmaps, unsigned int max_size_px)
{
reset();
if (!boost::filesystem::exists(filename))
return false;
if (boost::algorithm::iends_with(filename, ".svg"))
return load_from_svg(filename, use_mipmaps, max_size_px);
else
return false;
}
#else
bool GLTexture::load_from_file(const std::string& filename, bool use_mipmaps)
{
reset();
@ -78,10 +116,10 @@ bool GLTexture::load_from_file(const std::string& filename, bool generate_mipmap
::glGenTextures(1, &m_id);
::glBindTexture(GL_TEXTURE_2D, m_id);
::glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (GLsizei)m_width, (GLsizei)m_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, (const void*)data.data());
if (generate_mipmaps)
if (use_mipmaps)
{
// we manually generate mipmaps because glGenerateMipmap() function is not reliable on all graphics cards
unsigned int levels_count = _generate_mipmaps(image);
unsigned int levels_count = generate_mipmaps(image);
::glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 1 + levels_count);
::glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
}
@ -97,6 +135,7 @@ bool GLTexture::load_from_file(const std::string& filename, bool generate_mipmap
m_source = filename;
return true;
}
#endif // ENABLE_TEXTURES_FROM_SVG
void GLTexture::reset()
{
@ -109,26 +148,6 @@ void GLTexture::reset()
m_source = "";
}
unsigned int GLTexture::get_id() const
{
return m_id;
}
int GLTexture::get_width() const
{
return m_width;
}
int GLTexture::get_height() const
{
return m_height;
}
const std::string& GLTexture::get_source() const
{
return m_source;
}
void GLTexture::render_texture(unsigned int tex_id, float left, float right, float bottom, float top)
{
render_sub_texture(tex_id, left, right, bottom, top, FullTextureUVs);
@ -157,7 +176,7 @@ void GLTexture::render_sub_texture(unsigned int tex_id, float left, float right,
::glDisable(GL_BLEND);
}
unsigned int GLTexture::_generate_mipmaps(wxImage& image)
unsigned int GLTexture::generate_mipmaps(wxImage& image)
{
int w = image.GetWidth();
int h = image.GetHeight();
@ -195,5 +214,152 @@ unsigned int GLTexture::_generate_mipmaps(wxImage& image)
return (unsigned int)level;
}
#if ENABLE_TEXTURES_FROM_SVG
bool GLTexture::load_from_png(const std::string& filename, bool use_mipmaps)
{
// Load a PNG with an alpha channel.
wxImage image;
if (!image.LoadFile(wxString::FromUTF8(filename.c_str()), wxBITMAP_TYPE_PNG))
{
reset();
return false;
}
m_width = image.GetWidth();
m_height = image.GetHeight();
int n_pixels = m_width * m_height;
if (n_pixels <= 0)
{
reset();
return false;
}
// Get RGB & alpha raw data from wxImage, pack them into an array.
unsigned char* img_rgb = image.GetData();
if (img_rgb == nullptr)
{
reset();
return false;
}
unsigned char* img_alpha = image.GetAlpha();
std::vector<unsigned char> data(n_pixels * 4, 0);
for (int i = 0; i < n_pixels; ++i)
{
int data_id = i * 4;
int img_id = i * 3;
data[data_id + 0] = img_rgb[img_id + 0];
data[data_id + 1] = img_rgb[img_id + 1];
data[data_id + 2] = img_rgb[img_id + 2];
data[data_id + 3] = (img_alpha != nullptr) ? img_alpha[i] : 255;
}
// sends data to gpu
::glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
::glGenTextures(1, &m_id);
::glBindTexture(GL_TEXTURE_2D, m_id);
::glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (GLsizei)m_width, (GLsizei)m_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, (const void*)data.data());
if (use_mipmaps)
{
// we manually generate mipmaps because glGenerateMipmap() function is not reliable on all graphics cards
unsigned int levels_count = generate_mipmaps(image);
::glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 1 + levels_count);
::glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
}
else
{
::glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
::glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 1);
}
::glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
::glBindTexture(GL_TEXTURE_2D, 0);
m_source = filename;
return true;
}
bool GLTexture::load_from_svg(const std::string& filename, bool use_mipmaps, unsigned int max_size_px)
{
NSVGimage* image = nsvgParseFromFile(filename.c_str(), "px", 96.0f);
if (image == nullptr)
{
// printf("Could not open SVG image.\n");
reset();
return false;
}
float scale = (float)max_size_px / std::max(image->width, image->height);
m_width = (int)(scale * image->width);
m_height = (int)(scale * image->height);
int n_pixels = m_width * m_height;
if (n_pixels <= 0)
{
reset();
return false;
}
NSVGrasterizer* rast = nsvgCreateRasterizer();
if (rast == nullptr)
{
// printf("Could not init rasterizer.\n");
nsvgDelete(image);
reset();
return false;
}
// creates the temporary buffer only once, with max size, and reuse it for all the levels, if generating mipmaps
std::vector<unsigned char> data(n_pixels * 4, 0);
nsvgRasterize(rast, image, 0, 0, scale, data.data(), m_width, m_height, m_width * 4);
// sends data to gpu
::glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
::glGenTextures(1, &m_id);
::glBindTexture(GL_TEXTURE_2D, m_id);
::glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (GLsizei)m_width, (GLsizei)m_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, (const void*)data.data());
if (use_mipmaps)
{
// we manually generate mipmaps because glGenerateMipmap() function is not reliable on all graphics cards
int lod_w = m_width;
int lod_h = m_height;
GLint level = 0;
while ((lod_w > 1) || (lod_h > 1))
{
++level;
lod_w = std::max(lod_w / 2, 1);
lod_h = std::max(lod_h / 2, 1);
scale /= 2.0f;
nsvgRasterize(rast, image, 0, 0, scale, data.data(), lod_w, lod_h, lod_w * 4);
::glTexImage2D(GL_TEXTURE_2D, level, GL_RGBA, (GLsizei)lod_w, (GLsizei)lod_h, 0, GL_RGBA, GL_UNSIGNED_BYTE, (const void*)data.data());
}
::glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 1 + level);
::glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
}
else
{
::glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
::glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 1);
}
::glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
::glBindTexture(GL_TEXTURE_2D, 0);
m_source = filename;
nsvgDeleteRasterizer(rast);
nsvgDelete(image);
return true;
}
#endif // ENABLE_TEXTURES_FROM_SVG
} // namespace GUI
} // namespace Slic3r

View File

@ -37,20 +37,28 @@ namespace GUI {
GLTexture();
virtual ~GLTexture();
bool load_from_file(const std::string& filename, bool generate_mipmaps);
bool load_from_file(const std::string& filename, bool use_mipmaps);
#if ENABLE_TEXTURES_FROM_SVG
bool load_from_svg_file(const std::string& filename, bool use_mipmaps, unsigned int max_size_px);
#endif // ENABLE_TEXTURES_FROM_SVG
void reset();
unsigned int get_id() const;
int get_width() const;
int get_height() const;
unsigned int get_id() const { return m_id; }
int get_width() const { return m_width; }
int get_height() const { return m_height; }
const std::string& get_source() const;
const std::string& get_source() const { return m_source; }
static void render_texture(unsigned int tex_id, float left, float right, float bottom, float top);
static void render_sub_texture(unsigned int tex_id, float left, float right, float bottom, float top, const Quad_UVs& uvs);
protected:
unsigned int _generate_mipmaps(wxImage& image);
unsigned int generate_mipmaps(wxImage& image);
#if ENABLE_TEXTURES_FROM_SVG
private:
bool load_from_png(const std::string& filename, bool use_mipmaps);
bool load_from_svg(const std::string& filename, bool use_mipmaps, unsigned int max_size_px);
#endif // ENABLE_TEXTURES_FROM_SVG
};
} // namespace GUI

View File

@ -69,9 +69,6 @@ bool View3D::init(wxWindow* parent, Model* model, DynamicPrintConfig* config, Ba
m_canvas->set_config(config);
m_canvas->enable_gizmos(true);
m_canvas->enable_toolbar(true);
#if !ENABLE_REWORKED_BED_SHAPE_CHANGE
m_canvas->enable_force_zoom_to_bed(true);
#endif // !ENABLE_REWORKED_BED_SHAPE_CHANGE
#if !ENABLE_IMGUI
m_gizmo_widget = new wxPanel(this, wxID_ANY, wxDefaultPosition, wxDefaultSize);
@ -92,6 +89,12 @@ bool View3D::init(wxWindow* parent, Model* model, DynamicPrintConfig* config, Ba
return true;
}
void View3D::set_bed(Bed3D* bed)
{
if (m_canvas != nullptr)
m_canvas->set_bed(bed);
}
void View3D::set_view_toolbar(GLToolbar* toolbar)
{
if (m_canvas != nullptr)
@ -104,15 +107,10 @@ void View3D::set_as_dirty()
m_canvas->set_as_dirty();
}
void View3D::set_bed_shape(const Pointfs& shape)
void View3D::bed_shape_changed()
{
if (m_canvas != nullptr)
{
m_canvas->set_bed_shape(shape);
#if !ENABLE_REWORKED_BED_SHAPE_CHANGE
m_canvas->zoom_to_bed();
#endif // !ENABLE_REWORKED_BED_SHAPE_CHANGE
}
m_canvas->bed_shape_changed();
}
void View3D::select_view(const std::string& direction)
@ -345,6 +343,12 @@ Preview::~Preview()
}
}
void Preview::set_bed(Bed3D* bed)
{
if (m_canvas != nullptr)
m_canvas->set_bed(bed);
}
void Preview::set_view_toolbar(GLToolbar* toolbar)
{
if (m_canvas != nullptr)
@ -376,9 +380,10 @@ void Preview::set_enabled(bool enabled)
m_enabled = enabled;
}
void Preview::set_bed_shape(const Pointfs& shape)
void Preview::bed_shape_changed()
{
m_canvas->set_bed_shape(shape);
if (m_canvas != nullptr)
m_canvas->bed_shape_changed();
}
void Preview::select_view(const std::string& direction)

View File

@ -27,6 +27,7 @@ namespace GUI {
class GLCanvas3D;
class GLToolbar;
class Bed3D;
class View3D : public wxPanel
{
@ -48,10 +49,11 @@ public:
wxGLCanvas* get_wxglcanvas() { return m_canvas_widget; }
GLCanvas3D* get_canvas3d() { return m_canvas; }
void set_bed(Bed3D* bed);
void set_view_toolbar(GLToolbar* toolbar);
void set_as_dirty();
void set_bed_shape(const Pointfs& shape);
void bed_shape_changed();
void select_view(const std::string& direction);
void select_all();
@ -114,12 +116,13 @@ public:
wxGLCanvas* get_wxglcanvas() { return m_canvas_widget; }
GLCanvas3D* get_canvas3d() { return m_canvas; }
void set_bed(Bed3D* bed);
void set_view_toolbar(GLToolbar* toolbar);
void set_number_extruders(unsigned int number_extruders);
void set_canvas_as_dirty();
void set_enabled(bool enabled);
void set_bed_shape(const Pointfs& shape);
void bed_shape_changed();
void select_view(const std::string& direction);
void set_viewport_from_scene(GLCanvas3D* canvas);
void set_viewport_into_scene(GLCanvas3D* canvas);

View File

@ -49,6 +49,7 @@
#include "GLCanvas3D.hpp"
#include "GLToolbar.hpp"
#include "GUI_Preview.hpp"
#include "3DBed.hpp"
#include "Tab.hpp"
#include "PresetBundle.hpp"
#include "BackgroundSlicingProcess.hpp"
@ -1016,6 +1017,7 @@ struct Plater::priv
wxPanel* current_panel;
std::vector<wxPanel*> panels;
Sidebar *sidebar;
Bed3D bed;
View3D* view3D;
GLToolbar view_toolbar;
Preview *preview;
@ -1120,6 +1122,12 @@ struct Plater::priv
void update_object_menu();
// Set the bed shape to a single closed 2D polygon(array of two element arrays),
// triangulate the bed and store the triangles into m_bed.m_triangles,
// fills the m_bed.m_grid_lines and sets m_bed.m_origin.
// Sets m_bed.m_polygon to limit the object placement.
void set_bed_shape(const Pointfs& shape);
private:
bool init_object_menu();
bool init_common_menu(wxMenu* menu, const bool is_part = false);
@ -1192,18 +1200,15 @@ Plater::priv::priv(Plater *q, MainFrame *main_frame)
view3D = new View3D(q, &model, config, &background_process);
preview = new Preview(q, config, &background_process, &gcode_preview_data, [this](){ schedule_background_process(); });
view3D->set_bed(&bed);
preview->set_bed(&bed);
panels.push_back(view3D);
panels.push_back(preview);
this->background_process_timer.SetOwner(this->q, 0);
this->q->Bind(wxEVT_TIMER, [this](wxTimerEvent &evt) { this->update_restart_background_process(false, false); });
#if !ENABLE_REWORKED_BED_SHAPE_CHANGE
auto *bed_shape = config->opt<ConfigOptionPoints>("bed_shape");
view3D->set_bed_shape(bed_shape->values);
preview->set_bed_shape(bed_shape->values);
#endif // !ENABLE_REWORKED_BED_SHAPE_CHANGE
update();
auto *hsizer = new wxBoxSizer(wxHORIZONTAL);
@ -1255,10 +1260,12 @@ Plater::priv::priv(Plater *q, MainFrame *main_frame)
view3D_canvas->Bind(EVT_GLTOOLBAR_SPLIT_VOLUMES, &priv::on_action_split_volumes, this);
view3D_canvas->Bind(EVT_GLTOOLBAR_LAYERSEDITING, &priv::on_action_layersediting, this);
view3D_canvas->Bind(EVT_GLCANVAS_INIT, [this](SimpleEvent&) { init_view_toolbar(); });
view3D_canvas->Bind(EVT_GLCANVAS_UPDATE_BED_SHAPE, [this](SimpleEvent&) { set_bed_shape(config->option<ConfigOptionPoints>("bed_shape")->values); });
// Preview events:
preview->get_wxglcanvas()->Bind(EVT_GLCANVAS_VIEWPORT_CHANGED, &priv::on_viewport_changed, this);
preview->get_wxglcanvas()->Bind(EVT_GLCANVAS_QUESTION_MARK, [this](SimpleEvent&) { wxGetApp().keyboard_shortcuts(); });
preview->get_wxglcanvas()->Bind(EVT_GLCANVAS_UPDATE_BED_SHAPE, [this](SimpleEvent&) { set_bed_shape(config->option<ConfigOptionPoints>("bed_shape")->values); });
preview->get_wxglcanvas()->Bind(EVT_GLCANVAS_TAB, [this](SimpleEvent&) { select_next_view_3D(); });
view3D_canvas->Bind(EVT_GLCANVAS_INIT, [this](SimpleEvent&) { init_view_toolbar(); });
@ -2718,6 +2725,16 @@ bool Plater::priv::can_mirror() const
return get_selection().is_from_single_instance();
}
void Plater::priv::set_bed_shape(const Pointfs& shape)
{
bool new_shape = bed.set_shape(shape);
if (new_shape)
{
if (view3D) view3D->bed_shape_changed();
if (preview) preview->bed_shape_changed();
}
}
void Plater::priv::update_object_menu()
{
sidebar->obj_list()->append_menu_items_add_volume(&object_menu);
@ -3139,20 +3156,13 @@ void Plater::on_extruders_change(int num_extruders)
void Plater::on_config_change(const DynamicPrintConfig &config)
{
bool update_scheduled = false;
#if ENABLE_REWORKED_BED_SHAPE_CHANGE
bool bed_shape_changed = false;
#endif // ENABLE_REWORKED_BED_SHAPE_CHANGE
for (auto opt_key : p->config->diff(config)) {
p->config->set_key_value(opt_key, config.option(opt_key)->clone());
if (opt_key == "printer_technology")
this->set_printer_technology(config.opt_enum<PrinterTechnology>(opt_key));
else if (opt_key == "bed_shape") {
#if ENABLE_REWORKED_BED_SHAPE_CHANGE
bed_shape_changed = true;
#else
if (p->view3D) p->view3D->set_bed_shape(p->config->option<ConfigOptionPoints>(opt_key)->values);
if (p->preview) p->preview->set_bed_shape(p->config->option<ConfigOptionPoints>(opt_key)->values);
#endif // ENABLE_REWORKED_BED_SHAPE_CHANGE
update_scheduled = true;
}
else if (boost::starts_with(opt_key, "wipe_tower") ||
@ -3178,12 +3188,7 @@ void Plater::on_config_change(const DynamicPrintConfig &config)
}
else if (opt_key == "printer_model") {
// update to force bed selection(for texturing)
#if ENABLE_REWORKED_BED_SHAPE_CHANGE
bed_shape_changed = true;
#else
if (p->view3D) p->view3D->set_bed_shape(p->config->option<ConfigOptionPoints>("bed_shape")->values);
if (p->preview) p->preview->set_bed_shape(p->config->option<ConfigOptionPoints>("bed_shape")->values);
#endif // ENABLE_REWORKED_BED_SHAPE_CHANGE
update_scheduled = true;
}
else if (opt_key == "host_type" && this->p->printer_technology == ptSLA) {
@ -3196,13 +3201,8 @@ void Plater::on_config_change(const DynamicPrintConfig &config)
p->sidebar->show_send(prin_host_opt != nullptr && !prin_host_opt->value.empty());
}
#if ENABLE_REWORKED_BED_SHAPE_CHANGE
if (bed_shape_changed)
{
if (p->view3D) p->view3D->set_bed_shape(p->config->option<ConfigOptionPoints>("bed_shape")->values);
if (p->preview) p->preview->set_bed_shape(p->config->option<ConfigOptionPoints>("bed_shape")->values);
}
#endif // ENABLE_REWORKED_BED_SHAPE_CHANGE
p->set_bed_shape(p->config->option<ConfigOptionPoints>("bed_shape")->values);
if (update_scheduled)
update();

View File

@ -10,6 +10,9 @@
#include "Preset.hpp"
#include "3DScene.hpp"
#include "GLTexture.hpp"
class wxButton;
class wxBoxSizer;
class wxGLCanvas;