Chapter 17. Efficient Soft-Edged Shadows Using Pixel Shader ...

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

For the shadow map itself, we use a depth texture and a "compare" texture application mode that are standard in OpenGL 1.4. Note that using floating-point ... SkiptomaincontentHomeGPUGems2PartII:Shading,Lighting,andShadowsGPUGemsGPUGems2GPUGems3GPUGems2GPUGems2isnowavailable,righthere,online.Youcanpurchaseabeautifullyprintedversionofthisbook,andothersintheseries,ata30%discountcourtesyofInformITandAddison-Wesley. TheCDcontent,includingdemosandcontent,isavailableonthewebandfordownload.Chapter17.EfficientSoft-EdgedShadowsUsingPixelShaderBranchingYury UralskyNVIDIA CorporationRenderingrealisticshadowshasalwaysbeenahardproblemincomputergraphics.Softshadowspresentanevengreaterchallengetodevelopers,evenwhenreal-timeperformanceisnotrequired.Yetshadowsareimportantvisualcluesthathelpustoestablishspatialrelationshipsbetweenobjectsinascene.Inthepast,artistsunderstoodtheimportanceofshadowsandusedthemtotheiradvantagetoemphasizecertainobjectsandadddepthtotheirpictures.Real-worldshadowsalwaysappeartohaveacertainamountofblurrinessacrosstheiredges,andneverappearstrictlyhard.Evenonabright,sunnyday,whenshadowsarethesharpest,thisisanoticeableeffect.Eventhesun,beingahugedistanceawayfromtheEarth,can'tbeconsideredapointlightsource.Additionally,atmosphericscatteringdiffusesincominglight,blurringshadowsfromsunlightevenmore.Recentadvancesingraphicshardwarefunctionalityandperformancefinallyenableustoachievereal-timespeedswhenrenderingsoftshadows.Thischapterdiscussesamethodforrenderingahigh-qualityapproximationtorealsoftshadowsatinteractiveframerates.Themethodisbasedonadaptivelytakingmultipleshadow-mapsamplesinthefragmentshaderusingatechniquecalledpercentage-closerfiltering(PCF).Withthebranchingcapabilitiesoffragmentshaders,thisadaptivesamplingapproachprovidesincreasedvisualqualitywhilestillmaintaininghighperformance,comparedtoalwaystakingafixednumberofsamples.17.1CurrentShadowingTechniquesThemostwell-knownandrobusttechniquesforrenderingshadowsarestencilshadowvolumesandshadowmaps.Bothmethodshavetheiradvantagesanddisadvantages.Stencilshadowvolumesworkbyclassifyingpointsonthescenesurfaceswithrespecttofrustathatencompassspacewherelightingisblockedbyoccluders.Sidesofthesefrustaareformedbyplanescontaininglight-sourcepositionandedgesinanobjectsilhouette,asobservedfromthelightsource'spointofview.Thesidesoftheshadowvolumesarerenderedintoaspecialoff-screenbuffer,"tagging"pixelsinshadow.Afterthat,normalscenerenderingcancoloreachpixelaccordingtoitstagvalue.Thismethodissimpleandelegant,anditrequiresonlyminimalhardwaresupport.Itworkswellwithbothdirectionalandpointlightsources,anditcanbeintegratedprettyeasilyintoanypolygon-basedrenderingsystemsuchthateverysurfacewillbeshadowedproperly.Ontheotherhand,stencilshadowvolumesdependheavilyonhowcomplexthegeometryofashadowingobjectis,andtheyburnalotoffillrate.MostcurrentimplementationsofthestencilshadowvolumesalgorithmrequirequiteabitofworkontheCPUside,thoughthatsituationislikelytochangeinthefuture.Anotherdownsideofstencilshadowvolumesisthatthemethodisinherentlymultipass,whichfurtherrestrictsthepolygoncountofsceneswithwhichitcanbeused.Shadowmapsareessentiallyz-buffersrenderedasviewedfromthelightsource'sperspective.Theentiresceneisrenderedintotheshadowmapinaseparatepass.Whenthesceneitselfisrendered,thepositionofeachpixelistransformedtolightspace,andthenitsz-valueiscomparedtothevaluestoredintheshadowmap.Theresultofthiscomparisonisthenusedtodecidewhetherthecurrentpixelisinshadowornot.Shadowmappingisanimage-spacealgorithm,anditsuffersfromaliasingproblemsduetofiniteshadow-mapresolutionandresamplingissues.Imaginewearelookingatalargetrianglepositionededge-onwithrespecttoalightsourcesuchthattheshadowstretchesoverit.Asyoucanguess,itsprojectionwillcoveronlyahandfuloftexelsintheshadowmap,failingtoreproducetheshadowboundarywithdecentresolution.Anotherdisadvantageofshadowmapsisthattheydonotworkverywellforomnidirectionallightsources,requiringmultiplerenderingpassestogenerateseveralshadowmapsandcovertheentiresphereofdirections.Whentheirdisadvantagescanbeavoidedorminimized,shadowmapsaregreat.Shadowmapsarefullyorthogonaltotherestoftherenderingpipeline:everythingthatcanberenderedandthatcanproduceaz-valuecanbeshadowed.Shadowmapsscaleverywellasthetrianglecountincreases,andtheycanevenworkwithobjectsthathavepartiallytransparenttextures.Thesepropertieshavemadethisalgorithmthemethodofchoiceforofflinerendering.Neitherofthesealgorithmsisabletoproduceshadowswithsoftedges"outofthebox."Extensionstothesealgorithmsexistthatallowrenderingofsoft-edgedshadows,buttheyrelyonsamplingthesurfaceofthearealightsourceandperformingmultipleshadow-resolutionpassesforeachsampleposition,accumulatingtheresults.Thisincreasesthecostoftheshadowsdramatically,anditproducesvisiblecolorbandsinthepenumbraeoftheshadowsifthenumberofpassesisnothighenough.However,itispossibletoefficientlyapproximatesoftshadowsbymodifyingtheoriginalshadow-mappingalgorithm.Thepercentage-closerfilteringtechniquedescribedbyReevesetal.(1987)takesalargenumberofshadow-mapsamplesaroundthecurrentpointbeingshadedandfilterstheirresultstoproduceconvincingsoftshadows.17.2SoftShadowswithaSingleShadowMapWithPCF,insteadofsamplingthelightsourceandrenderingshadowmapsateachsampleposition,wecangetawaywithasingleshadowmap,samplingitseveraltimesintheareaaroundtheprojectedpointintheshadowmap,andthencombiningtheresults.Thismethoddoesnotproducephysicallycorrectsoftshadows,butthevisualeffectissimilartowhatweseeinreality.17.2.1BlurringHard-EdgedShadowsEffectively,wearegoingtoblurourhard-edgedshadowsothatitappearstohaveasofteredge.Thestraightforwardwaytoimplementthisistoperformshadow-mapcomparisonsseveraltimesusingoffsettexturecoordinatesandthenaveragetheresults.Intuitively,theamountofthisoffsetdetermineshowblurrythefinalshadowis.Notethatwecannotblurtheactualcontentsoftheshadowmap,becauseitcontainsdepthvalues.Instead,wehavetoblurtheresultsoftheshadow-maplookupwhenrenderingascene,whichrequiresustoperformseverallookups,eachwithapositionoffsetslightlyfromthepixel'strueposition.Thisapproachresemblesthetraditionalmethodforgeneratingsoftshadowsbyrenderingmultipleshadowmaps.Wejust"rotatetheproblem90degrees"andreplaceexpensivemultipleshadow-map-generatingpasseswithmuchlessexpensivemultiplelookupsintoasingleshadowmap.TheGLSLcodeinListing17-1demonstratesthisidea.Unfortunately,thenaiveapproachonlyworkswellwhenthepenumbraregionisverysmall.Otherwise,bandingartifactsbecomepainfullyobvious,asshowninFigure17-1.Whenthenumberofshadow-mapsamplesissmallcomparedtothepenumbrasize,our"soft"shadowwilllooklikeseveralhard-edgedshadows,superimposedoveroneanother.Togetridofthesebandingartifacts,wewouldhavetoincreasethenumberofshadow-mapsamplestoapointwherethealgorithmbecomesimpracticalduetoperformanceproblems.Figure17-1ANaiveApproachtoBlurringShadowsExample17-1.ImplementingaBasicAlgorithmforPCF#defineSAMPLES_COUNT32#defineINV_SAMPLES_COUNT(1.0f/SAMPLES_COUNT)uniformsampler2Ddecal;//decaltextureuniformsampler2Dspot;//projectedspotlightimageuniformsampler2DShadowshadowMap;//shadowmapuniformfloatfwidth;uniformvec2offsets[SAMPLES_COUNT];//thesearepasseddownfromvertexshadervaryingvec4shadowMapPos;varyingvec3normal;varyingvec2texCoord;varyingvec3lightVec;varyingvec3view;voidmain(void){floatshadow=0;floatfsize=shadowMapPos.w*fwidth;vec4smCoord=shadowMapPos;for(inti=0;i



請為這篇文章評分?