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.
276 lines
8.8 KiB
276 lines
8.8 KiB
|
14 years ago
|
#ifndef Magnum_SceneGraph_Object_h
|
||
|
|
#define Magnum_SceneGraph_Object_h
|
||
|
16 years ago
|
/*
|
||
|
15 years ago
|
Copyright © 2010, 2011, 2012 Vladimír Vondruš <mosra@centrum.cz>
|
||
|
16 years ago
|
|
||
|
|
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
|
||
|
14 years ago
|
* @brief Class Magnum::SceneGraph::Object
|
||
|
16 years ago
|
*/
|
||
|
|
|
||
|
|
#include <set>
|
||
|
|
|
||
|
|
#include "Magnum.h"
|
||
|
|
|
||
|
14 years ago
|
#include "magnumSceneGraphVisibility.h"
|
||
|
|
|
||
|
|
namespace Magnum { namespace SceneGraph {
|
||
|
16 years ago
|
|
||
|
16 years ago
|
class Scene;
|
||
|
14 years ago
|
class Camera;
|
||
|
16 years ago
|
|
||
|
14 years ago
|
/**
|
||
|
|
@todo User-specified Object implementation:
|
||
|
|
- for front-to-back sorting, LoD changes etc.
|
||
|
|
- for different parent/children implementation (e.g. no std::set, direct
|
||
|
|
access to scene etc.)
|
||
|
|
- for using doubles/halves instead of floats
|
||
|
|
- for using quat + position instead of matrices (where (asymmetric) scaling is
|
||
|
|
not needed)
|
||
|
|
*/
|
||
|
|
|
||
|
14 years ago
|
/** @ingroup scene
|
||
|
16 years ago
|
* @brief Base for all positioned objects
|
||
|
|
*
|
||
|
|
* @todo Transform transformation when changing parent, so the object stays in
|
||
|
|
* place.
|
||
|
|
*/
|
||
|
14 years ago
|
class SCENEGRAPH_EXPORT Object {
|
||
|
15 years ago
|
Object(const Object& other) = delete;
|
||
|
|
Object(Object&& other) = delete;
|
||
|
|
Object& operator=(const Object& other) = delete;
|
||
|
|
Object& operator=(Object&& other) = delete;
|
||
|
16 years ago
|
|
||
|
16 years ago
|
friend class Scene;
|
||
|
|
|
||
|
16 years ago
|
public:
|
||
|
|
/**
|
||
|
|
* @brief Constructor
|
||
|
|
* @param parent Parent object
|
||
|
|
*
|
||
|
|
* Sets all transformations to their default values.
|
||
|
|
*/
|
||
|
15 years ago
|
inline Object(Object* parent = nullptr): _parent(nullptr), dirty(true) {
|
||
|
16 years ago
|
setParent(parent);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @brief Destructor
|
||
|
|
*
|
||
|
|
* Removes itself from parent's children list and destroys all own
|
||
|
|
* children.
|
||
|
|
*/
|
||
|
16 years ago
|
virtual ~Object();
|
||
|
16 years ago
|
|
||
|
14 years ago
|
/** @{ @name Scene hierarchy */
|
||
|
|
|
||
|
16 years ago
|
/**
|
||
|
14 years ago
|
* @brief %Scene
|
||
|
15 years ago
|
* @return If the object is not assigned to any scene, returns nullptr.
|
||
|
16 years ago
|
*/
|
||
|
14 years ago
|
Scene* scene();
|
||
|
16 years ago
|
|
||
|
16 years ago
|
/** @brief Parent object */
|
||
|
14 years ago
|
inline Object* parent() { return _parent; }
|
||
|
16 years ago
|
|
||
|
|
/** @brief Child objects */
|
||
|
14 years ago
|
inline const std::set<Object*>& children() { return _children; }
|
||
|
16 years ago
|
|
||
|
|
/** @brief Set parent object */
|
||
|
14 years ago
|
Object* setParent(Object* parent);
|
||
|
16 years ago
|
|
||
|
14 years ago
|
/*@}*/
|
||
|
|
|
||
|
|
/** @{ @name Object transformation
|
||
|
16 years ago
|
*
|
||
|
14 years ago
|
* All transformations (except absoluteTransformation()) are relative
|
||
|
|
* to parent.
|
||
|
16 years ago
|
*/
|
||
|
14 years ago
|
|
||
|
14 years ago
|
/** @brief Transformation type */
|
||
|
|
enum class Transformation: char {
|
||
|
|
/** Global transformation, applied after all other transformations. */
|
||
|
|
Global = 0x00,
|
||
|
|
|
||
|
|
/** Local transformation, applied before all other transformations. */
|
||
|
|
Local = 0x01
|
||
|
|
};
|
||
|
|
|
||
|
14 years ago
|
/** @brief Transformation */
|
||
|
14 years ago
|
inline Matrix4 transformation() const {
|
||
|
|
return _transformation;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
14 years ago
|
* @brief Absolute transformation
|
||
|
14 years ago
|
*
|
||
|
14 years ago
|
* Returns absolute transformation matrix relative to the camera or
|
||
|
|
* root object, if no camera is specified. If the camera is specified,
|
||
|
|
* it should be part of the same scene as object.
|
||
|
14 years ago
|
*
|
||
|
|
* Note that the absolute transformation is computed from all parent
|
||
|
|
* objects every time it is asked, unless this function is
|
||
|
|
* reimplemented in a different way.
|
||
|
14 years ago
|
*/
|
||
|
|
virtual Matrix4 absoluteTransformation(Camera* camera = nullptr);
|
||
|
16 years ago
|
|
||
|
14 years ago
|
/** @brief Set transformation */
|
||
|
14 years ago
|
Object* setTransformation(const Matrix4& transformation);
|
||
|
16 years ago
|
|
||
|
|
/**
|
||
|
14 years ago
|
* @brief Multiply transformation
|
||
|
|
* @param transformation Transformation
|
||
|
14 years ago
|
* @param type Transformation type
|
||
|
16 years ago
|
*/
|
||
|
14 years ago
|
inline Object* multiplyTransformation(const Matrix4& transformation, Transformation type = Transformation::Global) {
|
||
|
|
setTransformation(type == Transformation::Global ?
|
||
|
|
transformation*_transformation : _transformation*transformation);
|
||
|
14 years ago
|
return this;
|
||
|
16 years ago
|
}
|
||
|
16 years ago
|
|
||
|
|
/**
|
||
|
|
* @brief Translate object
|
||
|
|
*
|
||
|
|
* Same as calling multiplyTransformation() with Matrix4::translation().
|
||
|
|
*/
|
||
|
14 years ago
|
inline Object* translate(Vector3 vec, Transformation type = Transformation::Global) {
|
||
|
|
multiplyTransformation(Matrix4::translation(vec), type);
|
||
|
14 years ago
|
return this;
|
||
|
16 years ago
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @brief Scale object
|
||
|
|
*
|
||
|
|
* Same as calling multiplyTransformation() with Matrix4::scaling().
|
||
|
|
*/
|
||
|
14 years ago
|
inline Object* scale(Vector3 vec, Transformation type = Transformation::Global) {
|
||
|
|
multiplyTransformation(Matrix4::scaling(vec), type);
|
||
|
14 years ago
|
return this;
|
||
|
16 years ago
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @brief Rotate object
|
||
|
|
*
|
||
|
|
* Same as calling multiplyTransformation() with Matrix4::rotation().
|
||
|
|
*/
|
||
|
14 years ago
|
inline Object* rotate(GLfloat angle, Vector3 vec, Transformation type = Transformation::Global) {
|
||
|
|
multiplyTransformation(Matrix4::rotation(angle, vec), type);
|
||
|
14 years ago
|
return this;
|
||
|
16 years ago
|
}
|
||
|
|
|
||
|
14 years ago
|
/*@}*/
|
||
|
|
|
||
|
14 years ago
|
/**
|
||
|
|
* @brief Draw object
|
||
|
|
* @param transformationMatrix %Matrix specifying object
|
||
|
|
* transformation relative to the scene.
|
||
|
|
* @param camera Active camera (containing
|
||
|
|
* projection matrix)
|
||
|
|
*
|
||
|
|
* Default implementation does nothing.
|
||
|
|
*/
|
||
|
14 years ago
|
virtual void draw(const Matrix4& transformationMatrix, Camera* camera);
|
||
|
14 years ago
|
|
||
|
16 years ago
|
/** @{ @name Caching helpers
|
||
|
|
*
|
||
|
14 years ago
|
* If the object (absolute) transformation or anything depending on it
|
||
|
|
* is used many times when drawing (such as e.g. position of light
|
||
|
|
* object), it's good to cache these values, so they don't have to be
|
||
|
14 years ago
|
* recalculated again on every request.
|
||
|
|
*
|
||
|
|
* If setDirty() is called on an object (or the object is transformed),
|
||
|
|
* it and all its children are marked as dirty. If any object is
|
||
|
|
* already dirty, it and all its children are skipped, because they
|
||
|
|
* are already dirty too.
|
||
|
16 years ago
|
*
|
||
|
14 years ago
|
* If setClean() is called on an object, it and all its parents are
|
||
|
|
* cleaned. If any object is already clean, it and all its parents are
|
||
|
|
* skipped, because they are already clean too.
|
||
|
16 years ago
|
*
|
||
|
|
* These functions are used to manage dirty status of the object. If
|
||
|
14 years ago
|
* the object doesn't cache anything, it's no need to bother about
|
||
|
|
* them, but if does, clean() should be reimplemented and used to
|
||
|
|
* regenerate the cache.
|
||
|
16 years ago
|
*/
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @brief Whether the object is dirty
|
||
|
|
* @return True, if transformation of the object, any parent or camera
|
||
|
|
* has changed since last asking, false otherwise.
|
||
|
|
*/
|
||
|
|
inline bool isDirty() const { return dirty; }
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @brief Set object and all its children as dirty
|
||
|
|
*
|
||
|
14 years ago
|
* Recursively calls setDirty() on every child. If the object is
|
||
|
|
* already marked as dirty, the function does nothing. It is usually
|
||
|
|
* not needed to reimplement this function, only if you for example
|
||
|
|
* need to reset some state on object which is not child of this. All
|
||
|
|
* computations should be done in setClean().
|
||
|
|
*
|
||
|
|
* Reimplementations should call this function at the end, i.e.:
|
||
|
|
* @code
|
||
|
|
* void setDirty() {
|
||
|
|
* // ...
|
||
|
14 years ago
|
*
|
||
|
14 years ago
|
* Object::setDirty();
|
||
|
|
* }
|
||
|
|
* @endcode
|
||
|
16 years ago
|
*/
|
||
|
|
virtual void setDirty();
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @brief Set object and all its parents as clean
|
||
|
|
*
|
||
|
14 years ago
|
* Recursively calls clean() on every parent which is not already
|
||
|
|
* clean.
|
||
|
16 years ago
|
*/
|
||
|
14 years ago
|
void setClean();
|
||
|
16 years ago
|
|
||
|
14 years ago
|
protected:
|
||
|
16 years ago
|
/**
|
||
|
14 years ago
|
* @brief Clean the object
|
||
|
16 years ago
|
*
|
||
|
14 years ago
|
* When reimplementing, use absolute transformation passed as
|
||
|
|
* parameter instead of absoluteTransformation(), which is not
|
||
|
|
* efficient. The reimplementation should call this function at the
|
||
|
|
* beginning, i.e.:
|
||
|
|
* @code
|
||
|
|
* void clean(const Matrix4& absoluteTransformation) {
|
||
|
|
* Object::clean(absoluteTransformation);
|
||
|
|
*
|
||
|
|
* // ...
|
||
|
|
* }
|
||
|
|
* @endcode
|
||
|
16 years ago
|
*/
|
||
|
14 years ago
|
virtual void clean(const Matrix4& absoluteTransformation);
|
||
|
14 years ago
|
|
||
|
|
/*@}*/
|
||
|
16 years ago
|
|
||
|
|
private:
|
||
|
16 years ago
|
Object* _parent;
|
||
|
|
std::set<Object*> _children;
|
||
|
16 years ago
|
Matrix4 _transformation;
|
||
|
16 years ago
|
bool dirty;
|
||
|
16 years ago
|
};
|
||
|
|
|
||
|
14 years ago
|
/* Implementations for inline functions with unused parameters */
|
||
|
|
inline void Object::draw(const Matrix4&, Camera*) {}
|
||
|
|
inline void Object::clean(const Matrix4&) { dirty = false; }
|
||
|
|
|
||
|
14 years ago
|
}}
|
||
|
16 years ago
|
|
||
|
|
#endif
|