Tutorial 49 - Cascaded Shadow Mapping - OGLdev
文章推薦指數: 80 %
If we can find a way to use a dedicated shadow map for closer objects and a different shadow map for distant objects then the first shadow map will only need to ...
Background
Let'stakeacloseuplookoftheshadowfromtutorial47:
Asyoucansee,theqaulityoftheshadowisnothigh.It'stooblocky.We'vetouchedonthereasonforthat
blockinessattheendoftutorial47andreferredtoitasPerspectiveAliasingwhich
meansalargenumberofpixelsinviewspacebeingmappedtothesamepixelintheshadowmap.Thismeans
thatallthesepixelswilleitherbeinshadoworinlight,contributingtothesenseofblockiness.
Inotherwords,sincetheresolutionoftheshadowmapisnothighenoughitcannotcovertheviewspace
adequately.Oneobviouswaytodealwiththisistoincreasetheresolutionoftheshadowmapbut
thatwillincreasethememoryfootprintofourappsoitmaynotbethebestcourseofaction.
Anotherwaytodealwiththisproblemistonoticethatshadowsclosertothecameraafarmoreimportant
intermsofqualitythanshadowofobjectsthatarefaraway.Distantobjectsaresmalleranywayand
usuallytheeyefocusesonwhathappenscloseby,leavingtherestasa"background".Ifwecanfindaway
touseadedicatedshadowmapforcloserobjectsandadifferentshadowmapfordistantobjectsthenthe
firstshadowmapwillonlyneedtocovertheasmallerregion,thusdecreasingtheratiothatwediscussesabove.
This,inanutshell,iswhatCascadedShadowMapping(a.k.aCSM)isallabout.Atthetimeofwritingthistutorial
CSMisconsideredoneofthebestwaystodealwithPerspectiveAliasing.Let'sseehowwecanimplementit.
Fromahighlevelviewwearegoingtosplittheviewfrustumintoseveralcascades(sinceitdoesn'tneed
tobejusttwoasinthepreviousexample).Forthepurposeofthistutorialwewillusethreecascades:near,middleand
far.Thealgorithmitselfisprettygenericsoyoucanusemorecascadesifyoufeellikeit.Everycascadewill
berenderedintoitsownprivateshadowmap.Theshadowalgorithmitselfwillremainthesamebutwhensampling
thedepthfromtheshadowmapwewillneedtoselecttheappropriatemapbasedonthedistancefromtheviewer.
Let'stakealookatagenericviewfrustum:
Asusual,wehaveasmallnearplaneandalargerfarplane.Nowlet'stakealookatthesamefustumfromabove:
Thenextstepistosplittherangefromthenearplanetothefarplaneintothreeparts.Wewillcallthisnear,middleandfar.
Inaddition,let'saddthelightdirection(thearrowontherighthandside):
Sohowarewegoingtorendereachcascadeintoitsownprivateshadowmap?Let'sthinkabouttheshadow
phaseintheshadowmappingalgorithm.Wesetupthingstorenderthescenefromthelightpointofview.This
meanscreatingaWVPmatrixwiththeworldtransformoftheobject,theviewtransformbasedonthelightand
aprojectionmatrix.Sincethistutorialisbasedontutorial47whichdealtwithshadowsofdirectionallights
theprojectionmatrixwillbeorthographic.IngeneralCSMsmakemoresenseinoutdoorsceneswherethe
mainlightsourceisusuallythesunsousingadirectionallighthereisnatural.Ifyoulookatthe
WVPmatrixaboveyouwillnoticethatthefirsttwoparts(worldandview)arethesameforallcascades.
Afterall,thepositionoftheobjectintheworldandtheorientationofthecamerabasedonthelightsource
arenotrelatedtothesplittingofthefrustumintocascades.Whatmattershereisonlytheprojectionmatrix
becauseitdefinestheextentoftheregionwhichwilleventuallyberendered.Andsinceorthographic
projectionsaredefinedusingaboxweneedtodefinethreedifferentboxeswhichwillbetranslatedinto
threedifferentorthographicprojectionmatrices.Theseprojectionmatriceswillbeusedtocreatethethree
WVPmatricestorendereachcascadeintoitsownshadowmap.
Themostlogicalthingtodowillbetomaketheseboxesassmallasposibleinordertokeeptheratio
ofviewpixelstoshadowmappixelsaslowaspossible.Thismeanscreatingaboundingboxforeachcascade
whichisorientedalongthelightdirectionvector.Let'screatesuchaboundingboxforthefirstcascade:
Nowlet'screateaboundingboxforthesecondcascade:
Andfinallyaboudingboxforthelastcascade:
Asyoucansee,thereissomeoverlapoftheboundingboxesduetotheorientationnofthelightwhich
meanssomepixelswillberenderedintomorethanoneshadowmap.Thereisnoproblemwiththat
aslongasallthepixelsofasinglecascadeareentirelyinsideasingleshadowmap.Theselectionof
theshadowmaptouseintheshaderforshadowcalculationswillbebasedonthedistanceofthepixelfrom
theactualviewer.
Calculationsoftheboundingboxesthatserveasthebasisfortheorthographicprojectioninthe
shadowphaseisthemostcomplicatedpartofthealgorithm.Theseboxesmustbedescribedinlightspace
becausetheprojectionscomeafterworldandviewtransforms(atwhichpointthelight"originates"from
theoriginandpointsalongthepositiveZaxis).Sincetheboxeswillbecalculatedasmin/maxvalues
onallthreeaxistheywillbealignedonthelightdirection,whichiswhatweneedforprojection.Tocalculate
theboundingboxweneedtoknowhoweachcascadelookslikeinlightspace.Todothatweneedtofollowthese
steps:
Calculatetheeightcornersofeachcascadeinviewspace.Thisiseasyandrequiressimpletrigonometry:
Theaboveimagerepresentsanarbitrarycascade(sinceeachcascadeonitsownisbasicallyafrustumand
sharesthesamefield-of-viewanglewiththeothercascades).Notethatwearelookingfromthetop
downtotheXZplane.WeneedtocalculateX1and
X2:
ThisgivesustheXandZcomponentsoftheeightcoordinatesofthecascadeinviewspace.Using
similarmathwiththeverticalfield-of-viewanglewecangettheYcomponentandfinalizethecoordinates.
Nowweneedtotransformthecascadecoordinatesfromviewspacebacktoworldspace.Let'ssaythatthe
viewerisorientedsuchthatinworldspacethefrustumlookslikethat(theredarrowisthelightdirection
butignoreitfornow):
Inordertotransformfromworldspacetoviewspacewemultiplytheworldpositionvectorby
theviewmatrix(whichisbasedonthecameralocationandrotation).Thismeansthatifwealready
havethecoordinatesofthecascadeinviewspacewemustmultiplythembytheinverseoftheviewmatrix
inordertotransformthemtoworldspace:
Withthefrustumcoordinatesinworldspacewecannowtransformthemtolightspaceasanyotherobject.
Rememberthatthelightspaceisexactlylikeviewspacebutinsteadofthecameraweusethelightsource.
Sincewearedealingwithadirectionallightthathasnooriginwejustneedtorotatetheworldsothat
thelightdirectionbecomesalignedwiththepositiveZaxis.Theoriginoflightcansimplybetheorigin
ofthelightspacecoordinatesystem(whichmeanswedon'tneedanytranslation).Ifwedothatusingtheprevious
diagram(withtheredarrowbeingthelightdirection)thecascadefrustuminlightspaceshouldlooklike:
Withthecascadecoordinatesfinallyinlightspacewejustneedtogenerateaboundingboxforit
bytakingthemin/maxvaluesoftheX/Y/Zcomponentsoftheeightcoordinates.Thisboundingbox
providesthevaluesfortheorthographicprojectionforrenderingthiscascadeintoitsshadowmap.
Bygeneratinganorthographicprojectionforeachcascadeseparatelywecannowrendereachcascade
intodifferentshadowmap.Duringthelightphasewewillcalculatetheshadowfactorbyselecting
ashadowmapbasedonthedistancefromtheviewer.
Sourcewalkthru
(ogldev_shadow_map_fbo.cpp:104)
boolCascadedShadowMapFBO::Init(unsignedintWindowWidth,unsignedintWindowHeight)
{
//CreatetheFBO
glGenFramebuffers(1,&m_fbo);
//Createthedepthbuffer
glGenTextures(ARRAY_SIZE_IN_ELEMENTS(m_shadowMap),m_shadowMap);
for(uinti=0;i
延伸文章資訊
- 1重疊的陰影圖- Win32 apps
shadow map coverage ... cascade overlap increases as light direction becomes parallel ... Interva...
- 2Tutorial 16 : Shadow mapping
Cascaded shadow maps
- 3LearnOpenGL - CSM
Cascaded shadow mapping is a direct answer to the third point, however while implementing it we w...
- 4Cascaded Shadow maps not quite right - Stack Overflow
I've mostly implemented cascading shadow maps (CSM), ... And samples the shadow map array with th...
- 5Tutorial 49 - Cascaded Shadow Mapping - OGLdev
If we can find a way to use a dedicated shadow map for closer objects and a different shadow map ...