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; };