Upload
sebastian-syta
View
221
Download
0
Embed Size (px)
Citation preview
8/2/2019 GPU Gems_ Chapter 7
1/13
5.03.2012 GPU Gems: Chapter 7. Rendering Countless Blades of Waving Grass NVIDIA Developer Zone
1/13eveloper.nvidia.com/content/gpu-gems-chapter-7-rendering-countless-blades-waving-grass
NVIDIA Deelope Zone
GPU Gem: Chape 7. Rendeing Conle Blade of Waing Ga
Last updated: Apr 05 2011
GPU Gems
GPU Gem is now av ailable, right h ere, online. You can purchase a beautifull printed
version of this book, an d others in th e series, at a 30 % discoun t court esy of Inform IT
an d Addison-Wesley .
Please v isit our Recent Documents page to see all the lat est whit epapers and
conference presentat ions tha t can help y ou with y our projects.
Chapter 7. Rendering Countless Blades of Waving Grass
Kurt Peler
Piranha Btes
7.1 Introduction
To sim ula te an idy llic na tu re scene in a r ealistic fashion, besides detailed trees and bushes, as well as a
com plex water a nd sky dome sim ula tion, we need a hig h-quality gr ass effect. We must be able to cov er
large a reas of the terr ain w ith it, w ithout m onopolizing t he GPU. The grass should look na tur ally grown
and should wav e realistically in the win d.
In th e past, a h igh-qualit y gr ass simu lation would hav e been considered too complex for r eal-time
applications. The Codecreatu res Benchm ar k (published by Codecult in 2 002) disprov ed this pessim istic
assert ion. See Figu re 7 -1. In this cha pter, w e describe a flexible, widely applicable grass sim ula tion
based on th e gra ss effect shown in th e benchm ark . Additionally , a special v ersion of our Codecreatu res
Benchm ark application is included in th is book's accompany ing m aterial, w hich offers an int eractiv e
demo m ode.
Dev eloper Cen ter s Tech nologies Tools
Resou rces Com m un ity
8/2/2019 GPU Gems_ Chapter 7
2/13
5.03.2012 GPU Gems: Chapter 7. Rendering Countless Blades of Waving Grass NVIDIA Developer Zone
2/13eveloper.nvidia.com/content/gpu-gems-chapter-7-rendering-countless-blades-waving-grass
Figure 7-1 Screenshot of the Codecreat ur es Benchm ar k Application
7.2 Overview
First, we should realize that a detailed modeling of the indiv idual bla des of gra ss is not m eanin gful,
becau se the num ber of poly gons that w ould be required for lar ger m eadows would be m uch too hig h. A
scene with count less blades of poly gonal gr ass would not be display able in real t im e with the gra phics
hardware av ailable today.
So we ha v e to build a simple and useful alter nat iv e that m eets the following conditions:
Many blades of gra ss mu st be represented by few poly gons.
Grass mu st appear dense from different lin es of sight.
In th e next section, we build gr ass objects that meet t hese conditions.
Additionally , we m ust be able to anim ate th e grass realistically . In Section 7 .4, we discuss thr ee
different animation methods.
7.3 Preparation of the Grass Objects
As we just ment ioned, m any blades of grass mu st be represented by few poly gons. We start by solv ing
this problem. Independent of the cam era position an d direction, the appearan ce should be like that of an
open coun tr y side. Fortun ately , the solut ion is not too difficult . In Section 7 .3 .1 , we start by combining
sev eral blades of grass and display ing t hem in one textur e. But th is is not enoug h: som e poly gons that
use this texture m ust be combined in such a way that the indiv idual poly gons are not noticeable (see
Section 7 .3.2 ). When th e v iewer m ov es around, we a dd or r emove gr ass objects in the distance by
blending t hem in or out. This ensures that the complete gra ss effect w ill hav e robust v isual qua lity .
7.3.1 Grass Texture
8/2/2019 GPU Gems_ Chapter 7
3/13
5.03.2012 GPU Gems: Chapter 7. Rendering Countless Blades of Waving Grass NVIDIA Developer Zone
3/13eveloper.nvidia.com/content/gpu-gems-chapter-7-rendering-countless-blades-waving-grass
Now let us see how t o build a text ur e for th e task we ha v e to solv e. The requir ed textur e has to cluster
sev eral blades of grass; otherw ise, it will h av e large tran sparent areas. We obtain th is simply by
draw ing solid grass stem s in a tran spar ent alpha cha nnel. In the color chann el, we should use different
shades of green an d y ellow to get a better differentiat ion of sing le blades. We m ay wa nt t o simu late
blades of gra ss in good and bad conditions, to represent differences in a ge or r ipeness, and ev en t o
disting uish front and back faces of the blades.
A concr ete exam ple of a gra ss textu re is shown in Figu re 7 -2.
Figure 7-2 Schematic of a Gra ss Texture
7.3.2 Grass Objects
This section explains how to combine som e poly gons, mapped wit h th e grass textu re built in the
previous section, in a way that the simulat ed grass appears dense, and without h ighlight ing indiv idual
poly gons. The technique also guar antees that the indiv idual poly gons are not v isible.
Because the user can nav igate freely thr ough the scene, a construction similar to the one shown in
Figure 7 -3 would be insufficient to produce a conv incing effect. A linear a rra ngement of the gr ass
poly gons would imm ediately ma ke the stru cture recognizable if someone were to v iew th e scene
perpendicular relativ e to the direction of the poly gons. Additionally , th e gra ss would look v ery thin in
this case. An arr angem ent like this one should be considered only with aut oma tic cam era na v igation or
unreachable, far-distant meadows.
Figure 7-3 Linear Arrangement
8/2/2019 GPU Gems_ Chapter 7
4/13
5.03.2012 GPU Gems: Chapter 7. Rendering Countless Blades of Waving Grass NVIDIA Developer Zone
4/13eveloper.nvidia.com/content/gpu-gems-chapter-7-rendering-countless-blades-waving-grass
To ensure good visual qualit y independent of the curr ent line of sight, w e hav e to cross the grass
poly gons. Using configurations that look like stars prov es very worthwh ile. Figur e 7 -4 presents two
possible v ar iant s of "gra ss objects," consisting of thr ee intersecting quads. We hav e to render th e
poly gons with disabled back-face cullin g to achiev e v isibility on both sides. To attain proper
illum ina tion, we should orient t he norm al v ectors of all v ertices par allel to the poly gons' v ertica l edges.
This gu ar ant ees correct lig ht ing for all g ra ss objects situ ated on slopes, with no differences due to the
brightness of the terrain.
Figure 7-4 Grass Objects
If we set th ese gra ss objects quit e close together in a lar ge ar ea, as shown in Figu re 7 -5, sort them bac k-
to-front a t r untim e, use alpha blending, an d enable z-testing/wr iting in the draw call, th en the
impression of a natu rally and thickly grown m eadow a ppears.
Figure 7-5An Expanse of Grass
7.4 Animation
To continu e with the next step, we wan t to realistically anim ate th e grass of a complete m eadow, built
with "gra ss objects" like th ose presented in Figu re 7 -5. This section describes three different v ar iant s of
anim ation. Each h as its pros and cons. Section 7 .4 .1 presents the gener al idea of our a nim ation
8/2/2019 GPU Gems_ Chapter 7
5/13
5.03.2012 GPU Gems: Chapter 7. Rendering Countless Blades of Waving Grass NVIDIA Developer Zone
5/13eveloper.nvidia.com/content/gpu-gems-chapter-7-rendering-countless-blades-waving-grass
met hods. In Section 7.4 .2 , clusters of sev eral gr ass objects stan ding close together ar e anim ated in the
same way . In Section 7 .4.3 , each v ertex gets its own tr anslation v ector. Finally, in Section 7 .4.4 , we
try to find the golden m ean: a different a nim ation for each g rass object.
7.4.1 The General Idea
In order t o achieve a high ly realistic anima tion, w e are going to use a calculation based on
trig onometr ic functions, especially sine and cosine. This calculat ion should take into accoun t the
position that h as to be moved (whether it is a v ertex or the center of an object or clu ster) and the
curr ent tim e. Also, th e direction a nd strength of the prev ailing w ind will be factors. Each of our
techn iques moves only the upper v ertices of the gra ss objects. In a v ertex shader it is easy to
differentiat e between these v ertices and the lower ones by ex am ining t he textu re coordina tes. All upper
vertices should hav e the same v coordina te for t he gr ass textur e: such as zero, or a v alu e close to it. Th e
framework in t he v ertex shader code, as shown in Listing 7 -1 , is the same in all th ree techniques; only
the pur e anim ation part differs. The anim ation code can be foun d in the following sections.
Example 7-1. Framework in the Vertex Shader
//
// E C / HLSL
// S 7.4.2, 7.4.3, 7.4.4
//
VS_INPUT {
3vPosition : POSITION;
3vNormal : NORMAL;
2TexCoords : TEXCOORD0;
// T S 7.4.4
3vObjectPosition : TEXCOORD1;
;
VS_OUTPUT {
4 vPosition : POSITION;
4 vDiffuse : COLOR;
2TexCoords : TEXCOORD0;
;
VS_TEMP {
3vPosition; 3vNormal;
;
44 mWorldViewProjMatrix;
8/2/2019 GPU Gems_ Chapter 7
6/13
5.03.2012 GPU Gems: Chapter 7. Rendering Countless Blades of Waving Grass NVIDIA Developer Zone
6/13eveloper.nvidia.com/content/gpu-gems-chapter-7-rendering-countless-blades-waving-grass
4 vLight;
fObjectHeight;
VS_OUTPUT main( VS_INPUT v)
{
VS_OUTPUT out;
VS_TEMP temp;
// A
(v.TexCoords.y = 0.9)
// A N I M A T I O N ( )
// I 7.4.2, 7.4.3, 7.4.4
. . . //
8/2/2019 GPU Gems_ Chapter 7
7/13
5.03.2012 GPU Gems: Chapter 7. Rendering Countless Blades of Waving Grass NVIDIA Developer Zone
7/13eveloper.nvidia.com/content/gpu-gems-chapter-7-rendering-countless-blades-waving-grass
inter ru pt the renderin g of a complete meadow quit e often and use a separat e dra w call for each clu ster.
Pros
Complex animation calculations are made through CPU-based algorithms.
Ther e are n o distortions, because of the constant distan ce of the upper v ertices of a polyg on.
Cons
Many draw calls are required to display a complete meadow.
Clusters ma y be appar ent due to sy nchronized anim ation of all v ertices of a complete object
cluster.
Algorithm
1 . On the CPU, calculate the cur rent tr anslation v ector for th e next cluster using the position of thecluster's center .
2. Set the translation v ector as a constant for the v ertex shader.
3. Execute a draw call for the cluster.
4. In the v ertex shader, add the tra nslation v ector to the positions of the upper v ertices. See Listing 7 -
2 .
Example 7-2. Code for Animation per Cluster of Grass Objects
//
// A C G O (7.4.2)
//
3vClusterTranslation; // C CPU
VS_OUTPUT main( VS_INPUT v){
. . .
// A N I M A T I O N ( )
// H 7.4.2
temp.vPosition = v.vPosition + vClusterTranslation;
temp.vNormal = (v.vNormal * fObjectHeight +
vClusterTranslation);
...
7.4.3 Animation per Vertex
8/2/2019 GPU Gems_ Chapter 7
8/13
5.03.2012 GPU Gems: Chapter 7. Rendering Countless Blades of Waving Grass NVIDIA Developer Zone
8/13eveloper.nvidia.com/content/gpu-gems-chapter-7-rendering-countless-blades-waving-grass
One of the m ain pr oblems wit h t he m ethod discussed in Section 7 .4 .2 is poor performa nce because of the
high num ber of draw calls, which indiv idually render only a small n um ber of poly gons. It would be
better if we could render a large ar ea cov ered with gra ss by using a m uch lower num ber of draw calls.
Howev er, we h av e to relocate th e complete anim ation computa tion into the v ertex shader to be able to
mov e each v ertex separately , relativ e to its position. See Figure 7 -7 .
Figure 7-7Anim ation per Vertex
Because the tr anslations for each v ertex ar e computed indiv idually , th e length of the edge between the
upper v ertices of the gr ass poly gons is no longer c onstan t, as shown in Figur e 7 -8. Th erefore, v isible
distortions may appear because of the inconstant lengt h a nd th ickness of each bla de of gra ss, but
ty pically th ese artifacts will not be very noticeable.
Figure 7-8 Textu re Distort ion
Additionally , the overall effect may seem m ore unn atur al tha n in th e prev ious method. Because the
tran slation of all v ertices in a n earby region is v ery similar , an absence of local chaos and a v eryhomogeneous anim ation results. We are able to elimin ate th is disadv ant age by using a pseudo-random
function in t he v ertex shader to achiev e more v aried results.
Pros
Only a few draw calls, perha ps ev en just one, are necessary to display a complete meadow.
Vary ing th e v ertex position in th e v ertex shader allows for th e continu ity of a rippling wa v e of
wind.
The clusters are indistinguishable.
Cons
8/2/2019 GPU Gems_ Chapter 7
9/13
5.03.2012 GPU Gems: Chapter 7. Rendering Countless Blades of Waving Grass NVIDIA Developer Zone
9/13eveloper.nvidia.com/content/gpu-gems-chapter-7-rendering-countless-blades-waving-grass
Distortion appears, due to the v ar iable distan ce of the u pper v ertices of a poly gon.
Anim ation m ay appear homogeneous due to a lack of local chaos.
The complexity of the anim ation calculation is limit ed.
Algorithm
1 . Set constants, such as time stamp and th e basic strength a nd direction of the win d, for th e v ertex
shader.
2. Execute one draw ca ll for the com plete m eadow or large ar ea of grass.
3 . Use the ver tex shader to calcu late anim ation based on v ertex position. See Listing 7 -3.
Example 7-3. Code for Animation per Vertex
//
// A V (7.4.3)
//
fTimeStamp;
3vWindDirection;
fWindStrength;
VS_OUTPUT main( VS_INPUT v)
{
. . .
// A N I M A T I O N ( )
// H 7.4.3
3vVertexTranslation = CalcTranslation(v.vPosition,
fTimeStamp,
vWindDirection,
fWindStrength);
temp.vPosition = v.vPosition + vVertexTranslation;
temp.vNormal = (v.vNormal * fObjectHeight +
vVertexTranslation);
. . .
7.4.4 Animation per Grass Object
To incr ease the apparent v isual complexity of the anim ation based on th e meth ods presented in Sections
7 .4.2 and 7 .4.3 , we combine an u ndistorted grass texture and a low nu mber of draw calls with local
cha osand th ereby gain the adv ant ages of both m ethods. We are able to combin e these methods
becau se we do not compute th e anim ation for each v ertex based on it s position; ra ther, w e do it based on
8/2/2019 GPU Gems_ Chapter 7
10/13
5.03.2012 GPU Gems: Chapter 7. Rendering Countless Blades of Waving Grass NVIDIA Developer Zone
10/13eveloper.nvidia.com/content/gpu-gems-chapter-7-rendering-countless-blades-waving-grass
the cent er position of the gra ss objecteach consisting of three inter secting qua ds (see Section 7 .3 .2 ).
Because neighboring gr ass objects now h av e different an im ations, we can r epresent th e desired local
chaos, as shown in Figur e 7 -9. Additionally , th e constant anim ation for each gr ass object prev ents the
horizonta l text ur e distortions.
Figure 7-9 Anim ation per Grass Object
To ma ke this possible, each v ertex m ust know th e center position of its object, either relat iv e to its
position or absolut e in t he w orld. The gr ass object position v ector needed for t his informa tion m ust be in
the v ertex form at (t hat is, stored in textu re coordinates), because the v ertex shader has to read this
value.
Pros
Only a few draw calls, perha ps ev en just one, are necessary to display a complete meadow.
Ther e are n o distortions, because of the constant distan ce of the upper v ertices of a polyg on.
Local v arian ce creates a more natu ral look.
Cons
Additional data is required in the v ertex form at, because each v ertex also contains th e center
position v alu e of its gra ss object.The complexity of the anim ation calcu lations is lim ited, in order to minim ize shader cost.
Algorithm
1 . Set constants, such as time stamp and th e basic strength a nd direction of the win d, for th e v ertex
shader.
2. Execute one draw ca ll for the com plete m eadow or large ar ea of grass.
3 . In the vertex shader, comput e anim ation based on th e center position of the gr ass object. See
Listing 7 -4.
Example 7-4. Code for Animation per Grass Object
8/2/2019 GPU Gems_ Chapter 7
11/13
5.03.2012 GPU Gems: Chapter 7. Rendering Countless Blades of Waving Grass NVIDIA Developer Zone
11/13eveloper.nvidia.com/content/gpu-gems-chapter-7-rendering-countless-blades-waving-grass
//
// A G O (7.4.4)
//
fTimeStamp;
3vWindDirection;
fWindStrength;
VS_OUTPUT main( VS_INPUT v)
{
. . .
// A N I M A T I O N ( )
// H 7.4.4
3vObjectTranslation = CalcTranslation(v.vObjectPosition,
fTimeStamp,
vWindDirection,
fWindStrength);
temp.vPosition = v.vPosition + vObjectTranslation;
temp.vNormal = (v.vNormal * fObjectHeight +
vObjectTranslation);
. . .
7.5 Conclusion
We hav e succeeded in building a realistic grass simu lation th at m eets the th ree most im portan t
requirements:
Extensiv e usability with out ov erly stressing perform ance
Natural appearance from all lines of sightAnimat ion based on prev ailing w ind conditions (with thr ee different v arian ts)
A special v ersion of the Codecreatu res Benchm ar k application, offering an in tera ctiv e dem o mode as
shown in Figure 7 -1 0, can be foun d on th e book's CD or Web site. In th e application, y ou ca n na v igat e
using a free cam era an d switch the render states. You are encouraged to exam ine this application an d
take a look behin d the scenes!
8/2/2019 GPU Gems_ Chapter 7
12/13
5.03.2012 GPU Gems: Chapter 7. Rendering Countless Blades of Waving Grass NVIDIA Developer Zone
12/13eveloper.nvidia.com/content/gpu-gems-chapter-7-rendering-countless-blades-waving-grass
Figure 7-10 Screenshot of the Codecreatu res Benchm ark
7.6 Further Reading
If you ar e interested in doing som e more resear ch on sim ula ting anim ated gra ss, here ar e some
resources that deal with this subject.
You ca n find art icles that describe vertex sha ders using a sine fun ction to do the procedural a nim ation
of the gra ss geometr y here:
NVIDIA Corporation. 2003. "Basic Profile Sample Shaders: Grass." In The Cg Toolkit User's Manual.
Av ailable online at hp://deelope.nidia.com/objec/cg_e_manal.hml
Isidoro, J., a nd D. Card. 20 02. "Anim ated Grass with Pixel and Vert ex Shaders." InDirect3D ShaderX,
edited by W. F. Engel. Wordwar e Publishing .
Other demos presentin g g rass effects are a v ailable on th e following t wo Web sites. These two demos also
use vertex shaders to calculate th e wav ing m otions of the grass geometr y :
NVIDIA Web site: hp://deelope.nidia.com/ ie.ap?IO=demo_ga
ATI Web site: hp://.ai.com/deelope/Sample/G a.hml
I would like to thank m colleagues at Piranha Btes and Codecult who contributed to the Codecreatures
Benchmark, espec iall Horst Dw orcak (Lead Artist), w ho had the idea to build and animate the grass
objects in the w a presented in the Benchmark; and Oliver Hoeller (Lead Programmer), who helped to
integrate the complete effect into the engine.
Book
GPU Gem: Pa I - Naal Effec
GPU Gems: Chapter 1 . Effectiv e Water Sim ula tion from Phy sical Models
GPU Gems: Chapter 2 . Rendering Water Ca ustics
8/2/2019 GPU Gems_ Chapter 7
13/13
5.03.2012 GPU Gems: Chapter 7. Rendering Countless Blades of Waving Grass NVIDIA Developer Zone
GPU Gem s: Cha pter 3 . Skin in t he "Dawn" Demo
GPU Gems: Chapter 4 . An im ation in th e "Dawn" Demo
GPU Gem s: Cha pter 5. Implem entin g Improv ed Perlin Noise
GPU Gem s: Cha pter 6 . Fire in t he "Vulcan " Dem o
GPU Gem s: Cha pter 7 . Renderin g Countless Blades of Wav ing Grass
GPU Gems: Cha pter 8. Sim ula ting Diffraction
Qicklink
The NVIDIA Registered Developer Progra mRegieed Deelope Webie
NVDeelope (old ie)
GDC 201 2
NVIDIA Gra phics SDK 1 1
CUDA Newsletter
CUDA Down loads
Applicat ion Engine Intr oduction
CUDA GPUs
Feaed Aicle
PreviousPauseNext
About
Contact
Copyright 2012 NVIDIA Corporation
Legal Inf ormation
Priv acy Policy
Code of Conduct
Zone In