cd2012 principles of interactive graphics lecture -10
DESCRIPTION
CD2012 Principles of Interactive Graphics Lecture -10. A complete 3D scene Abir Hussain (JPB6.33, [email protected]). Previous lecture. Smooth and flat shading Light source in OpenGL: glLightfv(source, parameter, pointer_to_array); Specifications of light materials in OpenGL: - PowerPoint PPT PresentationTRANSCRIPT
CD2012 Principles of Interactive Graphics
Lecture -10
A complete 3D scene
Abir Hussain (JPB6.33, [email protected])
© JMU, 2003 CD2012-10 2
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);
© JMU, 2003 CD2012-10 3
Today’s Lecture and Lab
Interaction in 3dand controlling
features
Buffering
Saving andrestoring the
graphics context
Enabling/disabling features
Putting it alltogether: a
complete 3D scene
© JMU, 2003 CD2012-10 4
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
© JMU, 2003 CD2012-10 5
Introduction
© JMU, 2003 CD2012-10 6
Enabling/disabling features
• We need to enable and disable features in OpenGL as required
• For example– Lighting– Textures– Depth testing– Double buffering
© JMU, 2003 CD2012-10 7
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.
© JMU, 2003 CD2012-10 8
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.
© JMU, 2003 CD2012-10 9
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()).
© JMU, 2003 CD2012-10 10
Examples
• We might selectively switch off lights for part of the scene Here the second teapot is unlit.
© JMU, 2003 CD2012-10 11
Examples
• If we don’t disable texture mapping we may get unexpected results.
• Here the teapot is textured and the cubes are black
© JMU, 2003 CD2012-10 12
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.
© JMU, 2003 CD2012-10 13
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
© JMU, 2003 CD2012-10 14
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
© JMU, 2003 CD2012-10 15
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
© JMU, 2003 CD2012-10 16
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.
© JMU, 2003 CD2012-10 17
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. Current
matrix copy
Add Discard
© JMU, 2003 CD2012-10 18
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
© JMU, 2003 CD2012-10 19
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
© JMU, 2003 CD2012-10 20
Examples
1. case 't':// Change the colour of teapot
if ( toggle_teapot )toggle_teapot = 0;
elsetoggle_teapot = 1;
glutPostRedisplay();break;
© JMU, 2003 CD2012-10 21
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;
© JMU, 2003 CD2012-10 22
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;
© JMU, 2003 CD2012-10 23
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)
© JMU, 2003 CD2012-10 24
Summary
• A complete 3D program
• Enabling/Disabling features
• Double buffering
• Interactions in 3D and controlling features