viewing pipeline
TRANSCRIPT
-
8/2/2019 Viewing Pipeline
1/34
COMP462 Computer Graphics
The Viewing Pipeline
Ahmed Ali
-
8/2/2019 Viewing Pipeline
2/34
Lesson Objectives
After completing this lesson, you will be able to:
View a geometric model in any orientation bytransforming it in three-dimensional space
Control the location in three-dimensional space fromwhich the model is viewed
Clip undesired portions of the model out of the scenethat's to be viewed
Manipulate the appropriate matrix stacks that controlmodel transformation for viewing and project the modelonto the screen
Combine multiple transformations to mimicsophisticated systems in motion, such as a solar system
or an articulated robot arm
-
8/2/2019 Viewing Pipeline
3/34
Introduction The point of computer graphics is to create a two-
dimensional image of three-dimensional objects (it has tobe two-dimensional because it's drawn on the screen), butyou need to think in three-dimensional coordinates whilemaking many of the decisions that determine what getsdrawn on the screen.
A common mistake people make when creating three-dimensional graphics is to start thinking too soon that thefinal image appears on a flat, two-dimensional screen.
Avoid thinking about which pixels need to be drawn, andinstead try to visualize three-dimensional space. Create
your models in some three-dimensional universe that liesdeep inside your computer, and let the computer do its jobof calculating which pixels to color.
A series of three computer operations(the viewingpipeline)convert an object's three-dimensional coordinatesto pixel positions on the screen
-
8/2/2019 Viewing Pipeline
4/34
The Camera Analogy
The steps with a camera (or a computer) might be thefollowing:
Setting up your camera support and pointing the cameraat the scene (viewing transformation).
Arranging the scene to be photographed into the desiredcomposition (modeling transformation)
Choosing a camera lens or adjusting the zoom(projection transformation)
Determining how large you want the final photograph tobe - for example, you might want it enlarged (viewporttransformation).
After these steps are performed, the picture can be
snapped, or the scene can be drawn.
-
8/2/2019 Viewing Pipeline
5/34
OpenGL Window-to-
Viewport Transformation
OpenGL will clip all primitives that lie outside the worldwindow
Primitives are displayed in a viewport of the screen
window
The window-to-viewport transformation transformsprimitives from world coordinates to viewport coordinates
-
8/2/2019 Viewing Pipeline
6/34
OpenGL Viewing Pipeline(Stages of Vertex Transformation)
glTranslate*/glRotate*/glScale*glLoadMatrix*/glMultMatrix*gluLookAt
gluOrtho2DglOrthogluPerspective
glFrustum
glViewport
The window-to-viewport transformation consists of a number of parts:
-
8/2/2019 Viewing Pipeline
7/34
To specify viewing, modeling, and projectiontransformations, you construct a 4 4 matrixM, which is then multiplied by the coordinatesof each vertex vin the scene to accomplishthe transformation v=Mv
Remember that vertices always have fourcoordinates (x ,y, z, w), though in most cases
wis 1 and for two-dimensional data zis 0.
OpenGL Viewing Pipeline(Stages of Vertex Transformation)
-
8/2/2019 Viewing Pipeline
8/34
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 somecombination of these operations. Viewing Transformations:Where are we viewing the scene from? analogous to positioning and aiming a camera.Several Ways to accomplish Using glTranslatef() and glRotatef()
Using the Utility Library routine gluLookAt() to define a line of sight. This routineencapsulates 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 aviewing transformation) so that it could be viewed, you could havemoved the cube away from the camera (with a modelingtransformation). 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
A
-
8/2/2019 Viewing Pipeline
9/34
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 modelviewmatrix using: glTranslate* glRotate*
glScale* glLoadMatrix* glMultMatrix* gluLookAt
B
-
8/2/2019 Viewing Pipeline
10/34
The Modelview MatrixPositioning 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 forthe camera
C
glTranslatef(-eyex, -eyey, -eyez);glRotatef(theta, 1.0, 0.0,0.0);glRotatef(phi, 0.0, 1.0, 0.0);
This is equivalentto:
-
8/2/2019 Viewing Pipeline
11/34
The Modelview Matrix -
ExampleglMatrixMode(GL_MODELVIEW);glLoadIdentity();gluLookAt(8,8,8,0,0,0,0,0,1);
glutSolidTorus(1, 2.0, 10, 20);
A
-
8/2/2019 Viewing Pipeline
12/34
The Modelview Matrix -
ExampleglMatrixMode(GL_MODELVIEW);glLoadIdentity();glTranslated(300.0, 200.0, 0.0); // translate all primitives to this point
glRecti(-30,-30,30,30); // 60 by 60 square at origin
glTranslatef(100,0,0); // this square will be translatedglRotatef(45,0,0,1); // and rotatedglRecti(-30,-30,30,30); // 60 by 60 square at origin
glFlush(); // send all output to display
B
-
8/2/2019 Viewing Pipeline
13/34
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
-
8/2/2019 Viewing Pipeline
14/34
The Modelview Matrix
Stack - ExampleglMatrixMode(GL_MODELVIEW);glLoadIdentity();glTranslatef(200,200,0); // translate all rectangles
glPushMatrix(); // save rotationglRotatef(45,0,0,1); //rotate this oneglRecti(0,0,50,50); // draw rectangle
glPopMatrix(); // restore rotation
glPushMatrix(); // save rotation
glTranslatef(100,0,0); // translate this oneglRotatef(70,0,0,1); // and also rotate itglRecti(0,0,50,50); // draw rectangle
glPopMatrix(); // restore rotation
glFlush(); // send all output to display
-
8/2/2019 Viewing Pipeline
15/34
Projection Transformation
Projection matrices transform 3-D coordinatesonto a 2-D image plane
It defines a viewing volume, that is used intwo ways.1. The viewing volume determines how an
object is projected onto the screen (that is,by using a perspective or anorthographic projection),
2. It defines which objects or portions ofobjects are clipped out of the final image.
2 basic types of projection:Perspective projection Parallel projection
Image planeImage plane
Projectionlinesconverge to apoint
Projectionlines areparallel
-
8/2/2019 Viewing Pipeline
16/34
Parallel Projections
Objects further from viewer do not appear smaller
Common in CAD applications
Parallel projections can be either:
Oblique projection
Image plane
Projection lines parallelbut not perpendicular
Orthographic (or orthogonal) projection
Image plane
Projection lines are parallel andperpendicular to image plane
-
8/2/2019 Viewing Pipeline
17/34
Perspective Projections
commonly used for animation, visual simulation, and any otherapplications that strive for some degree of realism because it'ssimilar to how our eye (or a camera) works.
Projection lines converge at the centre of projection
Imageplane
Centre ofprojection
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)
A
B
-
8/2/2019 Viewing Pipeline
18/34
Perspective Projections
View angle determines thedegree 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 projectionwe typically have two viewangles: Width angle
Height angle
B
C
-
8/2/2019 Viewing Pipeline
19/34
Perspective Projections
Look vectordetermines the direction thecamera is pointing Can be any vector in 3-D space
Up vector determines the orientation of thecamera E.g. are we holding the camera
horizontally/vertically/in between?
Position specified in a right-handedcoordinate system, e.g.
Upvector
Lookvector
Position
C
-
8/2/2019 Viewing Pipeline
20/34
Projections - Clipping Planes Most graphics packages will define near and far clipping
planes
Volume of space between near and far clipping planesdefines what the camera can see
Objects outside ofclipping planes will
not get drawn Intersecting objects
will be clipped
-
8/2/2019 Viewing Pipeline
21/34
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 planeaspect ratio, height angle, clipping planes (near/far)
-
8/2/2019 Viewing Pipeline
22/34
Projections Perspective
Projection View Volume Perspective projection view volume forms a
frustum
-
8/2/2019 Viewing Pipeline
23/34
Projections Orthogonal Projection
View Volume Orthographic projection view volume forms a
cuboid
A
-
8/2/2019 Viewing Pipeline
24/34
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 viewingcoordinate 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
A
B
-
8/2/2019 Viewing Pipeline
25/34
The Projection Matrix gluOrtho2D (xwmin, xwmax, ywmin, ywmax)
B
Orthographic projection
Intended for use with 2-D graphics
Defines a world window for clippingthat will be mapped to the specifiedviewport
No near and far clipping planes
C
-
8/2/2019 Viewing Pipeline
26/34
The Projection Matrix
C
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 specifiedviewport
Can also specify near and far clipping planes: dnearanddfarare the distances of the clipping planes from
the viewing coordinate origin along the z-axis
Near clipping plane is the image plane
D
-
8/2/2019 Viewing Pipeline
27/34
The Projection Matrix
D
glFrustum (xwmin, xwmax, ywmin, ywmax, dnear, dfar)
Perspective projection
Corners ofnear clipping plane have the followingcoordinates in the viewing coordinate system:
(xwmin, ywmin, -dnear)
(xwmin, ywmax, -dnear) (xwmax, ywmin, -dnear)
(xwmax, ywmax, -dnear)
The viewing direction is always parallel to -zHeightangle
Centre of projection is the origin of the
viewing coordinate system Image plane is the near clipping plane
-
8/2/2019 Viewing Pipeline
28/34
The Projection Matrix
E
gluPerspective (theta, aspect, dnear, dfar)
Perspective projection
Similar to glFrustum(), but you specify it in a different way. Rather thanspecifying corners of the near clipping plane, you specify the angle of the field ofview in the x-zplane and the aspect ratio of the width to height (x/y). (For asquare portion of the screen, the aspect ratio is 1.0.)
theta is the height anglein 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
theta=heightangle
dnear, dfarare distances ofclipping planes from centre of
projection along z-axis Image plane is the near
clipping plane
gluPerspective(theta,a,n,f) h=n.tan theta
2
w=h.a
-
8/2/2019 Viewing Pipeline
29/34
Perspective Projections
Near Image
PlaneFar Image Plane
-
8/2/2019 Viewing Pipeline
30/34
The Projection Matrix -Examples
gluOrtho2D example: All primitives with (x,y) coordinates between (0,0)
and (640,480) will be displayed
glOrtho example:
Defines a 24x24x30view volume with the imageplane at the origin:
glMatrixMode(GL_PROJECTION);glLoadIdentity();glOrtho(-12, 12, -12, 12, 0, 30); // left, right, bottom, top, near, far
glMatrixMode(GL_PROJECTION);glLoadIdentity();
gluOrtho2D(0.0, 640.0, 0.0, 480.0);
-
8/2/2019 Viewing Pipeline
31/34
The Viewport Matrix Determines the region of the screenwindow to be
used for drawing
After projection to the window, all points aretransformed 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 windowresize event to ensure a correct mapping betweenwindow and viewport dimensions:
-
8/2/2019 Viewing Pipeline
32/34
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
}
-
8/2/2019 Viewing Pipeline
33/34
Specifying the ScreenWindow
The screen window is the window onyour monitor that OpenGL uses todraw primitives
Size and position of screen windowspecified 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
-
8/2/2019 Viewing Pipeline
34/34
Summary
The OpenGL viewing pipeline: A series of matrix transformations that all primitives
undergo
Modelview matrix: combines modelling and viewingtransforms
Projection matrix: projects 3-D viewing coordinatesonto image plane
Viewport matrix: selects the part of the display windowto use for drawing