1 #ifndef slic3r_3DBed_hpp_
2 #define slic3r_3DBed_hpp_
3 
4 #include "GLTexture.hpp"
5 #include "3DScene.hpp"
6 #include "GLModel.hpp"
7 
8 #include <tuple>
9 #include <array>
10 
11 namespace Slic3r {
12 namespace GUI {
13 
14 class GLCanvas3D;
15 
16 class GeometryBuffer
17 {
18     struct Vertex
19     {
20         float position[3];
21         float tex_coords[2];
22 
VertexSlic3r::GUI::GeometryBuffer::Vertex23         Vertex()
24         {
25             position[0] = 0.0f; position[1] = 0.0f; position[2] = 0.0f;
26             tex_coords[0] = 0.0f; tex_coords[1] = 0.0f;
27         }
28     };
29 
30     std::vector<Vertex> m_vertices;
31 
32 public:
33     bool set_from_triangles(const Polygons& triangles, float z, bool generate_tex_coords);
34     bool set_from_lines(const Lines& lines, float z);
35 
36     const float* get_vertices_data() const;
get_vertices_data_size() const37     unsigned int get_vertices_data_size() const { return (unsigned int)m_vertices.size() * get_vertex_data_size(); }
get_vertex_data_size() const38     unsigned int get_vertex_data_size() const { return (unsigned int)(5 * sizeof(float)); }
get_position_offset() const39     size_t get_position_offset() const { return 0; }
get_tex_coords_offset() const40     size_t get_tex_coords_offset() const { return (size_t)(3 * sizeof(float)); }
get_vertices_count() const41     unsigned int get_vertices_count() const { return (unsigned int)m_vertices.size(); }
42 };
43 
44 class Bed3D
45 {
46     class Axes
47     {
48     public:
49         static const float DefaultStemRadius;
50         static const float DefaultStemLength;
51         static const float DefaultTipRadius;
52         static const float DefaultTipLength;
53 
54     private:
55         Vec3d m_origin{ Vec3d::Zero() };
56         float m_stem_length{ DefaultStemLength };
57         mutable GLModel m_arrow;
58 
59     public:
get_origin() const60         const Vec3d& get_origin() const { return m_origin; }
set_origin(const Vec3d & origin)61         void set_origin(const Vec3d& origin) { m_origin = origin; }
62         void set_stem_length(float length);
get_total_length() const63         float get_total_length() const { return m_stem_length + DefaultTipLength; }
64         void render() const;
65     };
66 
67 public:
68     enum EType : unsigned char
69     {
70         System,
71         Custom,
72         Num_Types
73     };
74 
75 private:
76     EType m_type;
77     Pointfs m_shape;
78     std::string m_texture_filename;
79     std::string m_model_filename;
80     mutable BoundingBoxf3 m_bounding_box;
81     mutable BoundingBoxf3 m_extended_bounding_box;
82     Polygon m_polygon;
83     GeometryBuffer m_triangles;
84     GeometryBuffer m_gridlines;
85     mutable GLTexture m_texture;
86     mutable GLModel m_model;
87     mutable Vec3d m_model_offset{ Vec3d::Zero() };
88     std::array<float, 4> m_model_color{ 0.235f, 0.235f, 0.235f, 1.0f };
89     // temporary texture shown until the main texture has still no levels compressed
90     mutable GLTexture m_temp_texture;
91     mutable unsigned int m_vbo_id;
92     Axes m_axes;
93 
94     mutable float m_scale_factor;
95 
96 public:
97     Bed3D();
~Bed3D()98     ~Bed3D() { reset(); }
99 
get_type() const100     EType get_type() const { return m_type; }
101 
is_custom() const102     bool is_custom() const { return m_type == Custom; }
103 
get_shape() const104     const Pointfs& get_shape() const { return m_shape; }
105     // Return true if the bed shape changed, so the calee will update the UI.
106     bool set_shape(const Pointfs& shape, const std::string& custom_texture, const std::string& custom_model, bool force_as_custom = false);
107 
get_bounding_box(bool extended) const108     const BoundingBoxf3& get_bounding_box(bool extended) const {
109         return extended ? m_extended_bounding_box : m_bounding_box;
110     }
111 
112     bool contains(const Point& point) const;
113     Point point_projection(const Point& point) const;
114 
115     void render(GLCanvas3D& canvas, bool bottom, float scale_factor,
116                 bool show_axes, bool show_texture) const;
117 
118 private:
119     void calc_bounding_boxes() const;
120     void calc_triangles(const ExPolygon& poly);
121     void calc_gridlines(const ExPolygon& poly, const BoundingBox& bed_bbox);
122     std::tuple<EType, std::string, std::string> detect_type(const Pointfs& shape) const;
123     void render_axes() const;
124     void render_system(GLCanvas3D& canvas, bool bottom, bool show_texture) const;
125     void render_texture(bool bottom, GLCanvas3D& canvas) const;
126     void render_model() const;
127     void render_custom(GLCanvas3D& canvas, bool bottom, bool show_texture) const;
128     void render_default(bool bottom) const;
129     void reset();
130 };
131 
132 } // GUI
133 } // Slic3r
134 
135 #endif // slic3r_3DBed_hpp_
136