Download presentation
Presentation is loading. Please wait.
Published byDiane Summers Modified over 8 years ago
1
The Viewing Pipeline (Chapter 4) 5/26/20161
2
Overview OpenGL viewing pipeline: OpenGL viewing pipeline: – Modelview matrix – Projection matrix Parallel and perspective projections Parallel and perspective projections – Viewport matrix 5/26/20162
3
OpenGL Window-to- Viewport Transformation OpenGL will clip all primitives that lie outside the world window OpenGL will clip all primitives that lie outside the world window Primitives are displayed in a viewport of the screen window Primitives are displayed in a viewport of the screen window The window-to-viewport transformation transforms primitives from world coordinates to viewport coordinates The window-to-viewport transformation transforms primitives from world coordinates to viewport coordinates 5/26/20163
4
glTranslate*/glRotate*/glScale* glLoadMatrix*/glMultMatrix* gluLookAt gluOrtho2D glOrtho gluPerspective glFrustum glViewport OpenGL Viewing Pipeline The window-to-viewport transformation consists of a number of parts: the viewing pipeline: The window-to-viewport transformation consists of a number of parts: the viewing pipeline: 5/26/20164
5
The Modelview Matrix A 3-D to 3-D transformation A 3-D to 3-D transformation Combines effect of modelling transformations and viewpoint Combines effect of modelling transformations and viewpoint Modelling transformations: Modelling transformations: – What transformations are applied to each primitive to construct the scene? Viewing Trasformations: Viewing Trasformations: – Where are we viewing the scene from? 5/26/20165
6
The Modelview Matrix First we need to tell OpenGL that we want to specify 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: Then we can manipulate the current modelview matrix using: – glTranslate* – glRotate* – glScale* – glLoadMatrix* – glMultMatrix* – gluLookAt 5/26/20166
7
The Modelview Matrix gluLookAt(x0, y0, z0, xref, yref, zref, Vx, Vy, Vz) gluLookAt(x0, y0, z0, xref, yref, zref, Vx, Vy, Vz) – Specifies the position/direction/orientation of the ‘camera’ – Position of camera is (x0, y0, z0) in world coordinates – Camera points towards (xref, yref, zref) in world coordinates – The vector (Vx, Vy, Vz) specfies the ‘up’ direction for the camera 5/26/20167
8
The Modelview Matrix - Example glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(8,8,8,0,0,0,0,0,1); glutSolidTorus(1, 2.0, 10, 20); 5/26/20168
9
The Modelview Matrix - Example glMatrixMode(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 translated glRotatef(45,0,0,1);// and rotated glRecti(-30,-30,30,30); // 60 by 60 square at origin glFlush();// send all output to display 5/26/20169
10
The Modelview Matrix Stack - Example glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(200,200,0); // translate all rectangles … glPushMatrix();// save rotation glRotatef(45,0,0,1);//rotate this one glRecti(0,0,50,50);// draw rectangle glPopMatrix(); // restore rotation glPushMatrix(); // save rotation glTranslatef(100,0,0);// translate this one glRotatef(70,0,0,1); // and also rotate it glRecti(0,0,50,50); // draw rectangle glPopMatrix();// restore rotation glFlush(); // send all output to display 5/26/201610
11
The OpenGL Matrix Stacks For each matrix in the viewing pipeline, OpenGL maintains a matrix stack 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: Manipulating the matrix stack: – glPushMatrix() Current matrix copied to second position on stack Current matrix copied to second position on stack All other matrices on stack move down one place All other matrices on stack move down one place – glPopMatrix() Current matrix is destroyed Current matrix is destroyed All other matrices move up one position on stack All other matrices move up one position on stack 5/26/201611
12
Projection Matrices Projection matrices transform 3-D coordinates onto a 2-D image plane Projection matrices transform 3-D coordinates onto a 2-D image plane 2 basic types of projection: 2 basic types of projection: Perspective projectionParallel projection Image plane Projection lines converge to a point Projection lines are parallel 5/26/201612
13
Parallel Projections Objects further from viewer do not appear smaller Objects further from viewer do not appear smaller Common in CAD applications Common in CAD applications Parallel projections can be either: Parallel projections can be either: Oblique projection Image plane Projection lines parallel but not perpendicular Orthographic (or orthogonal) projection Image plane Projection lines are parallel and perpendicular to image plane 5/26/201613
14
Perspective Projections Projection lines converge at the centre of projection Projection lines converge at the centre of projection Image planeCentre of projection Objects that are further away appear smaller Objects that are further away appear smaller Perspective projections can be specified by: Perspective projections can be specified by: Centre of projection Centre of projection Camera direction (look vector) Camera direction (look vector) Camera orientation (‘up’ vector) Camera orientation (‘up’ vector) Height/width of image plane (viewing angles) Height/width of image plane (viewing angles) 5/26/201614
15
Perspective Projections View angle determines the degree of zoom View angle determines the degree of zoom Like a cameraman changing the camera lens 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: For a perspective projection we typically have two view angles: – Width angle – Height angle 5/26/201615
16
Perspective Projections Look vector determines the direction the camera is pointing Look vector determines the direction the camera is pointing – Can be any vector in 3-D space Up vector determines the orientation of the camera 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 coordinate system, e.g. Position specified in a right-handed coordinate system, e.g. Up vector Look vector Position 5/26/201616
17
Projections - Clipping Planes Most graphics packages will define near and far 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’ Volume of space between near and far clipping planes defines what the camera can ‘see’ Objects outside of clipping planes will not get drawn Objects outside of clipping planes will not get drawn Intersecting objects will be clipped Intersecting objects will be clipped 5/26/201617
18
Projections – View Volumes The view volume of a camera is a specification of a bounded space that the camera can ‘see’ The view volume of a camera is a specification of a bounded space that the camera can ‘see’ View volume can be specified by: View volume can be specified by: – Camera position, look vector, up vector, image plane aspect ratio, height angle, clipping planes (near/far) 5/26/201618
19
Projections – Perspective Projection View Volume Perspective projection view volume forms a frustum Perspective projection view volume forms a frustum 5/26/201619
20
Projections – Orthogonal Projection View Volume Orthographic projection view volume forms a cuboid Orthographic projection view volume forms a cuboid 5/26/201620
21
The Projection Matrix A 3-D to 2-D transformation: A 3-D to 2-D transformation: – Orthographic projection – Perspective projection In OpenGL: 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: Specifying the projection matrix: – gluOrtho2D – glOrtho – gluPerspective – glFrustum 5/26/201621
22
The Projection Matrix gluOrtho2D (xwmin, xwmax, ywmin, ywmax) gluOrtho2D (xwmin, xwmax, ywmin, ywmax) Orthographic projection Orthographic projection Intended for use with 2-D graphics Intended for use with 2-D graphics Defines a world window for clipping that will be mapped to the specified viewport Defines a world window for clipping that will be mapped to the specified viewport No near and far clipping planes No near and far clipping planes 5/26/201622
23
The Projection Matrix glOrtho (xwmin, xwmax, ywmin, ywmax, dnear, dfar) glOrtho (xwmin, xwmax, ywmin, ywmax, dnear, dfar) Orthographic projection Orthographic projection Intended for use with 3-D graphics Intended for use with 3-D graphics Defines a world window for clipping that will be mapped to the specified viewport Defines a world window for clipping that will be mapped to the specified viewport Can also specify near and far clipping planes: 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 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 Near clipping plane is the image plane 5/26/201623
24
The Projection Matrix gluPerspective (theta, aspect, dnear, dfar) gluPerspective (theta, aspect, dnear, dfar) Perspective projection Perspective projection theta is the height angle in degrees theta is the height angle in degrees aspect is the ratio of the width to the height of the image plane (the aspect ratio) 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 Centre of projection is the origin of the viewing coordinate system theta = height angle dnear, dfar are distances of clipping planes from centre of projection along z-axis dnear, dfar are distances of clipping planes from centre of projection along z-axis Image plane is the near clipping plane Image plane is the near clipping plane 5/26/201624
25
The Projection Matrix glFrustum (xwmin, xwmax, ywmin, ywmax, dnear, dfar) glFrustum (xwmin, xwmax, ywmin, ywmax, dnear, dfar) Perspective projection Perspective projection Corners of near clipping plane have the following coordinates in the viewing coordinate system: Corners of near clipping plane have the following coordinates in the viewing coordinate system: (xwmin, ywmin, -dnear) (xwmin, ywmin, -dnear) (xwmin, ywmax, -dnear) (xwmin, ywmax, -dnear) (xwmax, ywmin, -dnear) (xwmax, ywmin, -dnear) (xwmax, ywmax, -dnear) (xwmax, ywmax, -dnear) Height angle Centre of projection is the origin of the viewing coordinate system Centre of projection is the origin of the viewing coordinate system Image plane is the near clipping plane Image plane is the near clipping plane 5/26/201625
26
The Projection Matrix - Examples gluOrtho2D example: gluOrtho2D example: – All primitives with (x,y) coordinates between (0,0) and (640,480) will be displayed glOrtho example: 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 glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0.0, 640.0, 0.0, 480.0); 5/26/201626
27
The Viewport Matrix Determines the region of the screen window to be used for drawing Determines the region of the screen window to be used for drawing Specifying the viewport matrix: Specifying the viewport matrix: – glViewport(xPos, yPos, xSize, ySize) Viewport has bottom-left corner at (xPos,yPos), and has a size in pixels of (xSize,ySize) Viewport has bottom-left corner at (xPos,yPos), and has a size in pixels of (xSize,ySize) 5/26/201627
28
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 } 5/26/201628
29
Specifying the Screen Window The screen window is the window on your monitor that OpenGL uses to draw primitives The screen window is the window on your monitor that OpenGL uses to draw primitives Size and position of screen window specified by: Size and position of screen window specified by: – glutInitWindowSize(width,height) Width and height of window in pixels Width and height of window in pixels glutInitWindowPosition(x,y) glutInitWindowPosition(x,y) Position (x,y) in pixels from the top-left of the screen Position (x,y) in pixels from the top-left of the screen 5/26/201629
30
Summary The OpenGL viewing pipeline: 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 5/26/201630
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.