You are on page 1of 13

15.03.

2012

GPU Gems: Chapter 7. Rendering Countless Blades of Waving Grass NVIDIA Developer Zone

NVIDIA De elope Zone Dev eloper Centers Resources Technologies Tools

Com m unity Blade of Wa ing G a

GPU Gem : Chap e 7 . Rende ing Co n le


Last updated: Apr 05 2011

GPU Gems
GPU Gem is now av ailable, right here, online. You can purchase a beautifull printed version of this book, and others in the series, at a 3 0% discount courtesy of Inform IT and Addison-Wesley . Please v isit our Recent Documents page to see all the latest whitepapers and conference presentations that can help y ou with y our projects.

Chapter 7. Rendering Countless Blades of Waving Grass


Kurt Pel er Piranha B tes

7.1 Introduction
To sim ulate an idy llic nature scene in a realistic fashion, besides detailed trees and bushes, as well as a com plex water and sky dom e sim ulation, we need a high-quality grass effect. We m ust be able to cov er large areas of the terrain with it, without m onopolizing the GPU. The grass should look naturally grown and should wav e realistically in the wind. In the past, a high-quality grass sim ulation would hav e been considered too com plex for real-tim e applications. The Codecreatures Benchm ark (published by Codecult in 2 002 ) disprov ed this pessim istic assertion. See Figure 7 -1 . In this chapter, we describe a flexible, widely applicable grass sim ulation based on the grass effect shown in the benchm ark. Additionally , a special v ersion of our Codecreatures Benchm ark application is included in this book's accom pany ing m aterial, which offers an interactiv e dem o m ode.

developer.nvidia.com/content/gpu-gems-chapter-7-rendering-countless-blades-waving-grass

1/13

15.03.2012

GPU Gems: Chapter 7. Rendering Countless Blades of Waving Grass NVIDIA Developer Zone

Figure 7-1 Screenshot of the Codecreatures Benchm ark Application

7.2 Overview
First, we should realize that a detailed m odeling of the indiv idual blades of grass is not m eaningful, because the num ber of poly gons that would be required for larger m eadows would be m uch too high. A scene with countless blades of poly gonal grass would not be display able in real tim e with the graphics hardware av ailable today . So we hav e to build a sim ple and useful alternativ e that m eets the following conditions: Many blades of grass m ust be represented by few poly gons. Grass m ust appear dense from different lines of sight. In the next section, we build grass objects that m eet these conditions. Additionally , we m ust be able to anim ate the grass realistically . In Section 7 .4 , we discuss three different anim ation m ethods.

7.3 Preparation of the Grass Objects


As we just m entioned, m any blades of grass m ust be represented by few poly gons. We start by solv ing this problem . Independent of the cam era position and direction, the appearance should be like that of an open country side. Fortunately , the solution is not too difficult. In Section 7 .3 .1 , we start by com bining sev eral blades of grass and display ing them in one texture. But this is not enough: som e poly gons that use this texture m ust be com bined in such a way that the indiv idual poly gons are not noticeable (see Section 7 .3 .2 ). When the v iewer m ov es around, we add or rem ov e grass objects in the distance by blending them in or out. This ensures that the com plete grass effect will hav e robust v isual quality .

7.3.1 Grass Texture

developer.nvidia.com/content/gpu-gems-chapter-7-rendering-countless-blades-waving-grass

2/13

15.03.2012

GPU Gems: Chapter 7. Rendering Countless Blades of Waving Grass NVIDIA Developer Zone

Now let us see how to build a texture for the task we hav e to solv e. The required texture has to cluster sev eral blades of grass; otherwise, it will hav e large transparent areas. We obtain this sim ply by drawing solid grass stem s in a transparent alpha channel. In the color channel, we should use different shades of green and y ellow to get a better differentiation of single blades. We m ay want to sim ulate blades of grass in good and bad conditions, to represent differences in age or ripeness, and ev en to distinguish front and back faces of the blades. A concrete exam ple of a grass texture is shown in Figure 7 -2 .

Figure 7-2 Schem atic of a Grass Texture

7.3.2 Grass Objects


