From 9625fe8f5b9c39a713a804b28dcf8bc0e33225c1 Mon Sep 17 00:00:00 2001
From: Enrico Turri <enricoturri@seznam.cz>
Date: Thu, 25 Jul 2019 09:45:43 +0200
Subject: [PATCH] Render custom bed model on custom beds

---
 src/slic3r/GUI/3DBed.cpp | 47 ++++++++++++++++++++++++----------------
 src/slic3r/GUI/3DBed.hpp |  1 +
 2 files changed, 29 insertions(+), 19 deletions(-)

diff --git a/src/slic3r/GUI/3DBed.cpp b/src/slic3r/GUI/3DBed.cpp
index 4f3d499b2..cebb17c23 100644
--- a/src/slic3r/GUI/3DBed.cpp
+++ b/src/slic3r/GUI/3DBed.cpp
@@ -394,24 +394,7 @@ Bed3D::EType Bed3D::detect_type(const Pointfs& shape) const
 void Bed3D::render_prusa(GLCanvas3D& canvas, const std::string& key, bool bottom) const
 {
     if (!bottom)
-    {
-        std::string filename = m_custom_model.empty() ? resources_dir() + "/models/" + key + "_bed.stl" : m_custom_model;
-        if ((m_model.get_filename() != filename) && m_model.init_from_file(filename))
-        {
-            // move the model a bit down to avoid z-fighting with the texture quad
-            m_model.set_offset(-0.03 * Vec3d::UnitZ());
-
-            // update extended bounding box
-            calc_bounding_boxes();
-        }
-
-        if (!m_model.get_filename().empty())
-        {
-            glsafe(::glEnable(GL_LIGHTING));
-            m_model.render();
-            glsafe(::glDisable(GL_LIGHTING));
-        }
-    }
+        render_model(m_custom_model.empty() ? resources_dir() + "/models/" + key + "_bed.stl" : m_custom_model);
 
     render_texture(m_custom_texture.empty() ? resources_dir() + "/icons/bed/" + key + ".svg" : m_custom_texture, bottom, canvas);
 }
@@ -421,6 +404,7 @@ void Bed3D::render_texture(const std::string& filename, bool bottom, GLCanvas3D&
     if (filename.empty())
     {
         m_texture.reset();
+        render_default();
         return;
     }
 
@@ -565,14 +549,39 @@ void Bed3D::render_texture(const std::string& filename, bool bottom, GLCanvas3D&
     }
 }
 
+void Bed3D::render_model(const std::string& filename) const
+{
+    if (filename.empty())
+        return;
+
+    if ((m_model.get_filename() != filename) && m_model.init_from_file(filename))
+    {
+        // move the model a bit down to avoid z-fighting with the texture quad
+        m_model.set_offset(-0.03 * Vec3d::UnitZ());
+
+        // update extended bounding box
+        calc_bounding_boxes();
+    }
+
+    if (!m_model.get_filename().empty())
+    {
+        glsafe(::glEnable(GL_LIGHTING));
+        m_model.render();
+        glsafe(::glDisable(GL_LIGHTING));
+    }
+}
+
 void Bed3D::render_custom(GLCanvas3D& canvas, bool bottom) const
 {
-    if (m_custom_texture.empty())
+    if (m_custom_texture.empty() && m_custom_model.empty())
     {
         render_default();
         return;
     }
 
+    if (!bottom)
+        render_model(m_custom_model);
+
     render_texture(m_custom_texture, bottom, canvas);
 }
 
diff --git a/src/slic3r/GUI/3DBed.hpp b/src/slic3r/GUI/3DBed.hpp
index 3571166da..ef731756d 100644
--- a/src/slic3r/GUI/3DBed.hpp
+++ b/src/slic3r/GUI/3DBed.hpp
@@ -120,6 +120,7 @@ private:
     EType detect_type(const Pointfs& shape) const;
     void render_prusa(GLCanvas3D& canvas, const std::string& key, bool bottom) const;
     void render_texture(const std::string& filename, bool bottom, GLCanvas3D& canvas) const;
+    void render_model(const std::string& filename) const;
     void render_custom(GLCanvas3D& canvas, bool bottom) const;
     void render_default() const;
     void reset();