charles/src/camera.cc

168 lines
3.1 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()
2014-07-19 17:24:52 -07:00
: mDirection(Vector3::Z),
2014-07-16 23:25:54 -07:00
mRight(1.33, 0, 0),
mUp(Vector3::Y)
{ }
2014-07-19 17:24:52 -07:00
Camera::Camera(const Camera& other)
: mDirection(other.mDirection),
mRight(other.mRight),
mUp(other.mUp)
{ }
Camera::~Camera()
{ }
2013-09-17 10:15:35 -07:00
/*
* Camera::GetOrigin --
*/
const Vector3&
Camera::GetOrigin()
const
{
return mOrigin;
}
/*
* Camera::SetOrigin --
*/
void
Camera::SetOrigin(const Vector3 &origin)
{
mOrigin = origin;
}
2014-07-19 17:24:52 -07:00
const Vector3&
2013-09-17 10:15:35 -07:00
Camera::get_direction()
const
{
2014-07-19 17:24:52 -07:00
return mDirection;
2013-09-17 10:15:35 -07:00
}
void
2014-07-19 17:24:52 -07:00
Camera::set_direction(const Vector3 &direction)
2013-09-17 10:15:35 -07:00
{
2014-07-19 17:24:52 -07:00
mDirection = direction;
2013-09-17 10:15:35 -07:00
}
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 - Perspective Camera
PerspectiveCamera::PerspectiveCamera()
: Camera()
{ }
PerspectiveCamera::PerspectiveCamera(const Camera& other)
: Camera(other)
{ }
Ray
PerspectiveCamera::compute_primary_ray(const int x,
const int width,
const int y,
const int height)
const
{
/*
* Center x and y in the pixel and convert them to be coordinates between
* -0.5 and 0.5.
*/
double x0 = (x + 0.5) / width - 0.5;
double y0 = ((height - 1.0) - (y - 0.5)) / height - 0.5;
Vector3 direction = LinearCombination(1.0, get_direction(),
x0, GetRight(),
y0, GetUp());
return Ray(GetOrigin(), direction.normalize());
}
#pragma mark - Orthographic Camera
OrthographicCamera::OrthographicCamera()
: Camera()
{ }
OrthographicCamera::OrthographicCamera(const Camera& other)
: Camera(other)
{ }
/*
* 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 width,
const int y,
const int height)
const
{
2014-07-19 16:30:26 -07:00
/*
* Center x and y in the pixel and convert them to be coordinates between
* -0.5 and 0.5.
*/
double x0 = (x + 0.5) / width + 0.5;
double y0 = ((height - 1.0) - (y - 0.5)) / height - 0.5;
2013-09-17 10:15:35 -07:00
Vector3 origin = LinearCombination(1.0, GetOrigin(),
2014-07-19 16:30:26 -07:00
x0, GetRight(),
y0, GetUp());
return Ray(origin, get_direction());
}