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.
212 lines
7.5 KiB
212 lines
7.5 KiB
|
14 years ago
|
#ifndef Magnum_SceneGraph_AbstractCamera_h
|
||
|
|
#define Magnum_SceneGraph_AbstractCamera_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::AbstractCamera, enum AspectRatioPolicy, alias Magnum::SceneGraph::AbstractCamera2D, Magnum::SceneGraph::AbstractCamera3D
|
||
|
16 years ago
|
*/
|
||
|
|
|
||
|
14 years ago
|
#include "Math/Matrix3.h"
|
||
|
|
#include "Math/Matrix4.h"
|
||
|
|
#include "AbstractFeature.h"
|
||
|
|
|
||
|
|
#include "magnumSceneGraphVisibility.h"
|
||
|
16 years ago
|
|
||
|
14 years ago
|
namespace Magnum { namespace SceneGraph {
|
||
|
16 years ago
|
|
||
|
14 years ago
|
template<std::uint8_t, class> class Drawable;
|
||
|
|
template<std::uint8_t, class, class> class FeatureGroup;
|
||
|
|
#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
|
||
|
|
|
||
|
14 years ago
|
/** @relates AbstractCamera
|
||
|
|
@brief Camera aspect ratio policy
|
||
|
|
|
||
|
|
@see AbstractCamera::setAspectRatioPolicy()
|
||
|
|
*/
|
||
|
|
enum class AspectRatioPolicy {
|
||
|
|
NotPreserved, /**< Don't preserve aspect ratio (default) */
|
||
|
|
Extend, /**< Extend on larger side of view */
|
||
|
|
Clip /**< Clip on smaller side of view */
|
||
|
|
};
|
||
|
14 years ago
|
|
||
|
14 years ago
|
#ifndef DOXYGEN_GENERATING_OUTPUT
|
||
|
|
namespace Implementation {
|
||
|
14 years ago
|
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);
|
||
|
14 years ago
|
}
|
||
|
|
#endif
|
||
|
|
|
||
|
14 years ago
|
/**
|
||
|
14 years ago
|
@brief Base for cameras
|
||
|
|
|
||
|
14 years ago
|
See Drawable documentation for more information. This class is not directly
|
||
|
|
instantiatable, use Camera2D or Camera3D subclasses instead.
|
||
|
14 years ago
|
|
||
|
|
@section AbstractCamera-explicit-specializations Explicit template specializations
|
||
|
|
|
||
|
|
The following specialization are explicitly compiled into SceneGraph library.
|
||
|
14 years ago
|
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.
|
||
|
14 years ago
|
|
||
|
|
- @ref AbstractCamera "AbstractCamera<2>"
|
||
|
|
- @ref AbstractCamera "AbstractCamera<3>"
|
||
|
|
|
||
|
14 years ago
|
@see Drawable, DrawableGroup, AbstractCamera2D, AbstractCamera3D
|
||
|
14 years ago
|
*/
|
||
|
|
template<std::uint8_t dimensions, class T = GLfloat> class SCENEGRAPH_EXPORT AbstractCamera: public AbstractFeature<dimensions, T> {
|
||
|
16 years ago
|
public:
|
||
|
14 years ago
|
/**
|
||
|
|
* @brief Constructor
|
||
|
|
* @param object Object holding the camera
|
||
|
|
*/
|
||
|
|
inline AbstractCamera(AbstractObject<dimensions, T>* object): AbstractFeature<dimensions, T>(object), _aspectRatioPolicy(AspectRatioPolicy::NotPreserved) {
|
||
|
|
AbstractFeature<dimensions, T>::setCachedTransformations(AbstractFeature<dimensions, T>::CachedTransformation::InvertedAbsolute);
|
||
|
|
}
|
||
|
14 years ago
|
|
||
|
|
virtual ~AbstractCamera() = 0;
|
||
|
16 years ago
|
|
||
|
|
/** @brief Aspect ratio policy */
|
||
|
14 years ago
|
inline AspectRatioPolicy aspectRatioPolicy() const { return _aspectRatioPolicy; }
|
||
|
16 years ago
|
|
||
|
14 years ago
|
/**
|
||
|
|
* @brief Set aspect ratio policy
|
||
|
|
* @return Pointer to self (for method chaining)
|
||
|
|
*/
|
||
|
14 years ago
|
AbstractCamera<dimensions, T>* setAspectRatioPolicy(AspectRatioPolicy policy);
|
||
|
16 years ago
|
|
||
|
|
/**
|
||
|
|
* @brief Camera matrix
|
||
|
|
*
|
||
|
|
* Camera matrix describes world position relative to the camera and is
|
||
|
|
* applied as first.
|
||
|
|
*/
|
||
|
14 years ago
|
inline typename DimensionTraits<dimensions, T>::MatrixType cameraMatrix() {
|
||
|
|
AbstractFeature<dimensions, T>::object()->setClean();
|
||
|
16 years ago
|
return _cameraMatrix;
|
||
|
|
}
|
||
|
16 years ago
|
|
||
|
|
/**
|
||
|
|
* @brief Projection matrix
|
||
|
|
*
|
||
|
|
* Projection matrix handles e.g. perspective distortion and is applied
|
||
|
|
* as last.
|
||
|
14 years ago
|
* @see projectionSize()
|
||
|
16 years ago
|
*/
|
||
|
14 years ago
|
inline typename DimensionTraits<dimensions, T>::MatrixType projectionMatrix() const { return _projectionMatrix; }
|
||
|
16 years ago
|
|
||
|
14 years ago
|
/**
|
||
|
|
* @brief Size of (near) XY plane in current projection
|
||
|
|
*
|
||
|
|
* Returns size of near XY plane computed from projection matrix.
|
||
|
|
* @see projectionMatrix()
|
||
|
|
*/
|
||
|
14 years ago
|
inline Math::Vector2<T> projectionSize() const {
|
||
|
|
return {T(2.0)/_projectionMatrix[0].x(), T(2.0)/_projectionMatrix[1].y()};
|
||
|
14 years ago
|
}
|
||
|
|
|
||
|
15 years ago
|
/** @brief Viewport size */
|
||
|
14 years ago
|
inline Math::Vector2<GLsizei> viewport() const { return _viewport; }
|
||
|
15 years ago
|
|
||
|
16 years ago
|
/**
|
||
|
15 years ago
|
* @brief Set viewport size
|
||
|
16 years ago
|
*
|
||
|
14 years ago
|
* Stores viewport size internally and recalculates projection matrix
|
||
|
|
* according to aspect ratio policy.
|
||
|
|
* @see setAspectRatioPolicy()
|
||
|
16 years ago
|
*/
|
||
|
14 years ago
|
virtual void setViewport(const Math::Vector2<GLsizei>& size);
|
||
|
15 years ago
|
|
||
|
14 years ago
|
/**
|
||
|
14 years ago
|
* @brief Draw
|
||
|
14 years ago
|
*
|
||
|
14 years ago
|
* Draws given group of drawables.
|
||
|
14 years ago
|
*/
|
||
|
14 years ago
|
virtual void draw(DrawableGroup<dimensions, T>& group);
|
||
|
14 years ago
|
|
||
|
14 years ago
|
protected:
|
||
|
14 years ago
|
/** Recalculates camera matrix */
|
||
|
|
inline void cleanInverted(const typename DimensionTraits<dimensions, T>::MatrixType& invertedAbsoluteTransformation) override {
|
||
|
|
_cameraMatrix = invertedAbsoluteTransformation;
|
||
|
|
}
|
||
|
14 years ago
|
|
||
|
|
#ifndef DOXYGEN_GENERATING_OUTPUT
|
||
|
|
inline void fixAspectRatio() {
|
||
|
14 years ago
|
_projectionMatrix = Implementation::aspectRatioFix<dimensions, T>(_aspectRatioPolicy, {rawProjectionMatrix[0].x(), rawProjectionMatrix[1].y()}, _viewport)*rawProjectionMatrix;
|
||
|
14 years ago
|
}
|
||
|
|
|
||
|
14 years ago
|
typename DimensionTraits<dimensions, T>::MatrixType rawProjectionMatrix;
|
||
|
14 years ago
|
AspectRatioPolicy _aspectRatioPolicy;
|
||
|
|
#endif
|
||
|
14 years ago
|
|
||
|
16 years ago
|
private:
|
||
|
14 years ago
|
typename DimensionTraits<dimensions, T>::MatrixType _projectionMatrix;
|
||
|
|
typename DimensionTraits<dimensions, T>::MatrixType _cameraMatrix;
|
||
|
16 years ago
|
|
||
|
14 years ago
|
Math::Vector2<GLsizei> _viewport;
|
||
|
14 years ago
|
};
|
||
|
|
|
||
|
14 years ago
|
template<std::uint8_t dimensions, class T> inline AbstractCamera<dimensions, T>::~AbstractCamera() {}
|
||
|
|
|
||
|
|
/**
|
||
|
|
@brief Base for two-dimensional cameras
|
||
|
14 years ago
|
|
||
|
14 years ago
|
Convenience alternative to <tt>%AbstractCamera<2, T></tt>. See AbstractCamera
|
||
|
|
for more information.
|
||
|
|
@note Not available on GCC < 4.7. Use <tt>%AbstractCamera<2, T></tt> instead.
|
||
|
|
@see AbstractCamera3D
|
||
|
|
@todoc Remove workaround when Doxygen supports alias template
|
||
|
|
*/
|
||
|
14 years ago
|
#ifndef DOXYGEN_GENERATING_OUTPUT
|
||
|
14 years ago
|
#ifndef MAGNUM_GCC46_COMPATIBILITY
|
||
|
|
template<class T = GLfloat> using AbstractCamera2D = AbstractCamera<2, T>;
|
||
|
|
#endif
|
||
|
|
#else
|
||
|
|
typedef AbstractCamera<2, T = GLfloat> AbstractCamera2D;
|
||
|
|
#endif
|
||
|
14 years ago
|
|
||
|
14 years ago
|
/**
|
||
|
|
@brief Base for three-dimensional cameras
|
||
|
|
|
||
|
|
Convenience alternative to <tt>%AbstractCamera<3, T></tt>. See AbstractCamera
|
||
|
|
for more information.
|
||
|
|
@note Not available on GCC < 4.7. Use <tt>%AbstractCamera<3, T></tt> instead.
|
||
|
|
@see AbstractCamera2D
|
||
|
|
@todoc Remove workaround when Doxygen supports alias template
|
||
|
|
*/
|
||
|
|
#ifndef DOXYGEN_GENERATING_OUTPUT
|
||
|
|
#ifndef MAGNUM_GCC46_COMPATIBILITY
|
||
|
|
template<class T = GLfloat> using AbstractCamera3D = AbstractCamera<3, T>;
|
||
|
|
#endif
|
||
|
|
#else
|
||
|
|
typedef AbstractCamera<3, T = GLfloat> AbstractCamera3D;
|
||
|
14 years ago
|
#endif
|
||
|
|
|
||
|
14 years ago
|
/* Make implementers' life easier */
|
||
|
|
#ifndef MAGNUM_GCC46_COMPATIBILITY
|
||
|
|
template<class T = GLfloat> using DrawableGroup2D = DrawableGroup<2, T>;
|
||
|
|
template<class T = GLfloat> using DrawableGroup3D = DrawableGroup<3, T>;
|
||
|
|
#endif
|
||
|
|
|
||
|
14 years ago
|
}}
|
||
|
16 years ago
|
|
||
|
|
#endif
|