我最近在cpu射线跟踪器中为phong着色添加了支持,并且颜色都弄乱了:
一些相关代码:
Vec3f camera_dir (xx_array[x],py,scene->getCameraDirZ());
camera_dir.normalize ();
Vec3f hit_normal = (1 - u - v) * vert1 + u * vert2 + v * vert3;
hit_normal.normalize ();
Vec3f light_ray_dir (current.pos - intersection);
float squared_length = light_ray_dir.normalize_return_squared_lenght ();
Vec3f reflected = light_ray_dir - 2 * (light_ray_dir * hit_normal) * hit_normal;
reflected.normalize();
specular_color += light_intensity * std::pow (std::max(0.f,reflected*camera_dir),mat.ns);
final_color = diffuse_color * mat.ks; + specular_color * mat.kd; (if is only difusse * mat.ks or mat.kd it produces the second image)
一些可能相关的信息
最后四行是连续的,尽管最后一行发生在for循环之外,该循环负责为每个灯光进行明暗度计算。
两条“ hit_normal”行较早发生,它们在上述循环开始之前。
前两个发生在光线跟踪器的开头,位于两个for图像像素循环的较早行中。
如果我交换
中的反映的计算 Vec3f reflected = light_ray_dir - 2 * (light_ray_dir * hit_normal) * hit_normal;
收件人:
Vec3f reflected = 2 * (light_ray_dir * hit_normal) * hit_normal - light_ray_dir;
图像仅稍有变化:
如代码所示,所有三个分量矢量(hit_normal,反射,light_ray_dir,camera_dir)均已标准化。
因此,我要求提供有关如何调试问题的建议,以及可能出问题的建议。感谢您的关注。