diff --git a/src/camera.h b/src/camera.h index 462a3df..79c1fad 100644 --- a/src/camera.h +++ b/src/camera.h @@ -1,7 +1,11 @@ #pragma once +#include + +#include "image.h" #include "ray.h" #include "raytracer.h" +#include "renderobject.h" #include "vec3.h" class Camera { @@ -27,6 +31,19 @@ class Camera { constexpr Vec3 centre() const { return centre_; } + constexpr void Render(Image& img, const RenderObject& world) const { + for (u32 j = 0; j < img.height(); j++) { + std::clog << "\rWriting line " << j << " of " << img.height() << std::flush; + + for (u32 i = 0; i < img.width(); i++) { + auto ray = CastRay(i, j); + + img[i, j] = Cast(ray, world); + } + } + } + + private: constexpr Vec3 Image2World(u32 i, u32 j) const { return pixel_00_ + i * d_u_pixel_ + j * d_v_pixel_; } @@ -39,7 +56,6 @@ class Camera { return Ray{centre(), ray_direction}; } - private: Vec3 centre_; // position of pixel 0, 0 diff --git a/src/main.cc b/src/main.cc index 1d1c406..26e5699 100644 --- a/src/main.cc +++ b/src/main.cc @@ -2,40 +2,13 @@ #include #include "camera.h" -#include "colour.h" #include "image.h" -#include "interval.h" -#include "ray.h" #include "raytracer.h" #include "renderobject.h" #include "renderobjectlist.h" #include "sphere.h" #include "vec3.h" -Colour RayColour(const Ray& ray, const RenderObject& world) { - auto hit_record = world.hit(ray, Interval::kPositive); - // auto hit_record = world.hit(ray, Interval{0.505, kInf}); - - if (hit_record.has_value()) { - // assert(hit_record.front_face); - - Vec3 n = hit_record->normal; - - if (!hit_record->front_face) { - // return Colour{0.0, 0.0, 0.0}; - return Colour::kBlack; - } - - return Colour{0.5 * (n + Vec3{1, 1, 1})}; - } - - auto unit_dir = ray.direction().normed(); - - double a = 0.5 * (unit_dir.y() + 1.0); - - return (1.0 - a) * Colour{1.0, 1.0, 1.0} + a * Colour{0.5, 0.7, 1.0}; -} - int main(int /* argc */, char* /* argv */[]) { // image @@ -66,15 +39,7 @@ int main(int /* argc */, char* /* argv */[]) { // render - for (u32 j = 0; j < img.height(); j++) { - std::clog << "\rWriting line " << j << " of " << img.height() << std::flush; - - for (u32 i = 0; i < img.width(); i++) { - auto ray = camera.CastRay(i, j); - - img[i, j] = RayColour(ray, world); - } - } + camera.Render(img, world); std::cout << img; diff --git a/src/renderobject.h b/src/renderobject.h index f57cfb3..bbbec7b 100644 --- a/src/renderobject.h +++ b/src/renderobject.h @@ -3,6 +3,7 @@ #include #include +#include "colour.h" #include "interval.h" #include "ray.h" #include "vec3.h" @@ -30,3 +31,27 @@ class RenderObject { }; using SharedRenderObject = std::shared_ptr; + +constexpr Colour Cast(const Ray& ray, const RenderObject& world) { + auto hit_record = world.hit(ray, Interval::kPositive); + // auto hit_record = world.hit(ray, Interval{0.505, kInf}); + + if (hit_record.has_value()) { + // assert(hit_record.front_face); + + Vec3 n = hit_record->normal; + + if (!hit_record->front_face) { + // return Colour{0.0, 0.0, 0.0}; + return Colour::kBlack; + } + + return Colour{0.5 * (n + Vec3{1, 1, 1})}; + } + + auto unit_dir = ray.direction().normed(); + + double a = 0.5 * (unit_dir.y() + 1.0); + + return (1.0 - a) * Colour{1.0, 1.0, 1.0} + a * Colour{0.5, 0.7, 1.0}; +}