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,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 @g++ -g -Wall -Wextra -Wpedantic main.cpp -o raytracer
image: raytracer image: raytracer

View file

@ -13,7 +13,8 @@ struct hittable_list : hittable {
/* Attributes */ /* Attributes */
std::vector<shared_ptr<hittable>> objects; std::vector<shared_ptr<hittable>> objects;
/* Constructor */ /* Constructors */
hittable_list () {}
hittable_list(shared_ptr<hittable> h) { add(h); } hittable_list(shared_ptr<hittable> h) { add(h); }
/* Methods */ /* Methods */

View file

@ -1,25 +1,25 @@
#include <stdio.h> #include <stdio.h>
#include <stdint.h> #include <stdint.h>
#include "vec3.hpp" #include "rtweekend.hpp"
#include "color.hpp"
#include "ray.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); 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); hit_record rec;
if (t > 0.0) if (world.hit(r, 0, INFINITY, rec))
{ {
vec3 N = normalize(r.at(t) - vec3(0,0,-1)); return 0.5 * (rec.normal + color(1,1,1));
return 0.5*color(N.x+1, N.y+1, N.z+1);
} }
vec3 unit_direction = normalize(r.direction); vec3 unit_direction = normalize(r.direction);
t = 0.5*(unit_direction.y + 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);
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) 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_width = 400;
const int32_t image_height = (int32_t) (image_width / aspect_ratio); 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 // Camera
double viewport_height = 2.0; double viewport_height = 2.0;
double viewport_width = aspect_ratio * viewport_height; double viewport_width = aspect_ratio * viewport_height;
@ -65,7 +70,7 @@ int main()
double u = (double)i / (image_width-1); double u = (double)i / (image_width-1);
double v = (double)j / (image_height-1); double v = (double)j / (image_height-1);
ray r = ray(origin, lower_left_corner + u*horizontal + v*vertical - origin); 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); write_color(stdout, pixel_color);
} }

19
rtweekend.hpp Normal file
View file

@ -0,0 +1,19 @@
#ifndef RTWEEKEND_H
#define RTWEEKEND_H
#include <math.h>
#include <memory>
/* Utility functions */
double degrees_to_radians(double d)
{
return d * M_PI / 180;
}
/* Common internal headers */
#include "ray.hpp"
#include "vec3.hpp"
#endif