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_solder_paste = true;
32  bool use_layer_colors = false;
33  Color solder_mask_color = {0, .5, 0};
34  Color substrate_color = {.2, .15, 0};
35  float explode = 0;
36  float highlight_intensity = .5;
37 
38  float cam_azimuth = 90;
39  float cam_elevation = 45;
40  float cam_distance = 20;
41  float cam_fov = 45;
42  glm::vec2 center;
43 
44 
45  enum class Projection { PERSP, ORTHO };
46  Projection projection = Projection::PERSP;
47 
48  class FaceVertex {
49  public:
50  FaceVertex(float ix, float iy, float iz, uint8_t ir, uint8_t ig, uint8_t ib)
51  : x(ix), y(iy), z(iz), r(ir), g(ig), b(ib), _pad(0)
52  {
53  }
54  float x;
55  float y;
56  float z;
57 
58  uint8_t r;
59  uint8_t g;
60  uint8_t b;
61  uint8_t _pad;
62  } __attribute__((packed));
63 
65  public:
66  ModelTransform(float ix, float iy, float a, bool flip, bool highlight)
67  : x(ix), y(iy), angle(a), flags(flip | (highlight << 1))
68  {
69  }
70  float x;
71  float y;
72  uint16_t angle;
73  uint16_t flags;
74 
75  float model_x = 0;
76  float model_y = 0;
77  float model_z = 0;
78  uint16_t model_roll = 0;
79  uint16_t model_pitch = 0;
80  uint16_t model_yaw = 0;
81  } __attribute__((packed));
82  void view_all();
83  void clear_3d_models();
84 
85 protected:
86  Appearance appearance;
87 
88  float width = 100;
89  float height = 100;
90 
91  CoverRenderer cover_renderer;
92  WallRenderer wall_renderer;
93  FaceRenderer face_renderer;
94  BackgroundRenderer background_renderer;
95 
96  void a_realize();
97  void resize_buffers();
98  void push();
99  enum class RenderBackground { YES, NO };
100  void render(RenderBackground mode = RenderBackground::YES);
101  virtual int a_get_scale_factor() const;
102  void prepare();
103  void prepare_packages();
104 
105  unsigned int num_samples = 1;
106 
107  const class Board *brd = nullptr;
108 
109  std::set<UUID> packages_highlight;
110 
111  void load_3d_model(const std::string &filename, const std::string &filename_abs);
112 
113  std::map<std::string, std::string> get_model_filenames(class Pool &pool);
114 
115  std::mutex models_loading_mutex;
116 
117  void update_max_package_height();
118 
119 private:
120  float get_layer_offset(int layer) const;
121  float get_layer_thickness(int layer) const;
122  bool layer_is_visible(int layer) const;
123 
124  std::pair<glm::vec3, glm::vec3> bbox;
125 
126  GLuint renderbuffer;
127  GLuint fbo;
128  GLuint depthrenderbuffer;
129 
130  glm::mat4 viewmat;
131  glm::mat4 projmat;
132  glm::vec3 cam_normal;
133 
134  float package_height_max = 0;
135  std::vector<FaceVertex> face_vertex_buffer; // vertices of all models, sequentially
136  std::vector<unsigned int> face_index_buffer; // indexes face_vertex_buffer to form triangles
137  std::map<std::string, std::pair<size_t, size_t>> models; // key: filename value: first: offset in face_index_buffer
138  // second: no of indexes
139 
140  std::vector<ModelTransform> package_transforms; // position and rotation of
141  // all board packages,
142  // grouped by package
143  std::map<std::string, std::pair<size_t, size_t>>
144  package_transform_idxs; // key: model filename: value: first: offset
145  // in package_transforms second: no of items
146 
147  float get_magic_number() const;
148 };
149 
150 } // namespace horizon
horizon::Canvas3DBase::FaceVertex
Definition: canvas3d_base.hpp:48
libzip::uint8_t
zip_uint8_t uint8_t
zip_uint8_t typedef.
Definition: zip.hpp:78
horizon::Canvas3DBase::ModelTransform
Definition: canvas3d_base.hpp:64
horizon::CanvasMesh
Definition: canvas_mesh.hpp:8
horizon::Board
Definition: board.hpp:43
horizon::CoverRenderer
Definition: cover.hpp:6
libzip::uint16_t
zip_uint16_t uint16_t
zip_uint16_t typedef.
Definition: zip.hpp:88
horizon::Color
Definition: common.hpp:215
horizon::FaceRenderer
Definition: face.hpp:6
horizon::BackgroundRenderer
Definition: background.hpp:5
horizon::Appearance
Definition: appearance.hpp:7
horizon::WallRenderer
Definition: wall.hpp:6
horizon::Canvas3DBase
Definition: canvas3d_base.hpp:15
horizon::Pool
Stores objects (Unit, Entity, Symbol, Part, etc.) from the pool.
Definition: pool.hpp:21