• Create Account

## Refraction not working (path tracing, Pascal)

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

### #1Bacterius  Members

13100
Like
0Likes
Like

Posted 13 January 2012 - 08:29 PM

Hi,
I've been working on a path tracer and I just implemented the four basic BRDF's (well, three, one is just a combination of two), and I'm using recursive radiance accumulation. I've got the specular, diffuse, and specular+diffuse BRDF's working well:

(do these look correct by the way? the distorted spheres and odd shadowing come from the pinhole camera which warps the image at the edges)

But I can't seem to get the refraction BRDF working, it just seems to reflect everything. This is my code (in Pascal Object):

function TRefractive.BRDF(Ray: TRay; Normal: TVector; Distance: Double; Rand: TPRNG): TRay;
Var
N1, N2, Index: Double;
Theta, Phi: Double;
Fresnel: Double;
begin
{ Choose the correct index of refraction. }
Theta := DotVector(Normal, Ray.Direction);
if Theta > 0 then
begin
N1 := 1;
N2 := FRefractiveIndex;
Normal := MulVector(Normal, -1);
end
else
begin
N1 := FRefractiveIndex;
N2 := 1;
Theta := -Theta;
end;

{ Compute the medium change index. }
Index := N2 / N1;

{ Check for total internal reflection. }
Phi := Index * Index * (1 - Theta * Theta);
if (Phi > 1) then
begin
{ Total internal reflection - fall back to specular BRDF. }
Result.Origin := AddVector(Ray.Origin, MulVector(Ray.Direction, Distance - SELF_INTERSECTION_EPSILON));
Result.Direction := NormalizeVector(AddVector(Ray.Direction, MulVector(Normal, 2 * Theta)));
end
else
begin
{ Compute the Fresnel term. }
Phi := Sqrt(1 - Phi); // Phi is the sine in Snell's law
Fresnel := (Sqr((N1 * Theta - N2 * Phi) / (N1 * Theta + N2 * Phi)) + Sqr((N2 * Theta - N1 * Phi) / (N2 * Theta + N1 * Phi))) * 0.5;

{ Perform a random trial to decide if the ray will reflect or refract. }
if (Rand.random <= Fresnel) then
begin
{ The ray is reflected, fall back to specular BRDF. }
Result.Origin := AddVector(Ray.Origin, MulVector(Ray.Direction, Distance - SELF_INTERSECTION_EPSILON));
Result.Direction := NormalizeVector(AddVector(Ray.Direction, MulVector(Normal, 2 * Theta)));
end
else
begin
{ The ray is refracted - use Snell Law. }
Result.Origin := AddVector(Ray.Origin, MulVector(Ray.Direction, Distance + SELF_INTERSECTION_EPSILON));
Result.Direction := NormalizeVector(AddVector(MulVector(Ray.Direction, Index), MulVector(Normal, Index * Theta - Phi)));
end;
end;
end;

The normal is assumed to by pointing outwards of the geometric primitive (assume this is for a sphere, so the normal points away from the center). So I first decide whether the ray is going into the sphere or out of it, and switch the refractive index and normal vector appropriately. Then I check for total internal reflection. If there is no total internal reflection I go ahead and compute the Fresnel term, and do a random trial to reflect or refract the ray. But even if the ray gets refracted, it just seems to get reflected anyhow by the refraction formula, and I'm not sure why. I hope my code is understandeable, does anyone have a clue what's going on?

Thanks!

Edit: hmm code syntax highlighting doesn't work very well for pascal I hope it's still readable

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

### #2Bacterius  Members

13100
Like
0Likes
Like

Posted 14 January 2012 - 02:05 PM

Solved! It turns out I needed to raise the recursion depth slightly, to account for multiple reflections inside the sphere (otherwise refracted rays eventually get discarded as they reach the maximum recursion depth, resulting in only reflected rays being taken into account). If anyone has that problem in the future, that might be your solution too!

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.