EECS 487: Interactive Computer Graphicssugih/courses/eecs487/...Ray Tracing Basic tasks: 1.Specify...

Post on 16-Oct-2020

3 views 0 download

Transcript of EECS 487: Interactive Computer Graphicssugih/courses/eecs487/...Ray Tracing Basic tasks: 1.Specify...

EECS487:InteractiveComputerGraphicsLecture28:•  RayTracingImplementation

RayTracingBasictasks:1.  Specifytheviewinggeometry:

eyecoordinateframe,imageplane,andviewfrustum

2.  Foreachpixelintheimageplane:a. buildarayineyecoordinatesb. transformtoworldcoordinates(why?)c. figureoutwhattherayhitsd. computeshading,e.g.,byusingPhongilluminationmodel

Thecoreofanyraytracingsystems,aswellasitsbottleneck(75%oftimespenthere)

Allrayintersectionproblemsboildowntothemathematicalprocessoffindingroots

Replacesviewingandprojectiontransforms

ImagePlaneandViewFrustum

Shirley,Funkhouser

Letthescreen/imageplanebeatthenearplane(w = n)

Givensinimagespace(i,j)andr, l, t, b, nineyespace(u,v,w),computesineyespace:seye

ScreenPixelsinEyeCoordinates

u

vs = pixel(i, j)

pixel(0, 0)

pixel(HRES, VRES)

Letll = (l, b, n)

(r, t, n)

su = l +(r − l)HRES

i + 0.5( )

sv = b +(t − b)VRES

j + 0.5( )Assummingsymmetricfrustum:

r = −l, t = −b

seye =

susvsw1

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

=

l + 2r i + 0.5HRES

b + 2t j + 0.5VRESn1

⎢⎢⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥⎥⎥

Zwicker

i

j

sworld =Meye→worldseye

=

sworld = e + l + 2r i + 0.5HRES

⎛⎝⎜

⎞⎠⎟ u + b + 2t j + 0.5

VRES⎛⎝⎜

⎞⎠⎟ v + nw

Since ll = e + lu + bv + nw

sworld = ll + 2r i + 0.5HRES

⎛⎝⎜

⎞⎠⎟ u + 2t j + 0.5

VRES⎛⎝⎜

⎞⎠⎟ v

ScreenPixelsinWorldCoordinates

1 0 0 ex0 1 0 ey0 0 1 ez0 0 0 1

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

ux vx wx 0uy vy wy 0

uz vz wz 00 0 0 1

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

seye =

ux vx wx exuy vy wy eyuz vz wz ez0 0 0 1

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

l + 2r i + 0.5HRES

b + 2t j + 0.5VRESn1

⎢⎢⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥⎥⎥

x

y

z

u

v

w

screen

s

ll

Note:noperspectiveprojectionmatrix,raygenerationtookcareofthat!

Merrell

v

u −w

BuildingaRayinEyeCoordinates

Anchorpoint:e = (xe, ye, ze, 1)

Ray:r = (e, d)

p = r(t) = e + t d,t > 0

Direction: d = s − e = (xd, yd, zd, 0)||d|| = 1preferred,butisnotalwaysso

s:screenintersection

Hart

x

y

z worldcoordinates

Assumingsymmetricfrustum:•  focallength:d = t /tan(ϕ),ϕ = fovy/2•  aspectratio:

a = HRES/VRES = (r − l)/(t − b)�= 2r/2t � r = at �

(rand tareunknown)

Then:•  sworld = ll + (2 at (i + 0.5)/HRES) u �

+ (2 t (j + 0.5)/VRES) v•  ll = e − dw − ru − tv = e − dw − atu − tv

•  dividingbyt:

ExpressedinAspectRatioandFoVy

llDIVt = eDIVt - 1/tan(phi)w – au – v for (j = 0; j < VRES; j++) { for (i = 0; i < HRES; i++) { sDIVt = llDIVt + 2au (double)(i+0.5)/HRES

+ 2v (double)(j+0.5)/VRES; color = raytrace(ray(e, sDIVt – eDIVt)); plot(i,j,color);

} }

