9 fov(Geometry::Angle<float>::from_turns(0.125)),
20 void Camera::set_field_of_view(const Geometry::Angle<float> &f)
23 update_projection_matrix();
26 void Camera::set_aspect(float a)
29 update_projection_matrix();
32 void Camera::set_depth_clip(float n, float f)
36 update_projection_matrix();
39 void Camera::set_frustum_axis(float x, float y)
43 update_projection_matrix();
46 void Camera::set_position(const Vector3 &p)
49 update_object_matrix();
52 void Camera::set_up_direction(const Vector3 &u)
54 up_dir = normalize(u);
55 update_object_matrix();
58 void Camera::set_look_direction(const Vector3 &l)
60 look_dir = normalize(l);
61 update_object_matrix();
64 void Camera::look_at(const Vector3 &p)
66 set_look_direction(p-position);
69 Vector3 Camera::project(const Vector4 &p) const
71 Vector4 r = proj_matrix*(view_matrix*p);
72 return Vector3(r)/r.w;
75 Vector3 Camera::project(const Vector3 &p) const
77 return project(Vector4(p.x, p.y, p.z, 1.0));
80 Vector4 Camera::unproject(const Vector4 &p) const
82 Vector4 r = invert(proj_matrix)*Vector4(p.x, p.y, p.z, 1.0f);
83 r = object_matrix*Vector4(r.x, r.y, r.z, p.w);
87 void Camera::apply() const
89 MatrixStack::projection() = proj_matrix;
90 MatrixStack::modelview() = view_matrix;
93 void Camera::update_projection_matrix()
95 float frustum_h = tan(fov/2.0f)*clip_near;
96 float frustum_w = frustum_h*aspect;
97 float left = frustum_w*(frustum_x-1.0f);
98 float right = frustum_w*(frustum_x+1.0f);
99 float bottom = frustum_h*(frustum_y-1.0f);
100 float top = frustum_h*(frustum_y+1.0f);
101 proj_matrix = Matrix::frustum(left, right, bottom, top, clip_near, clip_far);
104 void Camera::update_object_matrix()
106 Vector3 right_dir = normalize(cross(look_dir, up_dir));
108 columns[0] = Vector4(right_dir, 0.0f);
109 columns[1] = Vector4(cross(right_dir, look_dir), 0.0f);
110 columns[2] = Vector4(-look_dir, 0.0f);
111 columns[3] = Vector4(position, 1.0f);
112 object_matrix = Matrix::from_columns(columns);
113 view_matrix = invert(object_matrix);