rayzor

annotate src/camera.cc @ 17:79609d482762

the renderer renders, also fixed an unnoticed matrix conversion problem between scenegraph and min3d
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 14 Apr 2014 07:34:45 +0300
parents be616b58df99
children 859ccadca671
rev   line source
nuclear@1 1 #include "camera.h"
nuclear@15 2 #include "rayzor.h"
nuclear@1 3
nuclear@1 4 Camera::Camera()
nuclear@1 5 {
nuclear@12 6 type = NODE_CAMERA;
nuclear@15 7 fov = M_PI / 4.0;
nuclear@12 8 set_position(Vector3(0, 0, 10));
nuclear@1 9 }
nuclear@1 10
nuclear@1 11 void Camera::set_target(const Vector3 &target)
nuclear@1 12 {
nuclear@13 13 this->target.set_position(target);
nuclear@1 14 }
nuclear@1 15
nuclear@13 16 Vector3 Camera::get_target() const
nuclear@1 17 {
nuclear@13 18 return target.get_position();
nuclear@1 19 }
nuclear@1 20
nuclear@1 21 void Camera::set_fov(float fov)
nuclear@1 22 {
nuclear@1 23 this->fov = fov;
nuclear@1 24 }
nuclear@1 25
nuclear@1 26 float Camera::get_fov() const
nuclear@1 27 {
nuclear@1 28 return fov;
nuclear@1 29 }
nuclear@1 30
nuclear@12 31 void Camera::calc_matrix() const
nuclear@1 32 {
nuclear@15 33 if(!xform_valid) {
nuclear@15 34 xform.set_identity();
nuclear@15 35 xform.lookat(pos, target.get_position(), Vector3(0, 1, 0));
nuclear@15 36 xform_valid = true;
nuclear@15 37 }
nuclear@1 38 }
nuclear@12 39
nuclear@12 40 void Camera::draw() const
nuclear@12 41 {
nuclear@12 42 }
nuclear@12 43
nuclear@17 44 bool Camera::intersect(const Ray &ray, RayHit *hit) const
nuclear@12 45 {
nuclear@12 46 return false;
nuclear@12 47 }
nuclear@15 48
nuclear@15 49 Ray Camera::get_primary_ray(int x, int y) const
nuclear@15 50 {
nuclear@15 51 calc_inv_matrix();
nuclear@15 52
nuclear@15 53 float aspect = (float)fb_width / (float)fb_height;
nuclear@15 54 float pwidth = 2.0 * aspect / (float)fb_width;
nuclear@15 55 float pheight = 2.0 / (float)fb_height;
nuclear@15 56
nuclear@15 57 Vector3 dir;
nuclear@15 58 dir.x = (float)x * pwidth - aspect;
nuclear@15 59 dir.y = 1.0 - (float)y * pheight;
nuclear@15 60 dir.z = -1.0 / tan(fov / 2.0);
nuclear@15 61 dir.normalize();
nuclear@15 62
nuclear@15 63 return transform(inv_xform, Ray(Vector3(0, 0, 0), dir));
nuclear@15 64 }