2013-09-16 16:34:48 -07:00
|
|
|
/* 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)
|
2013-09-16 16:34:48 -07:00
|
|
|
{ }
|
|
|
|
|
|
|
|
|
2014-07-19 17:24:52 -07:00
|
|
|
Camera::Camera(const Camera& other)
|
|
|
|
: mDirection(other.mDirection),
|
|
|
|
mRight(other.mRight),
|
|
|
|
mUp(other.mUp)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
|
2013-09-16 16:34:48 -07:00
|
|
|
Camera::~Camera()
|
|
|
|
{ }
|
|
|
|
|
2013-09-17 10:15:35 -07:00
|
|
|
|
2014-07-20 12:37:04 -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
|
|
|
}
|
|
|
|
|
2014-07-16 23:31:39 -07:00
|
|
|
#pragma mark - Perspective Camera
|
|
|
|
|
2014-07-19 20:56:47 -07:00
|
|
|
PerspectiveCamera::PerspectiveCamera()
|
|
|
|
: Camera()
|
|
|
|
{ }
|
|
|
|
|
|
|
|
|
|
|
|
PerspectiveCamera::PerspectiveCamera(const Camera& other)
|
|
|
|
: Camera(other)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
|
2014-07-16 23:31:39 -07:00
|
|
|
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());
|
2014-08-02 14:10:28 -07:00
|
|
|
return Ray(GetOrigin(), direction.normalize());
|
2014-07-16 23:31:39 -07:00
|
|
|
}
|
|
|
|
|
2013-09-16 16:34:48 -07:00
|
|
|
#pragma mark - Orthographic Camera
|
|
|
|
|
2014-07-19 20:56:47 -07:00
|
|
|
OrthographicCamera::OrthographicCamera()
|
|
|
|
: Camera()
|
|
|
|
{ }
|
|
|
|
|
|
|
|
|
|
|
|
OrthographicCamera::OrthographicCamera(const Camera& other)
|
|
|
|
: Camera(other)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
|
2013-09-16 16:34:48 -07:00
|
|
|
/*
|
|
|
|
* OrthographicCamera::compute_primary_ray --
|
2014-07-16 23:26:41 -07:00
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* 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
|
2013-09-16 16:34:48 -07:00
|
|
|
* the view into the scene.
|
|
|
|
*/
|
|
|
|
Ray
|
2014-07-16 23:29:54 -07:00
|
|
|
OrthographicCamera::compute_primary_ray(const int x,
|
|
|
|
const int width,
|
|
|
|
const int y,
|
|
|
|
const int height)
|
2013-09-16 16:34:48 -07:00
|
|
|
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
|
|
|
|
2014-07-20 12:37:04 -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());
|
2013-09-16 16:34:48 -07:00
|
|
|
}
|