This section explains how to com bine som e poly gons, m apped with the grass texture built in the prev ious section, in a way that the sim ulated grass appears dense, and without highlighting indiv idual poly gons. The technique also guarantees that the indiv idual poly gons are not v isible. Because the user can nav igate freely through the scene, a construction sim ilar to the one shown in Figure 7 -3 would be insufficient to produce a conv incing effect. A linear arrangem ent of the grass poly gons would im m ediately m ake the structure recognizable if som eone were to v iew the scene perpendicular relativ e to the direction of the poly gons. Additionally , the grass would look v ery thin in this case. An arrangem ent like this one should be considered only with autom atic cam era nav igation or unreachable, far-distant m eadows.

Figure 7-3 Linear Arrangem ent


developer.nvidia.com/content/gpu-gems-chapter-7-rendering-countless-blades-waving-grass 3/13

15.03.2012

GPU Gems: Chapter 7. Rendering Countless Blades of Waving Grass NVIDIA Developer Zone

To ensure good v isual quality independent of the current line of sight, we hav e to cross the grass poly gons. Using configurations that look like stars prov es v ery worthwhile. Figure 7 -4 presents two possible v ariants of "grass objects," consisting of three intersecting quads. We hav e to render the poly gons with disabled back-face culling to achiev e v isibility on both sides. To attain proper illum ination, we should orient the norm al v ectors of all v ertices parallel to the poly gons' v ertical edges. This guarantees correct lighting for all grass objects situated on slopes, with no differences due to the brightness of the terrain.

Figure 7-4 Grass Objects If we set these grass objects quite close together in a large area, as shown in Figure 7 -5, sort them backto-front at runtim e, use alpha blending, and enable z-testing/writing in the draw call, then the im pression of a naturally and thickly grown m eadow appears.

Figure 7-5 An Expanse of Grass

7.4 Animation
To continue with the next step, we want to realistically anim ate the grass of a com plete m eadow, built with "grass objects" like those presented in Figure 7 -5. This section describes three different v ariants of anim ation. Each has its pros and cons. Section 7 .4 .1 presents the general idea of our anim ation
developer.nvidia.com/content/gpu-gems-chapter-7-rendering-countless-blades-waving-grass 4/13

15.03.2012

GPU Gems: Chapter 7. Rendering Countless Blades of Waving Grass NVIDIA Developer Zone

m ethods. In Section 7 .4 .2 , clusters of sev eral grass objects standing close together are anim ated in the sam e way . In Section 7 .4 .3 , each v ertex gets its own translation v ector. Finally , in Section 7 .4 .4 , we try to find the golden m ean: a different anim ation for each grass object.

7.4.1 The General Idea


In order to achiev e a highly realistic anim ation, we are going to use a calculation based on trigonom etric functions, especially sine and cosine. This calculation should take into account the position that has to be m ov ed (whether it is a v ertex or the center of an object or cluster) and the current tim e. Also, the direction and strength of the prev ailing wind will be factors. Each of our techniques m ov es only the upper v ertices of the grass objects. In a v ertex shader it is easy to differentiate between these v ertices and the lower ones by exam ining the texture coordinates. All upper v ertices should hav e the sam e v coordinate for the grass texture: such as zero, or a v alue close to it. The fram ework in the v ertex shader code, as shown in Listing 7 -1 , is the sam e in all three techniques; only the pure anim ation part differs. The anim ation code can be found in the following sections.

Example 7-1. Framework in the Vertex Shader

/ /

/ E /

C /HS LL

/ /

742 743 .., ..,

744 ..

/ /

