I am writing a raytracer in C ++ and I had a problem with refractions. I create a sphere and a plane of the earth, and the sphere must be refracted. However, it looks more like a sphere in a sphere: the "outer" sphere looks properly shaded, but not refracting, and the "inner" sphere looks like it is shaded. Here's a link to how it looks: http://imgur.com/QVGkeBT .
Here is the relevant code.
//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);
The sphere looked worse, then I remembered that I was normalizing my vectors, and it looks like this. Previously, it was only like the inner sphere. Inside the main raytrace function, I do the refraction in the same way as reflection, using the refracted ray instead. I also tried changing the incoming intersection point and ray using epsilon to check for self-refraction, as you can get shading.
Any help would be appreciated :)
source share