Computer Graphics Solution to Homework 4, CS417 Part CS417/CS418 Fall 1998 Due Monday, March 1, 1999 1 & 2. Explain how to render a scene with multiple spheres and light sources, complete with hidden surface removal via z-buffer, shadows via ray-tracing, and color clipping via global rescaling. Explicitly give all the mathematical formulas (derivations not required). PLEASE do not give Matlab code or a single block of pseudo-code. Break things up into smaller pieces, and explain in English, pseudo-code, Matlab, and/or math. Solution. Like the past HW's, Use the orthographic projection of looking straight down., so larger z values are closer. Start with following initializations: find the x and y offsets, find the height h and width w of the image, initialize the h * w z-buffer -1, and initialize the (h * w * 3) image array to the background color. If necessary, normalize and reverse the light vectors to point towards the light sources. For each sphere, loop over all pixels (x, y) covered by the sphere. If the sphere has center c = (c1, c2, c3) and radius r, these are the pixels with (x - c1)2 + (y - c2)2 <= r2. For each such pixel, compute the z value pr2 - (x - c1)2 - (y - c2)2 + c3 on the sphere. If it is closer then points previously seen (larger than the value in the z-buffer), then update the z-buffer with this new depth and update the color of the pixel. * This color of a pixel is the sum of three portions: ambient, diffuse, and specular. The diffuse and specular portions have contributions from each (visible) light source, which we assume are at infinity. Below, all coefficients of reflection are in the range 0..1. The ambient portion of the pixel color is the ambient coefficients ka (with r, g, b components) of reflection of the sphere multiplied component-wise with the ambient light color. Check that a light source with color C traveling in direction -L is visible at the point by checking that the dot product of the normal N = (x, y, z) - c and -L is positive and that a ray shot from (x, y, z) in direction L is not intercepted by another sphere. (Computations shown later.) For each visible light, the diffuse portion is (N * L)kdC (the products are component-wise), where kd contains the diffuse coefficients (with r, g, b components) of reflection, and N, L are normalized (N points outside the object, L points towards the light source.) For each visible light, the specular portion is (V * R)nksC, where V is the normalized vector from the eye towards the object, R = 2(N * L)N - L is the reflection of the light vector across the normal (N, L as in the previous paragraph), ks is the specular coefficient of reflection, n > 0 is the specular exponent of reflection (usually an integer). This term is used iff V * R is non-negative. * Light traveling in normalized direction L is visible at a point O on a sphere if no (other) sphere with center c and radius r blocks it: Check if the ray is going in the right direction, i.e. that the dot product (c - O) * L is positive. Check whether the closest approach of the ray is within the radius of the sphere: Calculate the projection p = O + ((c - O) * L)L of the vector from c to O onto the ray and verify that the distance between p and c is at most r. If any check fails, then there is no intersection with this sphere. * Color clip by global rescaling. Find the maximum value in any component of a pixel. Divide each color component by that maximum. 1 3. Explain how to do morphing between two images of the same size, i.e. how to compute the image at time t (0 <= t <= 1). Assume that there is only one guide line. Do not bother with anti-aliasing. Answer: Let PsQs be the guideline on the source image S. Let PdQd be the guideline on the destination image D. Interpolating endpoints gives us guideline PtQt at time t, where Pt = (1 - t)Ps + tPd and Qt = (1 - t)Qs + tQd. Let St be the result of mapping S by a combination --see below-- of rotation, scaling, and translation that takes PsQs to PtQt. Let Dt be the result of mapping D by a combination of rotation, scaling, and translation that takes PdQd to PtQt. The image at time t is obtained by blending these transformed source and destination images: (1 - t)St + tDt. * Both St and Dt are obtained from a combination of rotation, scaling, and translation that takes a line segment P Q to P 0Q0. Given the linear transformation T that does this, the new image is obtained by setting the color of a result pixel at (x, y) to the color at the corresponding original pixel (x0, y0), where (x0, y0, 1) = T -1(x, y, 1). (This is like one of our versions of the rotation code.) T can be thought of as first taking P Q to P0Q0, and then taking P0Q0 to P 0Q0, where P0Q0 is the line segment from (0, 0) to (1, 0). Q - P gives us scaled cosines and sines for rotation and scaling, and P gives use the displacements for translation, so A = 0@ Q1 - P1 -(Q2 - P2) P1 Q2 - P2 Q1 - P1 P2 0 0 1 1A B = 0@ Q01 - P 01 -(Q02 - P 02) P 01 Q02 - P 02 Q01 - P 01 P 02 0 0 1 1A are the matrices taking P0Q0 to P Q and P0Q0 to P 0Q0, respectively. (See HW1 for a derivation.) Thus, the transformation we want is T = A-1B. 2