V_NU { SIPT 3voiin:PSTO; Psto OIIN 3voml Nra :NRA; OML

2Txors:TXOR0 eCod ECOD;

/ T /

744 ..

3vbetoiin:TXOR1 OjcPsto ECOD; ;

V_UPT{ SOTU 4voiin:PSTO; Psto OIIN 4vifs Dfue :CLR OO; 2Txors:TXOR0 eCod ECOD; ; V_EP{ STM 3voiin Psto; 3voml Nra; ;

44molVePoMti; Wrdiwrjarx
developer.nvidia.com/content/gpu-gems-chapter-7-rendering-countless-blades-waving-grass 5/13

15.03.2012

GPU Gems: Chapter 7. Rendering Countless Blades of Waving Grass NVIDIA Developer Zone

vih; Lgt fbetegt OjcHih;

V_UPTmi( SOTU an { V_UPTot SOTU u; V_EPtm; STM ep

V_NU v SIPT )

/ A /

(.eCod. < 01 { / O: vTxorsy = .) /

(. T C

. > 09 = .)

/ ANIMATION ( /

/ I / ... / < C / -

742 743 .., ..,

744 ..

/ O / otvoiin= u.Psto ( 4tm.Psto,1, (epvoiin )

molVePoMti) Wrdiwrjarx; otvifs = u.Dfue (Lgt tm.Nra) vih, epvoml;

otTxors=vTxors u.eCod .eCod; ot u;

7.4.2 Animation per Cluster of Grass Objects


The following m ethod was used in the Codecreatures Benchm ark and produces a realistic look with gusting winds that constantly change strength and direction. Here, the shift of the upper poly gon v ertices happens uniform ly for a group of nearby grass objects. To produce a natural-looking anim ation, we should select a cluster size that is not too large. See Figure 7 -6 .

Figure 7-6 Anim ation per Cluster of Grass Objects The translation v ector for the anim ation is com puted by the CPU and is handed ov er to the v ertex shader as a constant param eter. Using a m ore expensiv e algorithm on the CPU allows us to take adv antage of a v ery com plex wind sim ulation. Because we supply each cluster of grass objects with its own translation v ector, we hav e to change this constant param eter for each cluster. So we hav e to
developer.nvidia.com/content/gpu-gems-chapter-7-rendering-countless-blades-waving-grass 6/13

15.03.2012

GPU Gems: Chapter 7. Rendering Countless Blades of Waving Grass NVIDIA Developer Zone

interrupt the rendering of a com plete m eadow quite often and use a separate draw call for each cluster.

Pros
Com plex anim ation calculations are m ade through CPU-based algorithm s. There are no distortions, because of the constant distance of the upper v ertices of a poly gon.

Cons
Many draw calls are required to display a com plete m eadow. Clusters m ay be apparent due to sy nchronized anim ation of all v ertices of a com plete object cluster.

Algorithm
1 . On the CPU, calculate the current translation v ector for the next cluster using the position of the cluster'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 translation 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 /

(..) 742

/ /

3vlseTasain / C Cutrrnlto; /

CU P

V_UPTmi( SOTU an { ...

V_NU v SIPT )

/ ANIMATION( /

/ H /

742 ..

tm.Psto =vvoiin+vlseTasain epvoiin .Psto Cutrrnlto; tm.Nra = epvoml (.Nra *fbetegt+ vvoml OjcHih vlseTasain; Cutrrnlto)

.. .

7.4.3 Animation per Vertex


developer.nvidia.com/content/gpu-gems-chapter-7-rendering-countless-blades-waving-grass 7/13

15.03.2012

GPU Gems: Chapter 7. Rendering Countless Blades of Waving Grass NVIDIA Developer Zone

One of the m ain problem s with the m ethod discussed in Section 7 .4 .2 is poor perform ance because of the high num ber of draw calls, which indiv idually render only a sm all num ber of poly gons. It would be better if we could render a large area cov ered with grass by using a m uch lower num ber of draw calls. Howev er, we hav e to relocate the com plete anim ation com putation into the v ertex shader to be able to m ov e each v ertex separately , relativ e to its position. See Figure 7 -7 .

Figure 7-7 Anim ation per Vertex Because the translations for each v ertex are com puted indiv idually , the length of the edge between the upper v ertices of the grass poly gons is no longer constant, as shown in Figure 7 -8. Therefore, v isible distortions m ay appear because of the inconstant length and thickness of each blade of grass, but ty pically these artifacts will not be v ery noticeable.

Figure 7-8 Texture Distortion Additionally , the ov erall effect m ay seem m ore unnatural than in the prev ious m ethod. Because the translation of all v ertices in a nearby region is v ery sim ilar, an absence of local chaos and a v ery hom ogeneous anim ation results. We are able to elim inate this disadv antage by using a pseudo-random function in the v ertex shader to achiev e m ore v aried results.

Pros
Only a few draw calls, perhaps ev en just one, are necessary to display a com plete m eadow. Vary ing the v ertex position in the v ertex shader allows for the continuity of a rippling wav e of wind. The clusters are indistinguishable.

Cons

developer.nvidia.com/content/gpu-gems-chapter-7-rendering-countless-blades-waving-grass

8/13

15.03.2012

GPU Gems: Chapter 7. Rendering Countless Blades of Waving Grass NVIDIA Developer Zone

Distortion appears, due to the v ariable distance of the upper v ertices of a poly gon. Anim ation m ay appear hom ogeneous due to a lack of local chaos. The com plexity of the anim ation calculation is lim ited.

Algorithm
1 . Set constants, such as tim e stam p and the basic strength and direction of the wind, for the v ertex shader. 2 . Execute one draw call for the com plete m eadow or large area of grass. 3 . Use the v ertex shader to calculate anim ation based on v ertex position. See Listing 7 -3 .

Example 7-3. Code for Animation per Vertex

/ /

/ A /

(..) 743

/ /

fietm; TmSap 3vidieto; WnDrcin fidtegh WnSrnt;

V_UPTmi( SOTU an { ...

V_NU v SIPT )

/ ANIMATION ( /

/ H /

743 ..

3vetxrnlto =ClTasainvvoiin VreTasain acrnlto(.Psto, fietm, TmSap vidieto, WnDrcin fidtegh; WnSrnt) tm.Psto =vvoiin+vetxrnlto; epvoiin .Psto VreTasain tm.Nra = epvoml (.Nra *fbetegt+ vvoml OjcHih vetxrnlto) VreTasain; ...

7.4.4 Animation per Grass Object


To increase the apparent v isual com plexity of the anim ation based on the m ethods presented in Sections 7 .4 .2 and 7 .4 .3 , we com bine an undistorted grass texture and a low num ber of draw calls with local chaos and thereby gain the adv antages of both m ethods. We are able to com bine these m ethods because we do not com pute the anim ation for each v ertex based on its position; rather, we do it based on
developer.nvidia.com/content/gpu-gems-chapter-7-rendering-countless-blades-waving-grass 9/13

15.03.2012

GPU Gems: Chapter 7. Rendering Countless Blades of Waving Grass NVIDIA Developer Zone

the center position of the grass object each consisting of three intersecting quads (see Section 7 .3 .2 ). Because neighboring grass objects now hav e different anim ations, we can represent the desired local chaos, as shown in Figure 7 -9 . Additionally , the constant anim ation for each grass object prev ents the horizontal texture distortions.

Figure 7-9 Anim ation per Grass Object To m ake this possible, each v ertex m ust know the center position of its object, either relativ e to its position or absolute in the world. The grass object position v ector needed for this inform ation m ust be in the v ertex form at (that is, stored in texture coordinates), because the v ertex shader has to read this v alue.

Pros
Only a few draw calls, perhaps ev en just one, are necessary to display a com plete m eadow. There are no distortions, because of the constant distance of the upper v ertices of a poly gon. Local v ariance creates a m ore natural look.

Cons
Additional data is required in the v ertex form at, because each v ertex also contains the center position v alue of its grass object. The com plexity of the anim ation calculations is lim ited, in order to m inim ize shader cost.

Algorithm
1 . Set constants, such as tim e stam p and the basic strength and direction of the wind, for the v ertex shader. 2 . Execute one draw call for the com plete m eadow or large area of grass. 3 . In the v ertex shader, com pute anim ation based on the center position of the grass object. See Listing 7 -4 .

Example 7-4. Code for Animation per Grass Object

developer.nvidia.com/content/gpu-gems-chapter-7-rendering-countless-blades-waving-grass

10/13

15.03.2012

GPU Gems: Chapter 7. Rendering Countless Blades of Waving Grass NVIDIA Developer Zone

/ /

/ A /

(..) 744

/ /

fietm; TmSap 3vidieto; WnDrcin fidtegh WnSrnt;

V_UPTmi( SOTU an { ...

V_NU v SIPT )

/ ANIMATION ( /

/ H /

744 ..

3vbetrnlto =ClTasainvvbetoiin OjcTasain acrnlto(.OjcPsto, fietm, TmSap vidieto, WnDrcin fidtegh; WnSrnt) tm.Psto =vvoiin+vbetrnlto; epvoiin .Psto OjcTasain tm.Nra = epvoml (.Nra *fbetegt+ vvoml OjcHih vbetrnlto) OjcTasain; ...

7.5 Conclusion
We hav e succeeded in building a realistic grass sim ulation that m eets the three m ost im portant requirem ents: Extensiv e usability without ov erly stressing perform ance Natural appearance from all lines of sight Anim ation based on prev ailing wind conditions (with three different v ariants) A special v ersion of the Codecreatures Benchm ark application, offering an interactiv e dem o m ode as shown in Figure 7 -1 0, can be found on the book's CD or Web site. In the application, y ou can nav igate using a free cam era and switch the render states. You are encouraged to exam ine this application and take a look behind the scenes!

developer.nvidia.com/content/gpu-gems-chapter-7-rendering-countless-blades-waving-grass

11/13

15.03.2012

GPU Gems: Chapter 7. Rendering Countless Blades of Waving Grass NVIDIA Developer Zone

Figure 7-10 Screenshot of the Codecreatures Benchm ark

7.6 Further Reading


If y ou are interested in doing som e m ore research on sim ulating anim ated grass, here are som e resources that deal with this subject. You can find articles that describe v ertex shaders using a sine function to do the procedural anim ation of the grass geom etry here: NVIDIA Corporation. 2 003 . "Basic Profile Sam ple Shaders: Grass." In The Cg Toolkit User's Manual. Av ailable online at h p://de elope .n idia.com/objec /cg_ e _man al.h ml

Isidoro, J., and D. Card. 2 002 . "Anim ated Grass with Pixel and Vertex Shaders." In Direct3D ShaderX, edited by W. F. Engel. Wordware Publishing. Other dem os presenting grass effects are av ailable on the following two Web sites. These two dem os also use v ertex shaders to calculate the wav ing m otions of the grass geom etry : NVIDIA Web site: h p://de elope .n idia.com/ ie .a p?IO=demo_g a ATI Web site: h p:// .a i.com/de elope /Sample /G a .h ml

I would like to thank m colleagues at Piranha B tes and Codecult who contributed to the Codecreatures Benchmark, especiall Horst Dworc ak (Lead Artist), who had the idea to build and animate the grass objects in the wa presented in the Benchmark; and Oliver Hoeller (Lead Programmer), who helped to integrate the complete effect into the engine. Book GPU Gem : Pa I - Na al Effec GPU Gem s: Chapter 1 . Effectiv e Water Sim ulation from Phy sical Models GPU Gem s: Chapter 2 . Rendering Water Caustics
developer.nvidia.com/content/gpu-gems-chapter-7-rendering-countless-blades-waving-grass 12/13

15.03.2012

GPU Gems: Chapter 7. Rendering Countless Blades of Waving Grass NVIDIA Developer Zone

GPU Gem s: Chapter 3 . Skin in the "Dawn" Dem o GPU Gem s: Chapter 4 . Anim ation in the "Dawn" Dem o GPU Gem s: Chapter 5. Im plem enting Im prov ed Perlin Noise GPU Gem s: Chapter 6 . Fire in the "Vulcan" Dem o GPU Gem s: Chapter 7 . Rendering Countless Blades of Wav ing Grass GPU Gem s: Chapter 8. Sim ulating Diffraction Q icklink The NVIDIA Registered Dev eloper Program Regi GDC 2 01 2 NVIDIA Graphics SDK 1 1 CUDA Newsletter CUDA Downloads Application Engine Introduction CUDA GPUs Fea ed A icle e ed De elope Web i e NVDe elope (old i e)

Zone In Prev iousPauseNext

About Contact Copy right 2012 NVIDIA Corporation

Legal Inf ormation Priv acy Policy Code of Conduct

developer.nvidia.com/content/gpu-gems-chapter-7-rendering-countless-blades-waving-grass

13/13

You might also like