Jump to content
  • Advertisement
Sign in to follow this  

Raytracer - Refraction problem

This topic is 5039 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to implement refraction in my raytracer, but it doesn't seem to be working right: http://andrewporritt.4t.com/cgi-bin/i/images/Raytracer.jpg There should be a big sphere in the middle with refractive index 1.33 and it kind of refracts correctly, except for the big white circle around the edges of the sphere and the refraction seems to be squashed up into the middle part of the sphere. I think the white circle is because I'm just clamping colour values to 1. The problem might be in the calculation of the refracted ray's direction. Here's the code:
// Calculate m / n
float n = pIntersection->m_fRefractiveRatio = pIntersection->m_fRefractiveIndex / ParentsRI;

// Get normalised D vector
Vec3D D = Normalise(r.m_vecDirection);

// Get correct normal
Vec3D N;
	N = -1 * pIntersection->m_vecNormal;
	N = Normalise(pIntersection->m_vecNormal);

// Calculate D.N
float DdotN = Dot(D, N);

// Calculate cos(theta)
float costheta = sqrtf(1 - (n * n) * (1 - (DdotN * DdotN)));

Vec3D RefractedDir = (n * D) - (n * costheta + (n * DdotN)) * N;

Share this post

Link to post
Share on other sites
You use a different equation to calculate refractive rays than I do, so I won't comment on your code specifically. However, an easy equation with a nice accompanying picture can be found in this document on page 8:


I will, however, share some wisdom with you that I accumulated after writing my own ray tracer and photon mapper. I often ran into bugs where looking at the picture would make any reasonable human being think that the error would be in one small portion of the code. 9 out of 10 times, that wasn't where the bug was located.

Here are some places to look:

1. The sphere doesn't look much like a sphere. This is rather important. It could mean there's a bug in your Normalize() function (this will goof up the rays you shoot from each pixel, along with throwing in some insidious bugs elsewhere in the code). As a rule of thumb, I'd say make sure the basics of the raytracer, such as rendering a sphere that's actually a sphere, work correctly before doing complex lighting calculations. That way, you can at least eliminate the rendering of your primitives from what might be going wrong.

2. How do you calculate shadows? The sphere seems to be between the light source and the checkerboard. A shadow should be cast on the checkerboard.

3. The entire checkerboard texture is seen in the refracted sphere, as well as the entire ceiling, floor, and both walls. I think the white halo is simply the background color. The only way such a phenomena could occur is if your refracted angle is greater than your incident angle (this only happens if your index of refraction is less than one, but you said yours was 1.33). Even if you had an index of refraction less than one, the given image would still be implausible, because generating that image would require the refracted angle be greater than 90 degrees. The fact that the checkerboard is getting minified instead of magnified should be a significant lead. I suggest you step through your calculations with a debugger and keep track of when/where your calculations cross the line between plausible and not plausible. That will probably be where you're messing up.

Share this post

Link to post
Share on other sites
Guest Anonymous Poster
You can get a negative value under the square root, if you do you should get a total reflection instead of a refraction.

Will pIntersection->m_fRefractiveIndex be the refraction index for the surrounding environment (air/vacuum) when you exit the sphere?

It also seems to me like you calculate n (usually called eta) the wrong way, it should be

ParentsRI / pIntersection->m_fRefractiveIndex

if I understand your code correctly.

Good luck

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!