Simulant  21.12-1292
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 
75  return vertex_range_count() ? &vertex_ranges_[0] : nullptr;
76  }
77 
78  std::size_t vertex_range_count() const {
79  return vertex_ranges_.size();
80  }
81 
82  void remove_all_vertex_ranges();
83 
84  void set_material(const MaterialPtr &material);
85  void set_material_at_slot(MaterialSlot var, const MaterialPtr& material);
86 
87  const MaterialPtr& material() const;
88  const MaterialPtr& material_at_slot(MaterialSlot var, bool fallback=false) const;
89 
90  MeshArrangement arrangement() const { return arrangement_; }
91 
92  bool reverse_winding();
93 
94  void generate_texture_coordinates_cube(uint32_t texture=0);
95 
96  /* Goes through the indexes in this submesh and changes the diffuse color of the vertices
97  * they point to */
98  void set_diffuse(const Color &color);
99 
100  /*
101  * Whether or not this submesh contributes to the adjacency info attached to the mesh
102  * which is used for silhouettes
103  */
104  void set_contributes_to_edge_list(bool v=true) {
105  contributes_to_edge_list_ = v;
106  }
107 
108  bool contributes_to_edge_list() const {
109  return contributes_to_edge_list_;
110  }
111 
112  /*
113  * Iterates the submesh indexes and breaks them into triangles (a, b, c). In the case of lines
114  * index c will be the same as index a.
115  */
116  void each_triangle(std::function<void (uint32_t, uint32_t, uint32_t)> cb);
117 
120  const AABB& aabb() const;
121 
122 public:
123  typedef sig::signal<void (SubMeshPtr, MaterialSlot, AssetID, AssetID)> MaterialChangedCallback;
124 
125  MaterialChangedCallback& signal_material_changed() {
126  return signal_material_changed_;
127  }
128 
129 private:
130  friend class Mesh;
131 
132  sig::connection material_change_connection_;
133 
134  Mesh* parent_;
135  SubmeshType type_;
136  sig::connection parent_update_connection_;
137 
138  std::array<MaterialPtr, MATERIAL_SLOT_MAX> materials_;
139 
140  MeshArrangement arrangement_;
141 
142  std::shared_ptr<IndexData> index_data_;
143  VertexRangeList vertex_ranges_;
144 
145  void _recalc_bounds(AABB& bounds);
146  void _recalc_bounds_indexed(AABB& bounds);
147  void _recalc_bounds_ranged(AABB& bounds);
148 
149  void _each_triangle_indexed(std::function<void (uint32_t, uint32_t, uint32_t)> cb);
150  void _each_triangle_ranged(std::function<void (uint32_t, uint32_t, uint32_t)> cb);
151 
152  MaterialChangedCallback signal_material_changed_;
153 
154  bool contributes_to_edge_list_ = true;
155 
156  /* This is updated and maintained by Mesh, it only lives here so
157  * there's a fast lookup for each submesh */
158  AABB bounds_;
159 
160 public:
161  S_DEFINE_PROPERTY(mesh, &SubMesh::parent_);
162  S_DEFINE_PROPERTY(index_data, &SubMesh::index_data_);
163 };
164 
165 }
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:71
smlt
Definition: animation.cpp:25
smlt::Color
Definition: color.h:32
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::sig::signal
Definition: signal.h:330
smlt::Nameable
The Nameable class.
Definition: nameable.h:12