Upcoming Events
VIEW Conference 2009
11/4 - 11/7 @ Turin, Italy

Project Horseshoe
11/5 - 11/8 @ Burnet, TX

Independent Game Conference West
11/5 - 11/6 @ Los Angeles, CA

IGDA Leadership Forum
11/12 - 11/13 @ San Francisco, CA

More events...


Quick Stats
6702 people currently visiting GDNet.
2337 articles in the reference section.

Help us fight cancer!
Join SETI Team GDNet!



Link to us

Link to us

  search:   

Realistic Natural Effect Rendering: Water I


Optical properties of real life water

The next sections assume that a valid water surface heightfield, along with the grid normals is available, and will discuss the visual properties of the surface needed for realistic rendering. Several different wave models and methods of heightfield generation will be discussed later in this series.

The final goal being the photorealistic rendering of a water surface, one should start by examining the visual properties of real world water. Consider the picture of a calm lake (Fig. 1). The first optical effect that strikes the eye is the reflection of the environment in the water. Incoming light from the environment is reflected by the surface, which essentially acts like a large mirror. This reflection is not perfectly stable and clear, but distorted by the turbulences of the surface. Next, when looking down through the water body to the ground, an effect known as refraction becomes apparent. When light passes through the boundary between two media of different density, it changes its direction due to the light speed differential. In this specific case, one medium is air, the other one is the water, and the boundary is the surface. From a visual point of view, refraction distorts the image of all objects under water, again according to the dynamics of the surface. Now, it seems light can sometimes be reflected by the surface and sometimes be transmitted through the water body. The ratio of reflected versus transmitted light at a certain point on the surface is determined by several factors. The most important one is the angle of the viewer to the surface. Consider again a real life lake. When looking straight down into the water, at a very large view angle, the surface is almost fully transparent and no reflection takes place. However, when looking at the surface from a distance, the view angle becomes smaller and the amount of reflection increases. At the same time, the transparency decreases, until the water surface becomes almost completely opaque. This optical property is called the Fresnel effect.

Picture of a lake
Fig. 1

When looking closer at the movement of a water surface, it becomes apparent that dynamics are distributed over the entire frequency spectrum. From large scale, low frequency features such as long and slow waves, to very small high frequency turbulences that almost seem chaotic and unpredictable in nature. Water is in fact a multi-resolution phenomenon, and this large dynamic range of possible fluctuations needs to be taken into account in order to achieve a realistic result under all viewing conditions. Fast vertex processing can conveniently approximate large-scale features, while small scale turbulences have to be treated per pixel.

Until now, perfectly clear and fully transparent water was assumed. In reality, water almost always contains a varying amount of impurities, most often in the form of small dirt particles floating in suspension. These particles, as well as the water molecules themselves, alter the optical properties by scattering and absorbing part of the transmitted light, decreasing the visibility under water and creating effects such as light shafts. They are the reason why shallow water seems more transparent than deep water. The longer the distance light has to travel through the medium, the higher the probability for it to be scattered or absorbed by a particle or molecule. Besides its effect on translucency, wavelength dependent light scattering and absorption gives water its inherent colouring, such as the commonly encountered blue and greenish tones.

Last but not least, water is an excellent specular reflector at shallow angles. The specular part of the standard Phong lighting model can be applied to the surface, using a large specular exponent. Fig. 2 shows the typical light band resulting from specular reflection. Under certain view angles, water can almost act as a perfect mirror, reflecting incoming light directly towards the viewer. Under high intensity light, such as from the sun, those direct reflected light paths result in bright sparkles (Fig. 3)


Picture of sun specular
Fig. 2

Picture of sun sparkles
Fig. 3

Rendering reflections with cubemaps

Since the introduction of hardware support for cubemaps, cubic environment mapping became a popular method of rendering reflective geometry [3]. Incident light around a specific point in space is stored in the environment map, which acts as a 360° lookup table. The cubemap is indexed by a 3-component direction vector, and returns the incoming light from the direction pointed by the vector. This behaviour can be used to create reflective objects. First, an RGBA image of the environment is stored in the cubemap: a camera with a 90° FOV is positioned at the centre of the cubemap, and six images are rendered along the major axes onto the respective sides of the cube. The environment, as seen over a full 360° sphere around the centre point is now encoded in the map. This step can be precomputed, if the environment is static. In a second pass, the reflective object is rendered, and a reflection vector is computed at each vertex or pixel. This reflection vector is used as an index into the environment cubemap, essentially retrieving the incoming light from the direction of the reflected view ray. Source 1 shows a simple vertex and pixel shader performing reflection using the cubemap approach (vertices and cubemap are assumed to be in world space).

void VP_cube_reflect( float4 inPos : POSITION,
                      float3 inNormal : NORMAL,
                      out float4 outPos : POSITION,
                      out float3 outTexCube : TEXCOORD0,
                      uniform float4x4 Mvp,
                      uniform float3 cameraPos )
{
    // transform vertex position by combined view projection matrix
    outPos = mul(Mvp, inPos);
	
    // create ray from camera position to the vertex, in world space
    float3 V = inPos - cameraPos;
	
    // compute the reflection vector, and assign it to texcoord 0
    outTexCube = reflect(V, inNormal);
}

void FP_cube_reflect( float3 inTexCube : TEXCOORD0,
                      out float4 outCol : COLOR,
                      uniform samplerCUBE EnvCubeMap )
{
    // sample cubemap with the reflection vector
    outCol = texCUBE(EnvCubeMap, inTexCube);
}
Source 1: cubemap reflection shader

However, environment mapping suffers from a critical drawback. Since the incoming light is only sampled around a single point in space, it is only valid for an object, if the environment is assumed to be infinitely far away. While this assumption usually holds up pretty well for very large distance environments such as sky scapes (for example the well known skybox), local reflections cannot be represented at all. The attempt of refleting a local object using an environment cubemap with result in severe visual artifacts. Still, the algorithm has the advantage of being single pass with static skies, making it a very fast method of adding reflections onto water surfaces without direct local geometry. It can also handle very steep waves, making it optimal for rough water bodies such as the ocean.

Tip:

Under certain circumstances, the reflection vector can point downwards, into the lower hemisphere of the cubemap. In reality, this leads to the water reflecting itself (often recursively), which we cannot model directly. A way to fake the visual appearance of such interreflections is to first mirror the upper half of the cubemap down into the lower half, then tint the lower half using the general water colour, and finally run it through a Gaussian blur filter. When using a static environment, this process can be precomputed offline and stored with the cubemap.




Planar mirrors for local reflections


Contents
  Introduction
  Optical properties of real life water
  Planar mirrors for local reflections
  Clipping planes
  Putting it all together

  Printable version
  Discuss this article

The Series
  Water I