# Refraction problem with my simple ray tracer

This topic is 4852 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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

##### Share on other sites
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

##### Share on other sites
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.

##### Share on other sites
Congratulations, it actually works ! :) (p.s. you should be happy :P)

##### Share on other sites
Sweet, Thank you guys! :D
yeah it can't be wrong...I have done the calculations by hand and they all match up.

##### Share on other sites
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.

##### Share on other sites
Quote:
 Original post by SharlinIt 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.

##### Share on other sites
Quote:
Original post by Anonymous Poster
Quote:
 Original post by SharlinIt 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.

##### Share on other sites
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, ...).

##### Share on other sites
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));

1. 1
Rutin
24
2. 2
3. 3
JoeJ
18
4. 4
5. 5

• 38
• 23
• 13
• 13
• 17
• ### Forum Statistics

• Total Topics
631708
• Total Posts
3001837
×