diff --git a/src/Object.cpp b/src/Object.cpp index 8ba541163..a1e873d18 100644 --- a/src/Object.cpp +++ b/src/Object.cpp @@ -43,6 +43,29 @@ void Object::setParent(Object* parent) { _parent = parent; } +Matrix4 Object::transformation(bool absolute) { + if(!absolute) return _transformation; + + Matrix4 t = _transformation; + + Object* p = parent(); + while(p != 0) { + t = p->transformation()*t; + + /* We got to the scene, multiply with camera matrix */ + if(p->parent() == p) { + Camera* camera = static_cast(p)->camera(); + if(camera && camera != this) t = camera->cameraMatrix()*t; + + break; + } + + p = p->parent(); + } + + return t; +} + Object::~Object() { /* Remove the object from parent's children */ setParent(0); diff --git a/src/Object.h b/src/Object.h index a6d746ff9..142b5ffb0 100644 --- a/src/Object.h +++ b/src/Object.h @@ -72,8 +72,17 @@ class Object { /** @brief Set parent object */ virtual void setParent(Object* parent); - /** @brief Transformation matrix */ - inline Matrix4 transformation() const { return _transformation; } + /** + * @brief Transformation matrix + * + * If the object is part of an scene and @c absolute is set to true, + * returns absolute transformation matrix (thus relative to actual + * camera), if the object is not part of an scene, returns + * transformation matrix composed of all matrices of parent objects. + * If @c absolute is set to false, returns transformation matrix + * relative to parent. + */ + virtual Matrix4 transformation(bool absolute = false); /** @brief Set transformation matrix */ inline void setTransformation(const Matrix4& transformation) { _transformation = transformation; }