Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 376 Introduction to Computer Graphics 04 / 09 / 2007 Instructor: Michael Eckmann.

Similar presentations


Presentation on theme: "CS 376 Introduction to Computer Graphics 04 / 09 / 2007 Instructor: Michael Eckmann."— Presentation transcript:

1 CS 376 Introduction to Computer Graphics 04 / 09 / 2007 Instructor: Michael Eckmann

2 Michael Eckmann - Skidmore College - CS 376 - Spring 2007 Today’s Topics Questions? computing the shadow, reflected, and refracted ray directions. antialiased ray tracing Go over the pseudocode for ray tracing ppm image file format

3 Computing the Ray directions Anyone have any idea how we would determine the ray equation for the shadow rays (given the intersection point and the position of the lights)? Now we will turn our discussion over to how to determine the direction of the reflected and refracted rays off of a surface.

4 Reflected Ray Given L, the incident ray (incoming ray) and the normal N of the surface (see figure 10-20 in our text) we wish to calculate the reflected ray R. We assume that L and N are unit vectors (if they're not, make them unit vectors). We will compute the unit vector R in the correct direction. Drawing on the board. Note that fig. 10-20 shows L in a direction away from the surface. Typically we will have a vector pointing towards the surface, so if we are going to follow the procedure in the text to compute R, we must negate the vector we have to get L in the direction as shown in 10-20. A is the angle between L & N, and between N & R, therefore we have L N = N R = cos A

5 Reflected Ray A is the angle between L & N, and between N & R, therefore we have L N = N R = cos A On the board (fig. 10-20) –Show the value of L N graphically –Show the sum of R and L graphically Therefore we can see that R + L = 2 (L N) N whose magnitude is 2 (L N) and direction is in the direction of N So to compute R we get R = 2 (L N) N – L

6 Reflected Ray If you have a unit vector say L' in the opposite direction of L (as shown in figure 10-20) compute R to be R = 2 (-L' N) N + L'

7 Refracted Ray Given L, the incident ray (incoming ray) and the normal N of the surface (see figure 10-30 in our text) we wish to calculate the refracted ray T. Recall that Snell's Law is a relationship between angle of incidence and refraction and indices of refraction.

8

9 Refracted Ray Snell's law states that sin ( theta r ) eta i -------------- = ----- sin ( theta i ) eta r which can be written as: sin ( theta r ) = ((eta i ) / (eta r )) * sin ( theta i )

10 Refracted Ray Assuming all of our vectors are unit vectors, using Snell's law, according to our textbook we can compute the unit refracted ray, T, to be T = (((eta i ) / (eta r )) cos ( theta r ) – cos ( theta r )) N – ((eta i ) / (eta r ))L See page 578 in our text. This assumes L is in the direction shown in the diagram of figure 10- 30.

11 RayTracing and Antialiasing As was stated earlier, Ray Tracing works by shooting a ray from the eye (CoP) through the center of a pixel and from the information about the surface it hits and the lights, etc. a color & intensity is determined for the pixel. Let's see some example situations that give way to aliased images.

12 RayTracing and Antialiasing An improvement to eliminate or at least reduce the aliasing effects just seen is to cast rays not through the center of the pixel, but through the corners of the pixel. Then compute a color at each corner. Then to set the color of the pixel do either: Supersampling –Take the average of the color of all four corners to set the pixel color. Adaptive Supersampling –in addition to using supersampling, if one corner's calculated color is significantly different than the other three (your call as to what is significantly different) then subdivide the pixel into four quadrants and shoot a ray through the additional 5 corners. Continue to do this until you decide to stop or when the four corners you're concerned with don't have one which is significantly different. –Then average the four corners of each of the subareas and the color of the pixel is determined by the colors of these subareas in proportion to their area. –Example on the board.

13 RayTracing Let's take a look at the handout which shows pseudocode for a recursive ray tracing algorithm and see what it entails. We'll have all our objects defined in the world (assume only spheres and polygons) with world coordinates. First part shows that we decide on a CoP and a view plane then go through the image to be created one pixel at a time from left to right on a scan line and top to bottom in scan lines. For each pixel we –Determine the ray through the center of the pixel starting at the CoP (how to we determine this?) –Then call RT_trace and pass in that ray and the number 1 (for the current depth in the ray tree) –RT_trace will return the color that the pixel should be RT_trace first determines which is the closest object of intersection (how would we do this?) –If the ray doesn't intersect any object, then return the background color (whatever you decide it to be) otherwise...

