From f934cc8fa86de13f1b6e15750aa48b94e934ec3a Mon Sep 17 00:00:00 2001 From: Camden Dixie O'Brien Date: Tue, 23 Sep 2025 15:36:08 +0100 Subject: [PATCH] Handle hits on multiple objects --- demo.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/demo.c b/demo.c index d9aae28..cf04dd8 100644 --- a/demo.c +++ b/demo.c @@ -26,7 +26,7 @@ static const vec3_t camera_pos = { 0.0, 0.0, 0.0 }; static const obj_t scene[] = { SPHERE(1.0, 0.0, -3.0, 1.0), - SPHERE(-2.0, 0.0, -5.0, 1.0), + SPHERE(-2.0, -0.5, -5.0, 1.0), SPHERE(0.0, -1001.0, 0.0, 1000.0), }; @@ -34,11 +34,14 @@ static pix_t pixbuf[W * H]; static vec3_t raycol(ray_t ray) { - hit_t hit; + bool got_hit = false; + hit_t hit = { .t = DBL_MAX }; for (unsigned i = 0; i < NELEMS(scene); ++i) { - if (scene[i].intersect(scene[i].params, ray, &hit, 0.0, DBL_MAX)) - return vec3_scale(vec3_add(hit.normal, white), 0.5); + if (scene[i].intersect(scene[i].params, ray, &hit, 0.0, hit.t)) + got_hit = true; } + if (got_hit) + return vec3_scale(vec3_add(hit.normal, white), 0.5); const double a = (ray.dir.y + 1.0) / 2.0; return vec3_add(vec3_scale(lightblue, a), vec3_scale(white, 1 - a));