160 likes | 313 Views
More on Ray Tracing. Glenn G. Chappell CHAPPELLG@member.ams.org U. of Alaska Fairbanks CS 481/681 Lecture Notes Wednesday, April 14, 2004. Review: Basic Ray Tracing [1/5]. In “normal” rendering: We deal with a series of objects, made of primitives.
E N D
More on Ray Tracing Glenn G. ChappellCHAPPELLG@member.ams.org U. of Alaska Fairbanks CS 481/681 Lecture Notes Wednesday, April 14, 2004
Review:Basic Ray Tracing [1/5] • In “normal” rendering: • We deal with a series of objects, made of primitives. • For each primitive, we determine which pixels it affects, if any. • Ray tracing turns this around: • We deal with pixels, one by one. • For each pixel, we ask what we see (which primitive?) when we look at it. CS 481/681
Review:Basic Ray Tracing [2/5] • The way we determine what we see when we look at a pixel is to draw an imaginary ray from the viewing position, through the pixel, into the scene. • We ask which objects in the scene the ray hits. • The first hit is the one that counts. Image Scene objects Current pixel First hit CS 481/681
Review:Basic Ray Tracing [3/5] • What do we do when we have a hit? • We determine what color the object is at that point. • Light sources and the object’s normal may affect the computation. • We can also do true specular reflection: • Reflect the ray and do the ray tracing computation again. • We can also do true refraction, for translucent objects. Original ray Reflected ray Normal CS 481/681
Review:Basic Ray Tracing [4/5] • Our discussion was centered around two questions to be answered: • Does a ray hit an object; if so, where? • If a ray hits an object, then, looking along the ray, what color do we see? • We outlined a simple OO design based on answering these questions. • Our design is simple, robust, and easy to extend. CS 481/681
Review:Basic Ray Tracing [5/5] • Classes • Ray • A ray is defined by its starting position and its direction. • For convenience, rays know how to reflect and refract. • Object • Knows how to answer the two questions (for itself). • Object classes are derived from a common base class. • The two questions are answered via virtual functions. • Hit • Stores the result of a hit test. • For convenience, holds: whether there was a hit, and if so, how far along the ray, where, and the object normal at the hit point. CS 481/681
More on Ray Tracing:Topics • Next topics: • A few notes. • Hit testing. • Example ray-tracing code. • Adding features to a ray tracer. CS 481/681
Ray Tracing Notes:A Third Method • We know about “normal” rendering and ray tracing. • There is a third option: photon tracing. • Here, we trace photons forward from the light source. • Since most photons do not hit the viewer’s eye, this is very inefficient. • However, it is also very accurate, esp. for things like caustics. • Quick Summary • “Normal” • Main loop goes over primitives. • Which pixels does this primitive affect? • Ray Tracing • Main loop goes over pixels. • What to I see when I look in this direction? • Photon Tracing • Main loop goes over photons. • Where does this light end up? CS 481/681
Ray Tracing Notes:The Design • Back to the ray tracer. I will add two more classes to the design: • Object List • This holds the entire scene. If true specular reflection is to be performed, an object needs to have access to information about the other objects. • Essentially one member function: trace a ray through the scene and return a color. • Needs to know what color to return if the ray does not hit an object. • Light List • It is easy to implement cheap-ish diffuse reflection, shadows, etc., in a ray tracer, if the location of light-sources is known. • I put this in my ray tracer, but it is currently not used. CS 481/681
Hit Testing:Introduction • The easiest type of 3-D object to render with the “normal” method is a triangle. • In ray tracing, “easy” means a quick hit test and color determination. • The easiest type of object to include in a ray-traced scene is a sphere. • This is why most early ray-traced scenes involved shiny balls. CS 481/681
Hit Testing:Ray-Sphere Intersection [1/4] • We look at how to do a ray-sphere intersection. • Suppose we have a ray with start position E and direction vector V. • We want to do a hit test with a sphere having center O and radius r. ??? V E O r CS 481/681
Hit Testing:Ray-Sphere Intersection [2/4] • The vector from E to O is EO = O – E. • Let v be the dot product of EO and V. • The value of v is the distance from E to the point halfway between the hits, if there are any hits. • So |EO|2 – v2 is the square of the distance from O to this halfway point. • If it exists! v V E sqrt[|EO|2 – v2] EO O r CS 481/681
Hit Testing:Ray-Sphere Intersection [3/4] • So: disc = r2 – [|EO|2 – v2] is the square of half the distance between the hit points. • If the hits exist! • The quantity disc is called the discriminant. • If disc < 0, then there are no hits! sqrt[disc] V E sqrt[|EO|2 – v2] EO O r CS 481/681
Hit Testing:Ray-Sphere Intersection [4/4] • The distances to the two hits, are v – sqrt[disc] and v + sqrt[disc]. • Based on all this, we can come up with a relatively simple algorithm to do hit testing on a sphere. • See grtobject.cpp for an implementation. CS 481/681
Hit Testing:Convex Polygons • Of course, we would like to be able to put polygons into a ray-traced scene. We need only deal with convex polygons. • We need to know how to do a hit test on a convex polygon. Here is an outline: • Find the plane the polygon lies in. • Do ray-plane intersection to find the point at which the ray hits the plane (unless it is parallel to the plane). • Check the hit point to see if it lies in the polygon. • This is done by looping over the edges of the polygon. For each edge, we check whether the point lies on the inside of the line through it. • If the point lies on the inside of all the edges, then it is inside the polygon, and we have a hit. • The normal is the normal of the plane. CS 481/681
Ray Tracing Code:Example • Now we look at the GRT program. • “GRT” = “Glenn’s Ray Tracer”. • In class, we went over the source to grtmain.cpp, grtray.cpp, grtobject.cpp, grttypes.cpp. • These files are on the web page. CS 481/681