charles/src/camera.cc

135 lines
2.3 KiB
C++
Raw Normal View History

/* camera.h
*
* The Camera is the eye into the scene. It defines several parameters and a single compute_primary_ray method
* that generates rays with which the ray tracer draws the scene.
*
* Eryn Wells <eryn@erynwells.me>
*/
#include "camera.h"
#pragma mark - Generic Camera
Camera::Camera()
: height(Vector3::Y),
width(4.0 / 3.0 * Vector3::X),
2014-07-16 23:25:54 -07:00
direction(Vector3::Z),
mRight(1.33, 0, 0),
mUp(Vector3::Y)
{ }
Camera::~Camera()
{ }
2013-09-17 10:15:35 -07:00
/*
* Camera::get_width --
* Camera::set_width --
* Camera::get_height --
* Camera::set_height --
*
* Get and set width and height vectors.
*/
const Vector3 &
Camera::get_width()
const
{
return width;
}
void
Camera::set_width(const Vector3 &w)
{
width = w;
}
const Vector3 &
Camera::get_height()
const
{
return height;
}
void
2013-09-20 09:21:25 -07:00
Camera::set_height(const Vector3 &h)
2013-09-17 10:15:35 -07:00
{
height = h;
}
/*
* Camera::get_direction --
* Camera::set_direction --
*
* Get and set direction vector.
*/
const Vector3 &
Camera::get_direction()
const
{
return direction;
}
void
Camera::set_direction(const Vector3 &d)
{
direction = d;
}
2013-09-20 09:21:25 -07:00
2014-07-16 23:25:54 -07:00
const Vector3&
Camera::GetRight()
const
{
return mRight;
}
void
Camera::SetRight(const Vector3& right)
{
mRight = right;
}
const Vector3&
Camera::GetUp()
2013-09-20 09:21:25 -07:00
const
{
2014-07-16 23:25:54 -07:00
return mUp;
}
void
Camera::SetUp(const Vector3& up)
{
mUp = up;
2013-09-20 09:21:25 -07:00
}
#pragma mark - Orthographic Camera
/*
* OrthographicCamera::compute_primary_ray --
*
* Compute a primary ray given an (x,y) coordinate pair. The orthographic camera projects rays parallel to the viewing
* direction through the (x,y) coordinate given. Thus, the size of the orthographic camera should be set to the size of
* the view into the scene.
*/
Ray
OrthographicCamera::compute_primary_ray(const int &x,
const int &y)
const
{
2013-09-17 10:15:35 -07:00
// Calculate the point on the image plane that the given (x,y) coordinate pair corresponds to.
2013-09-20 09:21:25 -07:00
float dir_x = (x / get_pixel_width()) + 0.5;
float dir_y = (y / get_pixel_height()) + 0.5;
Vector3 ray_origin = (dir_x * get_width()) + (dir_y * get_height()) + get_direction();
2013-09-17 10:15:35 -07:00
// Calculate the direction of the ray, given the camera's origin and normalize that vector.
2013-09-20 09:21:25 -07:00
Vector3 ray_direction = (ray_origin - get_origin()).normalize();
2013-09-17 10:15:35 -07:00
return Ray(get_origin(), ray_direction);
}