Refractor Error

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 :)

+6
source share
1 answer

I have not checked your refraction formulas, but this does not look right:

 //into air out of obj if(dot(ray,normal) < 0){ n1 = ior; n2 = 1.0f; *cos = dot(ray,-normal); } 

If the point product of the incident ray and the normal is less than zero, and if we assume that the normal points are outside the object (which probably should), then this case corresponds to air -> inside , so your refractive indices should be replaced. As now, you create a sphere with ior 1 / ior , and since the refractive index is less than 1, you observe total internal reflection at the edges.

Here is one of my implementations that you can take a look at if there is anything (it has more functions, but you should be able to identify the details you are interested in and check the consistency of your calculations). For me it looks right, so I believe that fixing refractive indices should do it.

The non-deterministic pattern in the center of the sphere, however, definitely looks like self-intersection. Make sure that in the case of reflection, gently press the reflected beam outside the rugged surface, and in case of refraction, gently press the refracted beam inside to avoid self-intersection.

+3
source

Source: https://habr.com/ru/post/944589/


All Articles