# color color

Ray Tracing IIWen-Chieh (Steve) LinNational Chiao-Tung UniversityShirley, Fundamentals of Computer Graphics, Chap 15Doug James CG slides

A Ray Tracing AlgorithmFor each pixel doCompute viewing rayif (ray hits an object with t in [0, ∞) thenCompute nEvaluate lighting equation and set pixel to that colorelseSet pixel color to background colorDCP4516 Introduction to Computer Graphics 2

Shadow Rays•Imagine ourselves at a point p•Looking along light direction–If seeing an object, p is in shadow–If seeing light, p is not in shadowDCP4516 Introduction to Computer Graphics 3

Specular Reflection•Viewer looking in direction d will see whatever theviewer see in direction rrd2(dn)n•In reality, some energy is lost when the lightreflects from the surface•This lost is different for different colors—colorshiftcolor c ccsraycolorDCP4516 Introduction to Computer Graphics 4

Recursive Ray Tracing•Calculate specular component–Reflect ray from eye on specular surface–Transmit ray from eye through transparent surface•Determine color of incoming ray by recursion•Trace to fixed depth•Cut off if contributionbelow thresholdDCP4516 Introduction to Computer Graphics 5

Refraction•Snell’s law–n = refractionindex of outsidemedium (air)nsinnsint–n t = refractionindex of insidemediumDCP4516 Introduction to Computer Graphics 6

Useful form for later derivationnsinsinn tcos2 1sinn122n 1(n(1 cosn2t2)tsin)2DCP4516 Introduction to Computer Graphics 7

Generalize to 3D•Set up 2D orthonormal basis in the plane ofn and d -n and b•Represent t and d in terms of n and btdbsin b cos( n)sin b cos( n)dncossinDCP4516 Introduction to Computer Graphics 8

Conversion into 3D (cont.)tdsinn(dncoscos( n)sinncos)ncosn tn tsinnsintn(dn( dnn))nn1n2t t2(1 (dn)2)DCP4516 Introduction to Computer Graphics 9

Total Internal Reflection•All energy is reflectedtn(dn( dnn))nn1n2t t2(1 (dn)2)If this less than zero, no refraction•Causes rich appearance glass objectsDCP4516 Introduction to Computer Graphics 10

Fresnel Equations•Refractivity varies with the incident angle•Schlick approximation(1 )(1 cos )50 0R( )R R Reflectance at normal incidenceR0nntt112DCP4516 Introduction to Computer Graphics 11

Beer’s Law•For homogeneous impurities, a light-carryingray’s intensity will be attenuated according toBeer’s lawdIdx•Solving with boundary conditionsI( 0) II0( 1) aI(0)a: degree of attenuationDCP4516 Introduction to Computer Graphics 12 CII(s)I: intensitydx: distanceI(0) eln(a)SI(s): intensity of beam at distance s

Example: total internal reflection,Beer’s law, Fresnel equationDCP4516 Introduction to Computer Graphics 13

Instancing•Distort all points on an object by atransformation matrix before the object isdisplayedDCP4516 Introduction to Computer Graphics 14

Apply instancing to ray tracingDCP4516 Introduction to Computer Graphics 15

Advantages of using instancing•Simpler intersection computation–Sphere vs. ellipsoid•Reducing storage–Many transformed objects can share the sameuntransformed objectDCP4516 Introduction to Computer Graphics 16

Interpolated Shading for Ray Tracing•Assume we know normals at vertices•How do we compute normal of interior point?•Need linear interpolation between 3 points•Barycentric coordinates•Yields same answer as scan conversionDCP4516 Introduction to Computer Graphics 17

Barycentric Coordinates in 1D•Linear interpolation–p(t) = (1 –t)p 1+ t p 2, 0 · t · 1–p(t) = p 1+ p 2where + = 1–p is between p 1and p 2iff 0 · , · 1•Geometric intuition–Weigh each vertex by ratio of distances from endsp 1 p p 2 •, are called barycentric coordinatesDCP4516 Introduction to Computer Graphics 18

Barycentric Coordinates in 2D•Given 3 points instead of 2p 1p 2p 3p•Define 3 barycentric coordinates, , , •p = p 1 + p 2 + p 3•p inside triangle iff 0 < , , < 1, + + = 1•How do we calculate , , given p?DCP4516 Introduction to Computer Graphics 19

Barycentric Coordinates for Triangle•Coordinates are ratios of triangle areasp 1p 2Areapp1p2Areapp 0p1p2Areappp 0 2Areapp p p 3AreaAreapp000pp111pp221•p = p 1 + p 2 + p 3DCP4516 Introduction to Computer Graphics 20

Computing Triangle Area•In 3 dimensions–Use cross product–Parallelogram formula–Area(ABC) = (1/2)|(B –A) x (C –A)|CAB–Optimization: project, use 2D formula•In 2 dimensions–Area(x-y-proj(ABC)) =(1/2)((b x–a x)(c y–a y) –(c x–a x) (b y–a y))DCP4516 Introduction to Computer Graphics 21

More magazines by this user
Similar magazines