Ray Tracer Common Project
Image rendering program based on the ray tracing technique.
entity.hpp
1 
15 #pragma once
16 
17 #include <unordered_set>
18 #include <list>
19 #include "core/vector.hpp"
20 #include "core/matrix.hpp"
21 
22 namespace rt{
23 
24 class Scene;
25 
50 class Entity{
51 public:
57  Entity(Entity *parent = 0);
58 
64  Entity(const vector & pos, Entity *parent = 0);
65 
71  Entity(double x, double y, double z, Entity *parent = 0);
72 
78  virtual ~Entity() = 0;
79 
86  void soil();
87 
91  bool isDirty() const;
92 
96  virtual void update();
97 
111  const matrix & globalMatrix() const;
112 
128  const matrix & invertedGlobalMatrix() const;
129 
135  vector globalPosition() const;
136 
140  const vector & position() const;
141 
145  const vector & rotation() const;
146 
153  void setPosition(const vector & pos);
154 
161  void setRotation(const vector & rot);
162 
168  Entity *parent() const;
169 
175  void setParent(Entity *);
176 
180  Scene *scene() const;
181 
188  void setScene(Scene *);
189 
193  const std::unordered_set<Entity*> & children() const;
194 
201  void addChild(Entity *);
202 
208  void removeChild(Entity *);
209 
228  template<class T>
229  void queryAll(std::list<const T*> & list) const{
230  const T* self = dynamic_cast<const T*>(this);
231  if(self)
232  list.push_back(self);
233  for(Entity *c : _children)
234  c->queryAll<T>(list);
235  }
236 
237 private:
238  Scene *_scene;
239  Entity *_parent;
240  std::unordered_set<Entity*> _children;
241 
242  void updateGlobalMatrix() const;
243 
244  vector _pos;
245  vector _rot;
246  mutable matrix _m;
247  mutable matrix _iv_m;
248  mutable bool _dirty;
249 };
250 
251 }
Basic space entity.
Definition: entity.hpp:50
virtual ~Entity()=0
Destructor.
Definition: entity.cpp:27
void setScene(Scene *)
Set the scene of the entity.
Definition: entity.cpp:124
void setPosition(const vector &pos)
Set the entity position.
Definition: entity.cpp:91
const vector & position() const
Entity location.
Definition: entity.cpp:83
const matrix & globalMatrix() const
The global matrix of the entity.
Definition: entity.cpp:69
void setRotation(const vector &rot)
Set the entity rotation.
Definition: entity.cpp:96
Complete scene.
Definition: scene.hpp:42
Definition: bitmap.cpp:4
void removeChild(Entity *)
Remove a child.
Definition: entity.cpp:146
const matrix & invertedGlobalMatrix() const
The invert of the global matrix of the entity.
Definition: entity.cpp:74
void soil()
Make an entity dirty.
Definition: entity.cpp:58
3D vector
Definition: vector.hpp:28
const std::unordered_set< Entity * > & children() const
Entity children.
Definition: entity.cpp:138
vector globalPosition() const
Global position of the entity.
Definition: entity.cpp:79
const vector & rotation() const
Entity rotation.
Definition: entity.cpp:87
4x4 matrix
Definition: matrix.hpp:33
virtual void update()
Update the global matrix of the entity and its children.
Definition: entity.cpp:34
void setParent(Entity *)
Set the parent entity.
Definition: entity.cpp:105
Entity(Entity *parent=0)
Default entity constructor.
Definition: entity.cpp:6
void queryAll(std::list< const T *> &list) const
Get the list of a specific type of entity.
Definition: entity.hpp:229
bool isDirty() const
Definition: entity.cpp:65
void addChild(Entity *)
Add a child.
Definition: entity.cpp:142
Entity * parent() const
The parent entity.
Definition: entity.cpp:101
Scene * scene() const
The scene of the entity.
Definition: entity.cpp:120