Add world representation as hittable list
This commit is contained in:
parent
68ae1fb265
commit
1eeb328b60
4 changed files with 39 additions and 14 deletions
2
Makefile
2
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
|
||||
|
|
|
|||
|
|
@ -13,7 +13,8 @@ struct hittable_list : hittable {
|
|||
/* Attributes */
|
||||
std::vector<shared_ptr<hittable>> objects;
|
||||
|
||||
/* Constructor */
|
||||
/* Constructors */
|
||||
hittable_list () {}
|
||||
hittable_list(shared_ptr<hittable> h) { add(h); }
|
||||
|
||||
/* Methods */
|
||||
|
|
|
|||
31
main.cpp
31
main.cpp
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
19
rtweekend.hpp
Normal file
19
rtweekend.hpp
Normal 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
|
||||
Loading…
Add table
Add a link
Reference in a new issue