Simulant  21.12-403
A portable game engine for Windows, OSX, Linux, Dreamcast, and PSP
submesh.h
1 #pragma once
2 
3 #include "../interfaces/boundable.h"
4 #include "../generic/property.h"
5 #include "../generic/managed.h"
6 #include "../interfaces/nameable.h"
7 
8 namespace smlt {
9 
10 class VertexData;
11 class IndexData;
12 class Mesh;
13 class Renderer;
14 
15 enum MaterialSlot : uint8_t {
16  MATERIAL_SLOT0 = 0,
17  MATERIAL_SLOT1,
18  MATERIAL_SLOT2,
19  MATERIAL_SLOT3,
20  MATERIAL_SLOT4,
21  MATERIAL_SLOT5,
22  MATERIAL_SLOT6,
23  MATERIAL_SLOT7,
24  MATERIAL_SLOT_MAX
25 };
26 
27 enum SubmeshType {
28  SUBMESH_TYPE_INDEXED,
29  SUBMESH_TYPE_RANGED,
30 };
31 
32 struct VertexRange {
33  uint32_t start;
34  uint32_t count;
35 };
36 
37 typedef std::vector<VertexRange> VertexRangeList;
38 
39 
40 class SubMesh:
41  public RefCounted<SubMesh>,
42  public Nameable {
43 
44 public:
45  /* Indexed submesh constructor */
46  SubMesh(Mesh* parent,
47  const std::string& name,
48  MaterialPtr material,
49  std::shared_ptr<IndexData>& index_data,
50  MeshArrangement arrangement = MESH_ARRANGEMENT_TRIANGLES
51  );
52 
53  /* Ranged submesh constructor */
54  SubMesh(Mesh* parent,
55  const std::string& name,
56  MaterialPtr material,
57  MeshArrangement arrangement = MESH_ARRANGEMENT_TRIANGLES
58  );
59 
60  virtual ~SubMesh();
61 
62  SubmeshType type() const;
63 
70  bool add_vertex_range(uint32_t start, uint32_t count);
71  void mark_changed();
72 
73  const VertexRange* vertex_ranges() const {
74  return &vertex_ranges_[0];
75  }
76 
77  std::size_t vertex_range_count() const {
78  return vertex_ranges_.size();
79  }
80 
81  void remove_all_vertex_ranges();
82 
83  void set_material(const MaterialPtr &material);
84  void set_material_at_slot(MaterialSlot var, const MaterialPtr& material);
85 
86  MaterialPtr material() const;
87  MaterialPtr material_at_slot(MaterialSlot var, bool fallback=false) const;
88 
89  MeshArrangement arrangement() const { return arrangement_; }
90 
91  bool reverse_winding();
92 
93  void generate_texture_coordinates_cube(uint32_t texture=0);
94 
95  /* Goes through the indexes in this submesh and changes the diffuse colour of the vertices
96  * they point to */
97  void set_diffuse(const Colour &colour);
98 
99  /*
100  * Whether or not this submesh contributes to the adjacency info attached to the mesh
101  * which is used for silhouettes
102  */
103  void set_contributes_to_edge_list(bool v=true) {
104  contributes_to_edge_list_ = v;
105  }
106 
107  bool contributes_to_edge_list() const {
108  return contributes_to_edge_list_;
109  }
110 
111  /*
112  * Iterates the submesh indexes and breaks them into triangles (a, b, c). In the case of lines
113  * index c will be the same as index a.
114  */
115  void each_triangle(std::function<void (uint32_t, uint32_t, uint32_t)> cb);
116 
119  const AABB& aabb() const;
120 
121 public:
122  typedef sig::signal<void (SubMeshPtr, MaterialSlot, MaterialID, MaterialID)> MaterialChangedCallback;
123 
124  MaterialChangedCallback& signal_material_changed() {
125  return signal_material_changed_;
126  }
127 
128 private:
129  friend class Mesh;
130 
131  sig::connection material_change_connection_;
132 
133  Mesh* parent_;
134  SubmeshType type_;
135  sig::connection parent_update_connection_;
136 
137  std::array<MaterialPtr, MATERIAL_SLOT_MAX> materials_;
138 
139  MeshArrangement arrangement_;
140 
141  std::shared_ptr<IndexData> index_data_;
142  VertexRangeList vertex_ranges_;
143 
144  void _recalc_bounds(AABB& bounds);
145  void _recalc_bounds_indexed(AABB& bounds);
146  void _recalc_bounds_ranged(AABB& bounds);
147 
148  void _each_triangle_indexed(std::function<void (uint32_t, uint32_t, uint32_t)> cb);
149  void _each_triangle_ranged(std::function<void (uint32_t, uint32_t, uint32_t)> cb);
150 
151  MaterialChangedCallback signal_material_changed_;
152 
153  bool contributes_to_edge_list_ = true;
154 
155  /* This is updated and maintained by Mesh, it only lives here so
156  * there's a fast lookup for each submesh */
157  AABB bounds_;
158 
159 public:
160  S_DEFINE_PROPERTY(mesh, &SubMesh::parent_);
161  S_DEFINE_PROPERTY(index_data, &SubMesh::index_data_);
162 };
163 
164 }
smlt::SubMesh::generate_texture_coordinates_cube
void generate_texture_coordinates_cube(uint32_t texture=0)
SubMesh::generate_texture_coordinates_cube.
Definition: submesh.cpp:340
smlt::sig::Connection
Definition: signal.h:65
smlt::SubMesh::aabb
const AABB & aabb() const
Definition: submesh.cpp:326
smlt::RefCounted
Definition: managed.h:65
smlt
Definition: animation.cpp:25
smlt::UniqueID< MaterialPtr >
smlt::Mesh
Definition: mesh.h:127
smlt::AABB
Definition: aabb.h:22
smlt::VertexRange
Definition: submesh.h:32
smlt::SubMesh::add_vertex_range
bool add_vertex_range(uint32_t start, uint32_t count)
Add a vertex range to this submesh. count vertices will be rendered from the start index in the mesh ...
Definition: submesh.cpp:54
smlt::SubMesh
Definition: submesh.h:42
smlt::Colour
Definition: colour.h:29
smlt::sig::signal
Definition: signal.h:330
smlt::Nameable
The Nameable class.
Definition: nameable.h:12