]> git.tdb.fi Git - libs/gl.git/blob - source/camera.cpp
Make better use of matrix operations in Camera
[libs/gl.git] / source / camera.cpp
1 #include <cmath>
2 #include "camera.h"
3 #include "matrix.h"
4
5 namespace Msp {
6 namespace GL {
7
8 Camera::Camera():
9         fov(Geometry::Angle<float>::from_turns(0.125)),
10         aspect(4.0/3.0),
11         clip_near(0.1),
12         clip_far(10),
13         frustum_x(0),
14         frustum_y(0),
15         position(0, 0, 0),
16         look_dir(0, 0, -1),
17         up_dir(0, 1, 0)
18 { }
19
20 void Camera::set_field_of_view(const Geometry::Angle<float> &f)
21 {
22         fov = f;
23 }
24
25 void Camera::set_aspect(float a)
26 {
27         aspect = a;
28 }
29
30 void Camera::set_depth_clip(float n, float f)
31 {
32         clip_near = n;
33         clip_far = f;
34 }
35
36 void Camera::set_frustum_axis(float x, float y)
37 {
38         frustum_x = x;
39         frustum_y = y;
40 }
41
42 void Camera::set_position(const Vector3 &p)
43 {
44         position = p;
45         update_object_matrix();
46 }
47
48 void Camera::set_up_direction(const Vector3 &u)
49 {
50         up_dir = normalize(u);
51         update_object_matrix();
52 }
53
54 void Camera::set_look_direction(const Vector3 &l)
55 {
56         look_dir = normalize(l);
57         update_object_matrix();
58 }
59
60 void Camera::look_at(const Vector3 &p)
61 {
62         set_look_direction(p-position);
63 }
64
65 Vector3 Camera::project(const Vector4 &p) const
66 {
67         Vector4 r = proj_matrix*(view_matrix*p);
68         return Vector3(r)/r.w;
69 }
70
71 Vector3 Camera::project(const Vector3 &p) const
72 {
73         return project(Vector4(p.x, p.y, p.z, 1.0));
74 }
75
76 Vector4 Camera::unproject(const Vector4 &p) const
77 {
78         Vector4 r = invert(proj_matrix)*LinAl::Vector<double, 4>(p.x, p.y, p.z, 1.0f);
79         r = object_matrix*Vector4(r.x, r.y, r.z, p.w);
80         return r;
81 }
82
83 void Camera::apply() const
84 {
85         float frustum_h = tan(fov/2.0f)*clip_near;
86         float frustum_w = frustum_h*aspect;
87         float left = frustum_w*(frustum_x-1.0f);
88         float right = frustum_w*(frustum_x+1.0f);
89         float bottom = frustum_h*(frustum_y-1.0f);
90         float top = frustum_h*(frustum_y+1.0f);
91         MatrixStack::projection() = Matrix::frustum(left, right, bottom, top, clip_near, clip_far);
92         MatrixStack::modelview() = view_matrix;
93 }
94
95 void Camera::update_object_matrix()
96 {
97         Vector3 right_dir = normalize(cross(look_dir, up_dir));
98         LinAl::Vector<double, 4> columns[4];
99         columns[0] = LinAl::Vector<double, 4>(right_dir, 0.0f);
100         columns[1] = LinAl::Vector<double, 4>(cross(right_dir, look_dir), 0.0f);
101         columns[2] = LinAl::Vector<double, 4>(-look_dir, 0.0f);
102         columns[3] = LinAl::Vector<double, 4>(position, 1.0f);
103         object_matrix = Matrix::from_columns(columns);
104         view_matrix = invert(object_matrix);
105 }
106
107 } // namespace GL
108 } // namespace Msp