mirror of https://github.com/mosra/magnum.git
Browse Source
* Camera is now templated also on underlying floating-point type. * Drawable objects can be split into groups (e.g. for separated rendering of transparent objects) * Added (long time missing) test for draw() function.pull/7/head
8 changed files with 571 additions and 201 deletions
@ -0,0 +1,145 @@ |
|||||||
|
#ifndef Magnum_SceneGraph_Camera_hpp |
||||||
|
#define Magnum_SceneGraph_Camera_hpp |
||||||
|
/*
|
||||||
|
Copyright © 2010, 2011, 2012 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 @ref compilation-speedup-hpp "Template implementation" for Camera.h |
||||||
|
*/ |
||||||
|
|
||||||
|
#include "Camera.h" |
||||||
|
|
||||||
|
#include <algorithm> |
||||||
|
|
||||||
|
#include "Drawable.h" |
||||||
|
#include "Scene.h" |
||||||
|
|
||||||
|
using namespace std; |
||||||
|
|
||||||
|
namespace Magnum { namespace SceneGraph { |
||||||
|
|
||||||
|
#ifndef DOXYGEN_GENERATING_OUTPUT |
||||||
|
namespace Implementation { |
||||||
|
|
||||||
|
template<std::uint8_t dimensions, class T> class Camera {}; |
||||||
|
|
||||||
|
template<class T> class Camera<2, T> { |
||||||
|
public: |
||||||
|
inline constexpr static Math::Matrix3<T> aspectRatioScale(const Math::Vector2<T>& scale) { |
||||||
|
return Math::Matrix3<T>::scaling({scale.x(), scale.y()}); |
||||||
|
} |
||||||
|
}; |
||||||
|
template<class T> class Camera<3, T> { |
||||||
|
public: |
||||||
|
inline constexpr static Math::Matrix4<T> aspectRatioScale(const Math::Vector2<T>& scale) { |
||||||
|
return Math::Matrix4<T>::scaling({scale.x(), scale.y(), 1.0f}); |
||||||
|
} |
||||||
|
}; |
||||||
|
|
||||||
|
template<std::uint8_t dimensions, class T> typename DimensionTraits<dimensions, T>::MatrixType aspectRatioFix(AspectRatioPolicy aspectRatioPolicy, const Math::Vector2<T>& projectionScale, const Math::Vector2<GLsizei>& viewport) { |
||||||
|
/* Don't divide by zero / don't preserve anything */ |
||||||
|
if(projectionScale.x() == 0 || projectionScale.y() == 0 || viewport.x() == 0 || viewport.y() == 0 || aspectRatioPolicy == AspectRatioPolicy::NotPreserved) |
||||||
|
return {}; |
||||||
|
|
||||||
|
Math::Vector2<T> relativeAspectRatio = Math::Vector2<T>::from(viewport)*projectionScale; |
||||||
|
|
||||||
|
/* Extend on larger side = scale larger side down
|
||||||
|
Clip on smaller side = scale smaller side up */ |
||||||
|
return Camera<dimensions, T>::aspectRatioScale( |
||||||
|
(relativeAspectRatio.x() > relativeAspectRatio.y()) == (aspectRatioPolicy == AspectRatioPolicy::Extend) ? |
||||||
|
Vector2(relativeAspectRatio.y()/relativeAspectRatio.x(), T(1.0)) : |
||||||
|
Vector2(T(1.0), relativeAspectRatio.x()/relativeAspectRatio.y())); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
#endif |
||||||
|
|
||||||
|
template<std::uint8_t dimensions, class T> AbstractCamera<dimensions, T>* AbstractCamera<dimensions, T>::setAspectRatioPolicy(AspectRatioPolicy policy) { |
||||||
|
_aspectRatioPolicy = policy; |
||||||
|
fixAspectRatio(); |
||||||
|
return this; |
||||||
|
} |
||||||
|
|
||||||
|
template<std::uint8_t dimensions, class T> void AbstractCamera<dimensions, T>::setViewport(const Math::Vector2<GLsizei>& size) { |
||||||
|
_viewport = size; |
||||||
|
fixAspectRatio(); |
||||||
|
} |
||||||
|
|
||||||
|
template<std::uint8_t dimensions, class T> void AbstractCamera<dimensions, T>::draw(DrawableGroup<dimensions, T>& group) { |
||||||
|
AbstractObject<dimensions, T>* scene = AbstractFeature<dimensions, T>::object()->sceneObject(); |
||||||
|
CORRADE_ASSERT(scene, "Camera::draw(): cannot draw when camera is not part of any scene", ); |
||||||
|
|
||||||
|
/* Compute camera matrix */ |
||||||
|
AbstractFeature<dimensions, T>::object()->setClean(); |
||||||
|
|
||||||
|
/* Compute transformations of all objects in the group relative to the camera */ |
||||||
|
std::vector<AbstractObject<dimensions, T>*> objects(group.size()); |
||||||
|
for(std::size_t i = 0; i != group.size(); ++i) |
||||||
|
objects[i] = group[i]->object(); |
||||||
|
std::vector<typename DimensionTraits<dimensions, T>::MatrixType> transformations = |
||||||
|
scene->transformationMatrices(objects, _cameraMatrix); |
||||||
|
|
||||||
|
/* Perform the drawing */ |
||||||
|
for(std::size_t i = 0; i != transformations.size(); ++i) |
||||||
|
group[i]->draw(transformations[i], this); |
||||||
|
} |
||||||
|
|
||||||
|
template<class T> Camera2D<T>* Camera2D<T>::setProjection(const Math::Vector2<T>& size) { |
||||||
|
/* Scale the volume down so it fits in (-1, 1) in all directions */ |
||||||
|
AbstractCamera<2, T>::rawProjectionMatrix = Math::Matrix3<T>::scaling(2.0f/size); |
||||||
|
|
||||||
|
AbstractCamera<2, T>::fixAspectRatio(); |
||||||
|
return this; |
||||||
|
} |
||||||
|
|
||||||
|
template<class T> Camera3D<T>* Camera3D<T>::setOrthographic(const Math::Vector2<T>& size, T near, T far) { |
||||||
|
_near = near; |
||||||
|
_far = far; |
||||||
|
|
||||||
|
Math::Vector2<T> xyScale = T(2.0)/size; |
||||||
|
T zScale = T(2.0)/(near-far); |
||||||
|
|
||||||
|
AbstractCamera<3, T>::rawProjectionMatrix = Math::Matrix4<T>( |
||||||
|
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<class T> Camera3D<T>* Camera3D<T>::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 |
||||||
@ -0,0 +1,192 @@ |
|||||||
|
#ifndef Magnum_SceneGraph_Drawable_h |
||||||
|
#define Magnum_SceneGraph_Drawable_h |
||||||
|
/*
|
||||||
|
Copyright © 2010, 2011, 2012 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::SceneGraph::Drawable, Magnum::SceneGraph::DrawableGroup, alias Magnum::SceneGraph::Drawable2D, Magnum::SceneGraph::Drawable3D, Magnum::SceneGraph::DrawableGroup2D, Magnum::SceneGraph::DrawableGroup3D |
||||||
|
*/ |
||||||
|
|
||||||
|
#include "AbstractGroupedFeature.h" |
||||||
|
|
||||||
|
namespace Magnum { namespace SceneGraph { |
||||||
|
|
||||||
|
template<std::uint8_t, class> class AbstractCamera; |
||||||
|
template<std::uint8_t, class> class Drawable; |
||||||
|
#ifndef MAGNUM_GCC46_COMPATIBILITY |
||||||
|
template<std::uint8_t dimensions, class T = GLfloat> using DrawableGroup = FeatureGroup<dimensions, Drawable<dimensions, T>, T>; |
||||||
|
#else |
||||||
|
template<std::uint8_t, class> class DrawableGroup; |
||||||
|
#endif |
||||||
|
|
||||||
|
/**
|
||||||
|
@brief %Drawable |
||||||
|
|
||||||
|
Adds drawing function to object. Each %Drawable is part of some DrawableGroup |
||||||
|
and the whole group is drawn with particular camera using AbstractCamera::draw(). |
||||||
|
|
||||||
|
@section Drawable-usage Usage |
||||||
|
|
||||||
|
First thing is add Drawable feature to some object and implement draw(). You |
||||||
|
can do it conveniently using multiple inheritance (see @ref scenegraph-features |
||||||
|
for introduction). Example: |
||||||
|
@code |
||||||
|
typedef SceneGraph::Object<SceneGraph::MatrixTransformation3D<>> Object3D; |
||||||
|
typedef SceneGraph::Scene<SceneGraph::MatrixTransformation3D<>> Scene3D; |
||||||
|
|
||||||
|
class DrawableObject: public Object3D, SceneGraph::Drawable3D<> { |
||||||
|
public: |
||||||
|
DrawableObject(Object* parent, SceneGraph::DrawableGroup3D<>* group): Object3D(parent), SceneGraph::Drawable3D<>(this, group) { |
||||||
|
// ...
|
||||||
|
} |
||||||
|
|
||||||
|
void draw(const Matrix4& transformationMatrix, AbstractCamera3D<>* camera) override { |
||||||
|
// ...
|
||||||
|
} |
||||||
|
} |
||||||
|
@endcode |
||||||
|
|
||||||
|
Then you add these objects to your scene and some drawable group and transform |
||||||
|
them as you like: |
||||||
|
@code |
||||||
|
Scene3D scene; |
||||||
|
SceneGraph::DrawableGroup3D<> group; |
||||||
|
|
||||||
|
(new DrawableObject(&scene, &group)) |
||||||
|
->translate(Vector3::yAxis(-0.3f)) |
||||||
|
->rotateX(deg(30.0f)); |
||||||
|
(new AnotherDrawableObject(&scene, &group)) |
||||||
|
->translate(Vector3::zAxis(0.5f)); |
||||||
|
// ...
|
||||||
|
@endcode |
||||||
|
|
||||||
|
The last thing you need is Camera attached to some object (thus using its |
||||||
|
transformation) and with it you can perform drawing in your draw event: |
||||||
|
@code |
||||||
|
Camera3D<> camera(&cameraObject); |
||||||
|
|
||||||
|
void MyApplication::drawEvent() { |
||||||
|
camera.draw(&group); |
||||||
|
} |
||||||
|
@endcode |
||||||
|
|
||||||
|
@see Drawable2D, Drawable3D, DrawableGroup2D, DrawableGroup3D |
||||||
|
*/ |
||||||
|
template<std::uint8_t dimensions, class T = GLfloat> class Drawable: public AbstractGroupedFeature<dimensions, Drawable<dimensions, T>, T> { |
||||||
|
public: |
||||||
|
/** @copydoc AbstractGroupedFeature::AbstractGroupedFeature() */ |
||||||
|
inline Drawable(AbstractObject<dimensions, T>* object, DrawableGroup<dimensions, T>* group = nullptr): AbstractGroupedFeature<dimensions, Drawable<dimensions, T>, T>(object, group) {} |
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Draw the object using given camera |
||||||
|
* @param transformationMatrix %Object transformation relative |
||||||
|
* to camera |
||||||
|
* @param camera Camera |
||||||
|
* |
||||||
|
* Projection matrix can be retrieved from AbstractCamera::projectionMatrix(). |
||||||
|
*/ |
||||||
|
virtual void draw(const typename DimensionTraits<dimensions, T>::MatrixType& transformationMatrix, AbstractCamera<dimensions, T>* camera) = 0; |
||||||
|
}; |
||||||
|
|
||||||
|
/**
|
||||||
|
@brief Two-dimensional drawable |
||||||
|
|
||||||
|
Convenience alternative to <tt>%Drawable<2, T></tt>. See Drawable for more |
||||||
|
information. |
||||||
|
@note Not available on GCC < 4.7. Use <tt>%Drawable<2, T></tt> instead. |
||||||
|
@see Drawable3D |
||||||
|
@todoc Remove workaround when Doxygen supports alias template |
||||||
|
*/ |
||||||
|
#ifndef DOXYGEN_GENERATING_OUTPUT |
||||||
|
#ifndef MAGNUM_GCC46_COMPATIBILITY |
||||||
|
template<class T = GLfloat> using Drawable2D = Drawable<2, T>; |
||||||
|
#endif |
||||||
|
#else |
||||||
|
typedef Drawable<2, T = GLfloat> Drawable2D; |
||||||
|
#endif |
||||||
|
|
||||||
|
/**
|
||||||
|
@brief Three-dimensional drawable |
||||||
|
|
||||||
|
Convenience alternative to <tt>%Drawable<3, T></tt>. See Drawable for more |
||||||
|
information. |
||||||
|
@note Not available on GCC < 4.7. Use <tt>%Drawable<3, T></tt> instead. |
||||||
|
@see Drawable2D |
||||||
|
@todoc Remove workaround when Doxygen supports alias template |
||||||
|
*/ |
||||||
|
#ifndef DOXYGEN_GENERATING_OUTPUT |
||||||
|
#ifndef MAGNUM_GCC46_COMPATIBILITY |
||||||
|
template<class T = GLfloat> using Drawable3D = Drawable<3, T>; |
||||||
|
#endif |
||||||
|
#else |
||||||
|
typedef Drawable<3, T = GLfloat> Drawable3D; |
||||||
|
#endif |
||||||
|
|
||||||
|
/**
|
||||||
|
@brief Group of drawables |
||||||
|
|
||||||
|
See Drawable for more information. |
||||||
|
@see DrawableGroup2D, DrawableGroup3D |
||||||
|
@todoc Remove workaround when Doxygen supports alias template |
||||||
|
*/ |
||||||
|
#if !defined(MAGNUM_GCC46_COMPATIBILITY) && !defined(DOXYGEN_GENERATING_OUTPUT) |
||||||
|
template<std::uint8_t dimensions, class T = GLfloat> using DrawableGroup = FeatureGroup<dimensions, Drawable<dimensions, T>, T>; |
||||||
|
#else |
||||||
|
template<std::uint8_t dimensions, class T = GLfloat> class DrawableGroup: public FeatureGroup<dimensions, Drawable<dimensions, T>, T> {}; |
||||||
|
#endif |
||||||
|
|
||||||
|
/**
|
||||||
|
@brief Group of two-dimensional drawables |
||||||
|
|
||||||
|
Convenience alternative to <tt>%DrawableGroup<2, T></tt>. See Drawable for |
||||||
|
more information. |
||||||
|
@note Not available on GCC < 4.7. Use <tt>%Drawable<2, T></tt> instead. |
||||||
|
@see DrawableGroup3D |
||||||
|
@todoc Remove workaround when Doxygen supports alias template |
||||||
|
*/ |
||||||
|
#ifndef DOXYGEN_GENERATING_OUTPUT |
||||||
|
#ifndef MAGNUM_GCC46_COMPATIBILITY |
||||||
|
template<class T = GLfloat> using DrawableGroup2D = DrawableGroup<2, T>; |
||||||
|
#endif |
||||||
|
#else |
||||||
|
typedef DrawableGroup<2, T = GLfloat> DrawableGroup2D; |
||||||
|
#endif |
||||||
|
|
||||||
|
/**
|
||||||
|
@brief Group of three-dimensional drawables |
||||||
|
|
||||||
|
Convenience alternative to <tt>%DrawableGroup<3, T></tt>. See Drawable for |
||||||
|
more information. |
||||||
|
@note Not available on GCC < 4.7. Use <tt>%Drawable<3, T></tt> instead. |
||||||
|
@see DrawableGroup2D |
||||||
|
@todoc Remove workaround when Doxygen supports alias template |
||||||
|
*/ |
||||||
|
#ifndef DOXYGEN_GENERATING_OUTPUT |
||||||
|
#ifndef MAGNUM_GCC46_COMPATIBILITY |
||||||
|
template<class T = GLfloat> using DrawableGroup3D = DrawableGroup<3, T>; |
||||||
|
#endif |
||||||
|
#else |
||||||
|
typedef DrawableGroup<3, T = GLfloat> DrawableGroup3D; |
||||||
|
#endif |
||||||
|
|
||||||
|
/* Make implementers' life easier */ |
||||||
|
#ifndef MAGNUM_GCC46_COMPATIBILITY |
||||||
|
template<class T = GLfloat> using AbstractCamera2D = AbstractCamera<2, T>; |
||||||
|
template<class T = GLfloat> using AbstractCamera3D = AbstractCamera<3, T>; |
||||||
|
#endif |
||||||
|
|
||||||
|
}} |
||||||
|
|
||||||
|
#endif |
||||||
@ -1,2 +1,3 @@ |
|||||||
corrade_add_test2(SceneGraphObjectTest ObjectTest.cpp LIBRARIES MagnumSceneGraphTestLib) |
corrade_add_test2(SceneGraphObjectTest ObjectTest.cpp LIBRARIES MagnumSceneGraphTestLib) |
||||||
|
corrade_add_test2(SceneGraphCameraTest CameraTest.cpp LIBRARIES MagnumSceneGraph) |
||||||
corrade_add_test2(SceneGraphSceneTest SceneTest.cpp LIBRARIES MagnumSceneGraph) |
corrade_add_test2(SceneGraphSceneTest SceneTest.cpp LIBRARIES MagnumSceneGraph) |
||||||
|
|||||||
Loading…
Reference in new issue