Add world representation as hittable list

This commit is contained in:
Phireh 2021-08-21 15:59:44 +02:00
commit 1eeb328b60
4 changed files with 39 additions and 14 deletions

View file

@ -1,25 +1,25 @@
#include <stdio.h>
#include <stdint.h>
#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<sphere>(point3(0,0,-1), 0.5));
world.add(std::make_shared<sphere>(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);
}