18 void Camera::set_field_of_view(float f)
23 void Camera::set_aspect(float a)
28 void Camera::set_depth_clip(float n, float f)
34 void Camera::set_position(const Vector3 &p)
40 void Camera::set_up_direction(const Vector3 &u)
42 float len = sqrt(u.x*u.x+u.y*u.y+u.z*u.z);
51 void Camera::set_look_direction(const Vector3 &l)
53 float len = sqrt(l.x*l.x+l.y*l.y+l.z*l.z);
62 void Camera::look_at(const Vector3 &p)
64 set_look_direction(Vector3(p.x-position.x, p.y-position.y, p.z-position.z));
67 Vector3 Camera::project(const Vector4 &p) const
69 float frustum_h = tan(fov/2);
70 float frustum_w = frustum_h*aspect;
71 float z_range = clip_far-clip_near;
73 float eye_x = matrix[0]*p.x+matrix[4]*p.y+matrix[8]*p.z+matrix[12]*p.w;
74 float eye_y = matrix[1]*p.x+matrix[5]*p.y+matrix[9]*p.z+matrix[13]*p.w;
75 float eye_z = matrix[2]*p.x+matrix[6]*p.y+matrix[10]*p.z+matrix[14]*p.w;
77 return Vector3(eye_x/frustum_w/-eye_z, eye_y/frustum_h/-eye_z,
78 (clip_far+clip_near)/z_range+2*clip_far*clip_near/(eye_z*z_range));
81 Vector4 Camera::unproject(const Vector4 &p) const
83 float frustum_h = tan(fov/2);
84 float frustum_w = frustum_h*aspect;
85 float z_range = clip_far-clip_near;
87 float z = (2*clip_far*clip_near)/(p.z*z_range-(clip_far+clip_near))-matrix[14]*p.w;
88 float x = p.x*-z*frustum_w-matrix[12]*p.w;
89 float y = p.y*-z*frustum_h-matrix[13]*p.w;
91 return Vector4(matrix[0]*x+matrix[1]*y+matrix[2]*z,
92 matrix[4]*x+matrix[5]*y+matrix[6]*z,
93 matrix[8]*x+matrix[9]*y+matrix[10]*z,
97 void Camera::apply() const
99 MatrixStack::projection() = Matrix::perspective(fov, aspect, clip_near, clip_far);
100 MatrixStack::modelview() = matrix;
103 void Camera::compute_matrix()
105 float x = look_dir.y*up_dir.z-look_dir.z*up_dir.y;
106 float y = look_dir.z*up_dir.x-look_dir.x*up_dir.z;
107 float z = look_dir.x*up_dir.y-look_dir.y*up_dir.x;
108 float len = sqrt(x*x+y*y+z*z);
115 mdata[1] = mdata[4]*look_dir.z-mdata[8]*look_dir.y;
116 mdata[5] = mdata[8]*look_dir.x-mdata[0]*look_dir.z;
117 mdata[9] = mdata[0]*look_dir.y-mdata[4]*look_dir.x;
119 mdata[2] = -look_dir.x;
120 mdata[6] = -look_dir.y;
121 mdata[10] = -look_dir.z;
123 mdata[12] = -position.x*mdata[0]-position.y*mdata[4]-position.z*mdata[8];
124 mdata[13] = -position.x*mdata[1]-position.y*mdata[5]-position.z*mdata[9];
125 mdata[14] = -position.x*mdata[2]-position.y*mdata[6]-position.z*mdata[10];