COMP462 Computer Graphics
The Viewing Pipeline
Ahmed Ali
Lesson Objectives
After completing this lesson, you will be able to: View a geometric model in any orientation by transforming it in three-dimensional space Control the location in three-dimensional space from which the model is viewed Clip undesired portions of the model out of the scene that's to be viewed Manipulate the appropriate matrix stacks that control model transformation for viewing and project the model onto the screen Combine multiple transformations to mimic sophisticated systems in motion, such as a solar system or an articulated robot arm
Introduction
The point of computer graphics is to create a two-
dimensional image of three-dimensional objects (it has to be two-dimensional because it's drawn on the screen), but you need to think in three-dimensional coordinates while making many of the decisions that determine what gets drawn on the screen. A common mistake people make when creating threedimensional graphics is to start thinking too soon that the final image appears on a flat, two-dimensional screen. Avoid thinking about which pixels need to be drawn, and instead try to visualize three-dimensional space. Create your models in some three-dimensional universe that lies deep inside your computer, and let the computer do its job of calculating which pixels to color. A series of three computer operations(the viewing pipeline) convert an object's three-dimensional coordinates to pixel positions on the screen
The Camera Analogy
The steps with a camera (or a computer) might be the following:
Setting up your camera support and pointing the camera
at the scene (viewing transformation). Arranging the scene to be photographed into the desired composition (modeling transformation) Choosing a camera lens or adjusting the zoom (projection transformation) Determining how large you want the final photograph to be - for example, you might want it enlarged (viewport transformation). After these steps are performed, the picture can be snapped, or the scene can be drawn.
OpenGL Window-toViewport Transformation
The window-to-viewport transformation transforms primitives from world coordinates to viewport coordinates
OpenGL will clip all primitives that lie outside the world
window Primitives are displayed in a viewport of the screen window
OpenGL Viewing Pipeline (Stages of Vertex Transformation)
The window-to-viewport transformation consists of a number of parts:
gluOrtho2D glTranslate*/glRotate*/glScale* glOrtho glLoadMatrix*/glMultMatrix* gluPerspective gluLookAt glViewport glFrustum
OpenGL Viewing Pipeline (Stages of Vertex Transformation)
To specify viewing, modeling, and projection
transformations, you construct a 4 4 matrix M, which is then multiplied by the coordinates of each vertex v in the scene to accomplish the transformation v=Mv Remember that vertices always have four coordinates (x ,y, z, w), though in most cases w is 1 and for two-dimensional data z is 0.
The Modelview Matrix
A 3-D to 3-D transformation Combines effect of modelling transformations and viewpoint Modelling transformations: What transformations are applied to each primitive to construct the
scene? Used to position and orient the model. you can rotate, translate, or scale the model - or perform some combination of these operations.
Where are we viewing the scene from? analogous to positioning and aiming a camera.
Viewing Transformations:
Several Ways to accomplish
Using glTranslatef() and glRotatef() Using the Utility Library routine gluLookAt() to define a line of sight. This routine
encapsulates a series of rotation and translation commands. Creating your own utility routine
Note that instead of pulling the camera back away from the cube (with a viewing transformation) so that it could be viewed, you could have moved the cube away from the camera (with a modeling transformation).
you need to think about the effect of both types of transformations simultaneously. This is also why modeling and viewing transformations are combined into the
The Modelview Matrix
First we need to tell OpenGL that we want to
specify the modelview matrix:
glMatrixMode(GL_MODELVIEW)
Then we can manipulate the current modelview
matrix using:
glTranslate* glRotate* glScale* glLoadMatrix* glMultMatrix* gluLookAt
The Modelview Matrix
Positioning the Camera
gluLookAt(eyex, eyey, eyez, lookx, looky, lookz, upx, upy, upz);
Specifies the position/direction/orientation of the camera - Position of camera is (eyex, eyey, eyez) in world coordinates - Camera points towards (centerx, centery, centerz) in world coordinates - The vector (upx, upy, upz) specfies the up direction for the camera
This is equivalent to: glTranslatef(-eyex, -eyey, eyez); glRotatef(theta, 1.0, 0.0, 0.0); glRotatef(phi, 0.0, 1.0, 0.0);
The Modelview Matrix Example
glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(8,8,8,0,0,0,0,0,1);
glutSolidTorus(1, 2.0, 10, 20);
The Modelview Matrix Example
glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslated(300.0, 200.0, 0.0);
glRecti(-30,-30,30,30); // translate all primitives to this point // 60 by 60 square at origin
glTranslatef(100,0,0); glRotatef(45,0,0,1); glRecti(-30,-30,30,30);
glFlush();
// this square will be translated // and rotated // 60 by 60 square at origin
// send all output to display
The OpenGL Matrix Stacks
For each matrix in the viewing pipeline, OpenGL
maintains a matrix stack
The current matrix is the one on top of the stack
Manipulating the matrix stack: glPushMatrix()
Current matrix copied to second position on stack All other matrices on stack move down one place
glPopMatrix() Current matrix is destroyed All other matrices move up one position on stack
The Modelview Matrix Stack - Example
glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(200,200,0); // translate all rectangles glPushMatrix(); glRotatef(45,0,0,1); glRecti(0,0,50,50); glPopMatrix(); glPushMatrix(); glTranslatef(100,0,0); glRotatef(70,0,0,1); glRecti(0,0,50,50); glPopMatrix(); glFlush(); // save rotation //rotate this one // draw rectangle // restore rotation // // // // // save rotation translate this one and also rotate it draw rectangle restore rotation
// send all output to display
Projection Transformation
Projection matrices transform 3-D coordinates
onto a 2-D image plane It defines a viewing volume, that is used in two ways.
1.
2.
The viewing volume determines how an object is projected onto the screen (that is, by using a perspective or an orthographic projection), It defines which objects or portions of objects are clipped out of the final image.
Parallel projection
Projection lines are parallel
2 basic types of projection:
Perspective projection
Projection lines converge to a point
Image plane
Image plane
Parallel Projections
Objects further from viewer do not appear smaller
Common in CAD applications Parallel projections can be either:
Orthographic (or orthogonal) projection Oblique projection
Image plane
Image plane
Projection lines are parallel and perpendicular to image plane
Projection lines parallel but not perpendicular
Perspective Projections
commonly used for animation, visual simulation, and any other
applications that strive for some degree of realism because it's similar to how our eye (or a camera) works. Projection lines converge at the centre of projection
Centre of projection
Image plane
Objects that are further away appear smaller Perspective projections can be specified by:
Centre of projection Camera direction (look vector) Camera orientation (up vector) Height/width of image plane (viewing angles)
Perspective Projections
View angle determines the
degree of zoom Like a cameraman changing the camera lens
Narrow angle: high zoom, little
depth effect Wide angle: low zoom, perspective distortion
For a perspective projection
we typically have two view angles:
Width angle Height angle
Perspective Projections
Look vector determines the direction the
camera is pointing
Can be any vector in 3-D space
Up vector Look vector
Up vector determines the orientation of the
camera
E.g. are we holding the camera
horizontally/vertically/in between?
Position specified in a right-handed
Position
coordinate system, e.g.
Projections - Clipping Planes
Most graphics packages will define near and far clipping
planes Volume of space between near and far clipping planes defines what the camera can see
Objects outside of clipping planes will not get drawn Intersecting objects will be clipped
Projections View Volumes
The view volume of a camera is a specification
of a bounded space that the camera can see View volume can be specified by:
Camera position, look vector, up vector, image plane
aspect ratio, height angle, clipping planes (near/far)
Projections Perspective Projection View Volume
Perspective projection view volume forms a
frustum
Projections Orthogonal Projection View Volume
Orthographic projection view volume forms a
cuboid
The Projection Matrix
A 3-D to 2-D transformation: Orthographic projection Perspective projection In OpenGL: The centre of projection is the origin of the viewing coordinate system (i.e. after the modelview matrix) The view direction (look vector) is the negative z-axis The up (up vector) is the positive y-axis Specifying the projection matrix: Orthographic projection
gluOrtho2D glOrtho
Perspective projection
gluPerspective glFrustum
The Projection Matrix
gluOrtho2D (xwmin, xwmax, ywmin, ywmax)
Orthographic projection Intended for use with 2-D graphics Defines a world window for clipping that will be mapped to the specified viewport No near and far clipping planes
The Projection Matrix
glOrtho (xwmin, xwmax, ywmin, ywmax, dnear, dfar)
Orthographic projection Intended for use with 3-D graphics Defines a world window for clipping that will be mapped to the specified viewport Can also specify near and far clipping planes:
dnear and dfar are the distances of the clipping planes from the viewing coordinate origin along the z-axis
Near clipping plane is the image plane
The Projection Matrix
glFrustum (xwmin, xwmax, ywmin, ywmax, dnear, dfar)
Perspective projection Corners of near clipping plane have the following coordinates in the viewing coordinate system:
(xwmin, ywmin, -dnear) (xwmin, ywmax, -dnear) (xwmax, ywmin, -dnear) (xwmax, ywmax, -dnear)
The viewing direction is always parallel to -z
Height angle
Centre of projection is the origin of the viewing coordinate system Image plane is the near clipping plane
The Projection Matrix
gluPerspective (theta, aspect, dnear, dfar) Perspective projection Similar to glFrustum(), but you specify it in a different way. Rather than specifying corners of the near clipping plane, you specify the angle of the field of view in the x-z plane and the aspect ratio of the width to height (x/y). (For a square portion of the screen, the aspect ratio is 1.0.) theta is the height angle in degrees(must be in the range [0180]) aspect is the ratio of the width to the height of the image plane (the aspect ratio) Centre of projection is the origin of the viewing coordinate system
dnear, dfar are distances of clipping planes from centre of projection along z-axis Image plane is the near clipping plane h=[Link] theta 2 w=h.a
gluPerspective(theta,a,n ,f)
theta = height angle
Perspective Projections
Near Image Plane
Far Image Plane
The Projection Matrix Examples example: gluOrtho2D
All primitives with (x,y) coordinates between (0,0)
and (640,480) will be displayed
glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0.0, 640.0, 0.0, 480.0);
glOrtho example:
Defines a 24x24x30 view volume with the image
plane at the origin:
glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-12, 12, -12, 12, 0, 30); // left, right, bottom, top, near, far
The Viewport Matrix
Determines the region of the screen window to be
used for drawing After projection to the window, all points are transformed to normalised device co-ordinates: glViewport(xPos, yPos, xSize, ySize) Viewport has bottom-left corner at (xPos,yPos), and has a size in pixels of (xSize,ySize) Used to relate the co-ordinate systems: Normally we re-create the window after a window resize event to ensure a correct mapping between window and viewport dimensions:
The Viewport Matrix Example
void drawObjects() { glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glutWireTorus(1, 2.0, 10, 20); glTranslated(7.0,0.0,0.0); glutWireTeapot(3); } /* GLUT callback Handlers */ void display() { glClear(GL_COLOR_BUFFER_BIT); glViewport(0, 0, 320, 240); drawObjects(); glViewport(320, 0, 320, 240); drawObjects(); glViewport(0, 240, 320, 240); drawObjects(); glViewport(320, 240, 320, 240); drawObjects(); glFlush(); // send all output to the display }
Specifying the Screen Window
The screen window is the window on
your monitor that OpenGL uses to draw primitives Size and position of screen window specified by:
glutInitWindowSize(width,height) Width and height of window in pixels
glutInitWindowPosition(x,y)
Position (x,y) in pixels from the top-left of the screen
Summary
The OpenGL viewing pipeline: A series of matrix transformations that all primitives undergo Modelview matrix: combines modelling and viewing transforms Projection matrix: projects 3-D viewing coordinates onto image plane Viewport matrix: selects the part of the display window to use for drawing