14 RayTracing RT_trace first determines which is the closest object of intersection (how would we do this?) –If the ray doesn't intersect any object, then return the background color (whatever you decide it to be) otherwise –call RT_shade with the closest object intersected, the ray, the point of intersection, the normal at that intersection (calculate this) and the depth (which is 1 the first time). How do we compute the point of intersection with the object? How do we compute the normal there? In RT_shade we set the color initially to be some ambient term (based on an ambient coefficient for the object, the object's color and our ambient light defined in our world. Go through all the lights in our world and determine the shadow rays one at a time. It then says “if the dot product of normal and direction to light is positive”. Can anyone describe what that tells us? –If a shadow ray is blocked by an opaque surface then ignore it. –If a shadow ray goes through a transparent surface reduce the amount of light transmitted by some factor (k t ) which is associated with the object (see Basic Transparency Model discussion on pages 578-579 in text). –If a shadow ray doesn't intersect anything, then just attenuate the light based on the distance. Use this attenuated light and the suface's diffuse property to compute the diffuse term.

15 RayTracing Then add each of the shadow ray/diffuse contributions to the color RT_shade then continues as long as depth < MaxDepth. Depth is initially 1 and MaxDepth can be set to something like 4. –Recursively call RT_trace for the reflection ray (and depth+1) if the surface is specularly reflective Scale the color returned by the specular coefficient of the surface Add this to the color of the pixel we're calculating –Recursively call RT_trace for the refraction ray (and depth+1) if the surface is transparent Scale the color returned by the transmission coefficient of the surface Add this to the color of the pixel we're calculating Return color

16 RayTracing Comments about ray tracing –To store which object is closest for a given ray through a pixel, we could use an altered form of the z-buffer method. –How did z-buffer method (aka depth buffer) work again?

17 RayTracing A few comments about ray tracing –To store which object is closest for a given ray through a pixel, we could use an altered form of the z-buffer method. –How did z-buffer method (aka depth buffer) work again? It stored the color of the nearest surface for each pixel into the frame buffer and the distance into a z-buffer Instead of storing the distance into the z-buffer, store which object is closest into an item-buffer. –Instead of creating the same depth tree for each ray (that is, the number of levels of recursion) at a particular level, determine if the expected maximum contribution (to the color of the pixel) of the ray to be cast will not be above some threshold. If it is deemed to be insignificant, don't cast it. If it has the potential to be significant, cast it. For example, a primary ray hits an object which could cast a reflection ray and that reflection ray also hits an object and casts a reflection ray and that one also hits an object and can cast a reflection ray. The surface that the second reflection ray hits might have a very small specular coefficient thereby influencing the color of the pixel insignificantly, so, then don't cast that ray (and you can save the computation for that ray as well as the ray it would have spawned, and so on.) Ideally in this situation, only the ones that count will be recursed to further depths.

18 Simple Image format PPM PPM is an image file format that is easy to write to from a program and easy to read into a program. It comes in two forms –Raw and plain The first line of this image file is a “magic number” consisting of two characters –P3 for plain –P6 for raw The second line contains two integers separated by whitespace representing the width and height (in that order) in pixels of this image. The next line contains one number which represents the maximum value for a color (e.g. 255 if you wish to describe each RGB value with a number between 0 and 255 (1 byte).) Although I describe that these things should all be on separate lines, all that is really required is whitespace between the parts that I say should be on separate lines.

19 Simple Image format PPM Next, the file contains height number of lines, each line of which contains width number of R, G, B values which are –3 ASCII decimal values between 0 and the maxvalue, each separated by whitespace for the plain format file –3 binary values (1 byte each if maxvalue < 256, 2 bytes each otherwise) If you wish to write raw format files using one byte per color value, then use type byte in Java. If you wish to write plain format, write out the color values to the file as plain text (e.g. A Red color value of 210 would appear in the file as 3 characters 2 1 and 0.) Let's look at example PPM files. They can be displayed in most image viewers and can be converted to other formats.

20 Suggestions for ray tracer prog. You will be required to save your ray traced images in a file (ppm is suggested since it is easy to write to) You will also be required to show your ray traced image on screen in openGL. Since you are computing the color of one pixel at a time, you can simply draw the pixel as a polygon with four vertices.


Download ppt "CS 376 Introduction to Computer Graphics 04 / 09 / 2007 Instructor: Michael Eckmann."

Similar presentations


Ads by Google