Refraction problem with my simple ray tracer

Started by
9 comments, last by RCube 19 years ago
I am having problem with my refraction. I am using snell's law to calculate the refraction ray from the incident ray and surface normal. Right now the image is rendered with only refraction and phong shading(no reflection or shadow, etc). The ball on the left(red) have a index of refraction of 1.03. The right(blue) ball have a index of refraction of 1.5. My env index of refraction is 1.0003. Can someone please tell me why is the blue ball doing that? I checked my calculations many times now. :( Here is the pic: image
Advertisement
Looks good to me. A solid ball of glass *would* look like that, the following admittedly extremely crude ascii image should clarify matters somewhat:


          ________          _-ba------->/___     \       _-        /    ---___\    _-       |            |-_-       |        ___ |- -_        \ ___---   /     -_b------->\________/        -_                             -a
See what happens when the blue ball has an index of refraction of 1.05. Then see what happens when it's 1.1, then 1.2, then 1.3, then 1.4, then 1.5. Then you should start to understand why you seeing that behaviour. An index of refraction of 1.5 causes rays to bend quite a lot.
Congratulations, it actually works ! :) (p.s. you should be happy :P)
huraay!
Sweet, Thank you guys! :D
yeah it can't be wrong...I have done the calculations by hand and they all match up.
It seems that you aren't taking into account total internal reflection, though. In the sqrt part of the refraction formula, if the discriminant is negative, TIR occurs and you have to reflect instead of refracting.
Quote:Original post by Sharlin
It seems that you aren't taking into account total internal reflection, though. In the sqrt part of the refraction formula, if the discriminant is negative, TIR occurs and you have to reflect instead of refracting.


I do check for TIR. If I get one, I cast a reflected ray.
Quote:Original post by Anonymous Poster
Quote:Original post by Sharlin
It seems that you aren't taking into account total internal reflection, though. In the sqrt part of the refraction formula, if the discriminant is negative, TIR occurs and you have to reflect instead of refracting.


I do check for TIR. If I get one, I cast a reflected ray.

its not showing though.

and your refraction DOES look somewhat 'off' to me aswell. the best thing is to build simple testscenes, reason out what it should look like, and see if it matches up for a couple of different cases.
It can be hard to tell if you're doing it right. Here's a simple idea: try and reproduce the scene in POV-Ray, using exactly the same variables (like camera, index of refraction, distances, colour, ...).
Here is the code:

Vector i, n, t; // i incident ray, n surface normal (all unit vectors)
float eta, c1, cs2;

if(inside)
{
// ray leaving
eta = obj->mat_refri / world_refri;
}
else
{
// ray entering
eta = world_refri / obj->mat_refri;
}

c1 = -dot(i, n); // cos(theta1)
cs2 = 1.0-eta*eta*(1.0-c1*c1); // cos^2(theta2)
if (cs2<0) return 0; // total internal reflection
t = i*eta + n*(eta*c1-sqrt(cs2));

This topic is closed to new replies.

Advertisement