Horizon
canvas3d_base.hpp
1 #pragma once
2 #include <glm/glm.hpp>
3 #include "common/common.hpp"
4 #include "canvas_mesh.hpp"
5 #include "canvas/appearance.hpp"
6 #include "util/uuid.hpp"
7 #include <mutex>
8 #include "cover.hpp"
9 #include "face.hpp"
10 #include "wall.hpp"
11 #include "background.hpp"
12 
13 namespace horizon {
14 
15 class Canvas3DBase {
16 public:
17  Canvas3DBase();
18  friend class CoverRenderer;
19  friend class WallRenderer;
20  friend class FaceRenderer;
21  friend class BackgroundRenderer;
22  CanvasMesh ca;
23  Color background_top_color;
24  Color background_bottom_color;
25  Color get_layer_color(int layer) const;
26 
27  bool show_solder_mask = true;
28  bool show_silkscreen = true;
29  bool show_substrate = true;
30  bool show_models = true;
31  bool show_dnp_models = false;
32  bool show_solder_paste = true;
33  bool use_layer_colors = false;
34  Color solder_mask_color = {0, .5, 0};
35  Color substrate_color = {.2, .15, 0};
36  float explode = 0;
37  float highlight_intensity = .5;
38 
39  float cam_azimuth = 90;
40  float cam_elevation = 45;
41  float cam_distance = 20;
42  float cam_fov = 45;
43  glm::vec2 center;
44 
45 
46  enum class Projection { PERSP, ORTHO };
47  Projection projection = Projection::PERSP;
48 
49  class FaceVertex {
50  public:
51  FaceVertex(float ix, float iy, float iz, float inx, float iny, float inz, uint8_t ir, uint8_t ig, uint8_t ib)
52  : x(ix), y(iy), z(iz), nx(inx), ny(iny), nz(inz), r(ir), g(ig), b(ib), _pad(0)
53  {
54  }
55  float x;
56  float y;
57  float z;
58  float nx;
59  float ny;
60  float nz;
61 
62  uint8_t r;
63  uint8_t g;
64  uint8_t b;
65  uint8_t _pad;
66  } __attribute__((packed));
67 
69  public:
70  ModelTransform(float ix, float iy, float a, bool flip, bool highlight)
71  : x(ix), y(iy), angle(a), flags(flip | (highlight << 1))
72  {
73  }
74  float x;
75  float y;
76  uint16_t angle;
77  uint16_t flags;
78 
79  float model_x = 0;
80  float model_y = 0;
81  float model_z = 0;
82  uint16_t model_roll = 0;
83  uint16_t model_pitch = 0;
84  uint16_t model_yaw = 0;
85  } __attribute__((packed));
86  void view_all();
87  void clear_3d_models();
88 
89 protected:
90  Appearance appearance;
91 
92  int width = 100;
93  int height = 100;
94 
95  CoverRenderer cover_renderer;
96  WallRenderer wall_renderer;
97  FaceRenderer face_renderer;
98  BackgroundRenderer background_renderer;
99 
100  void a_realize();
101  void resize_buffers();
102  void push();
103  enum class RenderBackground { YES, NO };
104  void render(RenderBackground mode = RenderBackground::YES);
105  virtual int a_get_scale_factor() const;
106  void prepare();
107  void prepare_packages();
108 
109  unsigned int num_samples = 1;
110 
111  const class Board *brd = nullptr;
112 
113  std::set<UUID> packages_highlight;
114 
115  void load_3d_model(const std::string &filename, const std::string &filename_abs);
116 
117  std::map<std::string, std::string> get_model_filenames(class IPool &pool);
118 
119  std::mutex models_loading_mutex;
120 
121  void update_max_package_height();
122 
123 private:
124  float get_layer_offset(int layer) const;
125  float get_layer_thickness(int layer) const;
126  bool layer_is_visible(int layer) const;
127 
128  std::pair<glm::vec3, glm::vec3> bbox;
129 
130  GLuint renderbuffer;
131  GLuint fbo;
132  GLuint depthrenderbuffer;
133 
134  glm::mat4 viewmat;
135  glm::mat4 projmat;
136  glm::vec3 cam_normal;
137 
138  float package_height_max = 0;
139  std::vector<FaceVertex> face_vertex_buffer; // vertices of all models, sequentially
140  std::vector<unsigned int> face_index_buffer; // indexes face_vertex_buffer to form triangles
141 
142  class ModelInfo {
143  public:
144  ModelInfo(size_t o, size_t n) : face_index_offset(o), count(n)
145  {
146  }
147  const size_t face_index_offset; // offset in face_index_buffer
148  const size_t count; // number of items in face_index_buffer
149  bool pushed = false;
150  };
151  std::map<std::string, ModelInfo> models; // key: filename
152 
153  std::vector<ModelTransform> package_transforms; // position and rotation of
154  // all board packages,
155  // grouped by package
156  std::map<std::pair<std::string, bool>, std::pair<size_t, size_t>>
157  package_transform_idxs; // key: first: model filename second: nopopulate
158  // value: first: offset in package_transforms second: no of items
159 
160  float get_magic_number() const;
161 };
162 
163 } // namespace horizon
Definition: appearance.hpp:7
Definition: background.hpp:5
Definition: board.hpp:42
Definition: canvas3d_base.hpp:49
Definition: canvas3d_base.hpp:68
Definition: canvas3d_base.hpp:15
Definition: canvas_mesh.hpp:8
Definition: common.hpp:234
Definition: cover.hpp:6
Definition: face.hpp:6
Definition: ipool.hpp:12
Definition: wall.hpp:6
zip_uint8_t uint8_t
zip_uint8_t typedef.
Definition: zip.hpp:78
zip_uint16_t uint16_t
zip_uint16_t typedef.
Definition: zip.hpp:88