Skip to main content
GameDev.net gamedev.net
🔒 Locked

Raytracer - Refraction problem

Started by wasd Jan 3, 2005 at 6:34 PM 1 replies 400+ views
Original Post
wasd
wasd
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;
if(!InternalRay)
	N = -1 * pIntersection->m_vecNormal;
else
	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;





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

http://www.cs.virginia.edu/~gfx/Courses/2004/Intro.Fall.04/handouts/06-light.pdf

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.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.