Skip to content

Commit

Permalink
fixed bug about displaying cylinder base
Browse files Browse the repository at this point in the history
  • Loading branch information
kurrrru committed Dec 20, 2024
1 parent 773cccc commit f829526
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 24 deletions.
7 changes: 5 additions & 2 deletions include/raytracing.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* raytracing.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: marimiyahara <marimiyahara@student.42.f +#+ +:+ +#+ */
/* By: nkawaguc <nkawaguc@student.42tokyo.jp> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/13 21:26:05 by marimiyahar #+# #+# */
/* Updated: 2024/12/17 01:03:24 by marimiyahar ### ########.fr */
/* Updated: 2024/12/20 11:17:07 by nkawaguc ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -27,6 +27,9 @@ int intersect_cylinder(t_vec origin, t_vec direction,
t_closest_obj *find_obj);
int intersect_plane(t_vec origin, t_vec direction, t_closest_obj *find_obj);

double intersect_cy_top_base(t_vec direction, t_vec oc, t_object *obj);
double intersect_cy_bottom_base(t_vec direction, t_vec oc, t_object *obj);

t_vec subtract(t_vec v1, t_vec v2);
t_vec add(t_vec v1, t_vec v2);
t_vec scale(t_vec v, double scalar);
Expand Down
37 changes: 15 additions & 22 deletions src/raytracing/intersect_cy.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: nkawaguc <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/16 00:20:54 by marimiyahar #+# #+# */
/* Updated: 2024/12/18 21:57:13 by nkawaguc ### ########.fr */
/* Updated: 2024/12/20 11:17:50 by nkawaguc ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -15,7 +15,7 @@
static int check_intersect(double *t, t_object *obj, t_vec origin,
t_vec direction);
static int calc_t(t_vec d_proj, t_vec oc_proj, double *t, t_object *obj);
static int calc_t_again(t_vec d_proj, t_vec oc_proj,
static int calc_t_again(t_vec direction, t_vec oc,
double *t, t_object *obj);

int intersect_cylinder(t_vec origin, t_vec direction, t_closest_obj *find_obj)
Expand Down Expand Up @@ -80,27 +80,20 @@ static int calc_t(t_vec d_proj, t_vec oc_proj, double *t, t_object *obj)
return (1);
}

static int calc_t_again(t_vec d_proj, t_vec oc_proj, double *t, t_object *obj)
static int calc_t_again(t_vec direction, t_vec oc, double *t, t_object *obj)
{
double divisor;
t_vec vec_from_center;
double t_top;
double t_bottom;

divisor = dot_product(d_proj, obj->norm_vector);
if (divisor == 0)
t_top = intersect_cy_top_base(direction, oc, obj);
t_bottom = intersect_cy_bottom_base(direction, oc, obj);
if (t_top < 0 && t_bottom < 0)
return (0);
*t = (-(dot_product(oc_proj, obj->norm_vector) + obj->height / 2)
/ dot_product(d_proj, obj->norm_vector));
vec_from_center = add(add(oc_proj, scale(d_proj, *t)),
scale(obj->norm_vector, obj->height / 2));
if (*t > 0 && sqrt(dot_product(vec_from_center, vec_from_center))
<= obj->radius + 1e-6)
return (1);
*t = (-(dot_product(oc_proj, obj->norm_vector) - obj->height / 2)
/ dot_product(d_proj, obj->norm_vector));
vec_from_center = add(add(oc_proj, scale(d_proj, *t)),
scale(obj->norm_vector, -obj->height / 2));
if (*t > 0 && sqrt(dot_product(vec_from_center, vec_from_center))
<= obj->radius + 1e-6)
return (1);
return (0);
if (t_top < 0)
*t = t_bottom;
else if (t_bottom < 0)
*t = t_top;
else
*t = fmin(t_top, t_bottom);
return (1);
}
53 changes: 53 additions & 0 deletions src/raytracing/intersect_cy_two_bases.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* intersect_cy_two_bases.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nkawaguc <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/20 11:05:30 by nkawaguc #+# #+# */
/* Updated: 2024/12/20 11:18:54 by nkawaguc ### ########.fr */
/* */
/* ************************************************************************** */

#include "../../include/minirt.h"

double intersect_cy_top_base(t_vec direction, t_vec oc, t_object *obj)
{
const double invalid_t = -1;
double t;
double divisor;
t_vec vec_from_center;

divisor = dot_product(direction, obj->norm_vector);
if (divisor == 0)
return (invalid_t);
t = -(dot_product(oc, obj->norm_vector) + obj->height / 2) / divisor;
if (t < 0)
return (invalid_t);
vec_from_center = add(add(oc, scale(direction, t)),
scale(obj->norm_vector, obj->height / 2));
if (sqrt(dot_product(vec_from_center, vec_from_center)) > obj->radius)
return (invalid_t);
return (t);
}

double intersect_cy_bottom_base(t_vec direction, t_vec oc, t_object *obj)
{
const double invalid_t = -1;
double t;
double divisor;
t_vec vec_from_center;

divisor = dot_product(direction, obj->norm_vector);
if (divisor == 0)
return (invalid_t);
t = -(dot_product(oc, obj->norm_vector) - obj->height / 2) / divisor;
if (t < 0)
return (invalid_t);
vec_from_center = add(add(oc, scale(direction, t)),
scale(obj->norm_vector, -obj->height / 2));
if (sqrt(dot_product(vec_from_center, vec_from_center)) > obj->radius)
return (invalid_t);
return (t);
}

0 comments on commit f829526

Please sign in to comment.