Hart

e

-ru-tv

ll

s

HRES

VRES

(r, t, n)−au−v

−dw

Ray-ObjectIntersection

raytrace(r = (e, d))returnstheintensityoflight(e.g.,anRGBtriple)arrivingattherayanchorateintheoppositedirection(−d)

ed

ray r

raytrace(r)

color raytrace(ray r, int depth) { color c = background; if ((hit = intersect(r)) != NULL) { hit->depth = depth – 1;

// shading details simplified, // see earlier version

if (hit->depth > 0) { c = raytrace(r, hit->depth);

} } return c; }

Hart08

Ray-ObjectIntersectionRay:r(t) = e + t dForeachraywemustfindthenearestintersectionpointwithallobjectsinthescene

Wecandefinethesceneusing:• surfacemodels:plane,triangle,polygon• solidmodels:sphere,cylinder

Recall:implicitsurfaces:forpointp = (x, y, z)insurface,f (p) = 0

Thenray-surfaceintersectioniswhenf (r(t)) = 0Solvefort, r(t)istheintersectionpoint

Zwicker06

Planeimplicitequation:f (p) = (p − q)•n = 0,wherenistheplanenormalandqapointintheplaneForp = r(t),theray-planeintersectionisat:

((e + td) − q)•n = 0,t = (q − e)•n/d•nEquivalently,forp = (xp, yp, zp),theimplicitplaneequationis:

Axp + Byp + Czp + D = 0• theunitnormaloftheplane:n = [A B C]T, A2 + B2 + C2 = 1• Disthedistancefromthecoordinatesystemorigin(signofDdetermineswhichsideoftheplanetheoriginisat)

Ray-planeintersectionisattsuchthat:

A(xe + txd) + B(ye + t yd) + C(ze + t zd) + D = 0

Ray-PlaneIntersection

Ray-TriangleIntersectionFindray-planeintersectionforplanedefinedbythetriangleIfintersectionexists:•  computebarycentriccoordinatesoftheintersectionpoint

•  ifbarycentriccoordinatesareallpositiveandsumto1,pointisaconvexcombinationoftheverticesofthetriangleandisinsidetriangle

•  otherwiseoutside•  (Möller&Trumborealgorithmdoesitin

1div,27muls,17adds)

Ray-BoxIntersectionsCouldintersectwith6facesindividually:O(n2)Better:boxistheintersectionof3slabsO(n)n:numberofcomparisons2Dexample(similarlyfor3D):xmin= xp+ txmin xdtxmin

= (xmin − xp)/xd

ymin= yp + tymin yd

tymin = (ymin − yp)/yd

tmin = max(txmin, tymin

)tmax = min(txmax

, tymax)

iftmin > tmax,boxismissediftmax < 0,boxisbehindeyeelser(tmin)isintersectionpoint

(xp, yp)

(xd, yd)

ymin

ymax

Ray-SphereIntersectionsAsphereisdefinedbythespherecenterc=(xc, yc, zc),andradiusr

Sphereimplicitfunction:||p − c|| = r(xp − xc)2 + (yp − yc)2 + (zp − zc)2 − r2 = 0

Ray-sphereintersection:((xe + txd) − xc)2 + ((ye + tyd) − yc)2 + ((ze + tzd) − zc)2 − r2 = 0

Multiplyingoutandsimplifying:0 = (xd

2 + yd2 + zd

2) t2 + 2(xd(xe − xc) + yd(ye − yc) + zd(ze − zc)) t �+ (xe − xc)2 + (ye − yc)2 + (ze − zc)2 − r2

0 = At2 + Bt + C

Ifdisnormalized,A = xd2 + yd

2 + zd2 = 1

Thesolutionsfortcanbefoundusingthequadraticequation: t = −B ± B2 − 4C

2

cr

ed

p

Ray-SphereIntersectionsB2 − 4Cisthediscriminant

Threepossibilities:1.  B2 − 4C < 0

