Académique Documents
Professionnel Documents
Culture Documents
2012
GPU Gems: Chapter 7. Rendering Countless Blades of Waving Grass NVIDIA Developer Zone
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.
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
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.
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 .
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.
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.
/ /
/ E /
C /HS LL
/ /
744 ..
/ /
/ T /
744 ..
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
V_NU v SIPT )
/ A /
(. T C
. > 09 = .)
/ ANIMATION ( /
/ I / ... / < C / -
744 ..
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.
/ /
/ A /
(..) 742
/ /
3vlseTasain / C Cutrrnlto; /
CU P
V_NU v SIPT )
/ ANIMATION( /
/ H /
742 ..
tm.Psto =vvoiin+vlseTasain epvoiin .Psto Cutrrnlto; tm.Nra = epvoml (.Nra *fbetegt+ vvoml OjcHih vlseTasain; Cutrrnlto)
.. .
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 .
/ /
/ A /
(..) 743
/ /
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; ...
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 .
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
/ /
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
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)
developer.nvidia.com/content/gpu-gems-chapter-7-rendering-countless-blades-waving-grass
13/13