From 5af44143e29e70dead405653615ac867efef392a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Fri, 31 Dec 2010 16:11:41 +0100 Subject: [PATCH] Function for getting absolute object transformation (relative to the camera). Usable for computation of light positions. --- src/Object.cpp | 23 +++++++++++++++++++++++ src/Object.h | 13 +++++++++++-- 2 files changed, 34 insertions(+), 2 deletions(-) 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; }