Adding a sphere

This commit is contained in:
Armin Friedl 2022-07-18 15:32:51 +02:00
parent a12fcfbe7b
commit ba40f3736c

View file

@ -4,9 +4,30 @@
#include <iostream>
double hit_sphere(const point3 &center, double radius, const ray &r) {
vec3 oc = r.origin() - center;
auto a = dot(r.direction(), r.direction());
auto b = 2.0 * dot(oc, r.direction());
auto c = dot(oc, oc) - radius * radius;
auto discriminant = b * b - 4 * a * c;
if (discriminant < 0) {
return -1.0;
} else {
return (-b - sqrt(discriminant)) / (2.0*a);
}
}
color ray_color(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*color(N.x()+1, N.y()+1, N.z()+1);
}
vec3 unit_direction = unit_vector(r.direction());
auto t = 0.5 * (unit_direction.y() + 1.0);
t = 0.5*(unit_direction.y() + 1.0);
return (1.0 - t) * color(1.0, 1.0, 1.0) + t * color(0.5, 0.7, 1.0);
}