220 likes | 388 Views
Advanced Computer Graphics Lecture 2: Ray Tracing. David Luebke cs551dl@cs.virginia.edu http://www.cs.virginia.edu/~cs551dl. Recursive Ray Tracing. Idea dates back to Descartes (1637) In graphics: Turner Whitted (1980) Used recursive ray tracing as general framework for:
E N D
Advanced Computer GraphicsLecture 2: Ray Tracing David Luebke cs551dl@cs.virginia.edu http://www.cs.virginia.edu/~cs551dl David Luebke 6/10/2014
Recursive Ray Tracing • Idea dates back to Descartes (1637) • In graphics: Turner Whitted (1980) • Used recursive ray tracing as general framework for: • Hidden surface problem • Shadow computation • Reflection/refraction • Much work in the 80’s, then fell into academic disfavor (Turner’s old joke) David Luebke 6/10/2014
Recursive Ray Tracing David Luebke 6/10/2014
Recursive Ray Tracing Pixel ImagePlane David Luebke 6/10/2014
Recursive Ray Tracing “Direct” Ray To Eye David Luebke 6/10/2014
Recursive Ray Tracing “Indirect” Ray To Eye David Luebke 6/10/2014
Recursive Ray Tracing • Physically, we’re interested in path of light rays from light source to eye. • In practice, we trace rays backwards from eye to source (Why?) David Luebke 6/10/2014
Recursive Ray Tracing • Physically, we’re interested in path of light rays from light source to eye. • In practice, we trace rays backwards from eye to source (Why?) • Computational efficiency: we want the finite subset of rays that leave source, bounce around, and pass through eye • Can’t predict where a ray will go, so start with rays we know reach eye David Luebke 6/10/2014
Basic Algorithm • Function TraceRay() Recursively trace ray Rand return resulting color C • IfRintersects any objects then • Find nearest object O • Find local color contribution • Spawn reflected and transmitted rays, using TraceRay() to find resulting colors • Combine colors; return result • else • return background color David Luebke 6/10/2014
Basic Algorithm: Code Object allObs[]; Color image[]; RayTraceScene() allObs = initObjects(); for (Yall rows in image) for (X all pixels in row) Ray R = calcPrimaryRay(X,Y); image[X,Y] = TraceRay(R);display(image); David Luebke 6/10/2014
Basic Algorithm: Code Color TraceRay(Ray R) ifrayHitsObjects(R) then Color localC, reflectC, refractC; Object O = findNearestObject(R); localC = shade(O,R); Ray reflectedRay = calcReflect(O,R) Ray refractedRay = calcRefract(O,R) reflectC = TraceRay(reflectedRay); refractC = TraceRay(refractedRay); return localC reflectC refractC else returnbackgroundColor David Luebke 6/10/2014
Refining theBasic Algorithm Color TraceRay(Ray R) ifrayHitsObjects(R) then Color localC, reflectC, refractC; Object O = findNearestObject(R); localC = shade(O,R); Ray reflectedRay = calcReflect(O,R) Ray refractedRay = calcRefract(O,R) reflectC = TraceRay(reflectedRay); refractC = TraceRay(refractedRay); return localC reflectC refractC else returnbackgroundColor David Luebke 6/10/2014
Ray-Object Intersection • Given a ray and a list of objects, what (if any) objects does the ray intersect? • Query: Does ray R intersect object O? • How to represent ray? • What kind of object? • Sphere • Polygon • Box • General quadric David Luebke 6/10/2014
R = O+ tD O t < 0 t = 1 t > 1 D Representing Rays • How might we represent rays? • We represent a ray parametrically: • A starting point O • A direction vector D • A scalar t • Why? David Luebke 6/10/2014
Ray-Sphere Intersection • Ray R = O + tD x = Ox + t * Dx y = Oy + t * Dy z = Oz + t * Dz • Sphere at (l, m, n) of radius r is: (x - l)2 + (y - m)2 + (z - n)2 = r 2 • Substitute for x,y,z and solve for t… David Luebke 6/10/2014
Ray-Sphere Intersection • Works out as a quadratic equation: at2 + bt + c = 0 where a = Dx2 + Dy2 + Dz2 b =2Dx(Ox - l) + 2Dy(Oy - m) + 2Dz(Oz - n) c = l2 + m2 + n2 + Ox2 + Oy2 + Oz2 - 2(l Ox + m Oy + n Oz + r2) David Luebke 6/10/2014
Ray-Sphere Intersection • If solving for t gives no real roots: ray does not intersect sphere • If solving gives 1 real root r, ray grazes sphere where t = r • If solving gives 2 real roots (r1, r2), ray intersects sphere at t = r1& t = r2 • Ignore negative values • Smallest value is first intersection David Luebke 6/10/2014
Ray-Sphere Intersection • Find intersection point Pi = (xi, yi, zi) by plugging t back into ray equation • Find normal at intersection point by subtracting sphere center from Pi and normalizing: (When might we need the normal? When not?) David Luebke 6/10/2014
Ray-Polygon Intersection • Polygons are the most common model representation (Why?) • Basic approach: • Find plane equation of polygon • Find intersection of ray and plane • Does polygon contain intersection point? David Luebke 6/10/2014
y N P2 P1 d x Ray-Polygon Intersection • Find plane equation of polygon:ax + by + cz + d = 0 • How? N = [a, b, c] d = N P1 (How to find N ?) David Luebke 6/10/2014
Ray-Polygon Intersection • Find intersection of ray and plane: t = -(aOx + bOy + cOz + d) / (aDx + bDy + cDz) • Does poly contain intersection point Pi ? • Book’s algorithm: • Draw line from Pi to each polygon vertex • Measure angles between lines (how?) • If sum of angles between lines is 360°, polygon contains Pi • Slow — better algorithms available David Luebke 6/10/2014
Ray-Box Intersection • Often want to find whether a ray hits an axis-aligned box (Why?) • One way: • Intersect ray with pairs of parallel planes that form box • If intervals of intersection overlap, the ray intersects the volume. David Luebke 6/10/2014