rayzor

diff src/camera.cc @ 15:be616b58df99

continued the renderer slightly
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 13 Apr 2014 09:54:36 +0300
parents 964f8ea5f095
children 79609d482762
line diff
     1.1 --- a/src/camera.cc	Sun Apr 13 08:06:21 2014 +0300
     1.2 +++ b/src/camera.cc	Sun Apr 13 09:54:36 2014 +0300
     1.3 @@ -1,9 +1,10 @@
     1.4  #include "camera.h"
     1.5 +#include "rayzor.h"
     1.6  
     1.7  Camera::Camera()
     1.8  {
     1.9  	type = NODE_CAMERA;
    1.10 -	fov = M_PI;
    1.11 +	fov = M_PI / 4.0;
    1.12  	set_position(Vector3(0, 0, 10));
    1.13  }
    1.14  
    1.15 @@ -29,14 +30,11 @@
    1.16  
    1.17  void Camera::calc_matrix() const
    1.18  {
    1.19 -	xform.set_identity();
    1.20 -	xform.lookat(pos, target.get_position(), Vector3(0, 1, 0));
    1.21 -	xform_valid = true;
    1.22 -}
    1.23 -
    1.24 -void Camera::calc_inv_matrix() const
    1.25 -{
    1.26 -	// TODO
    1.27 +	if(!xform_valid) {
    1.28 +		xform.set_identity();
    1.29 +		xform.lookat(pos, target.get_position(), Vector3(0, 1, 0));
    1.30 +		xform_valid = true;
    1.31 +	}
    1.32  }
    1.33  
    1.34  void Camera::draw() const
    1.35 @@ -47,3 +45,20 @@
    1.36  {
    1.37  	return false;
    1.38  }
    1.39 +
    1.40 +Ray Camera::get_primary_ray(int x, int y) const
    1.41 +{
    1.42 +	calc_inv_matrix();
    1.43 +
    1.44 +	float aspect = (float)fb_width / (float)fb_height;
    1.45 +	float pwidth = 2.0 * aspect / (float)fb_width;
    1.46 +	float pheight = 2.0 / (float)fb_height;
    1.47 +
    1.48 +	Vector3 dir;
    1.49 +	dir.x = (float)x * pwidth - aspect;
    1.50 +	dir.y = 1.0 - (float)y * pheight;
    1.51 +	dir.z = -1.0 / tan(fov / 2.0);
    1.52 +	dir.normalize();
    1.53 +
    1.54 +	return transform(inv_xform, Ray(Vector3(0, 0, 0), dir));
    1.55 +}