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