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.w, r.y/r.w, r.z/r.w, p.w);
106 Vector3 Camera::unproject(const Vector3 &p) const
108 return unproject(Vector4(p.x, p.y, p.z, 1.0f)).slice<3>(0);
111 void Camera::apply() const
113 MatrixStack::projection() = proj_matrix;
114 MatrixStack::modelview() = view_matrix;
117 void Camera::update_projection_matrix()
119 float frustum_h = (fov!=Geometry::Angle<float>::zero() ? tan(fov/2.0f)*clip_near : height/2);
120 float frustum_w = frustum_h*aspect;
121 float left = frustum_w*(frustum_x-1.0f);
122 float right = frustum_w*(frustum_x+1.0f);
123 float bottom = frustum_h*(frustum_y-1.0f);
124 float top = frustum_h*(frustum_y+1.0f);
125 if(fov>Geometry::Angle<float>::zero())
126 proj_matrix = Matrix::frustum(left, right, bottom, top, clip_near, clip_far);
128 proj_matrix = Matrix::ortho(left, right, bottom, top, clip_near, clip_far);
129 proj_matrix = Matrix::rotation(rotate, Vector3(0, 0, 1))*proj_matrix;
132 void Camera::update_object_matrix()
134 Vector3 right_dir = normalize(cross(look_dir, up_dir));
136 columns[0] = compose(right_dir, 0.0f);
137 columns[1] = compose(cross(right_dir, look_dir), 0.0f);
138 columns[2] = compose(-look_dir, 0.0f);
139 columns[3] = compose(position, 1.0f);
140 object_matrix = Matrix::from_columns(columns);
141 view_matrix = invert(object_matrix);