nuclear@1: #include "camera.h" nuclear@15: #include "rayzor.h" nuclear@1: nuclear@1: Camera::Camera() nuclear@1: { nuclear@12: type = NODE_CAMERA; nuclear@15: fov = M_PI / 4.0; nuclear@12: set_position(Vector3(0, 0, 10)); nuclear@1: } nuclear@1: nuclear@1: void Camera::set_target(const Vector3 &target) nuclear@1: { nuclear@13: this->target.set_position(target); nuclear@1: } nuclear@1: nuclear@13: Vector3 Camera::get_target() const nuclear@1: { nuclear@13: return target.get_position(); nuclear@1: } nuclear@1: nuclear@1: void Camera::set_fov(float fov) nuclear@1: { nuclear@1: this->fov = fov; nuclear@1: } nuclear@1: nuclear@1: float Camera::get_fov() const nuclear@1: { nuclear@1: return fov; nuclear@1: } nuclear@1: nuclear@12: void Camera::calc_matrix() const nuclear@1: { nuclear@15: if(!xform_valid) { nuclear@15: xform.set_identity(); nuclear@15: xform.lookat(pos, target.get_position(), Vector3(0, 1, 0)); nuclear@15: xform_valid = true; nuclear@15: } nuclear@1: } nuclear@12: nuclear@12: void Camera::draw() const nuclear@12: { nuclear@12: } nuclear@12: nuclear@17: bool Camera::intersect(const Ray &ray, RayHit *hit) const nuclear@12: { nuclear@12: return false; nuclear@12: } nuclear@15: nuclear@15: Ray Camera::get_primary_ray(int x, int y) const nuclear@15: { nuclear@15: calc_inv_matrix(); nuclear@15: nuclear@15: float aspect = (float)fb_width / (float)fb_height; nuclear@15: float pwidth = 2.0 * aspect / (float)fb_width; nuclear@15: float pheight = 2.0 / (float)fb_height; nuclear@15: nuclear@15: Vector3 dir; nuclear@15: dir.x = (float)x * pwidth - aspect; nuclear@15: dir.y = 1.0 - (float)y * pheight; nuclear@15: dir.z = -1.0 / tan(fov / 2.0); nuclear@15: dir.normalize(); nuclear@15: nuclear@15: return transform(inv_xform, Ray(Vector3(0, 0, 0), dir)); nuclear@15: }