CMSC427 Advanced shading – getting global illumination by local...

Preview:

Citation preview

CMSC427Advancedshading–gettingglobalilluminationbylocalmethods

Credit:slidesProf.Zwicker

• Shadows• Environmentmaps• Reflectionmapping• Irradianceenvironmentmaps• Ambientocclusion• Reflectionandrefraction• Toonshading

Topics

Why are shadows important?

• Cuesonscenelighting

3

Why are shadows important?

• Contactpoints• Depthcues

4

Why are shadows important?

• Realism

Without self-shadowing Without self-shadowing

5

Terminology

• Umbra:fullyshadowedregion• Penumbra:partiallyshadowedregion

(area) light source

receiver shadow

occluder

umbra

penumbra

6

Hard and soft shadows

• Pointanddirectionallightsleadtohardshadows,nopenumbra

• Arealightsourcesleadtosoftshadows,withpenumbra

point directional area

umbra penumbra7

Hard and soft shadows

Hard shadow,point light source

Soft shadow,area light source

8

Shadows for interactive rendering• Focusonhardshadows

• Softshadowsoftentoohardtocomputeininteractivegraphics

• Twomaintechniques• Shadowmapping• Shadowvolumes

• Manyvariations,subtleties• Stillactiveresearcharea

9

Shadow mappinghttp://en.wikipedia.org/wiki/Shadow_mappinghttp://www.opengl-tutorial.org/intermediate-tutorials/tutorial-16-shadow-mapping/

Mainidea• Scenepointislitbylightsourceifitisvisible fromlightsource

• Determinevisibilityfromlightsourcebyplacingcameraatlightsourceposition andrenderingscene

Scene points are lit if visible from light source

Determine visibility from light source by placing camera

at light source position10

Two pass algorithm

Firstpass• Renderscenebyplacingcameraatlightsourceposition

• Storedepthimage(shadowmap)

Depth image seen from light source

depth valuein shadow map

Secondpass• Renderscenefromcamera(eye)position

• Ateachpixel,comparedistancetolightsource(yellow)withvalueinshadowmap(red)

• Ifyellowdistanceislargerthanred,weareinshadow

• Ifdistanceissmallerorequal,pixelislit

Two pass algorithm

Final image with shadows

vb is in shadow pixel seen

from eye vb

depth valuein shadow map

12

Issues

• Limitedfieldofviewofshadowmap• Z-fighting• Samplingproblems

13

Limited field of view

• Whatifascenepointisoutsidethefieldofviewoftheshadowmap?

field of view of shadow map

Limited field of view

• Whatifascenepointisoutsidethefieldofviewoftheshadowmap?

• Usesixshadowmaps,arrangedinacube

• Requiresrenderingpassforeachshadowmap!

shadowmaps

• Intheory,depthvaluesforpointsvisiblefromlightsourceareequalinbothrenderingpasses

• Becauseoflimitedresolution,depthofpixelvisiblefromcameracouldbelargerthanshadowmapvalue

• Needtoadd biasinfirstpasstomakesurepixelsarelit

z-fighting

Camera image

Shadow mapImagepixels

Shadow mappixels Pixel is

consideredin shadow!

Depth of pixel visiblefrom camera

Depth of shadow map

Solution

• Addbias whenrenderingshadowmap• Movegeometryawayfromlightbysmallamount

• Findingcorrectamountofbiasistricky

Correct bias Not enough bias Too much bias17

Bias

Correct

Not enough Too much

18

Sampling problems

• Shadowmappixelmayprojecttomanyimagepixels

• Uglystair-steppingartifacts

19

Solutions

• Increaseresolutionofshadowmap• Notalwayssufficient

• Splitshadowmapintoseveralslices• Tweakprojectionforshadowmaprendering

• Lightspaceperspectiveshadowmaps(LiSPSM)http://www.cg.tuwien.ac.at/research/vr/lispsm/

• WithGLSLsourcecode!

• CombinationofsplittingandLiSPSM• Basisformostseriousimplementations• Listofadvancedtechniquessee

http://en.wikipedia.org/wiki/Shadow_mapping

20

LiSPSM

Basic shadow map Light space perspectiveshadow map

21

Percentage closer filtering

• Goal:avoidstair-steppingartifacts• Similartotexturefiltering,butwithatwist

http://http.developer.nvidia.com/GPUGems/gpugems_ch11.html

