11 fov(Geometry::Angle<float>::from_turns(0.125)),
22 update_projection_matrix();
23 update_object_matrix();
26 void Camera::set_field_of_view(const Geometry::Angle<float> &f)
29 update_projection_matrix();
32 void Camera::set_orthographic(float w, float h)
34 fov = Geometry::Angle<float>::zero();
38 update_projection_matrix();
41 void Camera::set_aspect_ratio(float a)
44 update_projection_matrix();
47 void Camera::set_depth_clip(float n, float f)
51 update_projection_matrix();
54 void Camera::set_frustum_axis(float x, float y)
58 update_projection_matrix();
61 void Camera::set_frustum_rotation(const Geometry::Angle<float> &r)
64 update_projection_matrix();
67 void Camera::set_position(const Vector3 &p)
70 update_object_matrix();
73 void Camera::set_look_direction(const Vector3 &l)
75 look_dir = normalize(l);
76 update_object_matrix();
79 void Camera::look_at(const Vector3 &p)
81 set_look_direction(p-position);
84 void Camera::set_up_direction(const Vector3 &u)
86 up_dir = normalize(u);
87 update_object_matrix();
90 void Camera::set_object_matrix(const Matrix &m)
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();
98 Vector3 Camera::project(const Vector4 &p) const
100 Vector4 r = proj_matrix*(view_matrix*p);
101 return r.slice<3>(0)/r.w;
104 Vector3 Camera::project(const Vector3 &p) const
106 return project(Vector4(p.x, p.y, p.z, 1.0));
109 Vector4 Camera::unproject(const Vector4 &p) const
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);
116 Vector3 Camera::unproject(const Vector3 &p) const
118 return unproject(Vector4(p.x, p.y, p.z, 1.0f)).slice<3>(0);
121 void Camera::update_projection_matrix()
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);
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;
135 shdata.uniform("clip_eye_matrix", proj_matrix);
136 shdata.uniform("eye_clip_matrix", invert(proj_matrix));
139 void Camera::update_object_matrix()
141 Vector3 right_dir = normalize(cross(look_dir, up_dir));
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);
150 shdata.uniform("world_eye_matrix", matrix);
151 shdata.uniform("eye_world_matrix", view_matrix);
154 void Camera::set_debug_name(const string &name)
157 shdata.set_debug_name(name+" [UBO]");
164 Camera::Loader::Loader(Camera &c):
165 DataFile::ObjectLoader<Camera>(c)
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);
177 void Camera::Loader::aspect_ratio(float a)
179 obj.set_aspect_ratio(a);
182 void Camera::Loader::depth_clip(float n, float f)
184 obj.set_depth_clip(n, f);
187 void Camera::Loader::field_of_view(float a)
189 obj.set_field_of_view(Geometry::Angle<float>::from_degrees(a));
192 void Camera::Loader::look_at(float x, float y, float z)
194 obj.look_at(Vector3(x, y, z));
197 void Camera::Loader::look_direction(float x, float y, float z)
199 obj.set_look_direction(Vector3(x, y, z));
202 void Camera::Loader::orthographic(float w, float h)
204 obj.set_orthographic(w, h);
207 void Camera::Loader::position(float x, float y, float z)
209 obj.set_position(Vector3(x, y, z));
212 void Camera::Loader::up_direction(float x, float y, float z)
214 obj.set_up_direction(Vector3(x, y, z));