Simulant  21.09-46
A portable game engine for Windows, OSX, Linux, Dreamcast, and PSP
stage_node.h
1 #pragma once
2 
3 #include "./tree_node.h"
4 #include "../interfaces/nameable.h"
5 #include "../interfaces/printable.h"
6 #include "../interfaces/transformable.h"
7 #include "../interfaces/updateable.h"
8 #include "../interfaces/boundable.h"
9 #include "../behaviours/behaviour.h"
10 #include "../interfaces/has_auto_id.h"
11 #include "../generic/data_carrier.h"
12 #include "../shadows.h"
13 #include "../generic/manual_object.h"
14 
15 #include "iterators/sibling_iterator.h"
16 #include "iterators/child_iterator.h"
17 #include "iterators/descendent_iterator.h"
18 #include "iterators/ancestor_iterator.h"
19 
20 namespace smlt {
21 
22 class RenderableFactory;
23 class Seconds;
24 
25 typedef sig::signal<void (AABB)> BoundsUpdatedSignal;
26 typedef sig::signal<void ()> CleanedUpSignal;
27 
28 /* Used for multiple levels of detail when rendering stage nodes */
29 
30 enum DetailLevel {
31  DETAIL_LEVEL_NEAREST = 0,
32  DETAIL_LEVEL_NEAR,
33  DETAIL_LEVEL_MID,
34  DETAIL_LEVEL_FAR,
35  DETAIL_LEVEL_FARTHEST,
36  DETAIL_LEVEL_MAX
37 };
38 
39 enum StageNodeType {
40  STAGE_NODE_TYPE_STAGE,
41  STAGE_NODE_TYPE_CAMERA,
42  STAGE_NODE_TYPE_ACTOR,
43  STAGE_NODE_TYPE_LIGHT,
44  STAGE_NODE_TYPE_PARTICLE_SYSTEM,
45  STAGE_NODE_TYPE_GEOM,
46  STAGE_NODE_TYPE_MESH_INSTANCER,
47  STAGE_NODE_TYPE_OTHER
48 };
49 
50 class StageNode:
51  public virtual DestroyableObject,
52  public TreeNode,
53  public virtual Nameable,
54  public Printable,
55  public Transformable,
56  public Updateable,
57  public virtual BoundableEntity,
58  public Organism,
59  public HasAutoID<StageNode>,
60  public TwoPhaseConstructed {
61 
62  DEFINE_SIGNAL(BoundsUpdatedSignal, signal_bounds_updated);
63 
64  // Fired when the node is cleaned up later, following destroy
65  DEFINE_SIGNAL(CleanedUpSignal, signal_cleaned_up);
66 
67 public:
69  friend class StageNode;
70 
72  root_(root) {}
73 
74  StageNode* root_;
75 
76  public:
77  SiblingIterator<false> begin() {
78  return SiblingIterator<false>(root_);
79  }
80 
82  return SiblingIterator<false>(root_, nullptr);
83  }
84  };
85 
87  friend class StageNode;
88 
90  root_(root) {}
91 
92  StageNode* root_;
93 
94  public:
95  ChildIterator<false> begin() {
96  return ChildIterator<false>((StageNode*) root_);
97  }
98 
99  ChildIterator<false> end() {
100  return ChildIterator<false>(root_, nullptr);
101  }
102  };
103 
105  friend class StageNode;
106 
108  root_(root) {}
109 
110  StageNode* root_;
111 
112  public:
113  DescendentIterator<false> begin() {
114  return DescendentIterator<false>((StageNode*) root_);
115  }
116 
118  return DescendentIterator<false>(root_, nullptr);
119  }
120  };
121 
123  friend class StageNode;
124 
126  root_(root) {}
127 
128  StageNode* root_;
129 
130  public:
131  AncestorIterator<false> begin() {
132  return AncestorIterator<false>((StageNode*) root_);
133  }
134 
136  return AncestorIterator<false>(root_, nullptr);
137  }
138  };
139 
140  AncestorIteratorPair each_ancestor() {
141  return AncestorIteratorPair(this);
142  }
143 
144  DescendentIteratorPair each_descendent() {
145  return DescendentIteratorPair(this);
146  }
147 
148  SiblingIteratorPair each_sibling() {
149  return SiblingIteratorPair(this);
150  }
151 
152  ChildIteratorPair each_child() {
153  return ChildIteratorPair(this);
154  }
155 
156  std::string repr() const override {
157  return name();
158  }
159 
160  StageNode(Stage* stage, StageNodeType node_type);
161 
162  virtual ~StageNode();
163 
164  StageNodeType node_type() const;
165 
170  void link_position(StageNode* other);
171 
172  /* Without a parent, these are the same as move_to/rotate_to. With a parent
173  * this applies a relative position / rotation to the parent position / rotation
174  * so the node appears where you want */
175  void move_to_absolute(const Vec3& position);
176  void move_to_absolute(float x, float y, float z);
177  void rotate_to_absolute(const Quaternion& rotation);
178  void rotate_to_absolute(const Degrees& degrees, float x, float y, float z);
179 
180  Vec3 absolute_position() const;
181  Quaternion absolute_rotation() const;
182  Vec3 absolute_scaling() const;
183  Mat4 absolute_transformation() const;
184 
185  bool is_visible() const;
186 
187  bool is_intended_visible() const { return is_visible_; }
188  void set_visible(bool visible);
189 
190  Property<generic::DataCarrier StageNode::*> data = { this, &StageNode::data_ };
191  Property<Stage* StageNode::*> stage = { this, &StageNode::stage_ };
192 
193  template<typename T>
194  void set_parent(const UniqueID<T>& id) {
195  set_parent(id.fetch());
196  }
197 
198  void set_parent(TreeNode* node);
199 
200  void destroy_after(const Seconds& seconds);
201 
202  void update(float dt) override;
203  void late_update(float dt) override;
204  void fixed_update(float step) override;
205 
206  bool parent_is_stage() const;
207 
208  void clean_up() override;
209 
210  const AABB transformed_aabb() const override;
211 
212  /* Control shading on the stage node (behaviour depends on the type of node) */
213  ShadowCast shadow_cast() const { return shadow_cast_; }
214  void set_shadow_cast(ShadowCast cast) {
215  shadow_cast_ = cast;
216  }
217 
218  ShadowReceive shadow_receive() const { return shadow_receive_; }
219  void set_shadow_receive(ShadowReceive receive) { shadow_receive_ = receive; }
220 
221  StageNode* find_descendent_with_name(const std::string& name);
222 
223  /* Return a list of renderables to pass into the render queue */
224  virtual void _get_renderables(
225  batcher::RenderQueue* render_queue,
226  const CameraPtr camera,
227  const DetailLevel detail_level
228  ) = 0;
229 
230  void set_cullable(bool v);
231  bool is_cullable() const;
232 
233 protected:
234  // Faster than properties, useful for subclasses where a clean API isn't as important
235  Stage* get_stage() const { return stage_; }
236 
237  void on_transformation_changed() override;
238  void on_parent_set(TreeNode* oldp, TreeNode* newp) override;
239 
240  virtual void update_transformation_from_parent();
241 
242  void recalc_bounds_if_necessary() const;
243  void mark_transformed_aabb_dirty();
244 
245  void mark_absolute_transformation_dirty();
246 private:
247  AABB calculate_transformed_aabb() const;
248 
249  Stage* stage_ = nullptr;
250  StageNode* parent_stage_node_ = nullptr;
251 
252  StageNodeType node_type_ = STAGE_NODE_TYPE_ACTOR;
253 
254  generic::DataCarrier data_;
255 
256  StageNode* linked_position_node_ = nullptr;
257  sig::connection linked_position_node_destroyed_;
258 
259  bool is_visible_ = true;
260  bool self_and_parents_visible_ = true;
261  void recalc_visibility();
262 
263  Vec3 absolute_position_;
264  Quaternion absolute_rotation_;
265  Vec3 absolute_scale_ = Vec3(1, 1, 1);
266 
267  mutable Mat4 absolute_transformation_;
268  mutable bool absolute_transformation_is_dirty_ = true;
269 
270  /* Mutable so that AABB accesses can be const, but we delay
271  * calculation until access */
272  mutable AABB transformed_aabb_;
273  mutable bool transformed_aabb_dirty_ = false;
274 
275  // By default, always cast and receive shadows
276  ShadowCast shadow_cast_ = SHADOW_CAST_ALWAYS;
277  ShadowReceive shadow_receive_ = SHADOW_RECEIVE_ALWAYS;
278 
279  /* Whether or not this node should be culled by the partitioner (e.g. when offscreen) */
280  bool cullable_ = true;
281 };
282 
283 
284 class ContainerNode : public StageNode {
285 public:
286  ContainerNode(Stage* stage, StageNodeType node_type):
287  StageNode(stage, node_type) {}
288 
289  /* Containers don't directly have renderables, but their children do */
290  void _get_renderables(batcher::RenderQueue*, const CameraPtr, const DetailLevel) override {
291 
292  }
293 
294  virtual ~ContainerNode() {}
295 };
296 
297 }
298 
299 #include "iterators/sibling_iterator.inc"
300 #include "iterators/child_iterator.inc"
301 #include "iterators/descendent_iterator.inc"
302 #include "iterators/ancestor_iterator.inc"
smlt::DescendentIterator
Definition: descendent_iterator.h:11
smlt::TreeNode
Definition: tree_node.h:17
smlt::StageNode::ChildIteratorPair
Definition: stage_node.h:86
smlt::StageNode::AncestorIteratorPair
Definition: stage_node.h:122
smlt::Printable
Definition: printable.h:7
smlt::StageNode::DescendentIteratorPair
Definition: stage_node.h:104
smlt::Organism
Definition: behaviour.h:132
smlt::StageNode::link_position
void link_position(StageNode *other)
Definition: stage_node.cpp:22
smlt::SiblingIterator
Definition: sibling_iterator.h:11
smlt::Stage
Definition: stage.h:91
smlt::Updateable
The Updateable class.
Definition: updateable.h:13
smlt
Definition: animation.cpp:25
smlt::TwoPhaseConstructed
Definition: managed.h:47
smlt::batcher::RenderQueue
Definition: render_queue.h:156
smlt::StageNode::SiblingIteratorPair
Definition: stage_node.h:68
smlt::ContainerNode
Definition: stage_node.h:284
smlt::DestroyableObject
Definition: manual_object.h:9
smlt::StageNode
Definition: stage_node.h:60
smlt::AncestorIterator
Definition: ancestor_iterator.h:11
smlt::HasAutoID
Definition: has_auto_id.h:8
smlt::Transformable
The Transformable class.
Definition: transformable.h:17
smlt::default_init_ptr< Camera >
smlt::BoundableEntity
The BoundableEntity class.
Definition: boundable.h:49
smlt::ChildIterator
Definition: child_iterator.h:11
smlt::sig::signal
Definition: signal.h:319
smlt::Nameable
The Nameable class.
Definition: nameable.h:12