Realistic Natural Effect Rendering: Water I
Putting it all togetherAs a reminder, the following matrices are used throughout the reflective water algorithm:
The reflective water is generated in two passes: first the environment around the water surface is reflected into a texture, and then the water mesh is rendered projecting the distorted reflection texture onto its surface:
Pass 1: reflection texture generation
At this point, the normal 3D scene can be rendered as usual. The reflection has been saved in the reflection texture for later processing. Pass 2 will be run as soon as the engine decides to render the water surface:
Pass 2: rendering the reflective water surface
Only pass 2 requires special shaders in order to render the water surface: a vertex shader to create the distorted projective texture coordinates, and a fragment shader to perform the projective texture lookup. Alpha blending can be used to make the water surface slightly transparent. In this case, additional functionality has to be added to the shaders, in order to pass the transparency into the alpha component of the output colour. A simple example of such a basic water shader combo is given below:
void VP_water_1( float4 inPos : POSITION,
float3 inNormal : NORMAL,
out float4 outPos : POSITION,
out float4 outTexProj : TEXCOORD0,
uniform float4x4 Mvp,
uniform float4x4 Mprojtex )
{
// transform vertex position by combined view projection matrix
outPos = mul(Mvp, inPos);
// the adjustable displacement factor
float d = 4.0;
// temporary variable to hold the displaced vertex position
float4 dPos;
// displace the xy components of the vertex position
dPos.xy = inPos.xy + d * inNormal.xy;
// the original z component is kept
dPos.z = inPos.z;
// the w component is always one for a point
dPos.w = 1.0;
// transform the displaced vertex position by the projective
// texture matrix and copy the result into homogeneous
// texture coordinate set 0
outTexProj = mul(Mprojtex, dPos);
}
void FP_water_1( float4 inTexProj : TEXCOORD0,
out float4 outCol : COLOR,
uniform sampler2D ReflectMap )
{
// projectively sample the 2D reflection texture
outCol.rgb = tex2Dproj(ReflectMap, inTexProj).rgb;
// optionally set alpha component to transparency,
// a constant value in this simple example
outCol.a = 0.8;
}
Source 4: projective displaced reflective water shader
The planar reflection technique described in this article allows basic water surfaces to reflect their local environment according to the surface dynamics. The next article will describe how to add refractions and account for the Fresnel effect. It will also discuss depth dependent visibility limitation, adding small-scale perpixel turbulences onto the surface, and how to perform basic specular water lighting. References[1] nVidia: Cg downloads Copyright © 2004 by Yann Lombard. All rights reserved. Images: http://www.freeimages.co.uk
|
| ||||||||||||||||||||||||||||||||||||||||||||||