Graphics Programming For Games II Jan 2011
Graphics Programming For Games II Jan 2011
January 2011
Subject:
Stage:
Award
Date:
January 2011
Time allowed:
2 hours
INSTRUCTIONS Answer any THREE questions from four All questions carry equal marks There are Appendices at the end of the examination paper containing information which may be useful in answering questions
Page 1 of 14
January 2011
Question 1 (a) Explain how blending is achieved using OpenGl. Note that Appendix A gives an example of simple two-dimensional blending. (6 marks) (b) For three-dimensional blending we should implement the following sequence of events: Draw opaque objects first. Then make depth buffer read-only and draw translucent objects. (i) Explain why opaque objects must be drawn before transparent objects. (2 marks) (ii) What is the purpose of making the depth buffer read-only? (4 marks)
(c) Appendix B shows some code that uses a display list and shows the resulting display. (i) Using the appendix as an example, give a detailed explanation of OpenGL display lists and how they can be used improve performance (frame rate, for example). In your answer, explain why the drawing of the four green larger triangles will require much more overall processing effort than the triangles in the display list. Hints: immediate mode, retained mode; what gets sent to the graphics card and when? (9 marks) (ii) Change the code such that the display list contains the five triangles, i.e. no need for the for(i = 0; i < 5; i++) loop. (4 marks)
Page 2 of 14
January 2011
Question 2
(a) The figure above gives a visual depiction of the OpenGL graphics pipeline. Are shaders concerned with clipping? Explanation of your answer should make reference to the diagram. (3 marks) (b) List the steps taken to provide shaders in an OpenGL application program. (6 marks) (c) Give brief explanations for the follow types of variables in GLSL: varying, uniform. (4 marks) (d) Give the code for a pass-through vertex shader and a pass-through fragment shader. (6 marks) (e) Below is a vertex shader and an associated fragment shader. Explain what each shader does and what the overall effect is. (6 marks) vertex shader:
varying vec3 normal; void main() { normal = gl_NormalMatrix * gl_Normal; } gl_Position = ftransform();
Page 3 of 14
January 2011
fragment shader:
varying vec3 normal; void main() { float intensity; vec4 color; vec3 n = normalize(normal); intensity = dot(vec3(gl_LightSource[0].position),n); if (intensity > 0.95) color = vec4(1.0,0.5,0.5,1.0); else if (intensity > 0.5) color = vec4(0.6,0.3,0.3,1.0); else if (intensity > 0.25) color = vec4(0.4,0.2,0.2,1.0); else color = vec4(0.2,0.1,0.1,1.0); gl_FragColor = color; }
Page 4 of 14
January 2011
Question 3 In the context of 2-D texture mapping describe each of the following (note that each item is worth five marks, so descriptions should be relatively pointed and not unnecessarily verbose or elaborate): (a) Mip-mapping (b) Tri-linear filtering (c) Specular suppression (d) Texture objects (e) Texture co-ordinates (only discuss manual generation; mention wrapping modes)
Page 5 of 14
January 2011
Question 4 Note: Appendix D contains equations and geometry related to the Phong Lighting Model. You may find this useful. (a) Appendix C shows code which produces a sphere, cone and teapot with demonstrating lighting. Use it to explain OpenGLs simulation of lighting and material reflectivity. In your explanation, make sure to cover the essential differences between ambient, diffuse, and specular reflection; mention the roles of normals in OpenGL programs such as the one given; mention how OpenGL combines lighting contributions. (12 marks) (b) Write the few lines of code would make the SolidSphere (from part a) into a red ball and much less shiny than it is now, but not completely matte. (3 marks) (c) Given the code fragment below, and, considering ambient and diffuse only, explain what will be the colour (shade) at vertex vf[0]. Assume that the light direction is in the direction of the normal; no attenuation. You should carefully explain your calculations. Your answer should be an RGB triple. GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat l_amb[] l_dif[] l_spc[] m_spc[] m_spc[] m_shn[] m_amb[] m_dif[] = = = = = = = = { { { { { { { { 0.2, 0.3, 0.4, 0.5, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 5.0 }; 0.5, 0.5, 0.8, 0.8, 0.8, 0.9, 1.0, 1.0, 0.0, 1.0 1.0 1.0 1.0 1.0 }; }; }; }; };
GLfloat l_pos[] = { 20.0, 20.0, 20.0, 0.0 }; glLightfv (GL_LIGHT0, GL_POSITION, l_pos); glLightfv (GL_LIGHT0, GL_DIFFUSE, l_dif); glLightfv (GL_LIGHT0, GL_AMBIENT, l_amb); glLightfv (GL_LIGHT0, GL_SPECULAR, l_spc); glMaterialfv(GL_FRONT, glMaterialfv(GL_FRONT, glMaterialfv(GL_FRONT, glMaterialfv(GL_FRONT, glVertex3fv(vf[0]); (7 marks) (d) Now, consider only specular reflection and assume that the viewing direction (V) is in the same direction as the specular reflection GL_SPECULAR, m_spc); GL_SHININESS, m_shn); GL_AMBIENT, m_amb); GL_DIFFUSE, m_dif);
Page 6 of 14
January 2011
direction (R) so that V.R = 1, what will be the colour of the light reflected, given the following code? Answer with an RGB triple. GLfloat l_spc[] = { 1.0, 0.5, 0.25, 1.0 }; GLfloat m_spc[] = { 1.0, 1.0, 1.0, 1.0 }; GLfloat m_shn[] = { 2.0 }; (3 marks)
Page 7 of 14
January 2011
Page 8 of 14
January 2011
Page 9 of 14
January 2011
Page 10 of 14
January 2011
Page 11 of 14
January 2011
Contd
Page 12 of 14
January 2011
Appendix C - continued
Page 13 of 14
January 2011
Page 14 of 14