X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=source%2Fcamera.cpp;h=b3cedbc62ad628813b5eee552302a2151ea586c1;hp=d119e1eff1b67245b9fca74c4058b5d5077af8e3;hb=77ad5a1a89aa43d9a3c8c58abff1ae184510cec6;hpb=af7ffc9c72e30ae377476f76a51a6676dd6e89aa diff --git a/source/camera.cpp b/source/camera.cpp index d119e1ef..b3cedbc6 100644 --- a/source/camera.cpp +++ b/source/camera.cpp @@ -36,7 +36,7 @@ void Camera::set_orthographic(float w, float h) update_projection_matrix(); } -void Camera::set_aspect(float a) +void Camera::set_aspect_ratio(float a) { aspect = a; update_projection_matrix(); @@ -56,15 +56,15 @@ void Camera::set_frustum_axis(float x, float y) update_projection_matrix(); } -void Camera::set_position(const Vector3 &p) +void Camera::set_frustum_rotation(const Geometry::Angle &r) { - position = p; - update_object_matrix(); + rotate = r; + update_projection_matrix(); } -void Camera::set_up_direction(const Vector3 &u) +void Camera::set_position(const Vector3 &p) { - up_dir = normalize(u); + position = p; update_object_matrix(); } @@ -79,6 +79,20 @@ void Camera::look_at(const Vector3 &p) set_look_direction(p-position); } +void Camera::set_up_direction(const Vector3 &u) +{ + up_dir = normalize(u); + update_object_matrix(); +} + +void Camera::set_object_matrix(const Matrix &m) +{ + position = m.column(3).slice<3>(0); + look_dir = normalize(-m.column(2).slice<3>(0)); + up_dir = normalize(m.column(1).slice<3>(0)); + update_object_matrix(); +} + Vector3 Camera::project(const Vector4 &p) const { Vector4 r = proj_matrix*(view_matrix*p); @@ -93,10 +107,15 @@ Vector3 Camera::project(const Vector3 &p) const 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); + r = matrix*Vector4(r.x/r.w, r.y/r.w, r.z/r.w, p.w); return r; } +Vector3 Camera::unproject(const Vector3 &p) const +{ + return unproject(Vector4(p.x, p.y, p.z, 1.0f)).slice<3>(0); +} + void Camera::apply() const { MatrixStack::projection() = proj_matrix; @@ -115,6 +134,7 @@ void Camera::update_projection_matrix() proj_matrix = Matrix::frustum(left, right, bottom, top, clip_near, clip_far); else proj_matrix = Matrix::ortho(left, right, bottom, top, clip_near, clip_far); + proj_matrix = Matrix::rotation(rotate, Vector3(0, 0, 1))*proj_matrix; } void Camera::update_object_matrix() @@ -125,8 +145,62 @@ void Camera::update_object_matrix() columns[1] = compose(cross(right_dir, look_dir), 0.0f); columns[2] = compose(-look_dir, 0.0f); columns[3] = compose(position, 1.0f); - object_matrix = Matrix::from_columns(columns); - view_matrix = invert(object_matrix); + matrix = Matrix::from_columns(columns); + view_matrix = invert(matrix); +} + + +Camera::Loader::Loader(Camera &c): + DataFile::ObjectLoader(c) +{ + add("aspect_ratio", &Loader::aspect_ratio); + add("depth_clip", &Loader::depth_clip); + add("field_of_view", &Loader::field_of_view); + add("look_at", &Loader::look_at); + add("look_direction", &Loader::look_direction); + add("orthographic", &Loader::orthographic); + add("position", &Loader::position); + add("up_direction", &Loader::up_direction); +} + +void Camera::Loader::aspect_ratio(float a) +{ + obj.set_aspect_ratio(a); +} + +void Camera::Loader::depth_clip(float n, float f) +{ + obj.set_depth_clip(n, f); +} + +void Camera::Loader::field_of_view(float a) +{ + obj.set_field_of_view(Geometry::Angle::from_degrees(a)); +} + +void Camera::Loader::look_at(float x, float y, float z) +{ + obj.look_at(Vector3(x, y, z)); +} + +void Camera::Loader::look_direction(float x, float y, float z) +{ + obj.set_look_direction(Vector3(x, y, z)); +} + +void Camera::Loader::orthographic(float w, float h) +{ + obj.set_orthographic(w, h); +} + +void Camera::Loader::position(float x, float y, float z) +{ + obj.set_position(Vector3(x, y, z)); +} + +void Camera::Loader::up_direction(float x, float y, float z) +{ + obj.set_up_direction(Vector3(x, y, z)); } } // namespace GL