From: Mikko Rasa Date: Mon, 25 Nov 2013 14:23:51 +0000 (+0200) Subject: Store and expose the projection matrix X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=commitdiff_plain;h=ea3487f52f8621f07deb47fa1af22a85b8b23519;hp=c6149a1e9f488f790378969ff655e6bc94369d16 Store and expose the projection matrix --- diff --git a/source/camera.cpp b/source/camera.cpp index 0afa033c..df4119a9 100644 --- a/source/camera.cpp +++ b/source/camera.cpp @@ -20,23 +20,27 @@ Camera::Camera(): void Camera::set_field_of_view(const Geometry::Angle &f) { fov = f; + update_projection_matrix(); } void Camera::set_aspect(float a) { aspect = a; + update_projection_matrix(); } void Camera::set_depth_clip(float n, float f) { clip_near = n; clip_far = f; + update_projection_matrix(); } void Camera::set_frustum_axis(float x, float y) { frustum_x = x; frustum_y = y; + update_projection_matrix(); } void Camera::set_position(const Vector3 &p) @@ -81,6 +85,12 @@ Vector4 Camera::unproject(const Vector4 &p) const } void Camera::apply() const +{ + MatrixStack::projection() = proj_matrix; + MatrixStack::modelview() = view_matrix; +} + +void Camera::update_projection_matrix() { float frustum_h = tan(fov/2.0f)*clip_near; float frustum_w = frustum_h*aspect; @@ -88,8 +98,7 @@ void Camera::apply() const 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() = view_matrix; + proj_matrix = Matrix::frustum(left, right, bottom, top, clip_near, clip_far); } void Camera::update_object_matrix() diff --git a/source/camera.h b/source/camera.h index 63455760..bfcc2987 100644 --- a/source/camera.h +++ b/source/camera.h @@ -22,6 +22,7 @@ private: Vector3 up_dir; Matrix view_matrix; Matrix object_matrix; + Matrix proj_matrix; public: Camera(); @@ -54,6 +55,9 @@ public: to world space. */ const Matrix &get_object_matrix() const { return object_matrix; } + /** Returns the projection matrix. */ + const Matrix &get_projection_matrix() const { return proj_matrix; } + Vector3 project(const Vector4 &) const; Vector3 project(const Vector3 &) const; Vector4 unproject(const Vector4 &) const; @@ -61,6 +65,7 @@ public: void apply() const; private: + void update_projection_matrix(); void update_object_matrix(); };