Chapter 12. Omnidirectional Shadow Mapping - NVIDIA ...

文章推薦指數: 80 %
投票人數:10人

Shadow mapping lets us visualize shadows cast from different types of light sources, such as directional lights, point lights, and spotlights. SkiptomaincontentHomeGPUGemsPartII:LightingandShadowsGPUGemsGPUGems2GPUGems3GPUGemsGPUGemsisnowavailable,righthere,online.Youcanpurchaseabeautifullyprintedversionofthisbook,andothersintheseries,ata30%discountcourtesyofInformITandAddison-Wesley.TheCDcontent,includingdemosandcontent,isavailableonthewebandfordownload.Chapter12.OmnidirectionalShadowMappingPhilippS.GerasimoviXBT.com12.1IntroductionOneofthemostdifficultproblemsinreal-timecomputergraphicsisgeneratinghigh-qualityshadows.Yet,theappearanceofsuchshadowsisoneofthemostimportantfactorsinachievinggraphicrealism.Incomputer-generatedscenes,anobject'sshadowenhancesourperceptionoftheobjectandtherelationshipbetweenobjects.Incomputergames,shadows—alongwithlighting,music,andspecialeffects—playaveryimportantroleinportrayingarealisticgameatmosphere.Forexample,shadowsareamajorpartofthestorylineinidSoftware'sDoom3,oneofthemosttechnologicallyadvancedgames.Figures12-1and12-2showexamplesofshadowsfromourowndemo,whichisprovidedonthebook'sCDandWebsite.Figure12-1ScreenshotofOurDemo,ShowingaLightSourceFlyingAboveaCharacterFigure12-2AClose-UpoftheCharacterinOurDemoGPUsnowallowustocreateimagespreviouslyavailableonlyinprofessional3Doffline-renderingprograms.ThegeometryprocessorsinmodernGPUscanprocessmillionsofprimitivesperframe,lettingusdesigncomplexworlds.Withtheadventofper-pixelshading,wecanproducerealisticmaterialsusingcomplexmathematicalandphysicallybasedmodelsoflighting.Twopopularmethodsareavailableforvisualizingshadowsinreal-timecomputergraphics:stencilshadowsandshadowmapping.12.1.1StencilShadowsThestencilshadowsmethod,whichisdemonstratedinDoom3,isusedwidelybygamedevelopers.ItoffersadvantagessuchasthelargenumberofGPUsthatsupportit(theonlyessentialhardwarefeatureissupportforaneight-bitstencilbuffer),itsindependencefromthetypeoflightsource,andthehighqualityofitsgeneratedshadows.However,thestencilshadowsapproachhassomeseriousdisadvantages:it'sheavilydependentonCPUwork,itcanproduceonlyhardshadows,itusesalargeamountoffillrate(whichmeansthateventhoughaGPUmaysupportthetechnique,itcouldrunpoorly),anditcannotbeusedwithhardware-tessellatedsurfaces.12.1.2ShadowMappingTheshadow-mappingalgorithmcametocomputergraphicsin1978whenitwasintroducedbyLanceWilliams(Williams1978).Today,thismethodisusedinamultitudeofHollywoodmoviesthatcontaincomputergraphicsandspecialeffects.Shadowmappingprojectsaspecialdynamicallycreatedtextureonscenegeometrytocalculateshadows.Itletsyourenderhardandsoftshadows,aswellasshadowsfromdifferenttypesoflightsources.Plus,itworkswithhardware-tessellatedsurfacesandwithGPU-animatedmeshes(suchasskinnedmeshes).AnumberofGPUmanufacturers,includingNVIDIA,supportshadowmappingdirectlyintheirhardwareandpromisetoenhancethissupportinthefuture.TheNVIDIAGeForce3,GeForce4Ti,andalltheGeForceFX(andmorerecent)GPUssupporthardwareshadowmapsthroughbothDirectXandOpenGL.(However,wedonotusethenativehardwareshadow-mappingfunctionalityinthischapter.)ThepossibilitiesofferedbytheNVIDIACineFXarchitecture—includingsupportforlongfragmentprogramswithtruefloating-pointprecisionaswellasfloating-pointtextureformats—enableanewlevelofqualityinshadowrendering.12.2TheShadow-MappingAlgorithm12.2.1ConditionsShadowmappingletsusvisualizeshadowscastfromdifferenttypesoflightsources,suchasdirectionallights,pointlights,andspotlights.Thetypeoflightsourcedictatesthetechnologyweneedtouse.Thischapterfocusesonvisualizingshadowscastfrompointlightsources.Pointlightsourcesarewidelyusedincomputergames,andthequalityofshadowscastbyobjectsilluminatedbytheselightsisveryimportant.Wealsohavetheseadditionalconditions:Cubemaps,traditionallyusedincomputergraphicstoimplementenvironmentreflections,arethebuildingblocksofourshadow-mappingalgorithm.WefocusourimplementationonDirectX9–compatiblegraphicshardware(forexample,productsintheGeForceFXfamilyfromNVIDIA,ortheRadeon9500andmorerecentproductsfromATI).WeuseDirectXasthegraphicsAPI(butouralgorithmworksjustaswellinOpenGL).Wetakeadvantageofvertexandpixelshaderswritteninahigh-levellanguage.WeuseHLSL(butCgisidealforthistask,too).12.2.2TheAlgorithmTherearetwoprimaryphasesinusingomnidirectionalshadowmaps:creatingtheshadowmapandprojectingit.Inthecreationphase,werenderthesquareddistancefromthelightsourceofallobjectsthatcastshadowsintotheshadowmaptexture(we'llseewhythedistanceissquaredalittlelater).Intheprojectionphase,werenderalltheobjectsthatreceiveshadows,andwecomparethesquareddistancefromtherenderedpixeltothelightsource.Thefollowingtechniquefillsallsixfacesofacubemap,inalldirections:+x,-x,+y,-y,+z,-z.Theshadowmapscanbeeitherprecalculated(forstaticscenes)orre-renderedeveryframe.Wefocusprimarilyonre-renderingtheshadowmapeachframeforfullydynamicshadows.Allobjectscastashadow,andreceiveashadow,fromeachlightsource.Andallobjectsself-shadow.Weuseasingleshadowmapforalllightsources,creatinganimagewithmultipassrenderingandperformingonepassforeachlightsource.Listing12-1isanexampleofpseudocodeforthisalgorithm.Becauseweuseamultipassalgorithm(thatis,makingonepassforeachlightsource),allobjectsmustbecompositedintotheframebuffer.Toreduceoverdrawandimproveperformance,werenderadepth-onlypassfirst.Thisstandardtechniqueensuresthatallsubsequentlightingpassesoccuronlyonvisiblepixels.Renderingtodepth-onlyisveryfast(manyGeForceFXGPUshavedouble-speed"depth-only"renderingfeatures),soitrequiresminimaloverhead,eveninlow-overdrawsituations.Transparentobjectsarenotrenderedinthedepth-onlypass,becausetransparentobjectsdonotupdatethedepthbuffer.SeeListing12-2.Example12-1.PseudocodefortheOmnidirectionalShadow-MappingAlgorithmfor(iLight=0;iLight



請為這篇文章評分?