-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixed bug about displaying cylinder base
- Loading branch information
Showing
3 changed files
with
73 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -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) | ||
|
@@ -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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |