From: Mikko Rasa Date: Sat, 21 Sep 2013 17:06:17 +0000 (+0300) Subject: Support off-center frustum in Camera X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=fac4decfa4744ac6dc7f13c0348b0ec1ec80f6a4;p=libs%2Fgl.git Support off-center frustum in Camera --- diff --git a/source/camera.cpp b/source/camera.cpp index a3ab730d..274604f2 100644 --- a/source/camera.cpp +++ b/source/camera.cpp @@ -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; } diff --git a/source/camera.h b/source/camera.h index 11d42985..a0e4d6f6 100644 --- a/source/camera.h +++ b/source/camera.h @@ -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 &); void set_aspect(float); void set_depth_clip(float, float); + void set_frustum_axis(float, float); const Geometry::Angle &get_field_of_view() const { return fov; } float get_aspect() const { return aspect; } float get_near_clip() const { return clip_near; }