12 update_projection_matrix();
13 update_object_matrix();
16 void Camera::set_field_of_view(const Geometry::Angle<float> &f)
19 update_projection_matrix();
22 void Camera::set_orthographic(float w, float h)
24 fov = Geometry::Angle<float>::zero();
28 update_projection_matrix();
31 void Camera::set_aspect_ratio(float a)
34 update_projection_matrix();
37 void Camera::set_depth_clip(float n, float f)
41 update_projection_matrix();
44 void Camera::set_frustum_axis(float x, float y)
48 update_projection_matrix();
51 void Camera::set_frustum_rotation(const Geometry::Angle<float> &r)
54 update_projection_matrix();
57 void Camera::set_position(const Vector3 &p)
60 update_object_matrix();
63 void Camera::set_look_direction(const Vector3 &l)
65 look_dir = normalize(l);
66 update_object_matrix();
69 void Camera::look_at(const Vector3 &p)
71 set_look_direction(p-position);
74 void Camera::set_up_direction(const Vector3 &u)
76 up_dir = normalize(u);
77 update_object_matrix();
80 void Camera::set_object_matrix(const Matrix &m)
82 position = m.column(3).slice<3>(0);
83 look_dir = normalize(-m.column(2).slice<3>(0));
84 up_dir = normalize(m.column(1).slice<3>(0));
85 update_object_matrix();
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 = 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::update_projection_matrix()
113 float frustum_h = (fov!=Geometry::Angle<float>::zero() ? tan(fov/2.0f)*clip_near : height/2);
114 float frustum_w = frustum_h*aspect;
115 float left = frustum_w*(frustum_x-1.0f);
116 float right = frustum_w*(frustum_x+1.0f);
117 float bottom = frustum_h*(frustum_y-1.0f);
118 float top = frustum_h*(frustum_y+1.0f);
119 if(fov>Geometry::Angle<float>::zero())
120 proj_matrix = Matrix::frustum(left, right, bottom, top, clip_near, clip_far);
122 proj_matrix = Matrix::ortho(left, right, bottom, top, clip_near, clip_far);
123 proj_matrix = Matrix::rotation(rotate, Vector3(0, 0, 1))*proj_matrix;
125 shdata.uniform("clip_eye_matrix", proj_matrix);
126 shdata.uniform("eye_clip_matrix", invert(proj_matrix));
129 void Camera::update_object_matrix()
131 Vector3 right_dir = normalize(cross(look_dir, up_dir));
133 columns[0] = compose(right_dir, 0.0f);
134 columns[1] = compose(cross(right_dir, look_dir), 0.0f);
135 columns[2] = compose(-look_dir, 0.0f);
136 columns[3] = compose(position, 1.0f);
137 matrix = Matrix::from_columns(columns);
138 view_matrix = invert(matrix);
140 shdata.uniform("world_eye_matrix", matrix);
141 shdata.uniform("eye_world_matrix", view_matrix);
144 void Camera::set_debug_name(const string &name)
147 shdata.set_debug_name(name+" [UBO]");
154 Camera::Loader::Loader(Camera &c):
155 DataFile::ObjectLoader<Camera>(c)
157 add("aspect_ratio", &Loader::aspect_ratio);
158 add("depth_clip", &Loader::depth_clip);
159 add("field_of_view", &Loader::field_of_view);
160 add("look_at", &Loader::look_at);
161 add("look_direction", &Loader::look_direction);
162 add("orthographic", &Loader::orthographic);
163 add("position", &Loader::position);
164 add("up_direction", &Loader::up_direction);
167 void Camera::Loader::aspect_ratio(float a)
169 obj.set_aspect_ratio(a);
172 void Camera::Loader::depth_clip(float n, float f)
174 obj.set_depth_clip(n, f);
177 void Camera::Loader::field_of_view(float a)
179 obj.set_field_of_view(Geometry::Angle<float>::from_degrees(a));
182 void Camera::Loader::look_at(float x, float y, float z)
184 obj.look_at(Vector3(x, y, z));
187 void Camera::Loader::look_direction(float x, float y, float z)
189 obj.set_look_direction(Vector3(x, y, z));
192 void Camera::Loader::orthographic(float w, float h)
194 obj.set_orthographic(w, h);
197 void Camera::Loader::position(float x, float y, float z)
199 obj.set_position(Vector3(x, y, z));
202 void Camera::Loader::up_direction(float x, float y, float z)
204 obj.set_up_direction(Vector3(x, y, z));