Browse Source

Initial support for 2D scene graph.

vectorfields
Vladimír Vondruš 14 years ago
parent
commit
d43e18a741
  1. 10
      src/SceneGraph/Camera.cpp
  2. 29
      src/SceneGraph/Camera.h
  3. 1
      src/SceneGraph/Object.cpp
  4. 41
      src/SceneGraph/Object.h
  5. 3
      src/SceneGraph/Scene.h

10
src/SceneGraph/Camera.cpp

@ -40,6 +40,7 @@ template<class MatrixType> MatrixType aspectRatioFix(AspectRatioPolicy aspectRat
} }
/* Explicitly instantiate the templates */ /* Explicitly instantiate the templates */
template Matrix3 aspectRatioFix<Matrix3>(AspectRatioPolicy, const Vector2&, const Math::Vector2<GLsizei>&);
template Matrix4 aspectRatioFix<Matrix4>(AspectRatioPolicy, const Vector2&, const Math::Vector2<GLsizei>&); template Matrix4 aspectRatioFix<Matrix4>(AspectRatioPolicy, const Vector2&, const Math::Vector2<GLsizei>&);
} }
@ -81,6 +82,14 @@ template<class MatrixType, class VectorType, class ObjectType, class SceneType,
} }
} }
void Camera2D::setProjection(const Vector2& size) {
/* Scale the volume down so it fits in (-1, 1) in all directions */
rawProjectionMatrix = Matrix3::scaling(2.0f/size);
projectionAspectRatio = size;
fixAspectRatio();
}
void Camera3D::setOrthographic(const Vector2& size, GLfloat near, GLfloat far) { void Camera3D::setOrthographic(const Vector2& size, GLfloat near, GLfloat far) {
_near = near; _near = near;
_far = far; _far = far;
@ -123,6 +132,7 @@ void Camera3D::setPerspective(GLfloat fov, GLfloat near, GLfloat far) {
} }
/* Explicitly instantiate the templates */ /* Explicitly instantiate the templates */
template class Camera<Matrix3, Vector2, Object2D, Scene2D, Camera2D>;
template class Camera<Matrix4, Vector3, Object3D, Scene3D, Camera3D>; template class Camera<Matrix4, Vector3, Object3D, Scene3D, Camera3D>;
}} }}

29
src/SceneGraph/Camera.h

@ -147,9 +147,16 @@ template<class MatrixType, class VectorType, class ObjectType, class SceneType,
#ifndef DOXYGEN_GENERATING_OUTPUT #ifndef DOXYGEN_GENERATING_OUTPUT
/* These templates are instantiated in source file */ /* These templates are instantiated in source file */
extern template class SCENEGRAPH_EXPORT Camera<Matrix3, Vector2, Object2D, Scene2D, Camera2D>;
extern template class SCENEGRAPH_EXPORT Camera<Matrix4, Vector3, Object3D, Scene3D, Camera3D>; extern template class SCENEGRAPH_EXPORT Camera<Matrix4, Vector3, Object3D, Scene3D, Camera3D>;
namespace Implementation { 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> { template<> class Camera<3> {
public: public:
inline constexpr static Matrix4 aspectRatioScale(const Vector2& scale) { inline constexpr static Matrix4 aspectRatioScale(const Vector2& scale) {
@ -159,6 +166,28 @@ namespace Implementation {
} }
#endif #endif
/** @brief %Camera for two-dimensional scenes */
class SCENEGRAPH_EXPORT Camera2D: public Camera<Matrix3, Vector2, Object2D, Scene2D, Camera2D> {
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 */ /** @brief %Camera for three-dimensional scenes */
class SCENEGRAPH_EXPORT Camera3D: public Camera<Matrix4, Vector3, Object3D, Scene3D, Camera3D> { class SCENEGRAPH_EXPORT Camera3D: public Camera<Matrix4, Vector3, Object3D, Scene3D, Camera3D> {
public: public:

1
src/SceneGraph/Object.cpp

@ -152,6 +152,7 @@ template<class MatrixType, class VectorType, class ObjectType, class SceneType,
} }
/* Explicitly instantiate the templates */ /* Explicitly instantiate the templates */
template class Object<Matrix3, Vector2, Object2D, Scene2D, Camera2D>;
template class Object<Matrix4, Vector3, Object3D, Scene3D, Camera3D>; template class Object<Matrix4, Vector3, Object3D, Scene3D, Camera3D>;
}} }}

41
src/SceneGraph/Object.h

@ -241,15 +241,56 @@ template<class MatrixType, class VectorType, class ObjectType, class SceneType,
template<class MatrixType, class VectorType, class ObjectType, class SceneType, class CameraType> inline void Object<MatrixType, VectorType, ObjectType, SceneType, CameraType>::draw(const MatrixType&, CameraType*) {} template<class MatrixType, class VectorType, class ObjectType, class SceneType, class CameraType> inline void Object<MatrixType, VectorType, ObjectType, SceneType, CameraType>::draw(const MatrixType&, CameraType*) {}
template<class MatrixType, class VectorType, class ObjectType, class SceneType, class CameraType> inline void Object<MatrixType, VectorType, ObjectType, SceneType, CameraType>::clean(const MatrixType&) { dirty = false; } template<class MatrixType, class VectorType, class ObjectType, class SceneType, class CameraType> inline void Object<MatrixType, VectorType, ObjectType, SceneType, CameraType>::clean(const MatrixType&) { dirty = false; }
class Camera2D;
class Camera3D; class Camera3D;
class Object2D;
class Object3D; class Object3D;
typedef Scene<Matrix3, Vector2, Object2D, Camera2D> Scene2D;
typedef Scene<Matrix4, Vector3, Object3D, Camera3D> Scene3D; typedef Scene<Matrix4, Vector3, Object3D, Camera3D> Scene3D;
#ifndef DOXYGEN_GENERATING_OUTPUT #ifndef DOXYGEN_GENERATING_OUTPUT
/* These templates are instantiated in source file */ /* These templates are instantiated in source file */
extern template class SCENEGRAPH_EXPORT Object<Matrix3, Vector2, Object2D, Scene2D, Camera2D>;
extern template class SCENEGRAPH_EXPORT Object<Matrix4, Vector3, Object3D, Scene3D, Camera3D>; extern template class SCENEGRAPH_EXPORT Object<Matrix4, Vector3, Object3D, Scene3D, Camera3D>;
#endif #endif
/** @brief Two-dimensional object */
class SCENEGRAPH_EXPORT Object2D: public Object<Matrix3, Vector2, Object2D, Scene2D, Camera2D> {
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 */ /** @brief Three-dimensional object */
class SCENEGRAPH_EXPORT Object3D: public Object<Matrix4, Vector3, Object3D, Scene3D, Camera3D> { class SCENEGRAPH_EXPORT Object3D: public Object<Matrix4, Vector3, Object3D, Scene3D, Camera3D> {
public: public:

3
src/SceneGraph/Scene.h

@ -42,6 +42,9 @@ template<class MatrixType, class VectorType, class ObjectType, class CameraType>
inline void draw(const MatrixType&, CameraType*) {} inline void draw(const MatrixType&, CameraType*) {}
}; };
/** @brief Two-dimensional scene */
typedef Scene<Matrix3, Vector2, Object2D, Camera2D> Scene2D;
/** @brief Three-dimensional scene */ /** @brief Three-dimensional scene */
typedef Scene<Matrix4, Vector3, Object3D, Camera3D> Scene3D; typedef Scene<Matrix4, Vector3, Object3D, Camera3D> Scene3D;

Loading…
Cancel
Save