在我正在构建的光线追踪器中,我已经让球体折射和焦散效果生效了,然而玻璃球看起来并不是特别好。我相信折射数学是正确的,因为光线似乎弯曲并且倒转了你期望的方式,但它看起来不像玻璃,只是像纸或其他东西。
我读到总反射在很大程度上造就了玻璃的外观,然而当我测试任何一个折射光线是否超过了临界角时,它们都没有,所以我的玻璃球没有总反射。我不确定这是否正常或者我做错了什么。我在下面发布了我的折射代码,如果有人有任何建议,我会非常乐意听取。
我读到总反射在很大程度上造就了玻璃的外观,然而当我测试任何一个折射光线是否超过了临界角时,它们都没有,所以我的玻璃球没有总反射。我不确定这是否正常或者我做错了什么。我在下面发布了我的折射代码,如果有人有任何建议,我会非常乐意听取。
/*
* 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.
}