]> git.tdb.fi Git - libs/gl.git/blob - source/render/camera.cpp
Use default member initializers for simple types
[libs/gl.git] / source / render / camera.cpp
1 #include <cmath>
2 #include "camera.h"
3 #include "matrix.h"
4
5 using namespace std;
6
7 namespace Msp {
8 namespace GL {
9
10 Camera::Camera()
11 {
12         update_projection_matrix();
13         update_object_matrix();
14 }
15
16 void Camera::set_field_of_view(const Geometry::Angle<float> &f)
17 {
18         fov = f;
19         update_projection_matrix();
20 }
21
22 void Camera::set_orthographic(float w, float h)
23 {
24         fov = Geometry::Angle<float>::zero();
25         height = h;
26         if(w)
27                 aspect = w/h;
28         update_projection_matrix();
29 }
30
31 void Camera::set_aspect_ratio(float a)
32 {
33         aspect = a;
34         update_projection_matrix();
35 }
36
37 void Camera::set_depth_clip(float n, float f)
38 {
39         clip_near = n;
40         clip_far = f;
41         update_projection_matrix();
42 }
43
44 void Camera::set_frustum_axis(float x, float y)
45 {
46         frustum_x = x;
47         frustum_y = y;
48         update_projection_matrix();
49 }
50
51 void Camera::set_frustum_rotation(const Geometry::Angle<float> &r)
52 {
53         rotate = r;
54         update_projection_matrix();
55 }
56
57 void Camera::set_position(const Vector3 &p)
58 {
59         position = p;
60         update_object_matrix();
61 }
62
63 void Camera::set_look_direction(const Vector3 &l)
64 {
65         look_dir = normalize(l);
66         update_object_matrix();
67 }
68
69 void Camera::look_at(const Vector3 &p)
70 {
71         set_look_direction(p-position);
72 }
73
74 void Camera::set_up_direction(const Vector3 &u)
75 {
76         up_dir = normalize(u);
77         update_object_matrix();
78 }
79
80 void Camera::set_object_matrix(const Matrix &m)
81 {
82         position = m.column(3).slice<3>(0);
83         look_dir = normalize(-m.column(2).slice<3>(0));
84         up_dir = normalize(m.column(1).slice<3>(0));
85         update_object_matrix();
86 }
87
88 Vector3 Camera::project(const Vector4 &p) const
89 {
90         Vector4 r = proj_matrix*(view_matrix*p);
91         return r.slice<3>(0)/r.w;
92 }
93
94 Vector3 Camera::project(const Vector3 &p) const
95 {
96         return project(Vector4(p.x, p.y, p.z, 1.0));
97 }
98
99 Vector4 Camera::unproject(const Vector4 &p) const
100 {
101         Vector4 r = invert(proj_matrix)*Vector4(p.x, p.y, p.z, 1.0f);
102         r = matrix*Vector4(r.x/r.w, r.y/r.w, r.z/r.w, p.w);
103         return r;
104 }
105
106 Vector3 Camera::unproject(const Vector3 &p) const
107 {
108         return unproject(Vector4(p.x, p.y, p.z, 1.0f)).slice<3>(0);
109 }
110
111 void Camera::update_projection_matrix()
112 {
113         float frustum_h = (fov!=Geometry::Angle<float>::zero() ? tan(fov/2.0f)*clip_near : height/2);
114         float frustum_w = frustum_h*aspect;
115         float left = frustum_w*(frustum_x-1.0f);
116         float right = frustum_w*(frustum_x+1.0f);
117         float bottom = frustum_h*(frustum_y-1.0f);
118         float top = frustum_h*(frustum_y+1.0f);
119         if(fov>Geometry::Angle<float>::zero())
120                 proj_matrix = Matrix::frustum(left, right, bottom, top, clip_near, clip_far);
121         else
122                 proj_matrix = Matrix::ortho(left, right, bottom, top, clip_near, clip_far);
123         proj_matrix = Matrix::rotation(rotate, Vector3(0, 0, 1))*proj_matrix;
124
125         shdata.uniform("clip_eye_matrix", proj_matrix);
126         shdata.uniform("eye_clip_matrix", invert(proj_matrix));
127 }
128
129 void Camera::update_object_matrix()
130 {
131         Vector3 right_dir = normalize(cross(look_dir, up_dir));
132         Vector4 columns[4];
133         columns[0] = compose(right_dir, 0.0f);
134         columns[1] = compose(cross(right_dir, look_dir), 0.0f);
135         columns[2] = compose(-look_dir, 0.0f);
136         columns[3] = compose(position, 1.0f);
137         matrix = Matrix::from_columns(columns);
138         view_matrix = invert(matrix);
139
140         shdata.uniform("world_eye_matrix", matrix);
141         shdata.uniform("eye_world_matrix", view_matrix);
142 }
143
144 void Camera::set_debug_name(const string &name)
145 {
146 #ifdef DEBUG
147         shdata.set_debug_name(name+" [UBO]");
148 #else
149         (void)name;
150 #endif
151 }
152
153
154 Camera::Loader::Loader(Camera &c):
155         DataFile::ObjectLoader<Camera>(c)
156 {
157         add("aspect_ratio", &Loader::aspect_ratio);
158         add("depth_clip", &Loader::depth_clip);
159         add("field_of_view", &Loader::field_of_view);
160         add("look_at", &Loader::look_at);
161         add("look_direction", &Loader::look_direction);
162         add("orthographic", &Loader::orthographic);
163         add("position", &Loader::position);
164         add("up_direction", &Loader::up_direction);
165 }
166
167 void Camera::Loader::aspect_ratio(float a)
168 {
169         obj.set_aspect_ratio(a);
170 }
171
172 void Camera::Loader::depth_clip(float n, float f)
173 {
174         obj.set_depth_clip(n, f);
175 }
176
177 void Camera::Loader::field_of_view(float a)
178 {
179         obj.set_field_of_view(Geometry::Angle<float>::from_degrees(a));
180 }
181
182 void Camera::Loader::look_at(float x, float y, float z)
183 {
184         obj.look_at(Vector3(x, y, z));
185 }
186
187 void Camera::Loader::look_direction(float x, float y, float z)
188 {
189         obj.set_look_direction(Vector3(x, y, z));
190 }
191
192 void Camera::Loader::orthographic(float w, float h)
193 {
194         obj.set_orthographic(w, h);
195 }
196
197 void Camera::Loader::position(float x, float y, float z)
198 {
199         obj.set_position(Vector3(x, y, z));
200 }
201
202 void Camera::Loader::up_direction(float x, float y, float z)
203 {
204         obj.set_up_direction(Vector3(x, y, z));
205 }
206
207 } // namespace GL
208 } // namespace Msp