漫反射材质的奇怪光线跟踪行为

Weird raytracing behavior for diffuse materials

本文关键字:光线跟踪 漫反射      更新时间:2023-10-16

我一直在阅读和尝试Peter Shirley的"Ray tracing in one weekend"。在漫反射材料部分之前,一切都进展顺利。基本上,我的算法似乎只是从特定角度投射阴影,而不是漫反射材料,我不知道问题可能来自哪里。

我通常都是一步一步地跟着这本书走的。 前面的部分给出了正确的结果,我从上一节添加到漫反射材料的唯一代码是下面的函数。

以下是漫反射材质代码的特定部分,它们基本上将光线反射到随机方向,从与碰撞点相切的球体中选择(抱歉,如果我的解释不够清楚)。

这是将随机点从球体切线到碰撞点的函数。

vec3 random_in_unitSphere(){
vec3 p;
std::default_random_engine generator;
std::uniform_real_distribution<float> distribution(0.0, 1.0);
do{
p = 2.0*vec3(distribution(generator),distribution(generator),distribution(generator)) - vec3(1,1,1);
}while (p.squared_length() >= 1.0);
return p;
}

这是计算像素颜色的功能(通过投射光线直到它没有击中任何东西)

vec3 color(const Ray& r,Hitable *world){
hit_record rec;
if(world->hit(r,0.0,FLT_MAX,rec)){
vec3 target = rec.p + rec.normal + random_in_unitSphere();
return 0.5*color(Ray(rec.p,target-rec.p),world);
}
else{
vec3 unit_direction = unit_vector(r.direction());
float t = 0.5*(unit_direction.y() + 1.0);
return (1.0-t)*vec3(1.0,1.0,1.0) + t*vec3(0.5,0.7,1.0);
}
}

这是负责为图像的每个像素投射光线的循环。

for(int j = ny-1 ;  j >= 0 ; j--){
for(int i = 0; i < nx ; i++){
vec3 col(0,0,0);
for(int s = 0; s < ns ; s++){
float u = float(i+ distribution(generator)) / float(nx);
float v = float(j+ distribution(generator)) / float(ny);
Ray r = camera.getRay(u,v);
vec3 p = r.pointAt(2.0);
col += color(r,world);
}
col /= float(ns);
int ir = int (255.99*col.r());
int ig = int (255.99*col.g());
int ib = int (255.99*col.b());
outfile<< ir << " " << ig << " " << ib << std::endl;
}
}

这是预期的输出: https://i.stack.imgur.com/G52MH.jpg

这就是我得到的:https://i.stack.imgur.com/XYwhd.jpg

谢谢!

问题很简单,每次生成随机向量时,您都在使用一个新的、默认初始化的伪随机数生成器。随机数生成器包含一些状态,需要保留此状态才能随着时间的推移看到不同的结果。

要解决此问题,只需以一种或另一种方式使您的随机数生成器保持静态:

vec3 random_in_unitSphere(){
vec3 p;
static std::default_random_engine generator{std::random_device{}()};
std::uniform_real_distribution<float> distribution(0.0, 1.0);
do{
p = 2.0*vec3(distribution(generator),distribution(generator),distribution(generator)) - vec3(1,1,1);
}while (p.squared_length() >= 1.0);
return p;
}

在这里,我还使用std::random_device(可能)为生成器添加一些现实世界的随机性。

随机方向函数对我来说看起来是错误的。看起来它应该产生三个方向余弦(wx,wy,wz),它们在半径=1的球体上是均匀的,这样

wx2+wy2+wz2= 1

第一个问题:每次输入函数时都构造随机引擎,因此所有值都相同。我只是把它放在Visual Studio 2017,C++14.1,x64,Win10和两个调用中

-0.383666 -0.804919 0.0944412
-0.383666 -0.804919 0.0944412

第二个问题 - 它不是一个随机维度,长度不等于 1。

更新

在 Wolfram 文章 http://mathworld.wolfram.com/SpherePointPicking.html 之后,这里是解决这两个问题的代码 - 它确实有 RNG 作为参数,所以状态会改变。其次,点现在在单位球面上正确采样,可以用作随机方向。只需将元组替换为vec3

#include <iostream>
#include <random>
#include <tuple>
std::tuple<float,float,float> random_in_unitSphere(std::mt19937& rng) {
std::uniform_real_distribution<float> distribution{};
float x1, x2, l;
do {
x1 = 2.0f * distribution(rng) - 1.0f;
x2 = 2.0f * distribution(rng) - 1.0f;
l = x1 * x1 + x2 * x2;
} while (l >= 1.0f);
float s = sqrt(1.0f - l);
return std::make_tuple(2.0f*x1*s, 2.0f*x2*s, 1.0f - 2.0f*l);
}
int main() {
std::mt19937 rng{ 987654321ULL };

float wx, wy, wz, squared_length;
std::tie(wx, wy, wz) = random_in_unitSphere(rng);
std::cout << wx << " " << wy << " " << wz << 'n';
squared_length = wx * wx + wy * wy + wz * wz;
std::cout << squared_length << 'n';
std::tie(wx, wy, wz) = random_in_unitSphere(rng);
std::cout << wx << " " << wy << " " << wz << 'n';
squared_length = wx * wx + wy * wy + wz * wz;
std::cout << squared_length << 'n';
return 0;
}

更新二

第二个问题是你在单位球体内均匀地生成了点。所以问题不在于方向 - 你的wx,wy,wz是很好的wrt方向,但你的方向矢量的长度。典型的光线跟踪代码是这样的(在某些伪代码中)

auto [x0,y0,z0] = start_new_ray();
auto [wx,wy,wz] = sample_direction();
float path = compute_path_in_geometry(x0,y0,z0,wx,wy,wz); // compute path from start point 0 in the wx,wy,wz direction to next object
// move ray to new surface
x1 = x0 + wx*path;
y1 = y0 + wy*path;
z1 = z0 + wz*path;
// do scattering, illumination, ... at (x1,y1,z1)

如果 (wx,wy,wz) 长度不是 1,则计算为 sqrt((x1-x0)2 + (y1-y0)2+(z1-z0)2) 的长度将不等于path。您的基本几何规则刚刚中断。