我正在用C++编写光线跟踪器,并且我一直在使用折射问题。我渲染一个球体和一个地平面,球体应该折射。然而,它看起来更像是一个球体内的球体:“外部”球体看起来被适当地遮蔽,但不会折射,而“内部”球体看起来像是自我阴影。下面是它的外观链接:http://imgur.com/QVGkeBT。Raytracer折射Bug
下面是相关的代码。
//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修改交叉点和射线的入射点,以检查自身折射,因为您可以获得阴影。
任何帮助,将不胜感激:)
请问您纠正后请给出正确答案吗? 谢谢 – 2013-10-24 16:50:07