diff --git a/src/SceneGraph/Camera.h b/src/SceneGraph/AbstractCamera.h similarity index 60% rename from src/SceneGraph/Camera.h rename to src/SceneGraph/AbstractCamera.h index 96056e300..48c7890d2 100644 --- a/src/SceneGraph/Camera.h +++ b/src/SceneGraph/AbstractCamera.h @@ -1,5 +1,5 @@ -#ifndef Magnum_SceneGraph_Camera_h -#define Magnum_SceneGraph_Camera_h +#ifndef Magnum_SceneGraph_AbstractCamera_h +#define Magnum_SceneGraph_AbstractCamera_h /* Copyright © 2010, 2011, 2012 Vladimír Vondruš @@ -16,7 +16,7 @@ */ /** @file - * @brief Class Magnum::SceneGraph::AbstractCamera, Magnum::SceneGraph::Camera2D, Magnum::SceneGraph::Camera3D, enum AspectRatioPolicy, alias Magnum::SceneGraph::AbstractCamera2D, Magnum::SceneGraph::AbstractCamera3D + * @brief Class Magnum::SceneGraph::AbstractCamera, enum AspectRatioPolicy, alias Magnum::SceneGraph::AbstractCamera2D, Magnum::SceneGraph::AbstractCamera3D */ #include "Math/Matrix3.h" @@ -25,11 +25,6 @@ #include "magnumSceneGraphVisibility.h" -#ifdef WIN32 /* I so HATE windows.h */ -#undef near -#undef far -#endif - namespace Magnum { namespace SceneGraph { template class Drawable; @@ -66,14 +61,16 @@ instantiatable, use Camera2D or Camera3D subclasses instead. @section AbstractCamera-explicit-specializations Explicit template specializations The following specialization are explicitly compiled into SceneGraph library. -For other specializations you have to use Camera.hpp implementation file to -avoid linker errors. See @ref compilation-speedup-hpp for more information. +For other specializations you have to use AbstractCamera.hpp implementation +file to avoid linker errors. See also relevant sections in +@ref Camera2D-explicit-specializations "Camera2D" and +@ref Camera3D-explicit-specializations "Camera3D" class documentation or +@ref compilation-speedup-hpp for more information. - @ref AbstractCamera "AbstractCamera<2>" - @ref AbstractCamera "AbstractCamera<3>" -@see Camera2D, Camera3D, Drawable, DrawableGroup, AbstractCamera2D, - AbstractCamera3D +@see Drawable, DrawableGroup, AbstractCamera2D, AbstractCamera3D */ template class SCENEGRAPH_EXPORT AbstractCamera: public AbstractFeature { public: @@ -203,118 +200,6 @@ template using AbstractCamera3D = AbstractCamera<3, T>; typedef AbstractCamera<3, T = GLfloat> AbstractCamera3D; #endif -/** -@brief Camera for two-dimensional scenes - -See Drawable documentation for more information. - -@section Object-explicit-specializations Explicit template specializations - -The following specialization are explicitly compiled into SceneGraph library. -For other specializations you have to use Camera.hpp implementation file to -avoid linker errors. See @ref compilation-speedup-hpp for more information. - - - @ref Camera2D "Camera2D" - -@see Camera3D, Drawable, DrawableGroup -*/ -template class SCENEGRAPH_EXPORT Camera2D: public AbstractCamera<2, T> { - public: - /** - * @brief Constructor - * @param object %Object holding this feature - * - * Sets orthographic projection to the default OpenGL cube (range @f$ [-1; 1] @f$ in all directions). - * @see setOrthographic() - */ - inline Camera2D(AbstractObject<2, T>* object): AbstractCamera<2, T>(object) {} - - /** - * @brief Set projection - * @param size Size of the view - * @return Pointer to self (for method chaining) - * - * The area of given size will be scaled down to range @f$ [-1; 1] @f$ - * on all directions. - */ - Camera2D* setProjection(const Math::Vector2& size); - - /* Overloads to remove WTF-factor from method chaining order */ - #ifndef DOXYGEN_GENERATING_OUTPUT - inline Camera2D* setAspectRatioPolicy(AspectRatioPolicy policy) { - AbstractCamera<2, T>::setAspectRatioPolicy(policy); - return this; - } - #endif -}; - -/** -@brief Camera for three-dimensional scenes - -See Drawable documentation for more information. - -@section Object-explicit-specializations Explicit template specializations - -The following specialization are explicitly compiled into SceneGraph library. -For other specializations you have to use Camera.hpp implementation file to -avoid linker errors. See @ref compilation-speedup-hpp for more information. - - - @ref Camera3D "Camera3D" - -@see Camera2D, Drawable, DrawableGroup -*/ -template class SCENEGRAPH_EXPORT Camera3D: public AbstractCamera<3, T> { - public: - /** - * @brief Constructor - * @param object %Object holding this feature - * - * Sets orthographic projection to the default OpenGL cube (range @f$ [-1; 1] @f$ in all directions). - * @see setOrthographic(), setPerspective() - */ - inline Camera3D(AbstractObject<3, T>* object): AbstractCamera<3, T>(object), _near(0.0f), _far(0.0f) {} - - /** - * @brief Set orthographic projection - * @param size Size of the view - * @param near Near clipping plane - * @param far Far clipping plane - * @return Pointer to self (for method chaining) - * - * The volume of given size will be scaled down to range @f$ [-1; 1] @f$ - * on all directions. - */ - Camera3D* setOrthographic(const Math::Vector2& size, T near, T far); - - /** - * @brief Set perspective projection - * @param fov Field of view angle - * @param near Near clipping plane - * @param far Far clipping plane - * @return Pointer to self (for method chaining) - * - * @todo Aspect ratio - */ - Camera3D* setPerspective(T fov, T near, T far); - - /** @brief Near clipping plane */ - inline T near() const { return _near; } - - /** @brief Far clipping plane */ - inline T far() const { return _far; } - - /* Overloads to remove WTF-factor from method chaining order */ - #ifndef DOXYGEN_GENERATING_OUTPUT - inline Camera3D* setAspectRatioPolicy(AspectRatioPolicy policy) { - AbstractCamera<3, T>::setAspectRatioPolicy(policy); - return this; - } - #endif - - private: - T _near, _far; -}; - /* Make implementers' life easier */ #ifndef MAGNUM_GCC46_COMPATIBILITY template using DrawableGroup2D = DrawableGroup<2, T>; diff --git a/src/SceneGraph/Camera.hpp b/src/SceneGraph/AbstractCamera.hpp similarity index 68% rename from src/SceneGraph/Camera.hpp rename to src/SceneGraph/AbstractCamera.hpp index 2d1e7a42a..bbd6166e1 100644 --- a/src/SceneGraph/Camera.hpp +++ b/src/SceneGraph/AbstractCamera.hpp @@ -1,5 +1,5 @@ -#ifndef Magnum_SceneGraph_Camera_hpp -#define Magnum_SceneGraph_Camera_hpp +#ifndef Magnum_SceneGraph_AbstractCamera_hpp +#define Magnum_SceneGraph_AbstractCamera_hpp /* Copyright © 2010, 2011, 2012 Vladimír Vondruš @@ -16,15 +16,12 @@ */ /** @file - * @brief @ref compilation-speedup-hpp "Template implementation" for Camera.h + * @brief @ref compilation-speedup-hpp "Template implementation" for AbstractCamera.h */ -#include "Camera.h" - -#include +#include "AbstractCamera.h" #include "Drawable.h" -#include "Scene.h" using namespace std; @@ -96,50 +93,6 @@ template void AbstractCamera::d group[i]->draw(transformations[i], this); } -template Camera2D* Camera2D::setProjection(const Math::Vector2& size) { - /* Scale the volume down so it fits in (-1, 1) in all directions */ - AbstractCamera<2, T>::rawProjectionMatrix = Math::Matrix3::scaling(2.0f/size); - - AbstractCamera<2, T>::fixAspectRatio(); - return this; -} - -template Camera3D* Camera3D::setOrthographic(const Math::Vector2& size, T near, T far) { - _near = near; - _far = far; - - Math::Vector2 xyScale = T(2.0)/size; - T zScale = T(2.0)/(near-far); - - AbstractCamera<3, T>::rawProjectionMatrix = Math::Matrix4( - xyScale.x(), T(0.0), T(0.0), T(0.0), - T(0.0), xyScale.y(), T(0.0), T(0.0), - T(0.0), T(0.0), zScale, T(0.0), - T(0.0), T(0.0), near*zScale-1, T(1.0) - ); - - AbstractCamera<3, T>::fixAspectRatio(); - return this; -} - -template Camera3D* Camera3D::setPerspective(T fov, T near, T far) { - _near = near; - _far = far; - - T xyScale = T(1.0)/tan(fov/2); /* == near/size */ - T zScale = T(1.0)/(near-far); - - AbstractCamera<3, T>::rawProjectionMatrix = Matrix4( - xyScale, T(0.0), T(0.0), T(0.0), - T(0.0), xyScale, T(0.0), T(0.0), - T(0.0), T(0.0), (far+near)*zScale, T(-1.0), - T(0.0), T(0.0), (2*far*near)*zScale, T(0.0) - ); - - AbstractCamera<3, T>::fixAspectRatio(); - return this; -} - }} #endif diff --git a/src/SceneGraph/CMakeLists.txt b/src/SceneGraph/CMakeLists.txt index 2268b0dd5..b9a271d03 100644 --- a/src/SceneGraph/CMakeLists.txt +++ b/src/SceneGraph/CMakeLists.txt @@ -1,6 +1,8 @@ set(MagnumSceneGraph_SRCS Camera.cpp) set(MagnumSceneGraph_HEADERS + AbstractCamera.h + AbstractCamera.hpp AbstractFeature.h AbstractGroupedFeature.h AbstractObject.h @@ -9,8 +11,10 @@ set(MagnumSceneGraph_HEADERS AbstractTranslationRotation3D.h AbstractTranslationRotationScaling2D.h AbstractTranslationRotationScaling3D.h - Camera.h - Camera.hpp + Camera2D.h + Camera2D.hpp + Camera3D.h + Camera3D.hpp Drawable.h MatrixTransformation2D.h MatrixTransformation3D.h diff --git a/src/SceneGraph/Camera.cpp b/src/SceneGraph/Camera.cpp index f76b3a6d1..dbb8e1081 100644 --- a/src/SceneGraph/Camera.cpp +++ b/src/SceneGraph/Camera.cpp @@ -13,9 +13,8 @@ GNU Lesser General Public License version 3 for more details. */ -#include "Camera.h" - -#include "Camera.hpp" +#include "Camera2D.hpp" +#include "Camera3D.hpp" namespace Magnum { namespace SceneGraph { diff --git a/src/SceneGraph/Camera2D.h b/src/SceneGraph/Camera2D.h new file mode 100644 index 000000000..9f9845797 --- /dev/null +++ b/src/SceneGraph/Camera2D.h @@ -0,0 +1,73 @@ +#ifndef Magnum_SceneGraph_Camera2D_h +#define Magnum_SceneGraph_Camera2D_h +/* + Copyright © 2010, 2011, 2012 Vladimír Vondruš + + 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::SceneGraph::Camera2D + */ + +#include "AbstractCamera.h" + +namespace Magnum { namespace SceneGraph { + +/** +@brief Camera for two-dimensional scenes + +See Drawable documentation for more information. + +@section Camera2D-explicit-specializations Explicit template specializations + +The following specialization are explicitly compiled into SceneGraph library. +For other specializations you have to use Camera2D.hpp implementation file to +avoid linker errors. See @ref compilation-speedup-hpp for more information. + + - @ref Camera2D "Camera2D" + +@see Camera3D, Drawable, DrawableGroup +*/ +template class SCENEGRAPH_EXPORT Camera2D: public AbstractCamera<2, T> { + public: + /** + * @brief Constructor + * @param object %Object holding this feature + * + * Sets orthographic projection to the default OpenGL cube (range @f$ [-1; 1] @f$ in all directions). + * @see setOrthographic() + */ + inline Camera2D(AbstractObject<2, T>* object): AbstractCamera<2, T>(object) {} + + /** + * @brief Set projection + * @param size Size of the view + * @return Pointer to self (for method chaining) + * + * The area of given size will be scaled down to range @f$ [-1; 1] @f$ + * on all directions. + */ + Camera2D* setProjection(const Math::Vector2& size); + + /* Overloads to remove WTF-factor from method chaining order */ + #ifndef DOXYGEN_GENERATING_OUTPUT + inline Camera2D* setAspectRatioPolicy(AspectRatioPolicy policy) { + AbstractCamera<2, T>::setAspectRatioPolicy(policy); + return this; + } + #endif +}; + +}} + +#endif diff --git a/src/SceneGraph/Camera2D.hpp b/src/SceneGraph/Camera2D.hpp new file mode 100644 index 000000000..818e5ea19 --- /dev/null +++ b/src/SceneGraph/Camera2D.hpp @@ -0,0 +1,39 @@ +#ifndef Magnum_SceneGraph_Camera2D_hpp +#define Magnum_SceneGraph_Camera2D_hpp +/* + Copyright © 2010, 2011, 2012 Vladimír Vondruš + + 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 @ref compilation-speedup-hpp "Template implementation" for Camera2D.h + */ + +#include "AbstractCamera.hpp" +#include "Camera2D.h" + +using namespace std; + +namespace Magnum { namespace SceneGraph { + +template Camera2D* Camera2D::setProjection(const Math::Vector2& size) { + /* Scale the volume down so it fits in (-1, 1) in all directions */ + AbstractCamera<2, T>::rawProjectionMatrix = Math::Matrix3::scaling(2.0f/size); + + AbstractCamera<2, T>::fixAspectRatio(); + return this; +} + +}} + +#endif diff --git a/src/SceneGraph/Camera3D.h b/src/SceneGraph/Camera3D.h new file mode 100644 index 000000000..c34e8ecb2 --- /dev/null +++ b/src/SceneGraph/Camera3D.h @@ -0,0 +1,100 @@ +#ifndef Magnum_SceneGraph_Camera3D_h +#define Magnum_SceneGraph_Camera3D_h +/* + Copyright © 2010, 2011, 2012 Vladimír Vondruš + + 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::SceneGraph::Camera3D + */ + +#include "AbstractCamera.h" + +#ifdef WIN32 /* I so HATE windows.h */ +#undef near +#undef far +#endif + +namespace Magnum { namespace SceneGraph { + +/** +@brief Camera for three-dimensional scenes + +See Drawable documentation for more information. + +@section Camera3D-explicit-specializations Explicit template specializations + +The following specialization are explicitly compiled into SceneGraph library. +For other specializations you have to use Camera3D.hpp implementation file to +avoid linker errors. See @ref compilation-speedup-hpp for more information. + + - @ref Camera3D "Camera3D" + +@see Camera2D, Drawable, DrawableGroup +*/ +template class SCENEGRAPH_EXPORT Camera3D: public AbstractCamera<3, T> { + public: + /** + * @brief Constructor + * @param object %Object holding this feature + * + * Sets orthographic projection to the default OpenGL cube (range @f$ [-1; 1] @f$ in all directions). + * @see setOrthographic(), setPerspective() + */ + inline Camera3D(AbstractObject<3, T>* object): AbstractCamera<3, T>(object), _near(0.0f), _far(0.0f) {} + + /** + * @brief Set orthographic projection + * @param size Size of the view + * @param near Near clipping plane + * @param far Far clipping plane + * @return Pointer to self (for method chaining) + * + * The volume of given size will be scaled down to range @f$ [-1; 1] @f$ + * on all directions. + */ + Camera3D* setOrthographic(const Math::Vector2& size, T near, T far); + + /** + * @brief Set perspective projection + * @param fov Field of view angle + * @param near Near clipping plane + * @param far Far clipping plane + * @return Pointer to self (for method chaining) + * + * @todo Aspect ratio + */ + Camera3D* setPerspective(T fov, T near, T far); + + /** @brief Near clipping plane */ + inline T near() const { return _near; } + + /** @brief Far clipping plane */ + inline T far() const { return _far; } + + /* Overloads to remove WTF-factor from method chaining order */ + #ifndef DOXYGEN_GENERATING_OUTPUT + inline Camera3D* setAspectRatioPolicy(AspectRatioPolicy policy) { + AbstractCamera<3, T>::setAspectRatioPolicy(policy); + return this; + } + #endif + + private: + T _near, _far; +}; + +}} + +#endif diff --git a/src/SceneGraph/Camera3D.hpp b/src/SceneGraph/Camera3D.hpp new file mode 100644 index 000000000..23bee2e44 --- /dev/null +++ b/src/SceneGraph/Camera3D.hpp @@ -0,0 +1,67 @@ +#ifndef Magnum_SceneGraph_Camera3D_hpp +#define Magnum_SceneGraph_Camera3D_hpp +/* + Copyright © 2010, 2011, 2012 Vladimír Vondruš + + 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 @ref compilation-speedup-hpp "Template implementation" for Camera3D.h + */ + +#include "AbstractCamera.hpp" +#include "Camera3D.h" + +using namespace std; + +namespace Magnum { namespace SceneGraph { + +template Camera3D* Camera3D::setOrthographic(const Math::Vector2& size, T near, T far) { + _near = near; + _far = far; + + Math::Vector2 xyScale = T(2.0)/size; + T zScale = T(2.0)/(near-far); + + AbstractCamera<3, T>::rawProjectionMatrix = Math::Matrix4( + xyScale.x(), T(0.0), T(0.0), T(0.0), + T(0.0), xyScale.y(), T(0.0), T(0.0), + T(0.0), T(0.0), zScale, T(0.0), + T(0.0), T(0.0), near*zScale-1, T(1.0) + ); + + AbstractCamera<3, T>::fixAspectRatio(); + return this; +} + +template Camera3D* Camera3D::setPerspective(T fov, T near, T far) { + _near = near; + _far = far; + + T xyScale = T(1.0)/tan(fov/2); /* == near/size */ + T zScale = T(1.0)/(near-far); + + AbstractCamera<3, T>::rawProjectionMatrix = Matrix4( + xyScale, T(0.0), T(0.0), T(0.0), + T(0.0), xyScale, T(0.0), T(0.0), + T(0.0), T(0.0), (far+near)*zScale, T(-1.0), + T(0.0), T(0.0), (2*far*near)*zScale, T(0.0) + ); + + AbstractCamera<3, T>::fixAspectRatio(); + return this; +} + +}} + +#endif diff --git a/src/SceneGraph/Test/CameraTest.cpp b/src/SceneGraph/Test/CameraTest.cpp index fa33df768..c3396e458 100644 --- a/src/SceneGraph/Test/CameraTest.cpp +++ b/src/SceneGraph/Test/CameraTest.cpp @@ -16,11 +16,13 @@ #include "CameraTest.h" #include "Math/Constants.h" -#include "SceneGraph/Camera.h" -#include "SceneGraph/Camera.hpp" /* only for aspectRatioFix(), so it doesn't have to be exported */ +#include "SceneGraph/AbstractCamera.hpp" /* only for aspectRatioFix(), so it doesn't have to be exported */ +#include "SceneGraph/Camera2D.h" +#include "SceneGraph/Camera3D.h" #include "SceneGraph/Drawable.h" #include "SceneGraph/MatrixTransformation2D.h" #include "SceneGraph/MatrixTransformation3D.h" +#include "SceneGraph/Scene.h" CORRADE_TEST_MAIN(Magnum::SceneGraph::Test::CameraTest)