From d43e18a741f07cdc6e68b475b9a2dbaede108799 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Thu, 16 Aug 2012 15:39:01 +0200 Subject: [PATCH] Initial support for 2D scene graph. --- src/SceneGraph/Camera.cpp | 10 ++++++++++ src/SceneGraph/Camera.h | 29 +++++++++++++++++++++++++++ src/SceneGraph/Object.cpp | 1 + src/SceneGraph/Object.h | 41 +++++++++++++++++++++++++++++++++++++++ src/SceneGraph/Scene.h | 3 +++ 5 files changed, 84 insertions(+) diff --git a/src/SceneGraph/Camera.cpp b/src/SceneGraph/Camera.cpp index 5726e9596..5ab0c81b8 100644 --- a/src/SceneGraph/Camera.cpp +++ b/src/SceneGraph/Camera.cpp @@ -40,6 +40,7 @@ template MatrixType aspectRatioFix(AspectRatioPolicy aspectRat } /* Explicitly instantiate the templates */ +template Matrix3 aspectRatioFix(AspectRatioPolicy, const Vector2&, const Math::Vector2&); template Matrix4 aspectRatioFix(AspectRatioPolicy, const Vector2&, const Math::Vector2&); } @@ -81,6 +82,14 @@ template; template class Camera; }} diff --git a/src/SceneGraph/Camera.h b/src/SceneGraph/Camera.h index 36a4c8d06..cf6ba0c4b 100644 --- a/src/SceneGraph/Camera.h +++ b/src/SceneGraph/Camera.h @@ -147,9 +147,16 @@ template; extern template class SCENEGRAPH_EXPORT Camera; namespace Implementation { + template<> class Camera<2> { + public: + inline constexpr static Matrix3 aspectRatioScale(const Vector2& scale) { + return Matrix3::scaling({scale.x(), scale.y()}); + } + }; template<> class Camera<3> { public: inline constexpr static Matrix4 aspectRatioScale(const Vector2& scale) { @@ -159,6 +166,28 @@ namespace Implementation { } #endif +/** @brief %Camera for two-dimensional scenes */ +class SCENEGRAPH_EXPORT Camera2D: public Camera { + public: + /** + * @brief Constructor + * @param parent Parent object + * + * 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) {} + + /** + * @brief Set projection + * @param size Size of the view + * + * The area of given size will be scaled down to range @f$ [-1; 1] @f$ + * on all directions. + */ + void setProjection(const Vector2& size); +}; + /** @brief %Camera for three-dimensional scenes */ class SCENEGRAPH_EXPORT Camera3D: public Camera { public: diff --git a/src/SceneGraph/Object.cpp b/src/SceneGraph/Object.cpp index 386cf86e9..f74602bb1 100644 --- a/src/SceneGraph/Object.cpp +++ b/src/SceneGraph/Object.cpp @@ -152,6 +152,7 @@ template; template class Object; }} diff --git a/src/SceneGraph/Object.h b/src/SceneGraph/Object.h index b86f48d20..691baf688 100644 --- a/src/SceneGraph/Object.h +++ b/src/SceneGraph/Object.h @@ -241,15 +241,56 @@ template inline void Object::draw(const MatrixType&, CameraType*) {} template inline void Object::clean(const MatrixType&) { dirty = false; } +class Camera2D; class Camera3D; +class Object2D; class Object3D; +typedef Scene Scene2D; typedef Scene Scene3D; #ifndef DOXYGEN_GENERATING_OUTPUT /* These templates are instantiated in source file */ +extern template class SCENEGRAPH_EXPORT Object; extern template class SCENEGRAPH_EXPORT Object; #endif +/** @brief Two-dimensional object */ +class SCENEGRAPH_EXPORT Object2D: public Object { + public: + /** @copydoc Object::Object */ + inline Object2D(Object2D* parent = nullptr): Object(parent) {} + + /** + * @brief Translate object + * + * Same as calling multiplyTransformation() with Matrix3::translation(). + */ + inline Object2D* translate(const Vector2& vec, Transformation type = Transformation::Global) { + multiplyTransformation(Matrix3::translation(vec), type); + return this; + } + + /** + * @brief Scale object + * + * Same as calling multiplyTransformation() with Matrix3::scaling(). + */ + inline Object2D* scale(const Vector2& vec, Transformation type = Transformation::Global) { + multiplyTransformation(Matrix3::scaling(vec), type); + return this; + } + + /** + * @brief Rotate object + * + * Same as calling multiplyTransformation() with Matrix3::rotation(). + */ + inline Object2D* rotate(GLfloat angle, Transformation type = Transformation::Global) { + multiplyTransformation(Matrix3::rotation(angle), type); + return this; + } +}; + /** @brief Three-dimensional object */ class SCENEGRAPH_EXPORT Object3D: public Object { public: diff --git a/src/SceneGraph/Scene.h b/src/SceneGraph/Scene.h index 35b36e501..73e3793f0 100644 --- a/src/SceneGraph/Scene.h +++ b/src/SceneGraph/Scene.h @@ -42,6 +42,9 @@ template inline void draw(const MatrixType&, CameraType*) {} }; +/** @brief Two-dimensional scene */ +typedef Scene Scene2D; + /** @brief Three-dimensional scene */ typedef Scene Scene3D;