1 Ray Tracing Lecture 10 © Jeff Parker, Nov 2009
2 Introduction OpenGL is based on a pipeline model in which primitives are rendered one at time No shadows (except by tricks or multiple renderings) No reflections (though we can use multiple renderings) No refraction – thus no caustics Global approaches Rendering equation Ray tracing Radiosity
3 Image Credits Ed Angel - University of New Mexico Allan Watt's text, 3D Computer Graphics Brian Salomon, UNC G. Scott Owen Paul Bourke Gilles Tran and others, as credited
Currently We have rasterization with Phong lighting Strictly local illumination model: Ambient Diffuse Specular Shadows can be accomplished: Shadow maps Shadow volumes Can generate reflections using environment mapping but: Approximate Expensive with many reflective objects Hard to capture complex interreflections
5 Illumination Direct Illumination A surface point receives light from all visible lights Global Illumination Even if point is in shadow, Rays may pass through translucent material Light rays will bounce off other material in scene Want to sum all light that hits an object Beyond Radiosity: The Light of Mies van der Rohe
6 Example Eric Veach and Leonidas Guibas Image by Eric Veach and Leonidas Guibas
Timeline [Appel 68] “Some techniques for shading machine renderings of solids” Basic Ray Casting – designed for pen plotters: provides Perspective Accurate shadows [Whitted 80] “An Improved Illumination Model for Shaded Display” Reflection Refraction Shadows Hidden Surface Removal
8 Perspective Ray Tracing provides natural perspective The schemes used to develop perspective were tracing rays (Albrecht Durer)
9 Albrecht Durer Two alternatives
10 Ray Tracing Track the path of light between light source and the eye
11 Ray Tracing Track the path of light between light source and the eye Where do we start – at light or at eye?
12 Steps in Ray Casting Shot a ray from the eye through each pixel of the screen Calculate which objects in the scene the ray hits If the ray misses all objects, display background color
13 Ray Casting Shot a ray from the eye through each pixel of the screen Calculate which objects in the scene the ray hits If it hits an object, we know basic color Now check to see if surface is illuminated or in shade
14 Shadow Ray Shoot a "shadow" ray from intersection point towards light If shadow ray hits object before it hits light, point is in shadow This is Appel's original Algorithm – called Ray Casting today Only uses local illumination – does not track light bouncing off intermediate surfaces
15 Ray Casting cast ray Intersect all objects: select minimal t Color = ambient term For every light Cast shadow ray color += local shading term
16 In practice Run the first few steps of the following applet rt_java/raytrace.html
Whitted Raytracing (1980)
Turner Witted “An Improved Illumination Model for Shaded Display” First global illumination model. An object’s color is influenced by lights and other objects in the scene First to simulate specular reflection and refractive transmission 1986 –Amiga Juggler home.comcast.net/~erniew/juggler.html#avi
19 Reflected Ray If the object is shiny, we send a third "reflection" ray If this hits an object, the color of the object will be reflected in the original screen point To see if the new point is in the shade, send shadow ray The new object may be shiny…
20 Full Algorithm We have sketched the first steps. Will need to recurse… Need to modify the path of refracted light May wish to add specular highlights …
What does it offer? Hidden Surface Removal Shading due to direct Illumination Global specular interaction effects Reflections Refraction of light Shadow Computation Shadows are hard-edged
22 Diffuse Surfaces Theoretically scattering at each point of intersection generates an infinite number of new rays that should be traced In practice, we only trace the transmitted and reflected rays but use the Phong model to compute shade at intersection Radiosity works best for perfectly diffuse (Lambertian) surfaces
23 POV Ray Persistence of Vision Raytracer POV Ray Images by Gilles Tran
24 POV Ray
25 POV Ray
Match point in image with it's construction
27 Ray Tree
28 In practice Run the more steps of the following applet – move objects rt_java/raytrace.html
29 Raytracing trace ray Intersect all objects color = ambient term For every light cast shadow ray color += local shading term If mirror color += colorrefl * trace reflected ray If transparent color += colortrans * trace transmitted ray
Raytracing Is Simple Paul Heckbert wrote a raytracer that fits on a business card Prints something like a 32x32 P3.ppm file to standard out. typedef struct{double x,y,z}vec;vec U,black,amb={.02,.02,.02};struct sphere{ vec cen,color;double rad,kd,ks,kt,kl,ir}*s,*best,sph[]={0.,6.,.5,1.,1.,1.,.9,.05,.2,.85,0.,1.7,-1.,8.,-.5,1.,.5,.2,1.,.7,.3,0.,.05,1.2,1.,8.,-.5,.1,.8,.8, 1.,.3,.7,0.,0.,1.2,3.,-6.,15.,1.,.8,1.,7.,0.,0.,0.,.6,1.5,-3.,- 3.,12.,.8,1., 1.,5.,0.,0.,0.,.5,1.5,};yx;double u,b,tmin,sqrt(),tan();double vdot(A,B)vec A,B;{return A.x*B.x+A.y*B.y+A.z*B.z;}vec vcomb(a,A,B)double a;vec A,B;{B.x+=a* A.x;B.y+=a*A.y;B.z+=a*A.z;return B;}vec vunit(A)vec A;{return vcomb(1./sqrt( vdot(A,A)),A,black);}struct sphere*intersect(P,D)vec P,D;{best=0;tmin=1e30;s= sph+5;while(s-- >sph)b=vdot(D,U=vcomb(-1.,P,s->cen)),u=b*b-vdot(U,U)+s->rad*s ->rad,u=u>0?sqrt(u):1e31,u=b- u>1e-7?b-u:b+u,tmin=u>=1e-7&&u ir;d= -vdot(D,N=vunit(vcomb(-1.,P=vcomb(tmin,D,P),s->cen )));if(d sph)if((e=l - >kl*vdot(N,U=vunit(vcomb(-1.,P,l->cen))))>0&&intersect(P,U)==l)color=vcomb(e,l- >color,color);U=s->color;color.x*=U.x;color.y*=U.y;color.z*=U.z;e=1-eta* eta*(1-d*d);return vcomb(s->kt,e>0?trace(level,P,vcomb(eta,D,vcomb(eta*d-sqrt (e),N,black))):black,vcomb(s- >ks,trace(level,P,vcomb(2*d,N,D)),vcomb(s->kd, color,vcomb(s->kl,U,black))));}main(){printf("%d %d\n",32,32);while(yx<32*32) U.x=yx%32-32/2,U.z=32/2- yx++/32,U.y=32/2/tan(25/ ),U=vcomb(255., trace(3,black,vunit(U)),black),printf("%.0f %.0f %.0f\n",U);}/*minray!*/
31 When does it end? Diffuse surface – will not transmit or bounce Recursion depth: Stop after a number of bounces Ray contribution: Stop if reflected / transmitted contribution becomes too small No reflectionOne reflectionTwo reflections
32 Computation Would like to handle all physical interactions Ray tracing paradigm is not computational Most rays do not affect what we see Scattering produces many (infinite) additional rays Alternative: Ray Casting Various techniques to speed up Ray Tracing
Depth one and two 33
Adding levels 34
Adding… 35
Aliasing 36
37 Ray Casting a Sphere Ray is parametric Sphere is quadric Resulting equation is a scalar quadratic equation which gives entry and exit points of ray (or no solution if ray misses)
Ray intersects Sphere Equations of Sphere and Ray Plug the values for (x, y, z) from parameterized ray into Sphere, and solve for t
What does it mean? Equations of Sphere and Ray There are three cases: no roots (discriminent is negative), 2 roots, one root What does each mean?
Finer Points If we have a solution with a negative t, we reject it (why? What does it mean?) If we have a solution with a very small positive t, we reject it (why?) Hint – this can arise with a reflection ray
Epsilon Move intersection by epsilon along surface normal
What does it mean? The general conics can be described as quadratics The general case of ray/conic intersection is similar General polygons, the workhorse of modern graphics, are harder
43 Ray Tracing Quadrics Constructive Solid Geometry Primitives are solids Build objects with set operations Union, intersection, set difference
Ray Plane Intersection Equations of Ray and Plane Plug the values for P from parameterized ray into Plane, and solve for t
Ray Plane Intersection Solve this again with explicit values.
Intersections Spheres Polygons Boxes (AA and oriented) Quadrics Parameteric patches Subdivision surfaces Surfaces of revolution Fractals …
47 Polyhedra Generally we want to intersect with closed objects such as polygons and polyhedra rather than planes Hence we have to worry about inside/outside testing For convex objects such as polyhedra there are some fast tests
48 Ray Tracing Polyhedra If ray enters an object, it must enter a front facing polygon and leave a back facing polygon Polyhedron is formed by intersection of planes Ray enters at furthest intersection with front facing planes Ray leaves at closest intersection with back facing planes If entry is further away than exit, ray must miss the polyhedron
49 Problems for Ray Tracing Aliasing Finding all the intersections The technique we outlined had you intersect ray with all objects – time consuming
Aliasing 50
51 Aliasing Three types of solutions Super sampling Adaptive Sampling Stochastic Sampling
52 Finding All Intersections The technique we outlined had you intersect ray with all objects – time consuming Major alternatives Describe scene as collection of objects Try to bundle rays Carve space up into regions – two approaches Volumetric Pixels (voxels) and keep track of which voxels each object hits Split space via half planes
53 Intersections with Objects To reduce the number of computations, we may put bounding boxes around complex objects in the scene If we miss bounding box, we miss contents Early Reject
54 Dividing Space Alternatives Do we work parallel to axis or to objects or to ray? Carve space up into regions – two approaches Volumetric Pixels (voxels) and keep track of which voxels each object hits Split space via half planes: Two alternatives Binary Space Partitioning (BSP) Trees k-Dimensional (kd) Trees
55 Grid
56 Grid Divide scene deltaX need not be deltaY
57 Precompute In advance, figure out which grid boxes each object hits Each grid box keeps list of objects
58 For each box along a ray The ray hits this box This box hits these objects Test ray against each object Return the closest intersection, if any
59 Multiple Visits An object may hit multiple boxes We may encounter the same object multiple times Only test for intersections once: mark the object
60 Grid If intersection is not within the grid box boundaries, continue There may be a better intersection in a later box
61 Grid BSP Trees – in this case, we check almost all, but we check in order
62 KD Trees
63 Summary Ray Tracing provides a way to generate rich images Was too expensive for many uses Faster machines and software Offline processing Much work has gone into speeding it up Demo The Cathedral
64 Resources Persistence of Vision (POV-RAY) – free ray tracer. Descriptions in HLL Radiance – another package radsite.lbl.gov/radiance/HOME.html Internet Ray Tracing Competition – many winners used POV-RAY SIGGRAPH Educational Material Applet that traces a simple scene tml Examples pl887.pairlitesite.com/teach/cs384g-04-fall/projects/ray/ray_examples/