240 likes | 350 Views
CD2012 Principles of Interactive Graphics Lecture -10. A complete 3D scene Abir Hussain (JPB6.33, a.hussain@livjm.ac.uk). Previous lecture. Smooth and flat shading Light source in OpenGL: glLightfv(source, parameter, pointer_to_array); Specifications of light materials in OpenGL:
E N D
CD2012Principles of Interactive GraphicsLecture -10 A complete 3D scene Abir Hussain (JPB6.33, a.hussain@livjm.ac.uk)
Previous lecture • Smooth and flat shading • Light source in OpenGL: • glLightfv(source, parameter, pointer_to_array); • Specifications of light materials in OpenGL: • glMaterialfv(face, type,pointer_to_array); CD2012-10
Today’s Lecture and Lab CD2012-10
Introduction • In lecture 6, we talk about cues to the depth of a scene • Perspective • Hidden line and surface removal • 3D shapes and surfaces • Viewpoints • Lighting • Shading • Texture and Materials CD2012-10
Introduction CD2012-10
Enabling/disabling features • We need to enable and disable features in OpenGL as required • For example • Lighting • Textures • Depth testing • Double buffering CD2012-10
Enabling/disabling features • For each pixel on the screen, the depth buffer keeps track of the distance between the viewpoint and the object occupying that pixel. • The depth buffer is generally used for hidden surface elimination. • For example, if a new candidate colour for that pixel appears, it’s drawn only if the corresponding object is closer than the previous object. CD2012-10
Enabling/disabling features • We use double buffering for smooth animation • Depth testing and Double buffering can be turned off for a static 2D scene. • Double buffering is needed for animation. • Depth testing is needed for 3D scenes. CD2012-10
Examples • Strange effects can see through objects 1. (Might be useful in an engineering illustration!). • This effect is achieved by disabling depth testing (In Init()). CD2012-10
Examples • We might selectively switch off lights for part of the scene Here the second teapot is unlit. CD2012-10
Examples • If we don’t disable texture mapping we may get unexpected results. • Here the teapot is textured and the cubes are black CD2012-10
Double buffering • Setting double buffering with • glutInitDisplayMode (GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); • This function specifies how the display should be initialised. • The built in constant GLUT_DOUBLE and GLUT_RGB which are Ored together • Indicating that a double display buffer should be allocated • The colour are specified using desired amounts of red, green, and blue. CD2012-10
Double buffering • This enables the programmer to store old and new copies of the screen in video memory and swap between them for smooth motion • Without this you would see the scene being redrawn • Change GLUT_DOUBLE to GLUT_SINGLE to see the effect CD2012-10
Saving and restoring the graphics context • So far we have used glLoadIdentity() to restore the graphics context to its original state • We can also temporally store the current graphics context and then restore it, e.g. drawing 12 cubes in a circle CD2012-10
Saving and restoring the graphics context • The steps • glRotated(30,0.0,1.0,0.0); • glTranslated(3.0,0.0,0.0); • coloured_cube(red, 0.5); • glTranslated(-3.0,0.0,0.0); • draw a cube relative to a centre point where the last object (yellow teapot) was drawn CD2012-10
Drawing in local context • We use glPushMatrix/ glPopMatrix to save and restore the context. • The glPushMatrix pushes all the matrices in the current stack down one level. • The current stack is determined by glMatrixMode. • The topmost matrix is copied, so its contents are duplicated in both the top and second from the top matrix. CD2012-10
Drawing in local context • If too many matrices are pushed, an error is generated. • The glPopMatrix pops the top matrix off the stack, destroying the contents of the popped matrix. CD2012-10
Drawing in local context glPushMatrix(); glDisable(GL_TEXTURE_2D); glTranslated(-2.0, -0.25, -2.0); teapot(yellow, 0.5); for (i=0; i < 11; i++) { glRotated(30,0.0,1.0,0.0); glTranslated(3.0,0.0,0.0); coloured_cube(red, 0.5); glTranslated(-3.0,0.0,0.0); } glEnable(GL_TEXTURE_2D); glPopMatrix(); //return to initial location CD2012-10
Interactions in 3D and controlling features • In addition to setting features at application start-up we can also change them at run-time. • The example code show • Changing the colour of an object (pink/blue teapot) • Changing the value of a light’s intensity. • The intensity is set as different values of gray light • The example also shows traversing into and out of a scene. • Faster speeds are achieved by taking bigger steps for each key press CD2012-10
Examples 1. case 't':// Change the colour of teapot if ( toggle_teapot ) toggle_teapot = 0; else toggle_teapot = 1; glutPostRedisplay(); break; CD2012-10
Examples 2. case 'a': // Move forward slowly glTranslated(0.0,0.0, 0.05); glutPostRedisplay(); break; 3. case 'A': // Move forward more quickly glTranslated(0.0,0.0, 0.1); glutPostRedisplay(); break; CD2012-10
Examples 4. case '-': // Decrease light brightness new_light_colour = new_light_colour - 0.1; light_colour[0] = new_light_colour; light_colour[1] = new_light_colour; light_colour[2] = new_light_colour; glLightfv(GL_LIGHT0, GL_DIFFUSE, light_colour); glutPostRedisplay(); break; CD2012-10
Next week lecture Reminder • Demonstrate your 2nd coursework in the Lab (12th of December) • Hand in your documentation and code (12th of November) Next week lecture • Revision lecture (past exam questions) CD2012-10
Summary • A complete 3D program • Enabling/Disabling features • Double buffering • Interactions in 3D and controlling features CD2012-10