Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#4] Fix the rotation bug due to incorrect front face setting. #17

Merged
merged 5 commits into from
Nov 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions hittables/rt_instance.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ static bool rt_instance_hit(const rt_hittable_t *hittable, const ray_t *ray, dou
vec3_add(&record->p, instance->offset);

vec3_t new_normal = rt_mat3_mul_vec3(&instance->transform_matrix_bb, &record->normal);
rt_hit_record_set_front_face(record, &transformed_ray, &new_normal);
rt_hit_record_set_front_face(record, ray, &new_normal);

return true;
}
Expand All @@ -105,8 +105,6 @@ static bool rt_instance_bb(const rt_hittable_t *hittable, double time0, double t
return false;
}

hittable_bb = rt_aabb(vec3_sum(hittable_bb.min, instance->offset), vec3_sum(hittable_bb.max, instance->offset));

vec3_t min = hittable_bb.min, max = hittable_bb.max;
for (int i = 0; i < 2; i++)
{
Expand All @@ -119,7 +117,6 @@ static bool rt_instance_bb(const rt_hittable_t *hittable, double time0, double t
k * hittable_bb.max.z + (1 - k) * hittable_bb.min.z);

vec3_t tester = rt_mat3_mul_vec3(&instance->transform_matrix_bb, &point);

for (int c = 0; c < 3; c++)
{
min.components[c] = fmin(min.components[c], tester.components[c]);
Expand All @@ -128,8 +125,7 @@ static bool rt_instance_bb(const rt_hittable_t *hittable, double time0, double t
}
}
}

*out_bb = rt_aabb(min, max);
*out_bb = rt_aabb(vec3_sum(min, instance->offset), vec3_sum(max, instance->offset));

return true;
}
Expand Down
9 changes: 5 additions & 4 deletions scenes/rt_scenes.c
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,8 @@ rt_hittable_list_t *rt_scene_instance_test(void)

rt_hittable_t *box = rt_box_new(point3(-1, -1, -1), point3(1, 1, 1), green);
rt_hittable_t *instance = rt_instance_new(box);
rt_instance_rotate_y(instance, 0);
rt_instance_translate(instance, point3(1, 0, 0));
rt_instance_rotate_y(instance, 45);
rt_instance_translate(instance, point3(1, 0, 0.1));

rt_hittable_list_add(objects, instance);
rt_hittable_list_add(objects, rt_box_new(point3(-0.25, -0.25, -0.25), point3(0.25, 0.25, 0.25), red));
Expand Down Expand Up @@ -332,8 +332,9 @@ rt_hittable_list_t *rt_scene_metal_test(void)
rt_hittable_list_add(objects, rt_sphere_new(point3(2, 2, 0), 1, rt_mt_metal_new(colour(0.8, 0.8, 0.9), 1.0)));

rt_hittable_t *box_instance =
rt_instance_new(rt_box_new(point3(-1, 0, 10), point3(1, 2, 12), rt_mt_metal_new(colour(0.8, 0.8, 0.9), 0.0)));
rt_instance_rotate_y(box_instance, 10);
rt_instance_new(rt_box_new(point3(-1, -1, -1), point3(1, 1, 1), rt_mt_metal_new(colour(0.8, 0.8, 0.9), 0.0)));
rt_instance_rotate_y(box_instance, 45);
rt_instance_translate(box_instance, vec3(2, 1, 11));
rt_hittable_list_add(objects, box_instance);

return objects;
Expand Down