diff options
| author | ladevieq <ladevie.quentin@gmail.com> | 2020-12-20 21:59:33 +0100 |
|---|---|---|
| committer | ladevieq <ladevie.quentin@gmail.com> | 2020-12-20 21:59:33 +0100 |
| commit | c7c4f8f82c66cfe89b72d176ee51dab8bd30dc95 (patch) | |
| tree | d0c2caeccda953d86806cb7c7b27c8941e0ef845 /src/main.cpp | |
| parent | fc601fd503ec1ebd08e4daffeb9aa0812aff2b71 (diff) | |
Add hittable interface and hit infos on hit
Diffstat (limited to 'src/main.cpp')
| -rw-r--r-- | src/main.cpp | 32 |
1 files changed, 12 insertions, 20 deletions
diff --git a/src/main.cpp b/src/main.cpp index e2ef071..f6b3012 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,31 +1,22 @@ #include <iostream> -#include "color.hpp" -#include "vec3.hpp" -#include "ray.hpp" +#include "rt.hpp" +#include "hittable_list.hpp" color ground_color { 1.0, 1.0, 1.0 }; color sky_color { 0.5, 0.7, 1.0 }; -bool hit_sphere(const point3& sphere_pos, double sphere_radius, const ray& r) { - // Vector from the ray origin to sphere position - vec3 origin_center = r.origin - sphere_pos; - double a = r.direction.dot(r.direction); - double b = 2.0 * r.direction.dot(origin_center); - double c = origin_center.dot(origin_center) - sphere_radius * sphere_radius; - double discriminant = b * b - 4.0 * a * c; +color ray_color(ray& r, const hittable& world) { + sphere sphere { { 0.0, 0.0, -1.0 }, 0.5 }; - return (discriminant > 0); -} - -color ray_color(ray& r) { auto unit_dir = r.direction.unit(); auto t = (unit_dir.y + 1.0) * 0.5; auto c = lerp(ground_color, sky_color, t); - if (hit_sphere(point3{ 0.0, 0.0, -1.0 }, .5, r)) { - c = color { 1.0, 0.0, 0.0 }; + struct hit_info info; + if (world.hit(r, 0.0, infinity, info)) { + c = 0.5 * (info.normal + vec3{ 1.0, 1.0, 1.0}); } return c; @@ -36,10 +27,11 @@ int main() { const double aspect_ratio = 16.0 / 9.0; const size_t width = 400; const size_t height = width / aspect_ratio; - // const size_t width = 256; - // const size_t height = 256; - // const double aspect_ratio = width / height; + // World hittable objects + hittable_list world {}; + world.add(std::make_shared<sphere>(point3(0,0,-1), 0.5)); + world.add(std::make_shared<sphere>(point3(0,-100.5,-1), 100)); // Viewport dimensions const double viewport_height = 2.0; @@ -78,7 +70,7 @@ int main() { ray ray { eye_pos, direction }; - write_color(std::cout, ray_color(ray)); + write_color(std::cout, ray_color(ray, world)); // write_color(std::cout, color(direction.unit().y, direction.unit().y, direction.unit().y)); // write_color(std::cout, color(y.y, y.y, y.y)); } |
