]> git.tdb.fi Git - libs/gl.git/commitdiff
Support off-center frustum in Camera
authorMikko Rasa <tdb@tdb.fi>
Sat, 21 Sep 2013 17:06:17 +0000 (20:06 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sat, 21 Sep 2013 17:06:17 +0000 (20:06 +0300)
source/camera.cpp
source/camera.h

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;
 }
 
index 11d42985c7bd4719a2073c4c16919b69b03b9979..a0e4d6f65fb3215a5d8f64e2fbc1fef58347377f 100644 (file)
@@ -15,6 +15,8 @@ private:
        // Some compilers have "near" and "far" keywords
        float clip_near;
        float clip_far;
+       float frustum_x;
+       float frustum_y;
        Vector3 position;
        Vector3 look_dir;
        Vector3 up_dir;
@@ -26,6 +28,7 @@ public:
        void set_field_of_view(const Geometry::Angle<float> &);
        void set_aspect(float);
        void set_depth_clip(float, float);
+       void set_frustum_axis(float, float);
        const Geometry::Angle<float> &get_field_of_view() const { return fov; }
        float get_aspect() const { return aspect; }
        float get_near_clip() const { return clip_near; }