Upcoming Events
Unite 2010
11/10 - 11/12 @ Montréal, Canada

GDC China
12/5 - 12/7 @ Shanghai, China

Asia Game Show 2010
12/24 - 12/27  

GDC 2011
2/28 - 3/4 @ San Francisco, CA

More events...
Quick Stats
106 people currently visiting GDNet.
2406 articles in the reference section.

Help us fight cancer!
Join SETI Team GDNet!
Link to us Events 4 Gamers
Intel sponsors gamedev.net search:

  Contents


 Introduction
 How Waves Are
 Simulated

 Transparent
 Surface Ray-
 Tracing


 Printable version

 


Part 2 - Transparent Surface Ray-Tracing

Now we have our wavemap, we want to have some fun with it. We take a beam of light and let it pass through the surface vertically. Because water has a higher density than air, the beam refracts towards the surface normal, and we can calculate where the beam hits whatever is under there (an image, for example).

First we have to find out what the angle between the incoming light and the surface normal is (figure c).

In figure c, the red line represents the surface normal. The vertical line running through the wavemap represents the incoming light, and the arrow connected to the vertical line is the refracted beam. As you can see, the angle between the refracted beam and the surface normal is smaller than the angle between the incoming beam and the surface normal.

Determining The Angle Of The Incoming Light
This is done by measuring the difference in height between (x, y) and (x-1, y), and between (x, y) and (x, y-1). This gives us a triangle with a base of 1. The angle equals arctan( height difference / 1 ), or arctan( height difference ). Look at figure d for an explanation:

Calculating the angle between the surface normal and the incoming light is very simple in our case. If we draw an imaginary triangle as shown here in red, all we need to do is determine alpha. We get the tangent of alpha when we divide y (height difference) by x (1). In other words, the height difference is the tangent of alpha, and alpha is ArcTan( height difference ).

To convince you of the fact that this is actually the angle between the surface normal and the incoming light, I rotated the red triangle 90 degrees counter-clockwise. As you can see, the hypothenusa runs parallel to the surface normal.

Next, we calculate the refraction. If you remember your highschool physics, you know that:

refraction index = sin( angle of incoming light ) / sin( angle of refracted light )

so that the angle of the refracted beam can be calculated like this:

angle of refracted light = arcsin( sin( angle of incoming light ) / refraction index )

where the refraction index is that of water: 2.0.

Third, we need to calculate where the refracted beam hits the image, or its relative position to the place where the incoming light beam originally entered:

displacement = tan( angle of refracted beam ) * height difference

Transparent Surface Ray-Tracing Example
The following code fragment is not optimized, because you would miss out on all the important details of the calculation.

Popup: Source Listing 2

The following program demonstrates these effects: DXEffect.exe  Source code


Roy Willemse can be contacted at r.willemse@dynamind.nl