首页 > 解决方案 > 在光线追踪器上进行 phong 着色的奇怪颜色

问题描述

我最近在 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、reflected、light_ray_dir、camera_dir)都被归一化。

因此,我寻求有关如何调试问题的建议,以及可能出错的建议。感谢您的关注。

标签: c++raytracingshading

解决方案


推荐阅读