]> git.tdb.fi Git - libs/gl.git/blobdiff - source/camera.cpp
Support off-center frustum in Camera
[libs/gl.git] / source / camera.cpp
index 406135796f3e10c1cc0135cb23a097ee51e32259..274604f27f32e74833925d29828ec1628d22f36a 100644 (file)
@@ -6,16 +6,18 @@ namespace Msp {
 namespace GL {
 
 Camera::Camera():
-       fov(M_PI/4),
+       fov(Geometry::Angle<float>::from_turns(0.125)),
        aspect(4.0/3.0),
        clip_near(0.1),
        clip_far(10),
+       frustum_x(0),
+       frustum_y(0),
        position(0, 0, 0),
        look_dir(0, 0, -1),
        up_dir(0, 1, 0)
 { }
 
-void Camera::set_field_of_view(float f)
+void Camera::set_field_of_view(const Geometry::Angle<float> &f)
 {
        fov = f;
 }
@@ -31,6 +33,12 @@ void Camera::set_depth_clip(float n, float f)
        clip_far = f;
 }
 
+void Camera::set_frustum_axis(float x, float y)
+{
+       frustum_x = x;
+       frustum_y = y;
+}
+
 void Camera::set_position(const Vector3 &p)
 {
        position = p;
@@ -58,7 +66,7 @@ void Camera::look_at(const Vector3 &p)
 
 Vector3 Camera::project(const Vector4 &p) const
 {
-       float frustum_h = tan(fov/2);
+       float frustum_h = tan(fov/2.0f);
        float frustum_w = frustum_h*aspect;
        float z_range = clip_far-clip_near;
 
@@ -75,7 +83,7 @@ Vector3 Camera::project(const Vector3 &p) const
 
 Vector4 Camera::unproject(const Vector4 &p) const
 {
-       float frustum_h = tan(fov/2);
+       float frustum_h = tan(fov/2.0f);
        float frustum_w = frustum_h*aspect;
        float z_range = clip_far-clip_near;
 
@@ -91,7 +99,13 @@ Vector4 Camera::unproject(const Vector4 &p) const
 
 void Camera::apply() const
 {
-       MatrixStack::projection() = Matrix::perspective(fov, aspect, clip_near, clip_far);
+       float frustum_h = tan(fov/2.0f)*clip_near;
+       float frustum_w = frustum_h*aspect;
+       float left = frustum_w*(frustum_x-1.0f);
+       float right = frustum_w*(frustum_x+1.0f);
+       float bottom = frustum_h*(frustum_y-1.0f);
+       float top = frustum_h*(frustum_y+1.0f);
+       MatrixStack::projection() = Matrix::frustum(left, right, bottom, top, clip_near, clip_far);
        MatrixStack::modelview() = matrix;
 }