]> git.tdb.fi Git - libs/gl.git/blob - source/camera.cpp
Initialize camera matrices in constructor
[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         update_projection_matrix();
20         update_object_matrix();
21 }
22
23 void Camera::set_field_of_view(const Geometry::Angle<float> &f)
24 {
25         fov = f;
26         update_projection_matrix();
27 }
28
29 void Camera::set_aspect(float a)
30 {
31         aspect = a;
32         update_projection_matrix();
33 }
34
35 void Camera::set_depth_clip(float n, float f)
36 {
37         clip_near = n;
38         clip_far = f;
39         update_projection_matrix();
40 }
41
42 void Camera::set_frustum_axis(float x, float y)
43 {
44         frustum_x = x;
45         frustum_y = y;
46         update_projection_matrix();
47 }
48
49 void Camera::set_position(const Vector3 &p)
50 {
51         position = p;
52         update_object_matrix();
53 }
54
55 void Camera::set_up_direction(const Vector3 &u)
56 {
57         up_dir = normalize(u);
58         update_object_matrix();
59 }
60
61 void Camera::set_look_direction(const Vector3 &l)
62 {
63         look_dir = normalize(l);
64         update_object_matrix();
65 }
66
67 void Camera::look_at(const Vector3 &p)
68 {
69         set_look_direction(p-position);
70 }
71
72 Vector3 Camera::project(const Vector4 &p) const
73 {
74         Vector4 r = proj_matrix*(view_matrix*p);
75         return Vector3(r)/r.w;
76 }
77
78 Vector3 Camera::project(const Vector3 &p) const
79 {
80         return project(Vector4(p.x, p.y, p.z, 1.0));
81 }
82
83 Vector4 Camera::unproject(const Vector4 &p) const
84 {
85         Vector4 r = invert(proj_matrix)*Vector4(p.x, p.y, p.z, 1.0f);
86         r = object_matrix*Vector4(r.x, r.y, r.z, p.w);
87         return r;
88 }
89
90 void Camera::apply() const
91 {
92         MatrixStack::projection() = proj_matrix;
93         MatrixStack::modelview() = view_matrix;
94 }
95
96 void Camera::update_projection_matrix()
97 {
98         float frustum_h = tan(fov/2.0f)*clip_near;
99         float frustum_w = frustum_h*aspect;
100         float left = frustum_w*(frustum_x-1.0f);
101         float right = frustum_w*(frustum_x+1.0f);
102         float bottom = frustum_h*(frustum_y-1.0f);
103         float top = frustum_h*(frustum_y+1.0f);
104         proj_matrix = Matrix::frustum(left, right, bottom, top, clip_near, clip_far);
105 }
106
107 void Camera::update_object_matrix()
108 {
109         Vector3 right_dir = normalize(cross(look_dir, up_dir));
110         Vector4 columns[4];
111         columns[0] = Vector4(right_dir, 0.0f);
112         columns[1] = Vector4(cross(right_dir, look_dir), 0.0f);
113         columns[2] = Vector4(-look_dir, 0.0f);
114         columns[3] = Vector4(position, 1.0f);
115         object_matrix = Matrix::from_columns(columns);
116         view_matrix = invert(object_matrix);
117 }
118
119 } // namespace GL
120 } // namespace Msp