From 1eeb328b6099bad145176380b34bd1324f085e54 Mon Sep 17 00:00:00 2001 From: Phireh Date: Sat, 21 Aug 2021 15:59:44 +0200 Subject: [PATCH] Add world representation as hittable list --- Makefile | 2 +- hittable_list.hpp | 3 ++- main.cpp | 31 ++++++++++++++++++------------- rtweekend.hpp | 19 +++++++++++++++++++ 4 files changed, 40 insertions(+), 15 deletions(-) create mode 100644 rtweekend.hpp diff --git a/Makefile b/Makefile index 41c85b6..5ea9605 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -raytracer: main.cpp vec3.hpp color.hpp ray.hpp hittable.hpp sphere.hpp hittable_list.hpp +raytracer: main.cpp vec3.hpp color.hpp ray.hpp hittable.hpp sphere.hpp hittable_list.hpp rtweekend.hpp @g++ -g -Wall -Wextra -Wpedantic main.cpp -o raytracer image: raytracer diff --git a/hittable_list.hpp b/hittable_list.hpp index 2a5c609..4a7483e 100644 --- a/hittable_list.hpp +++ b/hittable_list.hpp @@ -13,7 +13,8 @@ struct hittable_list : hittable { /* Attributes */ std::vector> objects; - /* Constructor */ + /* Constructors */ + hittable_list () {} hittable_list(shared_ptr h) { add(h); } /* Methods */ diff --git a/main.cpp b/main.cpp index 3f69801..53096f4 100644 --- a/main.cpp +++ b/main.cpp @@ -1,25 +1,25 @@ #include #include -#include "vec3.hpp" -#include "color.hpp" -#include "ray.hpp" +#include "rtweekend.hpp" -color ray_color(const ray& r); +#include "color.hpp" +#include "hittable_list.hpp" +#include "sphere.hpp" + +color ray_color(const ray& r, const hittable& world); double hit_sphere(const point3& center, double radius, const ray& r); -color ray_color(const ray& r) +color ray_color(const ray& r, const hittable& world) { - double t = hit_sphere(point3(0,0,-1), 0.5, r); - if (t > 0.0) + hit_record rec; + if (world.hit(r, 0, INFINITY, rec)) { - vec3 N = normalize(r.at(t) - vec3(0,0,-1)); - return 0.5*color(N.x+1, N.y+1, N.z+1); + return 0.5 * (rec.normal + color(1,1,1)); } vec3 unit_direction = normalize(r.direction); - t = 0.5*(unit_direction.y + 1.0); - - return (1-t) * color(1,1,1) + t*color(0.5, 0.7, 1.0); + double t = 0.5 * (unit_direction.y + 1.0); + return (1-t) * color(1,1,1) + t*color(0.5,0.7,1.0); } double hit_sphere(const point3& center, double radius, const ray& r) @@ -43,6 +43,11 @@ int main() const int32_t image_width = 400; const int32_t image_height = (int32_t) (image_width / aspect_ratio); + // World + hittable_list world; + world.add(std::make_shared(point3(0,0,-1), 0.5)); + world.add(std::make_shared(point3(0,-100.5,-1), 100)); + // Camera double viewport_height = 2.0; double viewport_width = aspect_ratio * viewport_height; @@ -65,7 +70,7 @@ int main() double u = (double)i / (image_width-1); double v = (double)j / (image_height-1); ray r = ray(origin, lower_left_corner + u*horizontal + v*vertical - origin); - color pixel_color = ray_color(r); + color pixel_color = ray_color(r, world); write_color(stdout, pixel_color); } diff --git a/rtweekend.hpp b/rtweekend.hpp new file mode 100644 index 0000000..ef9b14f --- /dev/null +++ b/rtweekend.hpp @@ -0,0 +1,19 @@ +#ifndef RTWEEKEND_H +#define RTWEEKEND_H + +#include +#include + +/* Utility functions */ +double degrees_to_radians(double d) +{ + return d * M_PI / 180; +} + +/* Common internal headers */ + +#include "ray.hpp" +#include "vec3.hpp" + + +#endif