Computer Graphics Homework 2 CS417/CS418 Fall 1998 Due Wednesday, February 17, 1999 + Make sure you've turned in the waiver and its requested e-mail. + Hand in each question SEPARATELY. + Make sure your names are legible and easily spotted on the front page of each part. + As always in this class, you are graded on correctness \emph{and also clarity and conciseness}. + Write legibly & sign & date the following (fill blanks appropriately): ``I, ____, CUID# ____, wrote up this assignment; my partner is ____, CUID# ____.'' + E-mail how many total hours this homework takes you by midnight, Friday, February 19. =============================================================================== CS417 part =============================================================================== 1. Complete the function below, using a z-buffer for hidden surface removal. ------------------------------------------------------------------------------- function [pixel] = drawspheres(c,r,ka,kd,ks,n,ambient,l,lc) % [pixel] = drawspheres(c,r,ka,kd,ks,n,l,lc): % return an image pixel (with integer world and view coordinates) % just large enough to show all the spheres under the specified lights. % % the i-th sphere has center c(1:3,i), radius r(i), % ambient coefficient ka(i), diffuse coefficients kd(1:3,i), % specular coefficient ks(i), specular exponent n(i) % (the ambient coefficients for each r,g,b component are ka(i)*kd(1:3,i)). % % the ambient light has color ambient(1:3) % % the j-th light travels in direction l(1:3,j), with color lc(1:3,j) y = c(2,:); y = [y-r y+r]; ybias = 1-min(y); x = c(1,:); x = [x-r x+r]; xbias = 1-min(x); high = ceil(max(y))-floor(min(y))+1; wide = ceil(max(x))-floor(min(x))+1; pixel = zeros(high,wide,3)-1; % use -1 to flag background v = [0 ; 0 ; 1]; % view vector (towards eye) % fill in each circle with its diffuse color -- % no attempt at lighting or hidden surface removal % (FIX THIS!) for i = 1:size(c,2) for x = 1:wide for y = 1:high if (x-xbias-c(1,i))^2 + (y-ybias-c(2,i))^2 <= r(i)^2 pixel(y,x,:) = kd(:,i); end % if distance^2 <= r^2 end % for y end % for x end % for i pixel(pixel<0) = 1; % set background to white ------------------------------------------------------------------------------- Notes: + You may entirely rewrite the loops. However, it is not a good idea in this case to eliminate all loops, and there are some subtleties. + Some things you must do are: + given x,y on a sphere, compute the z coordinate + compute the normal + figure out what to do with negative/positive dot products + compute the ambient contribution + compute the diffuse contribution + compute the reflected vector + compute the specular contribution Turn in your code, and the image from the following (it is suggested you store and run it as a script): c = [ ... 20 40 30 50 30 15 0 30 20]; r = [40 15 25]; ka = [.1 .2 .3]; kd = [ ... .7 0 0 0 .8 0 0 0 1]; ks = [.2 .4 .8]; n = [1 5 10]; ambient = [.4 .4 0]'; l = [ ... 0 -1 -1 1 -1 0 -1 -1 -1]; lc = [ ... 1 1 0 1 0 1 1 0 1]; image(drawspheres(c,r,ka,kd,ks,n,ambient,l,lc)); axis equal; axis xy =============================================================================== CS418 part =============================================================================== 2. Complete the function below. Turn in your code. ------------------------------------------------------------------------------- function [hit,where,normal] = raysphere(origin, direction, center, radius) % [HIT,WHERE,NORMAL] = RAYSPHERE(ORIGIN,DIRECTION,CENTER,RADIUS) % % compute the intersection of a ray with a sphere % % ORIGIN: start of ray % DIRECTION: normalized direction of ray % CENTER: center of sphere % RADIUS: radius of sphere % % HIT: whether ray hits sphere (0 = no, 1 = yes) % WHERE: where the intersection is % NORMAL: normalized normal of sphere at intersection -------------------------------------------------------------------------------