]> git.tdb.fi Git - libs/gl.git/blob - source/render/camera.cpp
Add support for integer vertex attributes
[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         shdata.uniform("eye_clip_matrix", invert(proj_matrix));
137 }
138
139 void Camera::update_object_matrix()
140 {
141         Vector3 right_dir = normalize(cross(look_dir, up_dir));
142         Vector4 columns[4];
143         columns[0] = compose(right_dir, 0.0f);
144         columns[1] = compose(cross(right_dir, look_dir), 0.0f);
145         columns[2] = compose(-look_dir, 0.0f);
146         columns[3] = compose(position, 1.0f);
147         matrix = Matrix::from_columns(columns);
148         view_matrix = invert(matrix);
149
150         shdata.uniform("world_eye_matrix", matrix);
151         shdata.uniform("eye_world_matrix", view_matrix);
152 }
153
154 void Camera::set_debug_name(const string &name)
155 {
156 #ifdef DEBUG
157         shdata.set_debug_name(name+" [UBO]");
158 #else
159         (void)name;
160 #endif
161 }
162
163
164 Camera::Loader::Loader(Camera &c):
165         DataFile::ObjectLoader<Camera>(c)
166 {
167         add("aspect_ratio", &Loader::aspect_ratio);
168         add("depth_clip", &Loader::depth_clip);
169         add("field_of_view", &Loader::field_of_view);
170         add("look_at", &Loader::look_at);
171         add("look_direction", &Loader::look_direction);
172         add("orthographic", &Loader::orthographic);
173         add("position", &Loader::position);
174         add("up_direction", &Loader::up_direction);
175 }
176
177 void Camera::Loader::aspect_ratio(float a)
178 {
179         obj.set_aspect_ratio(a);
180 }
181
182 void Camera::Loader::depth_clip(float n, float f)
183 {
184         obj.set_depth_clip(n, f);
185 }
186
187 void Camera::Loader::field_of_view(float a)
188 {
189         obj.set_field_of_view(Geometry::Angle<float>::from_degrees(a));
190 }
191
192 void Camera::Loader::look_at(float x, float y, float z)
193 {
194         obj.look_at(Vector3(x, y, z));
195 }
196
197 void Camera::Loader::look_direction(float x, float y, float z)
198 {
199         obj.set_look_direction(Vector3(x, y, z));
200 }
201
202 void Camera::Loader::orthographic(float w, float h)
203 {
204         obj.set_orthographic(w, h);
205 }
206
207 void Camera::Loader::position(float x, float y, float z)
208 {
209         obj.set_position(Vector3(x, y, z));
210 }
211
212 void Camera::Loader::up_direction(float x, float y, float z)
213 {
214         obj.set_up_direction(Vector3(x, y, z));
215 }
216
217 } // namespace GL
218 } // namespace Msp