From 899064ce423fffb8706174a3467e1473da221b46 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Wed, 16 Jul 2014 23:31:39 -0700 Subject: [PATCH] Add a perspective camera Finally! :D Verified (mostly) and carefully thought through by looking at the Pov-Ray code and doing the linear algebra by hand. Fun times. --- src/camera.cc | 22 ++++++++++++++++++++++ src/camera.h | 7 +++++++ 2 files changed, 29 insertions(+) diff --git a/src/camera.cc b/src/camera.cc index b2477a5..5dd7b46 100644 --- a/src/camera.cc +++ b/src/camera.cc @@ -108,6 +108,28 @@ Camera::SetUp(const Vector3& up) mUp = up; } +#pragma mark - Perspective Camera + +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(get_origin(), direction); +} + #pragma mark - Orthographic Camera /* diff --git a/src/camera.h b/src/camera.h index 9ee0008..812109f 100644 --- a/src/camera.h +++ b/src/camera.h @@ -44,7 +44,14 @@ private: Vector3 direction; Vector3 mRight; Vector3 mUp; +}; + +class PerspectiveCamera + : public Camera +{ + Ray compute_primary_ray(const int x, const int width, + const int y, const int height) const; };