]> git.tdb.fi Git - libs/gl.git/blobdiff - source/camera.cpp
Support off-center frustum in Camera
[libs/gl.git] / source / camera.cpp
index a3ab730d98ddd06ed167a8a0bd739b0e56554890..274604f27f32e74833925d29828ec1628d22f36a 100644 (file)
@@ -10,6 +10,8 @@ Camera::Camera():
        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)
@@ -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;
@@ -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;
 }