i
i
i
i
i
i
i
i
312 V Handheld Devices
Figure 3.5. Water effect using a permutated reflection texture and alpha blending.
intersecting the plane since these are the only objects that will be needed
during the render. If this still proves to be too expensive, the pass can be
reduced to just drawing the key objects in the scene, such as a skybox and
terrain.
2. Favor FBO use over reading from the frame buffer. Rather than using
a copy function such as glReadPixels(), a frame buffer object with a
texture bound to it should be used to store the output of the render pass
in a texture [PowerVR 10]. This avoids the need to copy data from one
memory location (the frame buffer) to another (texture memory), which
would cost valuable cycles and bandwidth within the system. Even more
important, it avoids direct access of the frame buffer that can result in a
loss of parallelism, as the CPU would often be stalled, waiting for the GPU
to render.
3. Render to texture at the lowest acceptable resolution. As the reflection
texture is going to be distorted anyway, a lower resolution may be acceptable
for it. A 256 × 256 texture has proven to be effective in the demo when
running at a 640 × 480 display resolution, but depending on the maximum
resolution of the screen on the platform being developed, this resolution
could be reduced further. Keep in mind that a drop from 256 × 256 to
128 × 128 will result in a 75% lower resolution and workload.
i
i
i
i
i
i
i
i
3. Shader-Based Water Effects 313
4. Avoid using discard to perform clipping. Although using the discard key-
word works for clipping techniques, its use decreases the efficiency of early
order-independent depth rejection performance advantages that the POW-
ERVR architecture offers (See Section 3.3.1 for more information).
3.2.4 Refraction Render Pass
In a case where the rendered water should appear to be fairly deep, adding refrac-
tion to the simulation can vastly improve the quality of the effect. To do this, an
approach similar to that taken during the reflection render pass should be used,
in which all objects below the water are rendered out to a texture (Figure 3.6).
Clipping (using the inverse of the water plane) can be assisted by rough culling
on the CPU beforehand. This reduces the GPU workload [PowerVR 10].
If the effect should produce very clear water, all elements of the scene below
the water should be rendered, including the skybox (or similar object) (Fig-
ure 3.7). If a murky water effect is required, a fogging effect can be used to fade
out objects at lower depths (discussed later in this section).
Once the scene has been rendered to a texture, it can then be utilized by
the water’s fragment shader. The screen-space texture coordinates (as used in
Section 3.2.3) are also used to sample the refraction texture. The refraction
sample is then combined with the reflection sample, either at a constant ratio
(e.g., 50/50), or using an equation such as the Fresnel term to provide a dynamic
Figure 3.6. Refraction stored as a texture.
i
i
i
i
i
i
i
i
314 V Handheld Devices
Figure 3.7. Water effect using only the refraction texture (without fogging).
Figure 3.8. Full water effect, showing edge artifact.
i
i
i
i
i
i
i
i
3. Shader-Based Water Effects 315
mixing of color, based on the current viewing angle. Figure 3.8 shows the full
effect of the water, where reflection and refraction textures are mixed using the
Fresnel term.
Fogging. A fogging effect to simulate water depth can be accomplished by setting
a maximum fogging depth (below which objects are no longer visible) and using
the value to perform a linear fade between the object’s original color and the fog
color based on the object’s depth. To do this, the refraction pass must use the fog
color as the clearing color; objects such as the skybox that should appear infinite,
and therefore past the maximum fogging distance, should be omitted from the
render (see Figure 3.9). Alternatively, a different fade can be used to create a
more realistic fogging effect, such as an exponential fade, but this may come at
an additional cost.
As the demo assumes the water plane’s normal will always lie along the pos-
itive y-axis, the w-component of the water plane equation can be negated and
passed into the shader as the water height (displacement from the center of the
world space along the y-axis). The depth value of the vertex is then calculated
in the vertex shader as an offset from this value:
vtx_depth=w-inVertex.y
The fragment shader then calculates the mixing ratio of the fog color and the
object’s color. This ratio is obtained using the following equation:
Ratio=(vtx_depth)/(max_fog_depth)
Figure 3.9. Water effect using only the refraction texture (with fogging).
i
i
i
i
i
i
i
i
316 V Handheld Devices
(In practice, cycles can be saved by multiplying by the reciprocal of
max fog depth instead of performing a division per vertex). This ratio is clamped
to the range [0, 1] and then is used to determine how much of the fogging color
is applied to the fragment.
On the development hardware, disabling the fogging effect gave a 3.5% in-
crease in performance level.
The max fog depth value can also be used on the CPU to cull geometry below
this depth. This is a good way to decrease the number of objects that need to
be rendered during the refraction pass. As with the reflection pass, this rough
culling reduces the amount of geometry submitted, thus saving bandwidth and
unnecessary computations.
Fresnel term. The Fresnel term is used to determine how much light is reflected
at the boundaries of two semitransparent materials (the rest of which is absorbed
through refraction into the second material). The strongest reflection occurs
when the angle of incidence of the light ray is large, and, conversely, reflection
decreases as the angle of incidence decreases (Figures 3.10 and 3.11). The Fresnel
term provides a ratio of transmitted-to-reflected light for a given incident light
ray.
In practice, this is used to determine the correct mix between the reflected
and refracted textures for any point on the water’s surface from the current view
position. This is the Fresnel principle in reverse, and the ratio can be obtained
using an approximation derived from the same equations.
Figure 3.10. Small angle of Fresnel reflection.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.197.128