我正在用 C++ 编写光线追踪器,但我遇到了一些折射问题。我正在渲染一个球体和一个地平面,球体应该会折射。然而,它看起来更像是一个球内的球:“外”球看起来被适当地着色,但没有折射,而“内”球看起来像是被自阴影覆盖。这是它的外观链接:http: //imgur.com/QVGkeBT。
这是相关的代码。
//inside main raytrace function
if(refraction > 0.0f){ //the surface is refractive
//calculate refraction vector
Ray refract(intersection,
objList[bestObj]->refractedRay(
ray.dir,intersection,&cos_theta,&R0));
//recurse
refrColor = raytrace(refract);
}
else{ //no refraction
refrColor = background;
}
//refractedRay(vec3,vec3,float*,float*)
//...initialize variables, do geometric transforms
//into air out of obj
if(dot(ray,normal) < 0){
n1 = ior;
n2 = 1.0f;
*cos = dot(ray,-normal);
}
//into obj out of air
else{
n1 = 1.0f;
n2 = ior;
*cos = dot(ray,normal);
normal = -normal;
}
//check value under sqrt
float n = n1/n2;
float disc = 1-(pow(n,2)*(1-pow(*cos,2)));
if(disc < 0){ //total internal reflection
return ray - 2*-(*cos)*normal; //reflection vector
}
return (n*ray)+(((n*(*cos))-sqrt(disc))*normal);
球体过去看起来更糟,然后我记得标准化我的向量,它看起来像这样。以前,它看起来只是整个内部球体。在主光线追踪函数中,我以与反射相同的方式进行折射,只是使用折射光线。我还尝试使用 epsilon 修改传入的交点和光线以检查自折射,因为您可以进入阴影。
任何帮助,将不胜感激 :)