mirror of https://github.com/mosra/magnum.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
162 lines
4.8 KiB
162 lines
4.8 KiB
#ifndef Magnum_Object_h |
|
#define Magnum_Object_h |
|
/* |
|
Copyright © 2010 Vladimír Vondruš <mosra@centrum.cz> |
|
|
|
This file is part of Magnum. |
|
|
|
Magnum is free software: you can redistribute it and/or modify |
|
it under the terms of the GNU Lesser General Public License version 3 |
|
only, as published by the Free Software Foundation. |
|
|
|
Magnum is distributed in the hope that it will be useful, |
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
GNU Lesser General Public License version 3 for more details. |
|
*/ |
|
|
|
/** @file |
|
* @brief Class Magnum::Object |
|
*/ |
|
|
|
#include <set> |
|
|
|
#include "Magnum.h" |
|
|
|
namespace Magnum { |
|
|
|
class Scene; |
|
|
|
/** |
|
* @brief Base for all positioned objects |
|
* |
|
* @todo Transform transformation when changing parent, so the object stays in |
|
* place. |
|
*/ |
|
class Object { |
|
DISABLE_COPY(Object) |
|
|
|
friend class Scene; |
|
|
|
public: |
|
/** |
|
* @brief Constructor |
|
* @param parent Parent object |
|
* |
|
* Sets all transformations to their default values. |
|
*/ |
|
inline Object(Object* parent = 0): _parent(0) { |
|
setParent(parent); |
|
} |
|
|
|
/** |
|
* @brief Destructor |
|
* |
|
* Removes itself from parent's children list and destroys all own |
|
* children. |
|
*/ |
|
virtual ~Object(); |
|
|
|
/** |
|
* @brief Scene |
|
* @return If the object is not assigned to any scene, returns 0. |
|
*/ |
|
Scene* scene() const; |
|
|
|
/** @brief Parent object */ |
|
inline Object* parent() const { return _parent; } |
|
|
|
/** @brief Child objects */ |
|
inline const std::set<Object*>& children() const { return _children; } |
|
|
|
/** @brief Set parent object */ |
|
virtual void setParent(Object* parent); |
|
|
|
/** @brief Transformation matrix */ |
|
inline Matrix4 transformation() const { return _transformation; } |
|
|
|
/** @brief Set transformation matrix */ |
|
inline void setTransformation(const Matrix4& transformation) { _transformation = transformation; } |
|
|
|
/** |
|
* @brief Multiply transformation matrix |
|
* @param transformation Transformation matrix |
|
* @param global Whether to apply transformation as global |
|
* (multiply from left side) or as local (multiply from right |
|
* side) |
|
* |
|
* Multiplies current transformation matrix by new matrix. |
|
*/ |
|
inline void multiplyTransformation(const Matrix4& transformation, bool global = true) { |
|
_transformation = global ? transformation*_transformation : _transformation*transformation; |
|
} |
|
|
|
/** |
|
* @brief Set transformation and parent from another object |
|
* |
|
* Sets parent and transformation from another object, so they will |
|
* appear in the same place. |
|
*/ |
|
inline void setTransformationFrom(Object* another) { |
|
setParent(another->parent()); |
|
setTransformation(another->transformation()); |
|
} |
|
|
|
/** |
|
* @brief Translate object |
|
* |
|
* Same as calling multiplyTransformation() with Matrix4::translation(). |
|
*/ |
|
inline void translate(Vector3 vec, bool global = true) { |
|
multiplyTransformation(Matrix4::translation(vec), global); |
|
} |
|
|
|
/** @copydoc translate(Vector3, bool) */ |
|
inline void translate(GLfloat x, GLfloat y, GLfloat z, bool global = true) { |
|
translate(Vector3(x, y, z), global); |
|
} |
|
|
|
/** |
|
* @brief Scale object |
|
* |
|
* Same as calling multiplyTransformation() with Matrix4::scaling(). |
|
*/ |
|
inline void scale(Vector3 vec, bool global = true) { |
|
multiplyTransformation(Matrix4::scaling(vec), global); |
|
} |
|
|
|
/** @copydoc scale(Vector3, bool) */ |
|
inline void scale(GLfloat x, GLfloat y, GLfloat z, bool global = true) { |
|
scale(Vector3(x, y, z), global); |
|
} |
|
|
|
/** |
|
* @brief Rotate object |
|
* |
|
* Same as calling multiplyTransformation() with Matrix4::rotation(). |
|
*/ |
|
inline void rotate(GLfloat angle, Vector3 vec, bool global = true) { |
|
multiplyTransformation(Matrix4::rotation(angle, vec), global); |
|
} |
|
|
|
/** @copydoc rotate(GLfloat, Vector3, bool) */ |
|
inline void rotate(GLfloat angle, GLfloat x, GLfloat y, GLfloat z, bool global = true) { |
|
rotate(angle, Vector3(x, y, z), global); |
|
} |
|
|
|
/** |
|
* @brief Draw object |
|
* |
|
* Default implementation does nothing. |
|
*/ |
|
virtual void draw(const Matrix4& transformationMatrix, const Matrix4& projectionMatrix) {} |
|
|
|
private: |
|
Object* _parent; |
|
std::set<Object*> _children; |
|
Matrix4 _transformation; |
|
}; |
|
|
|
} |
|
|
|
#endif
|
|
|