Simpleshadow mapping Percentage closer filtering

22

Percentage closer filtering

• Insteadoflookinguponeshadowmappixel,lookupseveral

• Performdepthtestforeachshadowmappixel• Computepercentageoflitshadowmappixels

23

Percentage closer filtering

• Supportedinhardwareforsmallfilters(2x2shadowmappixels)

• Canuselargerfilters(lookupmoreshadowmappixels)atcostofperformancepenalty

• Fakesoftshadows• Largerfilter,softershadowboundary

24

Shadow volumes

Shadowingobject

Partiallyshadowed object

Lightsource

Eye position(note that shadows are independent of the eye position)

Surface insideshadow volume(shadowed)

Surface outsideshadow volume(illuminated)

Shadowvolume(infinite extent)

25

In shadow or not

• Testifsurfacevisibleingivenpixelisinsideoroutsideshadowvolume1. Allocateacounterperpixel2. Castarayintothescene,startingfromeye,goingthroughgivenpixel

3. Incrementthecounterwhentherayenterstheshadowvolume

4.Decrementthecounterwhentherayleavestheshadowvolume

5.Whenwehittheobject,checkthecounter.• Ifcounter>0,inshadow• Otherwise,notinshadow

26

In shadow or not

OccluderLightsource

Eyeposition

+1 +2 +2+3

In shadow

+1

27

Implementation in rendering pipeline• Raytracingnotpossibletoimplementdirectly• Useafewtricks...

28

Shadow volume construction

• Needtogenerateshadowpolygonstoboundshadowvolume

• Extrudesilhouetteedgesfromlightsource

Extruded shadow volumes29

Shadow volume construction

• NeedstobedoneontheCPU• Silhouetteedgedetection

• Anedgeisasilhouetteifoneadjacenttriangleisfrontfacing,theotherbackfacingwithrespecttothelight

• Extrudepolygonsfromsilhouetteedges

30

Shadow test without ray tracing

Usingthestencilbuffer• Aframebuffer channel(likeRGBcolors,depth)thatcontainsaper-pixelcounter(integervalue)

• AvailableinOpenGL• Stenciltest

• Similartodepthtest(z-buffering)• Controlwhetherafragmentisdiscardedornot• Stencilfunction:isevaluatedtodecidewhethertodiscardafragment

• Stenciloperation:isperformedtoupdatethestencilbufferdependingontheresultofthetest

31

Shadow volume algorithms

Z-passapproach• Countleaving/enteringshadowvolumeeventsasdescribed

• Usestencilbuffertocountnumberofvisible(i.e.notoccludedfromcamera)front-facingandbackfacingshadowvolumepolygonsforeachpixel

• Ifequal,pixelisnotinshadowZ-failapproach• Countnumberofinvisible(i.e.occludedfromcamera)front-facingandback-facingshadowvolumepolygons

• Ifequal,pixelisnotinshadow

32

Z-pass approach: details

• Renderscenewithonlyambientlight• Updatedepthbuffer

• Turnoffdepthandcolorwrite,turnonstencil,keepthedepthteston• Initstencilbufferto0• Drawshadowvolumetwiceusingfaceculling

• 1stpass:renderfront facesandincrement stencilbufferwhendepthtestpasses

• 2ndpass:renderback facesanddecrement whendepthtestpasses• Ateachpixel

• Stencil!=0,inshadow• Stencil=0,lit

• Renderthesceneagainwithdiffuseandspecularlighting• Writetoframebufferonlypixelswithstencil=0

33

Issues

• Z-passfailsif• Eyeisinshadow• Shadowpolygonclippedbynearclipplane

34

Shadow volumes

• Pros• Doesnotrequirehardwaresupportforshadowmapping• Pixelaccurateshadows,nosamplingissues

• Cons• MoreCPUintensive(constructionofshadowvolumepolygons)

• Fill-rateintensive(needtodrawmanyshadowvolumepolygons)

• Expensiveforcomplexgeometry• Trickytohandleallcasescorrectly• Hardtoextendtosoftshadows

35

Shadow maps

• Pros:• LittleCPUoverhead• Noneedtoconstructextrageometrytorepresentshadows

• Hardwaresupport• Canfakesoftshadowseasily

• Cons:• Samplingissues• Depthbiasisnotcompletelyfoolproof

