9 fov(Geometry::Angle<float>::from_turns(0.125)),
19 update_projection_matrix();
20 update_object_matrix();
23 void Camera::set_field_of_view(const Geometry::Angle<float> &f)
26 update_projection_matrix();
29 void Camera::set_aspect(float a)
32 update_projection_matrix();
35 void Camera::set_depth_clip(float n, float f)
39 update_projection_matrix();
42 void Camera::set_frustum_axis(float x, float y)
46 update_projection_matrix();
49 void Camera::set_position(const Vector3 &p)
52 update_object_matrix();
55 void Camera::set_up_direction(const Vector3 &u)
57 up_dir = normalize(u);
58 update_object_matrix();
61 void Camera::set_look_direction(const Vector3 &l)
63 look_dir = normalize(l);
64 update_object_matrix();
67 void Camera::look_at(const Vector3 &p)
69 set_look_direction(p-position);
72 Vector3 Camera::project(const Vector4 &p) const
74 Vector4 r = proj_matrix*(view_matrix*p);
75 return Vector3(r)/r.w;
78 Vector3 Camera::project(const Vector3 &p) const
80 return project(Vector4(p.x, p.y, p.z, 1.0));
83 Vector4 Camera::unproject(const Vector4 &p) const
85 Vector4 r = invert(proj_matrix)*Vector4(p.x, p.y, p.z, 1.0f);
86 r = object_matrix*Vector4(r.x, r.y, r.z, p.w);
90 void Camera::apply() const
92 MatrixStack::projection() = proj_matrix;
93 MatrixStack::modelview() = view_matrix;
96 void Camera::update_projection_matrix()
98 float frustum_h = tan(fov/2.0f)*clip_near;
99 float frustum_w = frustum_h*aspect;
100 float left = frustum_w*(frustum_x-1.0f);
101 float right = frustum_w*(frustum_x+1.0f);
102 float bottom = frustum_h*(frustum_y-1.0f);
103 float top = frustum_h*(frustum_y+1.0f);
104 proj_matrix = Matrix::frustum(left, right, bottom, top, clip_near, clip_far);
107 void Camera::update_object_matrix()
109 Vector3 right_dir = normalize(cross(look_dir, up_dir));
111 columns[0] = Vector4(right_dir, 0.0f);
112 columns[1] = Vector4(cross(right_dir, look_dir), 0.0f);
113 columns[2] = Vector4(-look_dir, 0.0f);
114 columns[3] = Vector4(position, 1.0f);
115 object_matrix = Matrix::from_columns(columns);
116 view_matrix = invert(object_matrix);