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(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_up_direction(const Vector3 &u)
73 up_dir = normalize(u);
74 update_object_matrix();
77 void Camera::set_look_direction(const Vector3 &l)
79 look_dir = normalize(l);
80 update_object_matrix();
83 void Camera::look_at(const Vector3 &p)
85 set_look_direction(p-position);
88 Vector3 Camera::project(const Vector4 &p) const
90 Vector4 r = proj_matrix*(view_matrix*p);
91 return r.slice<3>(0)/r.w;
94 Vector3 Camera::project(const Vector3 &p) const
96 return project(Vector4(p.x, p.y, p.z, 1.0));
99 Vector4 Camera::unproject(const Vector4 &p) const
101 Vector4 r = invert(proj_matrix)*Vector4(p.x, p.y, p.z, 1.0f);
102 r = object_matrix*Vector4(r.x, r.y, r.z, p.w);
106 void Camera::apply() const
108 MatrixStack::projection() = proj_matrix;
109 MatrixStack::modelview() = view_matrix;
112 void Camera::update_projection_matrix()
114 float frustum_h = (fov!=Geometry::Angle<float>::zero() ? tan(fov/2.0f)*clip_near : height/2);
115 float frustum_w = frustum_h*aspect;
116 float left = frustum_w*(frustum_x-1.0f);
117 float right = frustum_w*(frustum_x+1.0f);
118 float bottom = frustum_h*(frustum_y-1.0f);
119 float top = frustum_h*(frustum_y+1.0f);
120 if(fov>Geometry::Angle<float>::zero())
121 proj_matrix = Matrix::frustum(left, right, bottom, top, clip_near, clip_far);
123 proj_matrix = Matrix::ortho(left, right, bottom, top, clip_near, clip_far);
124 proj_matrix = Matrix::rotation(rotate, Vector3(0, 0, 1))*proj_matrix;
127 void Camera::update_object_matrix()
129 Vector3 right_dir = normalize(cross(look_dir, up_dir));
131 columns[0] = compose(right_dir, 0.0f);
132 columns[1] = compose(cross(right_dir, look_dir), 0.0f);
133 columns[2] = compose(-look_dir, 0.0f);
134 columns[3] = compose(position, 1.0f);
135 object_matrix = Matrix::from_columns(columns);
136 view_matrix = invert(object_matrix);