•  norealroots,spherewasmissed,nointersection�alwayscheckthediscriminantfirst

2.  B2 − 4C = 0•  onerealroot,ray“grazes”thesphere,t0 = t1 = −B/2

3.  B2 − 4C > 0•  tworealroots

a.  t0 < 0, t1 > 0�negativevaluesoftindicatethattheraystartedinthesphere�onlypositiverootsarevalid

b.  0 < t0 < t1 thesmallerrootisclosertotheray’sstartingpoint,e �savetimebycomputingthesmallrootfirst

case1 case2 case3a case3b

t1

t0

t1t0

Merrell08

t = −B ± B2 − 4C2

Computationtimeperray-spheretest:•  17additions/subtractions•  17multiplies•  1squareroot

Computingnormal:thenormalnatanintersectionpointponasphereisthesameasthecoordinatesofpinthesphere’sframeofreference:

Ray-SphereIntersections

n p

c

r

n = p − cp − c

=p − cr

=xp − xc

ryp − ycr

zp − zcr

⎝⎜

⎠⎟

T

Merrell08

Asphereisdefinedbythespherecenterc=(xc, yc, zc),andradius:r

Sphereimplicitfunction:||p − c|| = r(p − c)•(p − c) − r2 = 0

Ray-sphereintersection:((e + td) − c)•((e + td) − c) − r2 = 0

Multiplyingoutandsimplifying:0 = (d•d) t2 + 2((e − c)•d) t + (e − c)•(e − c) − r 2

0 = t 2 + Bt + C

ifdisnormalized,||d|| = 1

Acceptance/rejectiontests:•  (e − c)•d > 0?•  (e − c)•(e − c) − r2 < 0?

Ray-SphereIntersections

cr

c

ed

ed

p

Let l = c − e, l2 = l•lif (l2 < r 2) eisinsideofsphere

tca = l • d //dnormalized,projectionoflondif (tca < 0andeisoutsideofsphere)�

raypointingawayfromsphere,nointersection

d2 = l2 − tca2

if (d2 > r 2) raymissessphere

thc = √(r 2 − d2)if(e is outside sphere)�

intersectionisatt0 = tca − thc else

intersectionisat t1 = tca + thc//tocomputepwithunnormalizedd,//normalizedfirst:uset/||d||

Worstcasecomputationreducedby4multsand1add

Ray-SphereIntersections−Geometric

d

ce

tcathc

lp0

p1d

Funkhouser09

r

EllipsoidIntersectionWehaveanoptimizedray-spheretest•  butwewanttoraytraceanellipsoid…

LetMbea4×4transformationmatrixthatdistortsasphere(f())intoanellipsoidForponellipsoid,f (M−1p) = 0f (M−1r(t))= f (M−1(e + t d))

= f (M−1e + t M−1d)

Intersectionpointmustbeinworldcoordinates•  tisthesameinbothcases

p = e + t dDon’tforgettotransformthenormal:

nellipsoid = (M−1)Tnsphere

f (M−1p) = 0

e

d

M−1e

M−1d

p

M−1p

M−1M

Hart

e

d

e d

e d de

IntersectionIngeneral,foranobjectthatistobetransformedbymatrixM,rayintersectionmaybeeasierdoneonoriginalobject,beforethetransformation

