From: Lukas Jiriste Date: Thu, 28 Nov 2024 22:03:25 +0000 (+0100) Subject: Fix cylinder rendering X-Git-Url: https://git.ljiriste.work/?a=commitdiff_plain;h=225a26a04c247f67c7bb1d90f905c74ca51b006a;p=42%2FminiRT.git Fix cylinder rendering I thought that the ray forms and acute angle with a vector from point of entry to cylinder center, but that was not true. Instead the projections to the plane orthogonal the rotational axis have this property. So in this commit I implement the test for it. --- diff --git a/src/scene.c b/src/scene.c index 1946693..42571c2 100644 --- a/src/scene.c +++ b/src/scene.c @@ -148,12 +148,12 @@ double get_intersection_arg_cylinder_around args = solve_quadratic(vec_scalar_mul(v, v), 2 * vec_scalar_mul(v, u), vec_scalar_mul(u, u) - cylinder->radius * cylinder->radius); intersect = ray_point(ray, args.first); - if (fabs(vec_scalar_mul(cylinder->rot_axis, intersect)) < cylinder->height / 2 - && vec_scalar_mul(ray->direction, vec_diff(cylinder->center, intersect)) > 0) + if (fabs(vec_scalar_mul(cylinder->rot_axis, vec_diff(intersect, cylinder->center))) < cylinder->height / 2 + && vec_scalar_mul(vec_vec_mul(cylinder->rot_axis, ray->direction), vec_vec_mul(cylinder->rot_axis, vec_diff(cylinder->center, intersect))) > 0) return (args.first); intersect = ray_point(ray, args.second); - if (fabs(vec_scalar_mul(cylinder->rot_axis, intersect)) < cylinder->height / 2 - && vec_scalar_mul(ray->direction, vec_diff(cylinder->center, intersect)) > 0) + if (fabs(vec_scalar_mul(cylinder->rot_axis, vec_diff(intersect, cylinder->center))) < cylinder->height / 2 + && vec_scalar_mul(vec_vec_mul(cylinder->rot_axis, ray->direction), vec_vec_mul(cylinder->rot_axis, vec_diff(cylinder->center, intersect))) > 0) return (args.second); return (NAN); } @@ -164,7 +164,7 @@ double get_intersection_arg_cylinder double res; res = get_intersection_arg_cylinder_base(ray, cylinder); - if (res == NAN) + if (isnan(res)) res = get_intersection_arg_cylinder_around(ray, cylinder); return (res); }