Download presentation
Presentation is loading. Please wait.
Published byPeter Park Modified over 9 years ago
1
Ray Tracing CptS 548 Advanced Computer Graphics
2
Why Write a Ray Tracer? More elegant than polygon scan conversion Testbed for numerous techniques and effects –modeling –rendering –texturing Easiest photorealistic renderer to implement
3
Overview Vector arithmetic Lookat viewing coordinates Ray casting Shadows Reflection Refraction
4
Homogeneous Coordinates Point: o = (x o, y o, z o, 1) Direction: d= (x d, y d, z d, 0) Points translate, directions do not Homogeneous coordinate always 0 or 1 No perspective transformation Ray: r = (o,d) = ((x o, y o, z o,1),(x d, y d, z d,0)) Ray direction d always unit length o d r
5
Dot Product a · b = dot(a,b) = x a x b + y a y b + z a z b Cosine of angle between a and b if both unit length Used to “cast a shadow” of one unit vector onto another a b (a·b) a
6
Cross Product a b = cross(a,b) = (y a z b - z a y b, z a x b - x a z b, x a y b - y a x b, 0) Returns direction perp. to plane of a, b Used to set up coordinate systems a b c = a b c a
7
Eye LUV Coordinates lookat eye l up u v l = (lookat - eye)/||(lookat - eye)|| v = (l up)/||(l up)|| u = v l
8
Pixels in World Coordinates eye dldl -av-av -u ll ll = eye + dl - av - u aspect ratio a = w/h focal length d = 1/tan(fovy/2) fovy
9
Casting Rays through Pixels for (j = 0; j < VRES; j++) { for (i = 0; i < HRES; i++) { p = ll + 2av (double)i/HRES + 2u (double)j/VRES; d = (p - eye)/||p - eye||; r = (eye,d); color = TraceRay(r); plot(i,j,color); }
10
TraceRay Attributes Invoked with ray parameter –Better if object database is global –Best if TraceRay is a member function of object database object (yikes) Returns a color vector (R,G,B, ) – indicates transparency –color = foreground + (1- ) background
11
Object Hierarchy Object intersects() TraceRay() Primitive normal() HitObject t hit n shade() Composite Sphere Polygon Patch List CSG
12
TraceRay Definition Color TraceRay(Ray r, int depth) { HitObject ho; color c = background; if (intersects(r,ho)) { c = ho.shade(lights);/* casts a shadow ray */ c += ho.ks*TraceRay(ho.reflect(r), depth-1); c += ho.kr*TraceRay(ho.refract(r), depth-1); } return c; }
13
List Definition class List : public Composite { Object item[100]; int n; boolean intersects(Ray r, HitObject &ho) { min_t = 1.e20; for (i = 0; i < n; i++) if (item[i].intersects(r,o) && o.t < min_t) { min_t = o.t; ho = o; } combine_shade(ho); return (min_t < 1.e20); } }
14
shade Definition Color shade(Lights lights) { Color c = ka; for (l = 0; l < lights.n; l++) { ldir = normalized(lights[l].pos - hit); if (!intersects(ray(hit,ldir))) c += kd*cd*dot(n, ldir) + … } return(c); }
15
reflect Definition r.dbounce o.n Ray reflect(Ray r) { Ray bounce; bounce.o = hit; bounce.d = 2.0*dot(n,-r.d)*n + r.d; return(bounce); }
16
Calculating Refraction Snell’s Law: i sin i = t sin t Let = i / t = sin t / sin i t = sin t m - cos t n m = (cos i n - i) / sin i = (sin t / sin i ) (cos i n - i) - cos t n cos i n - i i n -n-n ii tt t m = ( cos i - cos t )n - i
17
refract Definition Ray refract(Ray r) { Ray t; double ni = dot(n,-r.d); eta = current_index/new_index; t.o = hit; t.d =(eta*ni - sqrt(1.0 - eta*eta*(1-ni*ni)))*n + eta*r.d; return(t); }
18
Ray-Object Intersection intersects() member function of Object class Called with ray and hitobject Intersects ray with this object Returns intersection data in hitobject –t - parameter, hit - location, n - normal –new - cannot be static because of recursion –always primitive, never composite
19
Intersection Computation Parametric ray: r(t) = o + t d –t 0 –If ||d|| = 1, t is distance along ray Implicit object: f(x) = 0 Intersection occurs when f(r(t)) = 0 –Real function of one real variable –Intersection root finding
20
Sphere Object class Sphere : public Primitive { Point c; double r; boolean intersects(Ray r, HitObject ho); } c r
21
Sphere Intersection f(x)=(x - c) (x - c) - r 2 f(r(t)) = (o + t d - c) (o + t d - c) - r 2 = d d t 2 + 2 (o-c) d t + (o-c) (o-c) - r 2 A = d d = 1 B = 2 (o-c) d C = (o-c) (o-c) - r 2 D = B*B - 4*C; if (D < 0.0) return FALSE; rootD = sqrt(D); t0 = 0.5*(-B - rootD); t1 = 0.5*(-B + rootD); if (t0 >= 0) t = t0, return TRUE; if (t1 >= 0) t = t1, return TRUE; return FALSE;
22
Other HitObject Values hit = r.o + t*r.d n = hit - c
23
Ellipsoid Intersection f(x) = 0 f(T -1 x)=0 Let T be a 4x4 transformation T distorts a sphere into an ellipsoid f(T -1 r(t)) = f(T -1 (o + t d)) = f(T -1 o + t T -1 d)) Ellipsoid is implicit surface of f(T -1 x) Ellipsoid::intersects(r,&ho) { Ray Tir = (Ti * o, Ti * d); Sphere::intersects(Tir,ho); } o d T -1 o T -1 d T x T -1 x
24
What about the normal? f(x) = 0 f(T -1 x)=0 T Tx x n is tangent plane [a b c d] x is point [x y z 1] T Such that matrix product n x = 0 n (n Q) Tx = 0 What is Q? Q = T -1 (n T -1 ) Tx = n (T -1 T)x = 0 New normal n’ = n T -1 = (T -1 ) T n T n’n’
25
Intersecting Quadrics Just like sphere: x 2 + y 2 + z 2 - r 2 Cylinder? –x 2 + y 2 - r 2 Cone? –x 2 + y 2 - z 2 Variations? –Use the 4x4 transformation trick
26
Constructive Solid Geometry Construct shapes from primitives using boolean set operations Union: A+B? –A or B Intersection: A*B? –A and B Difference: A - B? –A and not B
27
CSG Intersections List of t-values –for left object L –for right object R Assume ray originates outside object –t = 0 means ray originates inside object Traverse both lists in increasing t order Roth Table determine resulting t-values
28
Accelerating Ray Tracing Q: Why is ray tracing so slow? A: It intersects every ray with every object Q: How can we make ray tracing faster? A: Coherence –Image coherence - neighboring pixel, same obj. –Spatial coherence - neighboring pt., same obj. –Temporal coherence - next frame, same image
29
Shadow Caching Neighboring points in a shadow are probably shadowed by the same object Start shadow ray intersection search with object intersected in last shadow search
30
Bounding Volume Cartman contains 100,000 polygons Ray-Cartman intersection = 100,000 ray-poly intersections Even if the ray misses Cartman Solution: Place a sphere around Cartman If ray misses sphere then ray misses Cartman
31
Bounding Volume Hierarchies
32
Grids 3-D array of lists Ray intersected with all objects in a given cell Cells visited in Bresenham order
33
Tagging the Object Intersection test for entire object Not just portion of object in grid cell Only intersect object once Cache temporary intersection info in object
34
Other Partitioning Structures Octree –Ray can parse through large empty areas –Requires less space –Subdivision takes time Binary Space Partition (BSP) Tree –Trees more balanced –Partitioning more flexible -> shallower trees –Added ray-plane intersections
35
Saving the Picture Windows –PrtSc key Copies current window Paste into any app Save as any file type –Libraries libtiff, etc. Unix –xwd saves a window as an xwd file convert to any format –xv grabs a window save as any format –Libraries
36
Submitting the Picture Format –JPEG 100% uncompressed –Don’t want compression artifacts –Might be mistaken for bugs Web page –~you/public_html/cs319 –small compressed version –link to full-size uncompressed version
37
Submitting the Code E-mail hart@eecs.wsu.edu Attach –compressed tar file –zip/gzip archive Contents –source code (+headers) –makefile –scene description files
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.