Jump to content
• Advertisement

# Refraction problem with my simple ray tracer

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

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 this post

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

#### Share this post

##### 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 this post

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

#### Share this post

##### 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 this post

##### 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 this post

##### 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 this post

##### 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 this post

##### 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 this post

##### 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));

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
Rutin
46
2. 2
3. 3
4. 4
5. 5
• Advertisement

• 13
• 10
• 12
• 10
• 13
• ### Forum Statistics

• Total Topics
632992
• Total Posts
3009755
• ### Who's Online (See full list)

There are no registered users currently online

×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!