From dd5425be72e000ba23538e7d565bea0336efc162 Mon Sep 17 00:00:00 2001 From: Enrico Turri Date: Wed, 27 Feb 2019 10:46:52 +0100 Subject: [PATCH 1/6] Attempt to fix rendering of printbed svg textures on Mac --- src/slic3r/GUI/3DBed.cpp | 10 ++++++++-- src/slic3r/GUI/GLTexture.cpp | 23 +++++++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/slic3r/GUI/3DBed.cpp b/src/slic3r/GUI/3DBed.cpp index d6d25b550bf..b67bf9eafa5 100644 --- a/src/slic3r/GUI/3DBed.cpp +++ b/src/slic3r/GUI/3DBed.cpp @@ -556,8 +556,10 @@ void Bed3D::render_prusa(const std::string &key, bool bottom) const ::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()); +//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +// ::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); } @@ -597,6 +599,10 @@ void Bed3D::render_prusa_shader(unsigned int vertices_count, bool transparent) c ::glBindTexture(GL_TEXTURE_2D, (GLuint)m_texture.get_id()); ::glBindBuffer(GL_ARRAY_BUFFER, m_vbo_id); +//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ + ::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()); +//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ::glEnableVertexAttribArray(0); ::glEnableVertexAttribArray(1); ::glDrawArrays(GL_TRIANGLES, 0, (GLsizei)vertices_count); diff --git a/src/slic3r/GUI/GLTexture.cpp b/src/slic3r/GUI/GLTexture.cpp index e7e20b27e77..436a37e1cc1 100644 --- a/src/slic3r/GUI/GLTexture.cpp +++ b/src/slic3r/GUI/GLTexture.cpp @@ -20,6 +20,8 @@ #include "nanosvg/nanosvgrast.h" #endif // ENABLE_TEXTURES_FROM_SVG +#include "libslic3r/Utils.hpp" + namespace Slic3r { namespace GUI { @@ -357,6 +359,27 @@ bool GLTexture::load_from_svg(const std::string& filename, bool use_mipmaps, uns nsvgDeleteRasterizer(rast); nsvgDelete(image); +//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +#if 1 + // debug output + wxImage output(m_width, m_height); + output.InitAlpha(); + + for (int h = 0; h < m_height; ++h) + { + int px_h = h * m_width; + for (int w = 0; w < m_width; ++w) + { + int offset = (px_h + w) * 4; + output.SetRGB(w, h, data.data()[offset + 0], data.data()[offset + 1], data.data()[offset + 2]); + output.SetAlpha(w, h, data.data()[offset + 3]); + } + } + + output.SaveFile(resources_dir() + "/icons/test_svg_import.png", wxBITMAP_TYPE_PNG); +#endif // 0 +//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ + return true; } #endif // ENABLE_TEXTURES_FROM_SVG From 49a5c0465b5b59627e9816097d9c2d942ffc69b5 Mon Sep 17 00:00:00 2001 From: Enrico Turri Date: Wed, 27 Feb 2019 15:19:03 +0100 Subject: [PATCH 2/6] Small refactoring in Bed3D and GLShader --- src/slic3r/GUI/3DBed.cpp | 79 +++++++++++++++++------------------- src/slic3r/GUI/3DBed.hpp | 2 +- src/slic3r/GUI/GLShader.cpp | 13 +++++- src/slic3r/GUI/GLShader.hpp | 1 + src/slic3r/GUI/GLTexture.cpp | 21 ---------- 5 files changed, 51 insertions(+), 65 deletions(-) diff --git a/src/slic3r/GUI/3DBed.cpp b/src/slic3r/GUI/3DBed.cpp index b67bf9eafa5..f271f9738e1 100644 --- a/src/slic3r/GUI/3DBed.cpp +++ b/src/slic3r/GUI/3DBed.cpp @@ -516,9 +516,9 @@ void Bed3D::render_prusa(const std::string &key, bool bottom) const 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); + glsafe(::glBindTexture(GL_TEXTURE_2D, m_texture.get_id())); + glsafe(::glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, max_anisotropy)); + glsafe(::glBindTexture(GL_TEXTURE_2D, 0)); } } @@ -542,9 +542,9 @@ void Bed3D::render_prusa(const std::string &key, bool bottom) const if (!m_model.get_filename().empty()) { - ::glEnable(GL_LIGHTING); + glsafe(::glEnable(GL_LIGHTING)); m_model.render(); - ::glDisable(GL_LIGHTING); + glsafe(::glDisable(GL_LIGHTING)); } } @@ -553,41 +553,40 @@ void Bed3D::render_prusa(const std::string &key, bool bottom) const { 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); + unsigned int stride = m_triangles.get_vertex_data_size(); + glsafe(::glGenBuffers(1, &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(::glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, stride, (GLvoid*)m_triangles.get_position_offset())); + glsafe(::glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, stride, (GLvoid*)m_triangles.get_tex_coords_offset())); + glsafe(::glBindBuffer(GL_ARRAY_BUFFER, 0)); } - ::glEnable(GL_DEPTH_TEST); - ::glDepthMask(GL_FALSE); + glsafe(::glEnable(GL_DEPTH_TEST)); + glsafe(::glDepthMask(GL_FALSE)); - ::glEnable(GL_BLEND); - ::glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + glsafe(::glEnable(GL_BLEND)); + glsafe(::glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)); - ::glEnable(GL_TEXTURE_2D); - ::glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); +// glsafe(::glEnable(GL_TEXTURE_2D)); +// glsafe(::glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE)); if (bottom) - ::glFrontFace(GL_CW); + glsafe(::glFrontFace(GL_CW)); - render_prusa_shader(triangles_vcount, bottom); + render_prusa_shader(bottom); if (bottom) - ::glFrontFace(GL_CCW); + glsafe(::glFrontFace(GL_CCW)); - ::glDisable(GL_TEXTURE_2D); +// glsafe(::glDisable(GL_TEXTURE_2D)); - ::glDisable(GL_BLEND); - ::glDepthMask(GL_TRUE); + glsafe(::glDisable(GL_BLEND)); + glsafe(::glDepthMask(GL_TRUE)); } } -void Bed3D::render_prusa_shader(unsigned int vertices_count, bool transparent) const +void Bed3D::render_prusa_shader(bool transparent) const { if (m_shader.get_shader_program_id() == 0) m_shader.init("printbed.vs", "printbed.fs"); @@ -597,19 +596,15 @@ void Bed3D::render_prusa_shader(unsigned int vertices_count, bool transparent) c 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); -//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ - ::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()); -//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ - ::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); + glsafe(::glBindTexture(GL_TEXTURE_2D, (GLuint)m_texture.get_id())); + glsafe(::glBindBuffer(GL_ARRAY_BUFFER, m_vbo_id)); + glsafe(::glEnableVertexAttribArray(0)); + glsafe(::glEnableVertexAttribArray(1)); + glsafe(::glDrawArrays(GL_TRIANGLES, 0, (GLsizei)m_triangles.get_vertices_count())); + glsafe(::glDisableVertexAttribArray(1)); + glsafe(::glDisableVertexAttribArray(0)); + glsafe(::glBindBuffer(GL_ARRAY_BUFFER, 0)); + glsafe(::glBindTexture(GL_TEXTURE_2D, 0)); m_shader.stop_using(); } @@ -760,7 +755,7 @@ void Bed3D::render_custom() const 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()); + glsafe(::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 @@ -774,7 +769,7 @@ void Bed3D::render_custom() const 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()); + glsafe(::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 @@ -792,7 +787,7 @@ void Bed3D::reset() { if (m_vbo_id > 0) { - ::glDeleteBuffers(1, &m_vbo_id); + glsafe(::glDeleteBuffers(1, &m_vbo_id)); m_vbo_id = 0; } } diff --git a/src/slic3r/GUI/3DBed.hpp b/src/slic3r/GUI/3DBed.hpp index edf3e5ee763..e60cdf94e1b 100644 --- a/src/slic3r/GUI/3DBed.hpp +++ b/src/slic3r/GUI/3DBed.hpp @@ -131,7 +131,7 @@ private: 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; + void render_prusa_shader(bool transparent) const; #else void render_prusa(const std::string &key, float theta, bool useVBOs) const; #endif // ENABLE_TEXTURES_FROM_SVG diff --git a/src/slic3r/GUI/GLShader.cpp b/src/slic3r/GUI/GLShader.cpp index 24d8d41b2d6..3d3148d02b1 100644 --- a/src/slic3r/GUI/GLShader.cpp +++ b/src/slic3r/GUI/GLShader.cpp @@ -225,6 +225,17 @@ bool GLShader::set_uniform(const char* name, const float* matrix) const return false; } +bool GLShader::set_uniform(const char* name, int value) const +{ + int id = get_uniform_location(name); + if (id >= 0) + { + ::glUniform1i(id, value); + return true; + } + return false; +} + /* # Set shader vector sub SetVector @@ -321,7 +332,7 @@ void Shader::set_uniform(const std::string& name, const float* matrix) const void Shader::set_uniform(const std::string& name, bool value) const { if (m_shader != nullptr) - m_shader->set_uniform(name.c_str(), value); + m_shader->set_uniform(name.c_str(), value ? 1 : 0); } unsigned int Shader::get_shader_program_id() const diff --git a/src/slic3r/GUI/GLShader.hpp b/src/slic3r/GUI/GLShader.hpp index 2f88d039362..f7e8c7716fd 100644 --- a/src/slic3r/GUI/GLShader.hpp +++ b/src/slic3r/GUI/GLShader.hpp @@ -26,6 +26,7 @@ public: bool set_uniform(const char *name, float value) const; bool set_uniform(const char* name, const float* matrix) const; + bool set_uniform(const char* name, int value) const; void enable() const; void disable() const; diff --git a/src/slic3r/GUI/GLTexture.cpp b/src/slic3r/GUI/GLTexture.cpp index 436a37e1cc1..19eff88e059 100644 --- a/src/slic3r/GUI/GLTexture.cpp +++ b/src/slic3r/GUI/GLTexture.cpp @@ -359,27 +359,6 @@ bool GLTexture::load_from_svg(const std::string& filename, bool use_mipmaps, uns nsvgDeleteRasterizer(rast); nsvgDelete(image); -//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ -#if 1 - // debug output - wxImage output(m_width, m_height); - output.InitAlpha(); - - for (int h = 0; h < m_height; ++h) - { - int px_h = h * m_width; - for (int w = 0; w < m_width; ++w) - { - int offset = (px_h + w) * 4; - output.SetRGB(w, h, data.data()[offset + 0], data.data()[offset + 1], data.data()[offset + 2]); - output.SetAlpha(w, h, data.data()[offset + 3]); - } - } - - output.SaveFile(resources_dir() + "/icons/test_svg_import.png", wxBITMAP_TYPE_PNG); -#endif // 0 -//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ - return true; } #endif // ENABLE_TEXTURES_FROM_SVG From 7f664a8ef34247369909a3a46c5edcc2b25dab7d Mon Sep 17 00:00:00 2001 From: Enrico Turri Date: Thu, 28 Feb 2019 09:04:17 +0100 Subject: [PATCH 3/6] 2nd attempt to fix rendering of printbed svg textures on Mac --- resources/shaders/printbed.vs | 10 +++++-- src/slic3r/GUI/3DBed.cpp | 53 ++++++++++++++++++++++++++++++----- src/slic3r/GUI/GLShader.cpp | 12 ++++++++ src/slic3r/GUI/GLShader.hpp | 5 ++++ 4 files changed, 71 insertions(+), 9 deletions(-) diff --git a/resources/shaders/printbed.vs b/resources/shaders/printbed.vs index 968bcce16a3..7d61e03dd5f 100644 --- a/resources/shaders/printbed.vs +++ b/resources/shaders/printbed.vs @@ -1,11 +1,17 @@ #version 110 +//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +attribute vec4 v_position; +//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ attribute vec2 v_tex_coords; varying vec2 tex_coords; void main() { - gl_Position = ftransform(); +//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ + gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * v_position; +// gl_Position = ftransform(); +//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ tex_coords = v_tex_coords; -} \ No newline at end of file +} diff --git a/src/slic3r/GUI/3DBed.cpp b/src/slic3r/GUI/3DBed.cpp index f271f9738e1..8b71f7a7800 100644 --- a/src/slic3r/GUI/3DBed.cpp +++ b/src/slic3r/GUI/3DBed.cpp @@ -553,12 +553,16 @@ void Bed3D::render_prusa(const std::string &key, bool bottom) const { if (m_vbo_id == 0) { - unsigned int stride = m_triangles.get_vertex_data_size(); +//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +// unsigned int stride = m_triangles.get_vertex_data_size(); +//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ glsafe(::glGenBuffers(1, &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(::glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, stride, (GLvoid*)m_triangles.get_position_offset())); - glsafe(::glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, stride, (GLvoid*)m_triangles.get_tex_coords_offset())); +//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +// glsafe(::glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, stride, (GLvoid*)m_triangles.get_position_offset())); +// glsafe(::glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, stride, (GLvoid*)m_triangles.get_tex_coords_offset())); +//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ glsafe(::glBindBuffer(GL_ARRAY_BUFFER, 0)); } @@ -596,13 +600,48 @@ void Bed3D::render_prusa_shader(bool transparent) const m_shader.start_using(); m_shader.set_uniform("transparent_background", transparent); +//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ + unsigned int stride = m_triangles.get_vertex_data_size(); + + GLint position_id = m_shader.get_attrib_location("v_position"); + GLint tex_coords_id = m_shader.get_attrib_location("v_tex_coords"); + + std::cout << "position_id: " << position_id << std::endl; + std::cout << "tex_coords_id: " << tex_coords_id << std::endl; + +// if (tex_coords_id != -1) +// glsafe(::glBindAttribLocation(m_shader.get_shader_program_id(), tex_coords_id, "v_tex_coords")); +//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ + glsafe(::glBindTexture(GL_TEXTURE_2D, (GLuint)m_texture.get_id())); glsafe(::glBindBuffer(GL_ARRAY_BUFFER, m_vbo_id)); - glsafe(::glEnableVertexAttribArray(0)); - glsafe(::glEnableVertexAttribArray(1)); +//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ + if (position_id != -1) + { + glsafe(::glEnableVertexAttribArray(position_id)); + glsafe(::glVertexAttribPointer(position_id, 3, GL_FLOAT, GL_FALSE, stride, (GLvoid*)m_triangles.get_position_offset())); + } + if (tex_coords_id != -1) + { + glsafe(::glEnableVertexAttribArray(tex_coords_id)); + glsafe(::glVertexAttribPointer(tex_coords_id, 2, GL_FLOAT, GL_FALSE, stride, (GLvoid*)m_triangles.get_tex_coords_offset())); + } + +// glsafe(::glEnableVertexAttribArray(0)); +// glsafe(::glEnableVertexAttribArray(1)); +// glsafe(::glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, stride, (GLvoid*)m_triangles.get_position_offset())); +// glsafe(::glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, stride, (GLvoid*)m_triangles.get_tex_coords_offset())); +//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ glsafe(::glDrawArrays(GL_TRIANGLES, 0, (GLsizei)m_triangles.get_vertices_count())); - glsafe(::glDisableVertexAttribArray(1)); - glsafe(::glDisableVertexAttribArray(0)); +//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ + if (tex_coords_id != -1) + glsafe(::glDisableVertexAttribArray(tex_coords_id)); + if (position_id != -1) + glsafe(::glDisableVertexAttribArray(position_id)); + +// glsafe(::glDisableVertexAttribArray(1)); +// glsafe(::glDisableVertexAttribArray(0)); +//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ glsafe(::glBindBuffer(GL_ARRAY_BUFFER, 0)); glsafe(::glBindTexture(GL_TEXTURE_2D, 0)); diff --git a/src/slic3r/GUI/GLShader.cpp b/src/slic3r/GUI/GLShader.cpp index 3d3148d02b1..ff6235296b2 100644 --- a/src/slic3r/GUI/GLShader.cpp +++ b/src/slic3r/GUI/GLShader.cpp @@ -317,6 +317,18 @@ void Shader::stop_using() const m_shader->disable(); } +//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +int Shader::get_attrib_location(const std::string& name) const +{ + return (m_shader != nullptr) ? m_shader->get_attrib_location(name.c_str()) : -1; +} + +int Shader::get_uniform_location(const std::string& name) const +{ + return (m_shader != nullptr) ? m_shader->get_uniform_location(name.c_str()) : -1; +} +//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ + void Shader::set_uniform(const std::string& name, float value) const { if (m_shader != nullptr) diff --git a/src/slic3r/GUI/GLShader.hpp b/src/slic3r/GUI/GLShader.hpp index f7e8c7716fd..db5fe283272 100644 --- a/src/slic3r/GUI/GLShader.hpp +++ b/src/slic3r/GUI/GLShader.hpp @@ -53,6 +53,11 @@ public: bool start_using() const; void stop_using() const; +//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ + int get_attrib_location(const std::string& name) const; + int get_uniform_location(const std::string& name) 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; From e255f0239aafd0ce0aced4205fdf7fe67c6f2400 Mon Sep 17 00:00:00 2001 From: Enrico Turri Date: Thu, 28 Feb 2019 09:37:55 +0100 Subject: [PATCH 4/6] Code cleanup --- resources/shaders/printbed.vs | 5 ----- src/slic3r/GUI/3DBed.cpp | 33 +++------------------------------ src/slic3r/GUI/GLShader.cpp | 2 -- src/slic3r/GUI/GLShader.hpp | 2 -- 4 files changed, 3 insertions(+), 39 deletions(-) diff --git a/resources/shaders/printbed.vs b/resources/shaders/printbed.vs index 7d61e03dd5f..ac47637820b 100644 --- a/resources/shaders/printbed.vs +++ b/resources/shaders/printbed.vs @@ -1,17 +1,12 @@ #version 110 -//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ attribute vec4 v_position; -//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ attribute vec2 v_tex_coords; varying vec2 tex_coords; void main() { -//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * v_position; -// gl_Position = ftransform(); -//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ tex_coords = v_tex_coords; } diff --git a/src/slic3r/GUI/3DBed.cpp b/src/slic3r/GUI/3DBed.cpp index 8b71f7a7800..2367938147a 100644 --- a/src/slic3r/GUI/3DBed.cpp +++ b/src/slic3r/GUI/3DBed.cpp @@ -553,16 +553,9 @@ void Bed3D::render_prusa(const std::string &key, bool bottom) const { if (m_vbo_id == 0) { -//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ -// unsigned int stride = m_triangles.get_vertex_data_size(); -//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ glsafe(::glGenBuffers(1, &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(::glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, stride, (GLvoid*)m_triangles.get_position_offset())); -// glsafe(::glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, stride, (GLvoid*)m_triangles.get_tex_coords_offset())); -//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ glsafe(::glBindBuffer(GL_ARRAY_BUFFER, 0)); } @@ -572,9 +565,6 @@ void Bed3D::render_prusa(const std::string &key, bool bottom) const 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)); - if (bottom) glsafe(::glFrontFace(GL_CW)); @@ -583,8 +573,6 @@ void Bed3D::render_prusa(const std::string &key, bool bottom) const if (bottom) glsafe(::glFrontFace(GL_CCW)); -// glsafe(::glDisable(GL_TEXTURE_2D)); - glsafe(::glDisable(GL_BLEND)); glsafe(::glDepthMask(GL_TRUE)); } @@ -600,22 +588,14 @@ void Bed3D::render_prusa_shader(bool transparent) const m_shader.start_using(); m_shader.set_uniform("transparent_background", transparent); -//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ unsigned int stride = m_triangles.get_vertex_data_size(); GLint position_id = m_shader.get_attrib_location("v_position"); GLint tex_coords_id = m_shader.get_attrib_location("v_tex_coords"); - std::cout << "position_id: " << position_id << std::endl; - std::cout << "tex_coords_id: " << tex_coords_id << std::endl; - -// if (tex_coords_id != -1) -// glsafe(::glBindAttribLocation(m_shader.get_shader_program_id(), tex_coords_id, "v_tex_coords")); -//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ - glsafe(::glBindTexture(GL_TEXTURE_2D, (GLuint)m_texture.get_id())); glsafe(::glBindBuffer(GL_ARRAY_BUFFER, m_vbo_id)); -//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ + if (position_id != -1) { glsafe(::glEnableVertexAttribArray(position_id)); @@ -627,21 +607,14 @@ void Bed3D::render_prusa_shader(bool transparent) const glsafe(::glVertexAttribPointer(tex_coords_id, 2, GL_FLOAT, GL_FALSE, stride, (GLvoid*)m_triangles.get_tex_coords_offset())); } -// glsafe(::glEnableVertexAttribArray(0)); -// glsafe(::glEnableVertexAttribArray(1)); -// glsafe(::glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, stride, (GLvoid*)m_triangles.get_position_offset())); -// glsafe(::glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, stride, (GLvoid*)m_triangles.get_tex_coords_offset())); -//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ glsafe(::glDrawArrays(GL_TRIANGLES, 0, (GLsizei)m_triangles.get_vertices_count())); -//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ + if (tex_coords_id != -1) glsafe(::glDisableVertexAttribArray(tex_coords_id)); + if (position_id != -1) glsafe(::glDisableVertexAttribArray(position_id)); -// glsafe(::glDisableVertexAttribArray(1)); -// glsafe(::glDisableVertexAttribArray(0)); -//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ glsafe(::glBindBuffer(GL_ARRAY_BUFFER, 0)); glsafe(::glBindTexture(GL_TEXTURE_2D, 0)); diff --git a/src/slic3r/GUI/GLShader.cpp b/src/slic3r/GUI/GLShader.cpp index ff6235296b2..f401f54662c 100644 --- a/src/slic3r/GUI/GLShader.cpp +++ b/src/slic3r/GUI/GLShader.cpp @@ -317,7 +317,6 @@ void Shader::stop_using() const m_shader->disable(); } -//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ int Shader::get_attrib_location(const std::string& name) const { return (m_shader != nullptr) ? m_shader->get_attrib_location(name.c_str()) : -1; @@ -327,7 +326,6 @@ int Shader::get_uniform_location(const std::string& name) const { return (m_shader != nullptr) ? m_shader->get_uniform_location(name.c_str()) : -1; } -//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ void Shader::set_uniform(const std::string& name, float value) const { diff --git a/src/slic3r/GUI/GLShader.hpp b/src/slic3r/GUI/GLShader.hpp index db5fe283272..58e2678d03c 100644 --- a/src/slic3r/GUI/GLShader.hpp +++ b/src/slic3r/GUI/GLShader.hpp @@ -53,10 +53,8 @@ public: bool start_using() const; void stop_using() const; -//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ int get_attrib_location(const std::string& name) const; int get_uniform_location(const std::string& name) const; -//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ void set_uniform(const std::string& name, float value) const; void set_uniform(const std::string& name, const float* matrix) const; From f8f21d74d9cc7175ff8de1e99f9b13dd42f87b83 Mon Sep 17 00:00:00 2001 From: bubnikv Date: Thu, 28 Feb 2019 11:11:13 +0100 Subject: [PATCH 5/6] Bumped up the alpha version number. --- version.inc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/version.inc b/version.inc index 6713416eb1c..f8c60feb690 100644 --- a/version.inc +++ b/version.inc @@ -2,7 +2,7 @@ # (the version numbers are generated by the build script from the git current label) set(SLIC3R_FORK_NAME "Slic3r Prusa Edition") -set(SLIC3R_VERSION "1.42.0-alpha5") +set(SLIC3R_VERSION "1.42.0-alpha6") set(SLIC3R_BUILD "${SLIC3R_VERSION}+UNKNOWN") set(SLIC3R_BUILD_ID "${SLIC3R_BUILD_ID}") set(SLIC3R_RC_VERSION "1,42,0,0") From b06c2598d4e935e699d3ba7cd10bf852677a23e1 Mon Sep 17 00:00:00 2001 From: bubnikv Date: Thu, 28 Feb 2019 11:20:01 +0100 Subject: [PATCH 6/6] Improvement of the initial placement of modifier meshes: Sphere and Cylinder are scaled to the same volume as Box Newly entered modifier meshes are rotated parallell to the world coordinates. If the instance coordinate system is rotated and scaled, it is not possible to create an unskewed modifier to world transformation. In that case the best possible transformation is found to minimize least squares error of the 8 corners of the new modifier mesh bounding box using Levenberg-Marquardt algorithm. FIXME: 1) The Levenberg-Marquardt non-linear least squares does not converge nicely, it may require some tuning. 2) Above all, if 1) is called, then often the skew of the modifier mesh is so high, that it is likely more useful to display the modifier with zero rotation and inverse scaling, so that the modifier will be of correct size, but not parallel to the world coordinates. --- src/libslic3r/Geometry.hpp | 1 + src/slic3r/GUI/GUI_ObjectList.cpp | 203 ++++++++++++++++++++++++++---- 2 files changed, 178 insertions(+), 26 deletions(-) diff --git a/src/libslic3r/Geometry.hpp b/src/libslic3r/Geometry.hpp index b43d5914391..380245b5fcf 100644 --- a/src/libslic3r/Geometry.hpp +++ b/src/libslic3r/Geometry.hpp @@ -242,6 +242,7 @@ public: void set_scaling_factor(const Vec3d& scaling_factor); void set_scaling_factor(Axis axis, double scaling_factor); + bool is_scaling_uniform() const { return std::abs(m_scaling_factor.x() - m_scaling_factor.y()) < 1e-8 && std::abs(m_scaling_factor.x() - m_scaling_factor.z()) < 1e-8; } const Vec3d& get_mirror() const { return m_mirror; } double get_mirror(Axis axis) const { return m_mirror(axis); } diff --git a/src/slic3r/GUI/GUI_ObjectList.cpp b/src/slic3r/GUI/GUI_ObjectList.cpp index c5d6fe9fde1..b7b52e10cf6 100644 --- a/src/slic3r/GUI/GUI_ObjectList.cpp +++ b/src/slic3r/GUI/GUI_ObjectList.cpp @@ -1173,10 +1173,161 @@ void ObjectList::load_part( ModelObject* model_object, } +// Find volume transformation, so that the chained (instance_trafo * volume_trafo) will be as close to identity +// as possible in least squares norm in regard to the 8 corners of bbox. +// Bounding box is expected to be centered around zero in all axes. +Geometry::Transformation volume_to_bed_transformation(const Geometry::Transformation &instance_transformation, const BoundingBoxf3 &bbox) +{ + Geometry::Transformation out; + + if (instance_transformation.is_scaling_uniform()) { + // No need to run the non-linear least squares fitting for uniform scaling. + // Just set the inverse. + out.set_from_transform(instance_transformation.get_matrix(true).inverse()); + } + else + { + Eigen::Matrix3d instance_rotation_trafo = + (Eigen::AngleAxisd(instance_transformation.get_rotation().z(), Vec3d::UnitZ()) * + Eigen::AngleAxisd(instance_transformation.get_rotation().y(), Vec3d::UnitY()) * + Eigen::AngleAxisd(instance_transformation.get_rotation().x(), Vec3d::UnitX())).toRotationMatrix(); + Eigen::Matrix3d instance_rotation_trafo_inv = + (Eigen::AngleAxisd(- instance_transformation.get_rotation().x(), Vec3d::UnitX()) * + Eigen::AngleAxisd(- instance_transformation.get_rotation().y(), Vec3d::UnitY()) * + Eigen::AngleAxisd(- instance_transformation.get_rotation().z(), Vec3d::UnitZ())).toRotationMatrix(); + Vec3d euler_angles_inv = Geometry::extract_euler_angles(instance_rotation_trafo_inv); + + Eigen::Matrix3d instance_trafo = instance_rotation_trafo * + Eigen::Scaling(instance_transformation.get_scaling_factor().cwiseProduct(instance_transformation.get_mirror())); + + // 8 corners of the bounding box. + auto pts = Eigen::MatrixXd(8, 3); + pts(0, 0) = bbox.min.x(); pts(0, 1) = bbox.min.y(); pts(0, 2) = bbox.min.z(); + pts(1, 0) = bbox.min.x(); pts(1, 1) = bbox.min.y(); pts(1, 2) = bbox.max.z(); + pts(2, 0) = bbox.min.x(); pts(2, 1) = bbox.max.y(); pts(2, 2) = bbox.min.z(); + pts(3, 0) = bbox.min.x(); pts(3, 1) = bbox.max.y(); pts(3, 2) = bbox.max.z(); + pts(4, 0) = bbox.max.x(); pts(4, 1) = bbox.min.y(); pts(4, 2) = bbox.min.z(); + pts(5, 0) = bbox.max.x(); pts(5, 1) = bbox.min.y(); pts(5, 2) = bbox.max.z(); + pts(6, 0) = bbox.max.x(); pts(6, 1) = bbox.max.y(); pts(6, 2) = bbox.min.z(); + pts(7, 0) = bbox.max.x(); pts(7, 1) = bbox.max.y(); pts(7, 2) = bbox.max.z(); + + // Current parameters: 3x scale, 3x rotation + auto beta = Eigen::MatrixXd(3 + 3, 1); + beta << 1., 1., 1., euler_angles_inv(0), euler_angles_inv(1), euler_angles_inv(2); + + { + // Trafo from world to the coordinate system of the modifier mesh, with the inverse rotation applied to the modifier. + Eigen::Matrix3d A_scaling = instance_trafo * instance_rotation_trafo_inv; + // Corners of the bounding box transformed into the modifier mesh coordinate space, with inverse rotation applied to the modifier. + auto qs = pts * A_scaling.inverse().transpose(); + // Fill in scaling based on least squares fitting of the bounding box corners. + for (int i = 0; i < 3; ++i) + beta(i) = pts.col(i).dot(qs.col(i)) / pts.col(i).dot(pts.col(i)); + } + + // Jacobian + // rows: 8 corners of a cube times 3 dimensions, + // cols: 3x scale, 3x rotation + auto J = Eigen::MatrixXd(8 * 3, 3 + 3); + + // Until convergence: + Eigen::Matrix3d s, dsx, dsy, dsz; + Eigen::Matrix3d rx, drx, ry, dry, rz, drz; + s.setIdentity(); + rx.setIdentity(); ry.setIdentity(); rz.setIdentity(); + dsx.setZero(); dsy.setZero(); dsz.setZero(); + drx.setZero(); dry.setZero(); drz.setZero(); + dsx(0, 0) = 1.; dsy(1, 1) = 1.; dsz(2, 2) = 1.; + + // Solve the non-linear Least Squares problem by Levenberg–Marquardt algorithm (modified Gauss–Newton iteration) + const double eps = 1.e-7; + auto beta_best = beta; + double beta_best_error = 1e10; + for (size_t iter = 0; iter < 200; ++ iter) { + // Current rotation & scaling transformation. + auto trafo = instance_trafo * + Eigen::AngleAxisd(beta(5), Vec3d::UnitZ()) * + Eigen::AngleAxisd(beta(4), Vec3d::UnitY()) * + Eigen::AngleAxisd(beta(3), Vec3d::UnitX()) * + Eigen::Scaling(Vec3d(beta(0), beta(1), beta(2))); + // Current error after rotation & scaling. + auto dy = (pts - pts * trafo.transpose()).eval(); + double err = 0; + for (int i = 0; i < 8; ++i) + err += dy.row(i).norm(); + if (err < beta_best_error) { + beta_best = beta; + beta_best_error = err; + } + // Fill in the Jacobian at current beta. + double cos_rx = cos(beta(3)); + double sin_rx = sin(beta(3)); + double cos_ry = cos(beta(4)); + double sin_ry = sin(beta(4)); + double cos_rz = cos(beta(5)); + double sin_rz = sin(beta(5)); + rx << 1., 0., 0., 0., cos_rx, -sin_rx, 0., sin_rx, cos_rx; + drx << 0., 0., 0., 0., -sin_rx, -cos_rx, 0., cos_rx, -sin_rx; + ry << cos_ry, 0., sin_ry, 0., 1., 0., -sin_ry, 0., cos_ry; + dry << -sin_ry, 0., cos_ry, 0., 0., 0., -cos_ry, 0., -sin_ry; + rz << cos_rz, -sin_rz, 0., sin_rz, cos_rz, 0., 0., 0., 1.; + drz << -sin_rz, -cos_rz, 0., cos_rz, -sin_rz, 0., 0., 0., 0.; + s(0, 0) = beta(0); + s(1, 1) = beta(1); + s(2, 2) = beta(2); + auto rot = (instance_trafo * rz * ry * rx).eval(); + auto jrx = pts * (instance_trafo * rz * ry * drx * s).transpose(); + auto jry = pts * (instance_trafo * rz * dry * rx * s).transpose(); + auto jrz = pts * (instance_trafo * drz * ry * rx * s).transpose(); + for (int r = 0; r < 8; ++ r) { + for (int i = 0; i < 3; ++ i) { + J(r * 3 + i, 0) = rot(i, 0) * pts(r, 0); + J(r * 3 + i, 1) = rot(i, 1) * pts(r, 1); + J(r * 3 + i, 2) = rot(i, 2) * pts(r, 2); + J(r * 3 + i, 3) = jrx(r, i); + J(r * 3 + i, 4) = jry(r, i); + J(r * 3 + i, 5) = jrz(r, i); + } + } + // Solving the normal equations for delta beta. + auto rhs = (J.transpose() * Eigen::Map(dy.data(), dy.size())).eval(); + double lambda = 1.; // 0.01; + auto A = (J.transpose() * J + Eigen::Matrix::Identity() * lambda).eval(); + auto L = A.ldlt(); + auto delta_beta = L.solve(rhs).eval(); + // Check for convergence. + auto delta_beta_max = delta_beta.cwiseAbs().maxCoeff(); + if (delta_beta_max < eps) + break; + beta = beta + delta_beta; + } + + out.set_rotation(Vec3d(beta_best(3), beta_best(4), beta_best(5))); + out.set_scaling_factor(Vec3d(std::abs(beta_best(0)), std::abs(beta_best(1)), std::abs(beta_best(2)))); + out.set_mirror(Vec3d(beta_best(0) > 0 ? 1. : -1, beta_best(1) > 0 ? 1. : -1, beta_best(2) > 0 ? 1. : -1)); + } + + return out; +} + void ObjectList::load_generic_subobject(const std::string& type_name, const ModelVolumeType type) { const auto obj_idx = get_selected_obj_idx(); - if (obj_idx < 0) return; + if (obj_idx < 0) + return; + + const GLCanvas3D::Selection& selection = wxGetApp().plater()->canvas3D()->get_selection(); + assert(obj_idx == selection.get_object_idx()); + // Selected instance index in ModelObject. Only valid if there is only one instance selected in the selection. + int instance_idx = selection.get_instance_idx(); + assert(instance_idx != -1); + if (instance_idx == -1) + return; + + // Selected object + ModelObject &model_object = *(*m_objects)[obj_idx]; + // Bounding box of the selected instance in world coordinate system including the translation, without modifiers. + BoundingBoxf3 instance_bb = model_object.instance_bounding_box(instance_idx); const wxString name = _(L("Generic")) + "-" + _(type_name); TriangleMesh mesh; @@ -1185,48 +1336,48 @@ void ObjectList::load_generic_subobject(const std::string& type_name, const Mode const auto& sz = BoundingBoxf(bed_shape).size(); const auto side = 0.1 * std::max(sz(0), sz(1)); - if (type_name == "Box") { + if (type_name == "Box") + // Sitting on the print bed, left front front corner at (0, 0). mesh = make_cube(side, side, side); - // box sets the base coordinate at 0, 0, move to center of plate - mesh.translate(-side * 0.5, -side * 0.5, 0); - } else if (type_name == "Cylinder") - mesh = make_cylinder(0.5*side, side); + // Centered around 0, sitting on the print bed. + // The cylinder has the same volume as the box above. + mesh = make_cylinder(0.564 * side, side); else if (type_name == "Sphere") - mesh = make_sphere(0.5*side, PI/18); - else if (type_name == "Slab") { - const auto& size = (*m_objects)[obj_idx]->bounding_box().size(); - mesh = make_cube(size(0)*1.5, size(1)*1.5, size(2)*0.5); - // box sets the base coordinate at 0, 0, move to center of plate and move it up to initial_z - mesh.translate(-size(0)*1.5 / 2.0, -size(1)*1.5 / 2.0, 0); - } + // Centered around 0, half the sphere below the print bed, half above. + // The sphere has the same volume as the box above. + mesh = make_sphere(0.62 * side, PI / 18); + else if (type_name == "Slab") + // Sitting on the print bed, left front front corner at (0, 0). + mesh = make_cube(instance_bb.size().x()*1.5, instance_bb.size().y()*1.5, instance_bb.size().z()*0.5); mesh.repair(); - auto new_volume = (*m_objects)[obj_idx]->add_volume(mesh); + // Mesh will be centered when loading. + ModelVolume *new_volume = model_object.add_volume(std::move(mesh)); new_volume->set_type(type); #if !ENABLE_GENERIC_SUBPARTS_PLACEMENT - new_volume->set_offset(Vec3d(0.0, 0.0, (*m_objects)[obj_idx]->origin_translation(2) - mesh.stl.stats.min(2))); + new_volume->set_offset(Vec3d(0.0, 0.0, model_object.origin_translation(2) - mesh.stl.stats.min(2))); #endif // !ENABLE_GENERIC_SUBPARTS_PLACEMENT #if !ENABLE_VOLUMES_CENTERING_FIXES new_volume->center_geometry(); #endif // !ENABLE_VOLUMES_CENTERING_FIXES #if ENABLE_GENERIC_SUBPARTS_PLACEMENT - const GLCanvas3D::Selection& selection = wxGetApp().plater()->canvas3D()->get_selection(); - int instance_idx = selection.get_instance_idx(); if (instance_idx != -1) { + // First (any) GLVolume of the selected instance. They all share the same instance matrix. const GLVolume* v = selection.get_volume(*selection.get_volume_idxs().begin()); - const Transform3d& inst_m = v->get_instance_transformation().get_matrix(true); - TriangleMesh vol_mesh(mesh); - vol_mesh.transform(inst_m); - Vec3d vol_shift = -vol_mesh.bounding_box().center(); - vol_mesh.translate((float)vol_shift(0), (float)vol_shift(1), (float)vol_shift(2)); - Vec3d world_mesh_bb_size = vol_mesh.bounding_box().size(); - BoundingBoxf3 inst_bb = (*m_objects)[obj_idx]->instance_bounding_box(instance_idx); - Vec3d world_target = Vec3d(inst_bb.max(0), inst_bb.min(1), inst_bb.min(2)) + 0.5 * world_mesh_bb_size; - new_volume->set_offset(inst_m.inverse() * (world_target - v->get_instance_offset())); + // Transform the new modifier to be aligned with the print bed. + const BoundingBoxf3 mesh_bb = new_volume->mesh.bounding_box(); + new_volume->set_transformation(volume_to_bed_transformation(v->get_instance_transformation(), mesh_bb)); + // Set the modifier position. + auto offset = (type_name == "Slab") ? + // Slab: Lift to print bed + Vec3d(0., 0., 0.5 * mesh_bb.size().z() + instance_bb.min.z() - v->get_instance_offset().z()) : + // Translate the new modifier to be pickable: move to the left front corner of the instance's bounding box, lift to print bed. + Vec3d(instance_bb.max(0), instance_bb.min(1), instance_bb.min(2)) + 0.5 * mesh_bb.size() - v->get_instance_offset(); + new_volume->set_offset(v->get_instance_transformation().get_matrix(true).inverse() * offset); } #endif // ENABLE_GENERIC_SUBPARTS_PLACEMENT