因此,在我正在构建的光线追踪器中,我已经获得了用于球体的折射以及焦散效果,但是玻璃球体看起来并不是特别好。我相信折射数学是正确的,因为光线似乎在反转你所期望的方式弯曲,但它看起来不像玻璃,它只是看起来像纸或其他东西。
我已经读过全内反射是造成玻璃看起来像它的大部分原因,但是当我测试我的折射光线是否超过临界角时,它们都没有,所以我的玻璃球体没有全内反射。我不确定这是正常的还是我做错了什么。我已经在下面发布了我的折射代码,所以如果有人有任何建议,我很想听听。
/*
* Parameter 'dir' lets you know whether the ray is starting
* from outside the sphere going in (0) or from in the sphere
* going back out (1).
*/
void Ray::Refract(Intersection *hit, int dir)
{
float n1, n2;
if(dir == 0){ n1 = 1.0; n2 = hit->mat->GetRefract(); }
if(dir == 1){ n1 = hit->mat->GetRefract(); n2 = 1.0; }
STVector3 N = hit->normal/hit->normal.Length();
if(dir == 1) N = -N;
STVector3 V = D/D.Length();
double c1 = -STVector3::Dot(N, V);
double n = n1/n2;
double c2 = sqrt(1.0f - (n*n)*(1.0f - (c1*c1)));
STVector3 Rr = (n * V) + (n * c1 - c2) * N;
/*These are the parameters of the current ray being updated*/
E = hit->point; //Starting point
D = Rr; //Direction
}
此方法在我的主要光线追踪方法 RayTrace() 期间调用,该方法以递归方式运行。下面是负责折射的一小部分:
if (hit->mat->IsRefractive())
{
temp.Refract(hit, dir); //Temp is my ray that is being refracted
dir++;
result += RayTrace(temp, dir); //Result is the return RGB value.
}