From dfe566886a0ff60bd8bf0851424c6702648ceee3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Sun, 16 Sep 2012 01:17:52 +0200 Subject: [PATCH] SceneGraph: disallow instantiation of Object and Camera base classes. Renamed them to AbstractObject and AbstractCamera to make this more evident to end user. --- src/SceneGraph/Camera.cpp | 28 ++++++++++++------------- src/SceneGraph/Camera.h | 44 +++++++++++++++++++++------------------ src/SceneGraph/Light.cpp | 2 +- src/SceneGraph/Object.cpp | 16 +++++++------- src/SceneGraph/Object.h | 38 +++++++++++++++++---------------- src/SceneGraph/Scene.h | 18 ++++++++-------- 6 files changed, 76 insertions(+), 70 deletions(-) diff --git a/src/SceneGraph/Camera.cpp b/src/SceneGraph/Camera.cpp index 5e5693261..4b9cba14b 100644 --- a/src/SceneGraph/Camera.cpp +++ b/src/SceneGraph/Camera.cpp @@ -45,40 +45,40 @@ template Matrix4 aspectRatioFix(AspectRatioPolicy, const Vector2&, cons } #endif -template Camera::Camera(typename Object::ObjectType* parent): Object::ObjectType(parent), _aspectRatioPolicy(AspectRatioPolicy::NotPreserved) {} +template AbstractCamera::AbstractCamera(typename AbstractObject::ObjectType* parent): AbstractObject::ObjectType(parent), _aspectRatioPolicy(AspectRatioPolicy::NotPreserved) {} -template typename Object::CameraType* Camera::setAspectRatioPolicy(AspectRatioPolicy policy) { +template typename AbstractObject::CameraType* AbstractCamera::setAspectRatioPolicy(AspectRatioPolicy policy) { _aspectRatioPolicy = policy; fixAspectRatio(); - return static_cast::CameraType*>(this); + return static_cast::CameraType*>(this); } -template void Camera::setViewport(const Math::Vector2& size) { +template void AbstractCamera::setViewport(const Math::Vector2& size) { _viewport = size; fixAspectRatio(); } -template void Camera::clean(const typename Object::MatrixType& absoluteTransformation) { - Object::ObjectType::clean(absoluteTransformation); +template void AbstractCamera::clean(const typename AbstractObject::MatrixType& absoluteTransformation) { + AbstractObject::ObjectType::clean(absoluteTransformation); _cameraMatrix = absoluteTransformation.inverted(); } -template void Camera::draw() { - typename Object::SceneType* s = this->scene(); +template void AbstractCamera::draw() { + typename AbstractObject::SceneType* s = this->scene(); CORRADE_ASSERT(s, "Camera: cannot draw without camera attached to scene", ); /* Recursively draw child objects */ drawChildren(s, cameraMatrix()); } -template void Camera::drawChildren(typename Object::ObjectType* object, const typename Object::MatrixType& transformationMatrix) { - for(typename Object::ObjectType* i = object->firstChild(); i; i = i->nextSibling()) { +template void AbstractCamera::drawChildren(typename AbstractObject::ObjectType* object, const typename AbstractObject::MatrixType& transformationMatrix) { + for(typename AbstractObject::ObjectType* i = object->firstChild(); i; i = i->nextSibling()) { /* Transformation matrix for the object */ - typename Object::MatrixType matrix = transformationMatrix*i->transformation(); + typename AbstractObject::MatrixType matrix = transformationMatrix*i->transformation(); /* Draw the object and its children */ - i->draw(matrix, static_cast::CameraType*>(this)); + i->draw(matrix, static_cast::CameraType*>(this)); drawChildren(i, matrix); } } @@ -128,7 +128,7 @@ Camera3D* Camera3D::setPerspective(GLfloat fov, GLfloat near, GLfloat far) { } /* Explicitly instantiate the templates */ -template class Camera<2>; -template class Camera<3>; +template class AbstractCamera<2>; +template class AbstractCamera<3>; }} diff --git a/src/SceneGraph/Camera.h b/src/SceneGraph/Camera.h index d4541ed1e..97507ab21 100644 --- a/src/SceneGraph/Camera.h +++ b/src/SceneGraph/Camera.h @@ -16,7 +16,7 @@ */ /** @file - * @brief Class Magnum::SceneGraph::Camera, Magnum::SceneGraph::Camera2D, Magnum::SceneGraph::Camera3D + * @brief Class Magnum::SceneGraph::AbstractCamera, Magnum::SceneGraph::Camera2D, Magnum::SceneGraph::Camera3D */ #include "Object.h" @@ -49,7 +49,7 @@ namespace Implementation { /** @brief %Camera object */ -template class SCENEGRAPH_EXPORT Camera: public Object::ObjectType { +template class SCENEGRAPH_EXPORT AbstractCamera: public AbstractObject::ObjectType { public: /** * @brief Aspect ratio policy @@ -66,8 +66,10 @@ template class SCENEGRAPH_EXPORT Camera: public Object::ObjectType* parent = nullptr); + /** @copydoc AbstractObject::AbstractObject() */ + AbstractCamera(typename AbstractObject::ObjectType* parent = nullptr); + + virtual ~AbstractCamera() = 0; /** @brief Aspect ratio policy */ inline AspectRatioPolicy aspectRatioPolicy() const { return _aspectRatioPolicy; } @@ -76,7 +78,7 @@ template class SCENEGRAPH_EXPORT Camera: public Object::CameraType* setAspectRatioPolicy(AspectRatioPolicy policy); + typename AbstractObject::CameraType* setAspectRatioPolicy(AspectRatioPolicy policy); /** * @brief Camera matrix @@ -84,7 +86,7 @@ template class SCENEGRAPH_EXPORT Camera: public Object::MatrixType cameraMatrix() { + inline typename AbstractObject::MatrixType cameraMatrix() { this->setClean(); return _cameraMatrix; } @@ -96,7 +98,7 @@ template class SCENEGRAPH_EXPORT Camera: public Object::MatrixType projectionMatrix() const { return _projectionMatrix; } + inline typename AbstractObject::MatrixType projectionMatrix() const { return _projectionMatrix; } /** * @brief Size of (near) XY plane in current projection @@ -127,41 +129,43 @@ template class SCENEGRAPH_EXPORT Camera: public Object::ObjectType::draw; /* Don't hide Object's draw() */ + using AbstractObject::ObjectType::draw; /* Don't hide Object's draw() */ protected: /** * Recalculates camera matrix. */ - void clean(const typename Object::MatrixType& absoluteTransformation); + void clean(const typename AbstractObject::MatrixType& absoluteTransformation); /** * @brief Draw object children * * Recursively draws all children of the object. */ - void drawChildren(typename Object::ObjectType* object, const typename Object::MatrixType& transformationMatrix); + void drawChildren(typename AbstractObject::ObjectType* object, const typename AbstractObject::MatrixType& transformationMatrix); #ifndef DOXYGEN_GENERATING_OUTPUT inline void fixAspectRatio() { - _projectionMatrix = Implementation::aspectRatioFix::MatrixType>(_aspectRatioPolicy, {rawProjectionMatrix[0].x(), rawProjectionMatrix[1].y()}, _viewport)*rawProjectionMatrix; + _projectionMatrix = Implementation::aspectRatioFix::MatrixType>(_aspectRatioPolicy, {rawProjectionMatrix[0].x(), rawProjectionMatrix[1].y()}, _viewport)*rawProjectionMatrix; } - typename Object::MatrixType rawProjectionMatrix; + typename AbstractObject::MatrixType rawProjectionMatrix; AspectRatioPolicy _aspectRatioPolicy; #endif private: - typename Object::MatrixType _projectionMatrix; - typename Object::MatrixType _cameraMatrix; + typename AbstractObject::MatrixType _projectionMatrix; + typename AbstractObject::MatrixType _cameraMatrix; Math::Vector2 _viewport; }; +template inline AbstractCamera::~AbstractCamera() {} + #ifndef DOXYGEN_GENERATING_OUTPUT /* These templates are instantiated in source file */ -extern template class SCENEGRAPH_EXPORT Camera<2>; -extern template class SCENEGRAPH_EXPORT Camera<3>; +extern template class SCENEGRAPH_EXPORT AbstractCamera<2>; +extern template class SCENEGRAPH_EXPORT AbstractCamera<3>; namespace Implementation { template<> class Camera<2> { @@ -180,7 +184,7 @@ namespace Implementation { #endif /** @brief %Camera for two-dimensional scenes */ -class SCENEGRAPH_EXPORT Camera2D: public Camera<2> { +class SCENEGRAPH_EXPORT Camera2D: public AbstractCamera<2> { public: /** * @brief Constructor @@ -189,7 +193,7 @@ class SCENEGRAPH_EXPORT Camera2D: public Camera<2> { * Sets orthographic projection to the default OpenGL cube (range @f$ [-1; 1] @f$ in all directions). * @see setOrthographic() */ - inline Camera2D(Object2D* parent = nullptr): Camera(parent) {} + inline Camera2D(Object2D* parent = nullptr): AbstractCamera(parent) {} /** * @brief Set projection @@ -203,7 +207,7 @@ class SCENEGRAPH_EXPORT Camera2D: public Camera<2> { }; /** @brief %Camera for three-dimensional scenes */ -class SCENEGRAPH_EXPORT Camera3D: public Camera<3> { +class SCENEGRAPH_EXPORT Camera3D: public AbstractCamera<3> { public: /** * @brief Constructor @@ -212,7 +216,7 @@ class SCENEGRAPH_EXPORT Camera3D: public Camera<3> { * Sets orthographic projection to the default OpenGL cube (range @f$ [-1; 1] @f$ in all directions). * @see setOrthographic(), setPerspective() */ - inline Camera3D(Object3D* parent = nullptr): Camera(parent), _near(0.0f), _far(0.0f) {} + inline Camera3D(Object3D* parent = nullptr): AbstractCamera(parent), _near(0.0f), _far(0.0f) {} /** * @brief Set orthographic projection diff --git a/src/SceneGraph/Light.cpp b/src/SceneGraph/Light.cpp index 9b5eb5860..385c6efc4 100644 --- a/src/SceneGraph/Light.cpp +++ b/src/SceneGraph/Light.cpp @@ -18,7 +18,7 @@ namespace Magnum { namespace SceneGraph { void Light::clean(const Matrix4& absoluteTransformation) { - Object::clean(absoluteTransformation); + Object3D::clean(absoluteTransformation); _position = absoluteTransformation[3]; } diff --git a/src/SceneGraph/Object.cpp b/src/SceneGraph/Object.cpp index 5b437a14e..a0a02922a 100644 --- a/src/SceneGraph/Object.cpp +++ b/src/SceneGraph/Object.cpp @@ -25,7 +25,7 @@ using namespace Magnum::Math; namespace Magnum { namespace SceneGraph { -template typename Object::ObjectType* Object::setParent(ObjectType* parent) { +template typename AbstractObject::ObjectType* AbstractObject::setParent(ObjectType* parent) { /* Skip if nothing to do or this is scene */ if(this->parent() == parent || isScene()) return static_cast(this); @@ -49,7 +49,7 @@ template typename Object::ObjectType* Object(this); } -template typename Object::MatrixType Object::absoluteTransformation(CameraType* camera) { +template typename AbstractObject::MatrixType AbstractObject::absoluteTransformation(CameraType* camera) { /* Shortcut for absolute transformation of camera relative to itself */ if(camera == this) return MatrixType(); @@ -77,7 +77,7 @@ template typename Object::MatrixType Object typename Object::SceneType* Object::scene() { +template typename AbstractObject::SceneType* AbstractObject::scene() { /* Goes up the family tree until it finds object which is parent of itself (that's the scene) */ ObjectType* p = parent(); @@ -89,7 +89,7 @@ template typename Object::SceneType* Object typename Object::ObjectType* Object::setTransformation(const MatrixType& transformation) { +template typename AbstractObject::ObjectType* AbstractObject::setTransformation(const MatrixType& transformation) { /* Setting transformation is forbidden for the scene */ /** @todo Assert for this? */ if(isScene()) return static_cast(this); @@ -99,7 +99,7 @@ template typename Object::ObjectType* Object(this); } -template void Object::setDirty() { +template void AbstractObject::setDirty() { /* The object (and all its children) are already dirty, nothing to do */ if(dirty) return; @@ -110,7 +110,7 @@ template void Object::setDirty() { i->setDirty(); } -template void Object::setClean() { +template void AbstractObject::setClean() { /* The object (and all its parents) are already clean, nothing to do */ if(!dirty) return; @@ -141,7 +141,7 @@ template void Object::setClean() { } /* Explicitly instantiate the templates */ -template class Object<2>; -template class Object<3>; +template class AbstractObject<2>; +template class AbstractObject<3>; }} diff --git a/src/SceneGraph/Object.h b/src/SceneGraph/Object.h index 4890ee622..b8422a84e 100644 --- a/src/SceneGraph/Object.h +++ b/src/SceneGraph/Object.h @@ -16,7 +16,7 @@ */ /** @file - * @brief Class Magnum::SceneGraph::Object, Magnum::SceneGraph::Object2D, Magnum::SceneGraph::Object3D + * @brief Class Magnum::SceneGraph::AbstractObject, Magnum::SceneGraph::Object2D, Magnum::SceneGraph::Object3D */ #include @@ -76,12 +76,12 @@ namespace Implementation { * @todo Transform transformation when changing parent, so the object stays in * place. */ -template class SCENEGRAPH_EXPORT Object: public Corrade::Containers::LinkedList::ObjectType>, public Corrade::Containers::LinkedListItem::ObjectType, typename Implementation::ObjectDimensionTraits::ObjectType> { +template class SCENEGRAPH_EXPORT AbstractObject: public Corrade::Containers::LinkedList::ObjectType>, public Corrade::Containers::LinkedListItem::ObjectType, typename Implementation::ObjectDimensionTraits::ObjectType> { #ifndef DOXYGEN_GENERATING_OUTPUT - Object(const Object& other) = delete; - Object(Object&& other) = delete; - Object& operator=(const Object& other) = delete; - Object& operator=(Object&& other) = delete; + AbstractObject(const AbstractObject& other) = delete; + AbstractObject(AbstractObject&& other) = delete; + AbstractObject& operator=(const AbstractObject& other) = delete; + AbstractObject& operator=(AbstractObject&& other) = delete; #endif public: @@ -108,7 +108,7 @@ template class SCENEGRAPH_EXPORT Object: public Corrade::Cont * * Sets all transformations to their default values. */ - inline Object(ObjectType* parent = nullptr): dirty(true) { + inline AbstractObject(ObjectType* parent = nullptr): dirty(true) { setParent(parent); } @@ -118,7 +118,7 @@ template class SCENEGRAPH_EXPORT Object: public Corrade::Cont * Removes itself from parent's children list and destroys all own * children. */ - virtual inline ~Object() {} + virtual ~AbstractObject() = 0; /** @{ @name Scene hierarchy */ @@ -309,21 +309,23 @@ template class SCENEGRAPH_EXPORT Object: public Corrade::Cont bool dirty; }; +template inline AbstractObject::~AbstractObject() {} + /* Implementations for inline functions with unused parameters */ -template inline void Object::draw(const MatrixType&, CameraType*) {} -template inline void Object::clean(const MatrixType&) { dirty = false; } +template inline void AbstractObject::draw(const MatrixType&, CameraType*) {} +template inline void AbstractObject::clean(const MatrixType&) { dirty = false; } #ifndef DOXYGEN_GENERATING_OUTPUT /* These templates are instantiated in source file */ -extern template class SCENEGRAPH_EXPORT Object<2>; -extern template class SCENEGRAPH_EXPORT Object<3>; +extern template class SCENEGRAPH_EXPORT AbstractObject<2>; +extern template class SCENEGRAPH_EXPORT AbstractObject<3>; #endif /** @brief Two-dimensional object */ -class SCENEGRAPH_EXPORT Object2D: public Object<2> { +class SCENEGRAPH_EXPORT Object2D: public AbstractObject<2> { public: - /** @copydoc Object::Object */ - inline Object2D(Object2D* parent = nullptr): Object(parent) {} + /** @copydoc AbstractObject::AbstractObject() */ + inline Object2D(Object2D* parent = nullptr): AbstractObject(parent) {} /** * @brief Translate object @@ -371,10 +373,10 @@ class SCENEGRAPH_EXPORT Object2D: public Object<2> { }; /** @brief Three-dimensional object */ -class SCENEGRAPH_EXPORT Object3D: public Object<3> { +class SCENEGRAPH_EXPORT Object3D: public AbstractObject<3> { public: - /** @copydoc Object::Object */ - inline Object3D(Object3D* parent = nullptr): Object(parent) {} + /** @copydoc AbstractObject::AbstractObject() */ + inline Object3D(Object3D* parent = nullptr): AbstractObject(parent) {} /** * @brief Translate object diff --git a/src/SceneGraph/Scene.h b/src/SceneGraph/Scene.h index 9874f40c4..f3cfde551 100644 --- a/src/SceneGraph/Scene.h +++ b/src/SceneGraph/Scene.h @@ -24,23 +24,23 @@ namespace Magnum { namespace SceneGraph { /** @brief %Scene */ -template class SCENEGRAPH_EXPORT Scene: public Object::ObjectType { +template class SCENEGRAPH_EXPORT Scene: public AbstractObject::ObjectType { public: - /** @copydoc Object::isScene() */ + /** @copydoc AbstractObject::isScene() */ inline bool isScene() const { return true; } /** @todo Some deleted functions belong only to Scene2D, some only to Scene3D - what to do? */ #ifndef DOXYGEN_GENERATING_OUTPUT - void setParent(typename Object::ObjectType* parent) = delete; - void setTransformation(const typename Object::MatrixType& transformation) = delete; - void multiplyTransformation(const typename Object::MatrixType& transformation, typename Object::Transformation type = Object::Transformation::Global) = delete; - void translate(const typename Object::VectorType& vec, typename Object::Transformation type = Object::Transformation::Global) = delete; - void scale(const typename Object::VectorType& vec, typename Object::Transformation type = Object::Transformation::Global) = delete; - void rotate(GLfloat angle, const typename Object::VectorType& vec, typename Object::Transformation type = Object::Transformation::Global) = delete; + void setParent(typename AbstractObject::ObjectType* parent) = delete; + void setTransformation(const typename AbstractObject::MatrixType& transformation) = delete; + void multiplyTransformation(const typename AbstractObject::MatrixType& transformation, typename AbstractObject::Transformation type = AbstractObject::Transformation::Global) = delete; + void translate(const typename AbstractObject::VectorType& vec, typename AbstractObject::Transformation type = AbstractObject::Transformation::Global) = delete; + void scale(const typename AbstractObject::VectorType& vec, typename AbstractObject::Transformation type = AbstractObject::Transformation::Global) = delete; + void rotate(GLfloat angle, const typename AbstractObject::VectorType& vec, typename AbstractObject::Transformation type = AbstractObject::Transformation::Global) = delete; #endif private: - inline void draw(const typename Object::MatrixType&, typename Object::CameraType*) {} + inline void draw(const typename AbstractObject::MatrixType&, typename AbstractObject::CameraType*) {} }; /** @brief Two-dimensional scene */