• applyM−1torayandintersectobjectsintheirlocal(objectcoordinates

Shirley02

Instancing

Objectstoredinuntransformedstatealongwiththetransformationmatrix

Thetransformationoftheobjectisdelayeduntilinstantiation/renderingtime

Bonus:re-useobjectswithoutreplicatingtheminmemory!

Shirley02

CaveatforInstantiationwithScalingIfMincludesscaling,don’tre-normalized:you’llgettherighttwheninversetransformingintersection(M−1td)•  ifyoure-normalized,tOS ≠ tWSandmustberescaledafterinversetransform

•  ifyoudon’tre-normalized,tOS = tWS �intersectionfound!•  butdon’trelyontOSbeingtruedistanceduringintersectionroutines

ObjectSpaceWorldSpace

tWS tOS

ObjectSpaceWorldSpace

tWS tOS

Durand08 Akenine-Möller03

RulesofThumbforIntersectionTesting

Performacceptanceandrejectiontest•  trythemearlyontomakeafastexit

Postponeexpensivecalculationsifpossible

Usedimensionreduction•  e.g.,3 1Dtestsinsteadofonecomplex3Dtest,or2Dinsteadof3D

Sharecomputationsbetweenobjectsifpossible

Useinstancing,delaytransformation

AcceleratingIntersectionTestsFindthefirst-hitobject

Simplestlinearapproach:O(Npixels*Mobjects)Accelerationtechniques(sublinearinMobjects):usespatialdatastructuretoreducethenumberoftestsneeded• spatialsubdivision:spacepartitioning• objectsubdivision:hierarchicalboundingvolumes

Probablythesinglemostimportantefficiencyimprovement• othersincludeshadowcaching:startshadowintersectionsearchwiththelastobjectintersected

SpatialSubdivisionDivideupspaceandrecordwhatobjectsareineachcell•  storeobjectsina3Darray•  traceraythroughvoxelarray

Forexample:uniformgrid,quadtree/octree,BSPtree,kd-tree(mostpopular,k-dimensional,axis-alignedBSP)

quad Hanrahan09,Curless08

HierarchicalBoundingVolumesArrangesceneintoatree•  internalnodesconsistofprimitiveswithverysimpleintersectiontests(boxesorspheres)

•  eachinternalnode’svolumecontainallobjectsinsubtree•  leafnodescontaintheoriginalgeometry

r1

r2r3

Curless08,Hart08

PrecisionProblemsNumericalinaccuracymaycauseintersectiontobebelowsurface�causingsurfacetoincorrectly

shadowitself

Movealittlealongsurfacenormalbeforeshootingshadowray,ormovealittlealongshadowraytocomputeintersection

O’Brien08,Shirley02,James07

PrecisionProblemsAlsowhencomputingreflectionrayAndwhencomputingintersectionwithedgesintrianglemeshes�mustreportintersection

Durand08

TransmissionRayExitCaveats

TocomputeFresnelreflectancecoefficientwhenexitinganobject,remembertoinvertthenormalandtransmissionraySimilarlyforthecomputationoftherefractedrayexitingtheobjectIncomputingray-objectintersectionattheexitpoint,besuretotranslatebyεintherightdirection

RayTracingvs.PipelinedRasterization

RayTracing• ray-centric• needstostoresceneinmemory

• (mostly)randomaccesstoscene

PipelinedRasterization• trianglecentric• needstostoreimage(anddepth)inmemory

• (mostly)randomaccesstoframebuffer

Whichrequireslessmemory?Sceneorframebuffer?framebuffer

Whichimageiseasiesttoaccessrandomly?framebufferduetoregularsampling

Durand

InteractiveRayTracing

Advantagesofraytracing:•  canhandleverycomplexscenesrelativelyeasily

•  sublinearcomplexitywithacceleration(hierarchicalbbox),neednotprocessalltrianglesinscene

•  providecomplexmaterialsandshadingforfree•  easy(butexpensive)toaddglobalillumination,specularities,etc.

Butraytracingishistoricallyslowbecause•  hardtoaccessdatainmemory-coherentway,cannottakeadvantageofincrementalcomputation

•  requiresmanysamplesforcomplexlightingandmaterials

Ramamoorthi

InteractiveRaytracingLeveragepowerofmodernCPUs:developcache-aware,parallelimplementations

ModernGPUshavegeneralstreamingarchitecture:canmapvariouselementsofraytracingkernelslikeeyerays,intersectiontests,etc.intovertexorfragmentprograms

Searchyoutubefor“nvidiaOptiX”and“realtimeraytracing”

Ramamoorthi08

hothardware.com/News/NVIDIA-Shows-Interactive-Ray-Tracing-on-GPUs/