]> git.tdb.fi Git - libs/gl.git/blobdiff - source/render/camera.cpp
Rearrange soucre files into subdirectories
[libs/gl.git] / source / render / camera.cpp
diff --git a/source/render/camera.cpp b/source/render/camera.cpp
new file mode 100644 (file)
index 0000000..3bba0ea
--- /dev/null
@@ -0,0 +1,201 @@
+#include <cmath>
+#include "camera.h"
+#include "matrix.h"
+
+namespace Msp {
+namespace GL {
+
+Camera::Camera():
+       fov(Geometry::Angle<float>::from_turns(0.125)),
+       height(0),
+       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<float> &f)
+{
+       fov = f;
+       update_projection_matrix();
+}
+
+void Camera::set_orthographic(float w, float h)
+{
+       fov = Geometry::Angle<float>::zero();
+       height = h;
+       if(w)
+               aspect = w/h;
+       update_projection_matrix();
+}
+
+void Camera::set_aspect_ratio(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_frustum_rotation(const Geometry::Angle<float> &r)
+{
+       rotate = r;
+       update_projection_matrix();
+}
+
+void Camera::set_position(const Vector3 &p)
+{
+       position = p;
+       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);
+}
+
+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);
+       return r.slice<3>(0)/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 = 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::update_projection_matrix()
+{
+       float frustum_h = (fov!=Geometry::Angle<float>::zero() ? tan(fov/2.0f)*clip_near : height/2);
+       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);
+       if(fov>Geometry::Angle<float>::zero())
+               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()
+{
+       Vector3 right_dir = normalize(cross(look_dir, up_dir));
+       Vector4 columns[4];
+       columns[0] = compose(right_dir, 0.0f);
+       columns[1] = compose(cross(right_dir, look_dir), 0.0f);
+       columns[2] = compose(-look_dir, 0.0f);
+       columns[3] = compose(position, 1.0f);
+       matrix = Matrix::from_columns(columns);
+       view_matrix = invert(matrix);
+}
+
+
+Camera::Loader::Loader(Camera &c):
+       DataFile::ObjectLoader<Camera>(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<float>::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
+} // namespace Msp