UNIVERSITY OF EAST ANGLIA School of Computing Sciences Main Series UG Examination 2012-13 GRAPHICS 1 CMPC2G04 Time allowed: 2 hours Answer THREE questions out of FOUR. (40 marks each) Notes are not permitted in this examination. Do not turn over until you are told to do so by the invigilator. CMPC2G04 Module Contact: Dr R J Lapeer, CMP Copyright of the University of East Anglia Version 1
Page 2 1. The window in Figure 1 (a) below shows a bug which is capable of crawling across the screen in any possible direction. The user interface is keyboard and mouse based. The left and right keyboard arrows allow the bug to rotate around its own axis in anti-clockwise and clockwise directions respectively, whilst the up and down arrows allow the bug to accelerate or decelerate respectively. Once the left mouse button is activated, the bug starts moving forwards according to a velocity vector with a speed set by the up and down arrows and a direction set by the left and right arrows. Additionally, three rotating clusters of three triangles, squares and circles, respectively, surround the bug. The individual polygons rotate around their centre, whilst their clusters can rotate around the cluster centre and finally all clusters rotate collectively around the screen centre (similar to a teacup ride on the fairground). There is collision detection enabled between the bug and the rotating objects, the latter being capable of knocking the bug backwards once they collide. The aim of the game is to let the bug escape without being hit by the rotating objects. A demo (executable) of this simple game was shown during the lectures. (a) (b) Figure 1. a. Figure 1(b) shows the update of the bug s position. The variable dmove in the code corresponds to the speed (length of velocity vector v) of the bug and is determined by the up and downward arrows. From the figure it can be seen that the update for the x coordinate is given by the following equation: x n x0 dmove cos( / 2 )
Page 3 Where x 0 is the x-coordinate of the current position and x n is the x-coordinate of the next position. Tasks: i. Write down the equation used to update the y-coordinate. [3 marks] ii. Why is /2 added to? [4 marks] b. A snippet of the display function for this 2D game is shown below: void display(void) { glclear(gl_color_buffer_bit); setrotmat(m,pi*spin[0]/180.0,2); glcolor3f(0.0,1.0,0.0); Idisplay(theTriangle,-25.0,25.0); glcolor3f(1.0,0.0,0.0); Idisplay(theSquare,0,-35.355); glcolor3f(0.0,0.0,1.0); Idisplay(theCircle,25.0,25.0); glpushmatrix(); glenable(gl_texture_2d); glbindtexture(gl_texture_2d,texname); glcolor3f(1.0,1.0,1.0); xn=xo+dmove*cos(pi/2+alpha*pi/180.0); yn=yo+dmove*sin(pi/2+alpha*pi/180.0); xo=xn;yo=yn; settramat(mb1,xn,yn,0.0); setrotmat(mb2,pi*alpha/180.0,2); MultMat(mb1,mb2,mb); glloadmatrixf(mb); glbegin(gl_quads); gltexcoord2f(0.0,0.0); glvertex2f(bugpoly.vert[0].x,bugpoly.vert[0].y); gltexcoord2f(1.0,0.0); glvertex2f(bugpoly.vert[1].x,bugpoly.vert[1].y); gltexcoord2f(1.0,1.0); glvertex2f(bugpoly.vert[2].x,bugpoly.vert[2].y); gltexcoord2f(0.0,1.0); glvertex2f(bugpoly.vert[3].x,bugpoly.vert[3].y); glend(); glpopmatrix(); glutswapbuffers(); glflush(); gldisable(gl_texture_2d); PLEASE TURN OVER
Page 4 The function settramat replaces the fourth column of a 4x4 transformation matrix stored in a vector of length 16 called mb1, with [xn yn 0.0 1.0] T. The function setrotmat takes an angle (float) as second argument and an axis ID (int) as third argument (0=x,1=y,2=z) and creates a 4 x4 rotational matrix, stored in a vector of length 16. i. Assuming the angle (second argument) is 90 degrees, write down the corresponding 4x4 matrix. [4 marks] ii. Write down the body of the function setrotmat(float *m,float angle,int axis) using a switch/case statement to specify the code for each different axis. Since the code only needs to work in 2D, you can leave the case statements for two particular axes blank. You can make use of a function setidentity(m) to set your matrix m equal to the identity matrix. [10 marks] iii. After the positional coordinates of the bug are updated to their new positions xn and yn, respectively, the updated values are assigned to x0 and y0 in the function display. Why is this necessary? Could this be performed more optimally without using x0 and y0? If so, rewrite the code snippet. [4 marks] c. Without going into an in depth implementation of the code for the bug game, write down a function body illustrating a nested hierarchy of three rotations such as the one displayed in the game by the clusters of triangles, circles and squares, i.e. the teacup ride effect. Call the function idisplay() and use the glpushmatrix(), glpopmatrix(), gltranslatef(x,y,z) and glrotatef(a,x,y,z) functions. You can use variables such as poly[i] (i=0 for circles, 1 for triangles, 2 for squares) and a pseudo function such as display(poly[i])to display individual polygons. Use the global variables centre_angle, cluster_angle and poly_angle for the rotational angles at each respective level. You do not need to update these angles in this function as that happens somewhere else and angles are the same for each type of polygon (so no need to put some random effects in it). The x,y screen coordinates of the centres of each cluster are (-25.0,25.0) for the triangles; (0,-35.355) for the squares and (25.0,25.0) for the circles. Hint: the radius (call it Radius1) of the clusters around the centre is sqrt(25^2+25^2 35.355). The positions of the circles, triangles and squares is 30º, 150º and 270º respectively (note the 120º increment between angles). The radius within each cluster is 10.0 (call it Radius2). Within the cluster, the polygons are initially positioned (See Fig. 1(a)) at 90º, 210º and 330º (note the 120º increment between angles). Hint: Build your program from the middle outwards, i.e. start with the local transformations of the individual polygons, then in their clusters, then the clusters around the centre. [15 marks]
Page 5 2. a. In the context of rasterising outline fonts, describe what is meant by the terms: Hinting, Antialiasing and Sub-pixel rendering. b. In the context of the CIE colour spaces: [10 marks] i. Describe the CIE XYZ colour space. What was the motivation behind its development? What is the main difference between the shapes of the CIE RGB and CIE XYZ colour matching functions? [5 marks] ii. Given any XYZ triplet, how do you calculate its chromaticity coordinates xy? If an xy coordinate equals [0.3 0.33], what is its corresponding z coordinate? [2 marks] iii. Draw the CIE xy diagram containing the shape of a typical display colour gamut. In the diagram, indicate the locations of the spectral curve, purple line and display primaries. [5 marks] c. You downloaded a.jpg image from the web. How would you change the saturation of this image? Your answer should include a clear description of all the steps of the algorithm. [9 marks] d. Describe the RGB, CMY and HSV colour spaces and their main applications. [9 marks] PLEASE TURN OVER
Page 6 3. a. We consider the mid-point algorithm for line drawing. In the lectures we have seen how to determine the E-NE pixels in the 0-45 degrees octant based on the mid-point criterion using a decision variable d. i. Write down the mid-point criterion (determine d) when considering the N- NE pixels in the 45-90 degree octant. Use Figure 2 as a guide. Also write down the conditions (i.e. with respect to parameters a,b and m) for which the criterion is valid. [12 marks ] ii. What is the main advantage of the midpoint algorithm over the DDA algorithm? [3 marks ] iii. Briefly discuss anti-aliasing techniques for line drawing. [9 marks ] Figure 2. b. Imagine we start with a square at the origin as shown in Figure 3(a). We wish to derive the articulated geometry (some kind of robotic arm) as shown in Figure 3(b). Specify the 2D transformation matrices (in the right order) to arrive at this geometry by solely using three squares all starting in the origin as shown in Figure 1a. Do this in three steps, i.e. first for the gantry, then the first link, then for the second link, as seen in the lectures. [16 marks]
Page 7 Figure 3. PLEASE TURN OVER
Page 8 4. a. Consider the scan-line algorithm for the polygon in Figure 4. i. Show the relevant scan lines on an updated figure. Also indicate which points need to be lowered by drawing a dot underneath and label this new point with the primed label of the original point. [ 8 marks ] ii. Determine the initial bucket-sorted edge table using linked lists. [12 marks] Figure 4.
Page 9 b. The figure below shows a region with one line to be clipped. Illustrate how the Cohen-Sutherland clipping algorithm (programming code shown below) will clip this line using a step by step approach, i.e. by clearly referring to the programming code line numbers which are processed during program execution. Also make sure you mention the state of any variable that is altered when a particular line is processed. Update Figure 5 with the new points generated during the execution of the program. [20 marks] Figure 5. PLEASE TURN OVER
Page 10 1. void ClipLine_Cohen_Sutherland(coord winmin, coord winmax, coord p0, coord p1){ 2. unsigned char code0,code1; 3. int done = FALSE, draw = FALSE; 4. float m; 5. while(!done){ 6. code0 = encode(p0,winmin,winmax); 7. code1 = encode(p1,winmin,winmax); 8. if(accept(code0,code1)){ /* Line is entirely inside clip-window */ 9. done = TRUE; 10. draw = TRUE; 11. else { 12. if(reject(code0,code1)) 13. done = TRUE; 14. else { 15. if(inside(code0)){ /* Init. Point inside clip-window => swap */ 16. swappts(&p0,&p1); 17. swapcodes(&code0,&code1); 18. if(p1.x!= p0.x){ /* No vertical line */ 19. m=((float)(p1->y-p0->y))/((float)(p1->xp0->x)); 20. if(code0 & LEFT_EDGE){ 21. p0.y += (winmin.x-p0.x)*m; 22. p0.x = winmin.x; 23. else if(code0 & RIGHT_EDGE){ 24. p0.y += (winmax.x-p0.x)*m; 25. p0.x = winmax.x; 26. else if(code0 & BOTTOM_EDGE){ 27. if(p1.x!= p0.x)/* None-vert.lines*/ 28. p0.x += (winmin.y-p0.y)/m; 29. p0.y = winmin.y; 30. else if(code0 TOP_EDGE){ 31. if(p1.x!= p0.x)/* None-vert.lines*/ 32. p0.x += (winmax.y-p0.y)/m; 33. p0.y = winmax.y; /* end_while */ 34. if(draw)line_dda_b(p0.x,p0.y,p1.x,p1.y); END OF PAPER