I am trying to implement reflection but I have problems with formulas I have found.
Calculate the Standards N and V, to make these 2 vectors unitary. Next, calculate the scalar product N.V. To finish with the forumulas below calculate R coordinate : Rx, Ry and Rz.
R = -2N * V.N + V.
At the moment I have this but it does not look great:
And this is my function of reflection :
t_color reflection(t_rt *rt, t_obj *obj, t_lvector *vec, t_color tmp_color)
{
static int number_loop;
t_vector new_vec;
t_vector eye;
t_vector normal;
t_color color;
float scalaire;
t_obj *new_obj;
if (++number_loop == 200)
{
number_loop = 0;
return (tmp_color);
}
color.r = tmp_color.r;
color.g = tmp_color.g;
color.g = tmp_color.b;
find_normal(&normal, vec, obj);
scalaire = (rt->vec->x * normal.x) +
(rt->vec->y * normal.y) + (rt->vec->z * normal.z);
eye.x = vec->px;
eye.y = vec->py;
eye.z = vec->pz;
new_vec.x = -2 * normal.x * scalaire + rt->vec->x;
new_vec.y = -2 * normal.y * scalaire + rt->vec->y;
new_vec.z = -2 * normal.z * scalaire + rt->vec->z;
rt->vec = &new_vec;
rt->eye = &eye;
new_obj = find_nearest_obj(rt->obj, &eye, &new_vec);
if (new_obj)
color = calculate_light(rt, new_obj);
return (color);
}
vec->pXYZ is the coordinate of where the ray hits an object.
Thanks !
PS : Sorry for my bad english, I am french.