• Shadowmappinghasbecomemorepopularwithbetterhardwaresupport

36

Resources

• Overview,lotsoflinkshttp://www.realtimerendering.com/

• Basicshadowmapshttp://en.wikipedia.org/wiki/Shadow_mapping

• Avoidingsamplingproblemsinshadowmapshttp://www.comp.nus.edu.sg/~tants/tsm/tsm.pdfhttp://www.cg.tuwien.ac.at/research/vr/lispsm/

• Fakingsoftshadowswithshadowmapshttp://people.csail.mit.edu/ericchan/papers/smoothie/

• Alternative:shadowvolumeshttp://en.wikipedia.org/wiki/Shadow_volume

37

More realistic illumination

• Inrealworld,ateachpointinscenelightarrivesfromalldirections

• Notjustfrompointlightsources

• Environmentmaps• Store“omni-directional”illuminationasimages• Eachpixelcorrespondstolightfromacertaindirection

38

Capturing environment maps

• “360degrees”panoramicimage• Insteadof360degreespanoramicimage,takepictureofmirrorball(lightprobe)

Light probes [Paul Debevec, http://www.debevec.org/Probes/] 39

Environment maps as light sourcesSimplifyingassumption• Assumelightcapturedbyenvironmentmapisemittedinfinitelyfaraway

• Environmentmapconsistsofdirectionallightsources

• Valueofenvironmentmapisdefinedforeachdirection,independentofpositioninscene

• Usesingleenvironmentmapaslightsourceatalllocations inthescene

• Approximation!

40

Environment maps as light sources• Howdoyoucomputeshadingofadiffusesurfaceusinganenvironmentmap?

• Whatismoreexpensivetocompute,shadingadiffuseoraspecularsurface?

41

Environment maps applications

• Useenvironmentmapas“lightsource”

Global illumination[Sloan et al.]

Reflection mapping

42

Sphere & cube maps

• Storeincidentlightonsphereoronsixfacesofacube

Spherical map Cube map

Elevation, azimuthalangle

Elevationq const.

Northpoleq=90

Southpoleq=-90

43

Cube maps in OpenGL

Applicationsetup• Load,bindacubeenvironmentmap

glBindTexture(GL_TEXTURE_CUBE_MAP, …);// the six cube facesglTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X,…);glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X,…);glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y,…);…glEnable(GL_TEXTURE_CUBE_MAP);

• Moredetails• “OpenGLShadingLanguage”,RandiRost• “OpenGLSuperbible”,Sellersetal.• Onlinetutorials

44

Cube maps in OpenGL

Look-up• Givendirection(x,y,z)• Largestcoordinatecomponentdeterminescubemapface

• Dividingbymagnitudeoflargestcomponentyieldscoordinateswithinface

• Look-upfunctionbuiltintoGLSL• Use(x,y,z) directionastexturecoordinatestosamplerCube

45

Environment map data

• Alsocalled„lightprobes“http://www.debevec.org/Probes/

• Toolforhighdynamicrangedata(HDR)http://projects.ict.usc.edu/graphics/HDRShop/

• Pre-renderedlightprobesforgameshttp://docs.unity3d.com/Manual/LightProbes.html

Lightprobes(http://www.debevec.org/Probes/) 46

Reflection mapping

• Simulatemirrorreflection• Computereflectionvectorateachpixelusingviewdirectionandsurfacenormal

• Usereflectionvectortolookupcubemap• Renderingcubemapitselfisoptional

Reflection mapping 47

Reflection mapping in GLSL

Vertexshader• Computeviewingdirectionforeachvertex• Reflectiondirection

• UseGLSLbuilt-inreflect function

• PassreflectiondirectiontofragmentshaderFragmentshader• Look-upcubemapusinginterpolatedreflectiondirectionin float3 refl;uniform samplerCube envMap;texture(envMap, refl);

48

Reflection mapping examples

• Approximation,reflectionsarenotaccurate

[NVidia]

49

Shading using environment map

• Assumption:distantlighting• Incidentlightisafunctionofdirection,butnotposition

• Realisticshadingrequires• Takeintoaccountlightfromalldirections• Includeocclusion

Illumination from environment

Same environment map for both points“Illumination is a function of direction,but not position”

50

Mathematical model

• AssumeLambertian(diffuse)material,BRDFkd• Ignoreocclusionfornow

• Illuminationfrompointlightsources

• Illuminationfromenvironmentmapusinghemisphericalintegral

• Directionsw• HemisphereofdirectionsW• Environmentmap,radiancefromeachdirectionc(w)

51

Irradiance environment maps

• Precomputeirradiance asafunctionofnormal

• Storeasirradianceenvironmentmap

• Shadingcomputationatrendertime• Dependsonlyonnormal,notposition

Environment map Irradiance map52

Irradiance environment maps

Directional light Environment illumination

Images from http://www.cs.berkeley.edu/~ravir/papers/envmap/ 53

Implementation

• Precomputeirradiancemapfromenvironment• HDRShoptool,“diffuseconvolution”

http://projects.ict.usc.edu/graphics/HDRShop/

• Atrendertime,lookupirradiancemapusingsurfacenormal

• Whenobjectrotates,rotatenormalaccordingly

• Canalsoapproximateglossyreflection• Blurenvironmentmaplessheavily• Lookupblurredenvironmentmapusingreflectionvector

54

Today

Moreshading• Environmentmaps• Reflectionmapping• Irradianceenvironmentmaps• Ambientocclusion• Reflectionandrefraction• Toonshading

55

Including occlusion

• Ateachpoint,environmentispartiallyoccludedbygeometry

• Addlightonlyfromun-occludeddirections

Visualization of un-occluded directions56

Including occlusion

VisibilityfunctionVx(w)• Binaryfunctionofdirectionw• Indicatesifenvironmentisoccluded• Dependsonpositionx

Environment map Visibility functions

Vx0=0

Vx1=0Vx0=1

Vx1=1

x0

x1

57

Mathematical model

• Diffuseilluminationwithvisibility

• Ambientocclusion• “Fraction”ofenvironmentthatisnotoccludedfromapointx

• Scalarvalue

• Approximation:diffuseshadinggivenbyirradianceweightedbyambientocclusion

Vx=0

Vx=1

58

Ambient occlusion

Ambientocclusion Diffuseshading Ambientocclusioncombined(usingmultiplication)withdiffuseshading

http://en.wikipedia.org/wiki/Ambient_occlusion59

Implementation

• Precomputation (off-line,beforerendering)• Computeambientocclusiononaper-vertexbasis• Usingraytracing• Freetoolthatsavesmesheswithper-vertexambientocclusion

http://www.xnormal.net/

• Caution• Basicpre-computedambientocclusiondoesnotworkforanimatedobjects

60

Shading integral

• Ambient occlusion with irradiance environmentmaps is crude approximation to general shadingintegral

• BRDFfor (non-diffuse)material

Reflected radiance c(wo)

Outgoing direction wo

Incident directions wi

HemisphereW61

Shading integral

• Accurate evaluation is expensiveto compute• Requires numerical integration

• Many tricksfor more accurate and generalapproximation than ambient occlusion and irradianceenvironment maps exist

• Spherical harmonics shadinghttp://www.research.scea.com/gdc2003/spherical-harmonic-lighting.pdf

62

Note

• Visuallyinterestingresultsusingcombination(sum)ofdiffuseshadingwithambientocclusionandreflectionmapping

http://www.research.scea.com/gdc2003/spherical-harmonic-lighting.pdf

Diffuseshading withambient occlusion Reflection mapping Combination (sum)

63

Today

Moreshading• Environmentmaps• Reflectionmapping• Irradianceenvironmentmaps• Ambientocclusion• Reflectionandrefraction• Toonshading

64

Toon shading

• Simplecartoonstyleshader• Emphasizesilhouettes• Discretestepsfordiffuseshading,highlights• SometimescalledCELshading

http://en.wikipedia.org/wiki/Cel-shaded_animation

Off-line toon shader GLSL toon shader 65

Toon shading

• Silhouetteedgedetection• Computedotproductofviewingdirectionv andnormaln

• Use1Dtexturetodefineedgerampuniform sample1D edgeramp; e=texture1D(edgeramp,edge);

0

1edgeramp

edge66

Toon shading

• Computediffuseandspecularshading

• Use1Dtexturesdiffuseramp, specularramp tomapdiffuseandspecularshadingtocolors

• Finalcoloruniform sampler1D diffuseramp;uniform sampler1D specularramp;c = e * (texture(diffuse,diffuseramp)+

texture(specular,specularramp));

67

Recommended