add hittable world

This commit is contained in:
K Tyl 2020-06-06 18:28:30 +01:00
parent 3d670c83ed
commit bcbc0719b3
1 changed files with 15 additions and 30 deletions

View File

@ -1,6 +1,8 @@
#include "rtweekend.h"
#include "hittable_list.h"
#include "sphere.h"
#include "colour.h"
#include "vec3.h"
#include "ray.h"
#include <iostream>
@ -8,37 +10,16 @@ const double ASPECT_RATIO = 16.0 / 9.0;
const int WIDTH = 384;
const int HEIGHT = static_cast<int>(WIDTH / ASPECT_RATIO);
double hit_sphere(const point3& centre, double radius, const ray& r)
colour ray_colour(const ray& r, const hittable& world)
{
vec3 oc = r.origin() - centre;
auto a = r.direction().length_squared();
auto half_b = dot(oc, r.direction());
auto c = oc.length_squared() - radius*radius;
auto discriminant = half_b*half_b - a*c;
if (discriminant < 0)
hit_record rec;
if (world.hit(r, 0, infinity, rec))
{
return -1.0;
}
else
{
return (-half_b - sqrt(discriminant)) / a;
}
}
colour ray_colour(const ray& r)
{
auto t = hit_sphere(point3(0,0,-1), 0.5, r);
if (t > 0.0)
{
vec3 N = unit_vector(r.at(t) - vec3(0,0,-1));
return 0.5*colour(N.x()+1, N.y()+1, N.z()+1);
return 0.5 * (rec.normal + colour(1,1,1));
}
vec3 unit_direction = unit_vector(r.direction());
t = 0.5 * (unit_direction.y() + 1.0);
auto t = 0.5 * (unit_direction.y() + 1.0);
auto a = colour(1.0, 0.5, 0.6);
auto b = colour(0.0, 0.0, 0.0);
@ -48,6 +29,8 @@ colour ray_colour(const ray& r)
int main()
{
std::cout << "P3\n" << WIDTH << ' ' << HEIGHT << "\n255\n";
auto viewport_height = 2.0;
auto viewport_width = ASPECT_RATIO * viewport_height;
auto focal_length = 1.0;
@ -57,7 +40,9 @@ int main()
auto vertical = vec3(0, viewport_height, 0);
auto lower_left_corner = origin - horizontal/2 - vertical/2 - vec3(0,0,focal_length);
std::cout << "P3\n" << WIDTH << ' ' << HEIGHT << "\n255\n";
hittable_list world;
world.add(make_shared<sphere>(point3(0,0,-1), 0.5));
world.add(make_shared<sphere>(point3(0,-100.5,-1), 100));
for (int y = HEIGHT - 1; y >= 0; --y)
{
@ -68,7 +53,7 @@ int main()
auto v = double(y) / (HEIGHT-1);
ray r(origin, lower_left_corner + u*horizontal + v*vertical - origin);
colour pixel_colour = ray_colour(r);
colour pixel_colour = ray_colour(r, world);
write_colour(std::cout, pixel_colour);
}
}