X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=source%2Fcamera.cpp;h=b3cedbc62ad628813b5eee552302a2151ea586c1;hp=fca3ea33d504773d90e073c598175a33d9be2f46;hb=HEAD;hpb=e66511d118f702928c9649bb802a7a2faf0b5c11 diff --git a/source/camera.cpp b/source/camera.cpp deleted file mode 100644 index fca3ea33..00000000 --- a/source/camera.cpp +++ /dev/null @@ -1,120 +0,0 @@ -#include -#include "camera.h" -#include "matrix.h" - -namespace Msp { -namespace GL { - -Camera::Camera(): - fov(Geometry::Angle::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) -{ - update_projection_matrix(); - update_object_matrix(); -} - -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) -{ - position = p; - update_object_matrix(); -} - -void Camera::set_up_direction(const Vector3 &u) -{ - up_dir = normalize(u); - update_object_matrix(); -} - -void Camera::set_look_direction(const Vector3 &l) -{ - look_dir = normalize(l); - update_object_matrix(); -} - -void Camera::look_at(const Vector3 &p) -{ - set_look_direction(p-position); -} - -Vector3 Camera::project(const Vector4 &p) const -{ - Vector4 r = proj_matrix*(view_matrix*p); - return Vector3(r)/r.w; -} - -Vector3 Camera::project(const Vector3 &p) const -{ - return project(Vector4(p.x, p.y, p.z, 1.0)); -} - -Vector4 Camera::unproject(const Vector4 &p) const -{ - Vector4 r = invert(proj_matrix)*Vector4(p.x, p.y, p.z, 1.0f); - r = object_matrix*Vector4(r.x, r.y, r.z, p.w); - return r; -} - -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; - 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); - proj_matrix = Matrix::frustum(left, right, bottom, top, clip_near, clip_far); -} - -void Camera::update_object_matrix() -{ - Vector3 right_dir = normalize(cross(look_dir, up_dir)); - Vector4 columns[4]; - columns[0] = Vector4(right_dir, 0.0f); - columns[1] = Vector4(cross(right_dir, look_dir), 0.0f); - columns[2] = Vector4(-look_dir, 0.0f); - columns[3] = Vector4(position, 1.0f); - object_matrix = Matrix::from_columns(columns); - view_matrix = invert(object_matrix); -} - -} // namespace GL -} // namespace Msp