Handle hits on multiple objects

This commit is contained in:
2025-09-23 15:36:08 +01:00
parent 252489f6d9
commit f934cc8fa8

11
demo.c
View File

@@ -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));