9 fov(Geometry::Angle<float>::from_turns(0.125)),
20 update_projection_matrix();
21 update_object_matrix();
24 void Camera::set_field_of_view(const Geometry::Angle<float> &f)
27 update_projection_matrix();
30 void Camera::set_orthographic(float w, float h)
32 fov = Geometry::Angle<float>::zero();
36 update_projection_matrix();
39 void Camera::set_aspect_ratio(float a)
42 update_projection_matrix();
45 void Camera::set_depth_clip(float n, float f)
49 update_projection_matrix();
52 void Camera::set_frustum_axis(float x, float y)
56 update_projection_matrix();
59 void Camera::set_frustum_rotation(const Geometry::Angle<float> &r)
62 update_projection_matrix();
65 void Camera::set_position(const Vector3 &p)
68 update_object_matrix();
71 void Camera::set_look_direction(const Vector3 &l)
73 look_dir = normalize(l);
74 update_object_matrix();
77 void Camera::look_at(const Vector3 &p)
79 set_look_direction(p-position);
82 void Camera::set_up_direction(const Vector3 &u)
84 up_dir = normalize(u);
85 update_object_matrix();
88 void Camera::set_object_matrix(const Matrix &m)
90 position = m.column(3).slice<3>(0);
91 look_dir = normalize(-m.column(2).slice<3>(0));
92 up_dir = normalize(m.column(1).slice<3>(0));
93 update_object_matrix();
96 Vector3 Camera::project(const Vector4 &p) const
98 Vector4 r = proj_matrix*(view_matrix*p);
99 return r.slice<3>(0)/r.w;
102 Vector3 Camera::project(const Vector3 &p) const
104 return project(Vector4(p.x, p.y, p.z, 1.0));
107 Vector4 Camera::unproject(const Vector4 &p) const
109 Vector4 r = invert(proj_matrix)*Vector4(p.x, p.y, p.z, 1.0f);
110 r = matrix*Vector4(r.x/r.w, r.y/r.w, r.z/r.w, p.w);
114 Vector3 Camera::unproject(const Vector3 &p) const
116 return unproject(Vector4(p.x, p.y, p.z, 1.0f)).slice<3>(0);
119 void Camera::apply() const
121 MatrixStack::projection() = proj_matrix;
122 MatrixStack::modelview() = view_matrix;
125 void Camera::update_projection_matrix()
127 float frustum_h = (fov!=Geometry::Angle<float>::zero() ? tan(fov/2.0f)*clip_near : height/2);
128 float frustum_w = frustum_h*aspect;
129 float left = frustum_w*(frustum_x-1.0f);
130 float right = frustum_w*(frustum_x+1.0f);
131 float bottom = frustum_h*(frustum_y-1.0f);
132 float top = frustum_h*(frustum_y+1.0f);
133 if(fov>Geometry::Angle<float>::zero())
134 proj_matrix = Matrix::frustum(left, right, bottom, top, clip_near, clip_far);
136 proj_matrix = Matrix::ortho(left, right, bottom, top, clip_near, clip_far);
137 proj_matrix = Matrix::rotation(rotate, Vector3(0, 0, 1))*proj_matrix;
140 void Camera::update_object_matrix()
142 Vector3 right_dir = normalize(cross(look_dir, up_dir));
144 columns[0] = compose(right_dir, 0.0f);
145 columns[1] = compose(cross(right_dir, look_dir), 0.0f);
146 columns[2] = compose(-look_dir, 0.0f);
147 columns[3] = compose(position, 1.0f);
148 matrix = Matrix::from_columns(columns);
149 view_matrix = invert(matrix);
153 Camera::Loader::Loader(Camera &c):
154 DataFile::ObjectLoader<Camera>(c)
156 add("aspect_ratio", &Loader::aspect_ratio);
157 add("depth_clip", &Loader::depth_clip);
158 add("field_of_view", &Loader::field_of_view);
159 add("look_at", &Loader::look_at);
160 add("look_direction", &Loader::look_direction);
161 add("orthographic", &Loader::orthographic);
162 add("position", &Loader::position);
163 add("up_direction", &Loader::up_direction);
166 void Camera::Loader::aspect_ratio(float a)
168 obj.set_aspect_ratio(a);
171 void Camera::Loader::depth_clip(float n, float f)
173 obj.set_depth_clip(n, f);
176 void Camera::Loader::field_of_view(float a)
178 obj.set_field_of_view(Geometry::Angle<float>::from_degrees(a));
181 void Camera::Loader::look_at(float x, float y, float z)
183 obj.look_at(Vector3(x, y, z));
186 void Camera::Loader::look_direction(float x, float y, float z)
188 obj.set_look_direction(Vector3(x, y, z));
191 void Camera::Loader::orthographic(float w, float h)
193 obj.set_orthographic(w, h);
196 void Camera::Loader::position(float x, float y, float z)
198 obj.set_position(Vector3(x, y, z));
201 void Camera::Loader::up_direction(float x, float y, float z)
203 obj.set_up_direction(Vector3(x, y, z));