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