Original Post
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;