Browse Source

Fluent interface for Object transformation.

Now you can use method chaining while setting Object transformation -
scale, rotation, translation and parent.
pull/279/head
Vladimír Vondruš 14 years ago
parent
commit
02ae00fcfc
  1. 12
      src/Object.cpp
  2. 16
      src/Object.h

12
src/Object.cpp

@ -21,9 +21,9 @@ using namespace std;
namespace Magnum {
void Object::setParent(Object* parent) {
Object* Object::setParent(Object* parent) {
/* Skip if nothing to do or this is scene */
if(_parent == parent || _parent == this) return;
if(_parent == parent || _parent == this) return this;
/* Add the object to children list of new parent */
if(parent != nullptr) {
@ -31,7 +31,7 @@ void Object::setParent(Object* parent) {
/* Only Fry can be his own grandfather */
Object* p = parent;
while(p != nullptr && p->parent() != p) {
if(p == this) return;
if(p == this) return this;
p = p->parent();
}
@ -46,6 +46,7 @@ void Object::setParent(Object* parent) {
_parent = parent;
setDirty();
return this;
}
Matrix4 Object::absoluteTransformation(Camera* camera) {
@ -93,11 +94,12 @@ Scene* Object::scene() {
return nullptr;
}
void Object::setTransformation(const Matrix4& transformation) {
if(_parent == this) return;
Object* Object::setTransformation(const Matrix4& transformation) {
if(_parent == this) return this;
_transformation = transformation;
setDirty();
return this;
}
void Object::setDirty() {

16
src/Object.h

@ -74,7 +74,7 @@ class MAGNUM_EXPORT Object {
inline const std::set<Object*>& children() { return _children; }
/** @brief Set parent object */
void setParent(Object* parent);
Object* setParent(Object* parent);
/**
* @brief Transformation matrix
@ -96,7 +96,7 @@ class MAGNUM_EXPORT Object {
virtual Matrix4 absoluteTransformation(Camera* camera = nullptr);
/** @brief Set transformation matrix */
void setTransformation(const Matrix4& transformation);
Object* setTransformation(const Matrix4& transformation);
/**
* @brief Multiply transformation matrix
@ -107,8 +107,9 @@ class MAGNUM_EXPORT Object {
*
* Multiplies current transformation matrix by new matrix.
*/
inline void multiplyTransformation(const Matrix4& transformation, bool global = true) {
inline Object* multiplyTransformation(const Matrix4& transformation, bool global = true) {
setTransformation(global ? transformation*_transformation : _transformation*transformation);
return this;
}
/**
@ -116,8 +117,9 @@ class MAGNUM_EXPORT Object {
*
* Same as calling multiplyTransformation() with Matrix4::translation().
*/
inline void translate(Vector3 vec, bool global = true) {
inline Object* translate(Vector3 vec, bool global = true) {
multiplyTransformation(Matrix4::translation(vec), global);
return this;
}
/**
@ -125,8 +127,9 @@ class MAGNUM_EXPORT Object {
*
* Same as calling multiplyTransformation() with Matrix4::scaling().
*/
inline void scale(Vector3 vec, bool global = true) {
inline Object* scale(Vector3 vec, bool global = true) {
multiplyTransformation(Matrix4::scaling(vec), global);
return this;
}
/**
@ -134,8 +137,9 @@ class MAGNUM_EXPORT Object {
*
* Same as calling multiplyTransformation() with Matrix4::rotation().
*/
inline void rotate(GLfloat angle, Vector3 vec, bool global = true) {
inline Object* rotate(GLfloat angle, Vector3 vec, bool global = true) {
multiplyTransformation(Matrix4::rotation(angle, vec), global);
return this;
}
/** @{ @name Caching helpers

Loading…
Cancel
Save