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);
138 void Camera::update_object_matrix()
140 Vector3 right_dir = normalize(cross(look_dir, up_dir));
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);
149 shdata.uniform("eye_world_matrix", view_matrix);
152 void Camera::set_debug_name(const string &name)
155 shdata.set_debug_name(name+" [UBO]");
162 Camera::Loader::Loader(Camera &c):
163 DataFile::ObjectLoader<Camera>(c)
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);
175 void Camera::Loader::aspect_ratio(float a)
177 obj.set_aspect_ratio(a);
180 void Camera::Loader::depth_clip(float n, float f)
182 obj.set_depth_clip(n, f);
185 void Camera::Loader::field_of_view(float a)
187 obj.set_field_of_view(Geometry::Angle<float>::from_degrees(a));
190 void Camera::Loader::look_at(float x, float y, float z)
192 obj.look_at(Vector3(x, y, z));
195 void Camera::Loader::look_direction(float x, float y, float z)
197 obj.set_look_direction(Vector3(x, y, z));
200 void Camera::Loader::orthographic(float w, float h)
202 obj.set_orthographic(w, h);
205 void Camera::Loader::position(float x, float y, float z)
207 obj.set_position(Vector3(x, y, z));
210 void Camera::Loader::up_direction(float x, float y, float z)
212 obj.set_up_direction(Vector3(x, y, z));