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::update_projection_matrix()
121 float frustum_h = (fov!=Geometry::Angle<float>::zero() ? tan(fov/2.0f)*clip_near : height/2);
122 float frustum_w = frustum_h*aspect;
123 float left = frustum_w*(frustum_x-1.0f);
124 float right = frustum_w*(frustum_x+1.0f);
125 float bottom = frustum_h*(frustum_y-1.0f);
126 float top = frustum_h*(frustum_y+1.0f);
127 if(fov>Geometry::Angle<float>::zero())
128 proj_matrix = Matrix::frustum(left, right, bottom, top, clip_near, clip_far);
130 proj_matrix = Matrix::ortho(left, right, bottom, top, clip_near, clip_far);
131 proj_matrix = Matrix::rotation(rotate, Vector3(0, 0, 1))*proj_matrix;
134 void Camera::update_object_matrix()
136 Vector3 right_dir = normalize(cross(look_dir, up_dir));
138 columns[0] = compose(right_dir, 0.0f);
139 columns[1] = compose(cross(right_dir, look_dir), 0.0f);
140 columns[2] = compose(-look_dir, 0.0f);
141 columns[3] = compose(position, 1.0f);
142 matrix = Matrix::from_columns(columns);
143 view_matrix = invert(matrix);
147 Camera::Loader::Loader(Camera &c):
148 DataFile::ObjectLoader<Camera>(c)
150 add("aspect_ratio", &Loader::aspect_ratio);
151 add("depth_clip", &Loader::depth_clip);
152 add("field_of_view", &Loader::field_of_view);
153 add("look_at", &Loader::look_at);
154 add("look_direction", &Loader::look_direction);
155 add("orthographic", &Loader::orthographic);
156 add("position", &Loader::position);
157 add("up_direction", &Loader::up_direction);
160 void Camera::Loader::aspect_ratio(float a)
162 obj.set_aspect_ratio(a);
165 void Camera::Loader::depth_clip(float n, float f)
167 obj.set_depth_clip(n, f);
170 void Camera::Loader::field_of_view(float a)
172 obj.set_field_of_view(Geometry::Angle<float>::from_degrees(a));
175 void Camera::Loader::look_at(float x, float y, float z)
177 obj.look_at(Vector3(x, y, z));
180 void Camera::Loader::look_direction(float x, float y, float z)
182 obj.set_look_direction(Vector3(x, y, z));
185 void Camera::Loader::orthographic(float w, float h)
187 obj.set_orthographic(w, h);
190 void Camera::Loader::position(float x, float y, float z)
192 obj.set_position(Vector3(x, y, z));
195 void Camera::Loader::up_direction(float x, float y, float z)
197 obj.set_up_direction(Vector3(x, y, z));