Ray Tracing MIT EECS 6.837 Most slides are taken from Frédo Durand and Barb Cutler Some slides courtesy of Leonard McMillan 1 2 3 4 Ray Tracing Ray Tracing • Ray Tracing kills two birds with one stone: – Solves the Hidden Surface Removal problem – Evaluates an improved global illumination model • shadows • ideal specular reflections • ideal specular refractions – Enables direct rendering of a large variety of geometric primitives • Book: A. Glassner, An Introduction to Ray Tracing • Web: http://www.cs.cf.ac.uk/Ray.Tracing Recursive ray tracing: Turner Whitted, 1980 5 6 1
Backward Tracing Overview of today • Shadows • Reflection • Refraction • Recursive Ray Tracing 7 8 Ray Casting (a.k.a. Ray Shooting) Ray Casting with diffuse shading Color castRay(ray) For every pixel (x,y) Hit hit(); Construct a ray from the eye color[x,y]=castRay(ray) For every object ob ob->intersect(ray, hit, tmin); • Complexity? Color col=ambient*hit->getColor(); For every light L – O(n * m) col=col+hit->getColorL()*L->getColor* – n: number of objects, m: number of pixels L->getDir()->Dot3( hit->getNormal() ); Return col; 9 10 Encapsulating shading Questions? Color castRay(ray) • Image computed using Hit hit(); the RADIANCE For every object ob system by Greg Ward ob->intersect(ray, hit, tmin); Color col=ambient*hit-> getMaterial() ->getDiffuse(); For every light L col=col+hit-> getMaterial()->shade (ray, hit, L->getDir(), L->getColor()); Return col; 11 12 2
Shadows Shadows – problem? Color castRay(ray) Color castRay(ray) Hit hit(); Hit hit(); For every object ob For every object ob ob->intersect(ray, hit, tmin); ob->intersect(ray, hit, tmin); Color col=ambient*hit->getMaterial()->getDiffuse(); Color col=ambient*hit->getMaterial()->getDiffuse(); For every light L For every light L Ray ray2(hitPoint, L->getDir()); Hit hit2(L->getDist(),,) Ray ray2(hitPoint, L->getDir()); Hit hit2(L->getDist(),,) For every object ob For every object ob ob->intersect(ray2, hit2, 0); ob->intersect(ray2, hit2, 0); If (hit->getT> L->getDist()) If (hit->getT> L->getDist()) col=col+hit->getMaterial()->shade col=col+hit->getMaterial()->shade (ray, hit, L->getDir(), L->getColor()); (ray, hit, L->getDir(), L->getColor()); Return col; Return col; 14 15 Avoiding self shadowing Shadow optimization Color castRay(ray) • Shadow rays are special Hit hit(); For every object ob ob->intersect(ray, hit, tmin); Color col=ambient*hit->getMaterial()->getDiffuse(); • How can we accelerate our code? For every light L Ray ray2(hitPoint, L->getDir()); Hit hit2(L->getDist(),,) For every object ob ob->intersect(ray2, hit2, epsilon ); If (hit->getT> L->getDist()) col=col+hit->getMaterial()->shade (ray, hit, L->getDir(), L->getColor()); Return col; 16 17 Shadow optimization Shadow ray casting history • We only want to know whether there is an • Due to Appel [1968] intersection, not which one is closest • First shadow method in graphics • Special routine Object3D::intersectShadowRay() • Not really used until the 80s – Stops at first intersection 18 19 3
Questions? Overview of today • Image Henrik Wann Jensen • Shadows • Reflection • Refraction • Recursive Ray Tracing 20 21 Mirror Reflection Mirror Reflection • Compute mirror contribution • Cast ray – In direction symmetric w.r.t normal • Cast ray • Don’t forget to add epsilon – In direction symmetric wrt normal to the ray • Multiply by reflection coefficient (color) Without epsilon With epsilon 22 23 Reflection Reflection • Reflection angle = view angle • Reflection angle = view angle r r ( r r ) N r = − • 2 R V V N N N R R V V θ R θ R θ V θ V V N N V N N V 24 25 4
Questions? Overview of today • Image by Henrik Wann Jensen • Shadows • Reflection • Refraction • Recursive Ray Tracing 29 30 Transparency Qualitative refraction • From “Color and Light in Nature” by Lynch and Livingston • Compute transmitted contribution • Cast ray – In refracted direction • Multiply by transparency coefficient (color) 31 32 Refraction Total internal reflection θ η • From “Color and Light in Nature” by Lynch and Livingstone sin = = η i t Snell-Descartes Law θ η r sin t i ˆ θ − ˆ ˆ cos N N I i ˆ θ N cos ˆ θ I i i ˆ M θ t ˆ − T ˆ N Note that I is the negative of the incoming ray 34 35 5
Wavelength Rainbow n • Refraction is wavelength-dependent • Refraction depends on wavelength o i s s e • Newton’s experiment r g i • Rainbow is caused by D • Usually ignored in graphics refraction+internal reflection+refraction • Maximum for angle around 42 degrees From “Color and Light in Nature” by Lynch and Livingstone Pittoni, 1725, Allegory to Newton , 1725, Allegory to Newton Pittoni Pittoni, 1725, Allegory to Newton Pink Floyd, The Dark Side of the Moon 39 41 43 44 Overview of today Recap: Ray Tracing traceRay • Shadows Intersect all objects Ambient shading For every light • Reflection Shadow ray shading If mirror • Refraction Trace reflected ray If transparent Trace transmitted ray • Recursive Ray Tracing 45 46 6
The depth of reflection Avoiding infinite recursion Color traceRay (ray) Stopping criteria: For every object ob ob-> intersect (ray, hit, tmin); Color col=ambient*hit->getMaterial()->getDiffuse(); For every light L • Recursion depth If ( not castShadowRay ( hit->getPoint(), L->getDir()) col=col+hit->getMaterial()-> shade (ray, hit, L->getDir(), L->getColor()); If (hit->getMaterial()->isMirror()) Ray rayMirror (hit->getPoint(), – Stop after getMirrorDir(ray->getDirection(), hit->getNormal()); Col=col+hit->getMaterial->getMirrorColor() * traceRay (rayMirror); a number of bounces If (hit->getMaterial()->isTransparent() Ray rayTransmitted(hit->getPoint(), getRefracDir(ray, hit->getNormal(), curentRefractionIndex, hit->Material- >getRefractionIndex()); • Ray contribution Col=col+hit->getMaterial->getTransmittedColor() * traceRay (rayTransmitted); Return col; – Stop if transparency/transmitted attenuation becomes too small Usually do both 49 50 Recursion for reflection Ray-Surface Intersection = • Implicit surfaces: f ( x , y , z ) 0 – Use a parametric representation for the ray: = + R ( t ) O tD = + R ( t ) O tD x x x = + R ( t ) O tD y y y = + ( ) R t O tD z z z 0 recursion 1 recursion 2 recursions – Substitute into the implicit equation: + + + = f ( O tD , O tD , O tD ) 0 x x y y z z – Solve the resulting equation – Examples: plane, sphere 51 52 The Ray Tree Ray Tracing History • Ray Casting: Appel, 1968 T 3 Eye R 2 • CSG and quadrics: Goldstein & Nagel 1971 N 2 T 1 R 3 • Recursive ray tracing: Whitted, 1980 R 1 N 3 L 1 L 2 N 1 L 3 L 1 R 1 T 1 L 2 L 3 N i surface normal Eye R 2 R 3 T 3 R i reflected ray L i shadow ray T i transmitted (refracted) ray 53 56 7
Does Ray Tracing simulate physics? • Photons go from the light to the eye, not the other way • What we do is backward ray tracing 57 58 Forward ray tracing Forward ray tracing • Start from the light source • Start from the light source • But low probability to reach the eye • But low probability to reach the eye – What can we do about it? – What can we do about it? – Always send a ray to the eye • Still not efficient 59 60 The Rendering equation BRDF • Clean mathematical framework for light- • Reflectance properties, shading and BRDF transport simulation • At each point, outgoing light in one direction is the integral of incoming light in all directions multiplied by reflectance property 63 64 8
Ambient Occlusion Am bient Occlusion 65 66 Diffuse Diffuse Only and Am bient 67 68 69 70 9
71 72 73 10
Recommend
More recommend