add red sphere
This commit is contained in:
parent
30d841f7c6
commit
1101f71d8f
|
@ -16,7 +16,19 @@ const SDL_WINDOWPOS_UNDEFINED = @bitCast(c_int, sdl.c.SDL_WINDOWPOS_UNDEFINED_MA
|
||||||
|
|
||||||
const fps = 60;
|
const fps = 60;
|
||||||
|
|
||||||
|
fn hitSphere(center: Point3, radius: f32, ray: Ray) bool {
|
||||||
|
const ssr = ray.origin.sub(center); // Sphere-space ray, (A - C) in book
|
||||||
|
const a = Vec3.dot(ray.direction, ray.direction);
|
||||||
|
const b = 2.0 * Vec3.dot(ssr, ray.direction);
|
||||||
|
const c = Vec3.dot(ssr, ssr) - (radius * radius);
|
||||||
|
const discriminant = b * b - 4 * a * c;
|
||||||
|
return discriminant > 0;
|
||||||
|
}
|
||||||
|
|
||||||
fn rayColor(ray: Ray) Color {
|
fn rayColor(ray: Ray) Color {
|
||||||
|
if (hitSphere(Point3{ .x = 0, .y = 0, .z = -1 }, 0.5, ray)) {
|
||||||
|
return Color{ .x = 1, .y = 0, .z = 0 };
|
||||||
|
}
|
||||||
const unitDirection = vec3.unitVector(ray.direction);
|
const unitDirection = vec3.unitVector(ray.direction);
|
||||||
const t = 0.5 * (unitDirection.y + 1.0);
|
const t = 0.5 * (unitDirection.y + 1.0);
|
||||||
const white = Color{ .x = 1.0, .y = 1.0, .z = 1.0 };
|
const white = Color{ .x = 1.0, .y = 1.0, .z = 1.0 };
|
||||||
|
|
Loading…
Reference in a new issue