Jump to content
  • Advertisement
Sign in to follow this  
Bacterius

Refraction not working (path tracing, Pascal)

This topic is 2522 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

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:

16l0hw9.png
wu5b1e.png

(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 sad.png I hope it's still readable

Share this post


Link to post
Share on other sites
Advertisement
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!

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!