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