我正在尝试实现反射,但我遇到了我找到的公式的问题。
计算标准N和V,使这两个向量成为单一的。 接下来,计算标量乘积N.V.完成下面的论坛计算R坐标:Rx,Ry和Rz。
R = -2N * V.N + V。
目前我有这个,但看起来并不好:
这是我反思的功能:
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是光线撞击物体的坐标。
谢谢!
PS:抱歉我的英语不好,我是法国人。