320 likes | 401 Views
SI31 Advanced Computer Graphics AGR. Lecture 3 Viewing Transformation Getting Started with OpenGL Introduction to Projections. Viewing Transformation. world co-ordinates. modelling co-ordinates. MODELLING TRANSFORMATION. The Story So Far...Lecture 2.
E N D
SI31Advanced Computer GraphicsAGR Lecture 3 Viewing Transformation Getting Started with OpenGL Introduction to Projections
world co-ordinates modelling co-ordinates MODELLINGTRANSFORMATION The Story So Far...Lecture 2 • We have seen how we can model objects, by transforming them from their local co-ordinate representation into a world co-ordinate system
Viewing • Graphics display devices are 2D rectangular screens • Hence we need to understand how to transform our 3D world to a 2D surface • This involves: • selecting the observer position (or camera position) • selecting the view plane (or camera film plane) • selecting the type of projection
Viewing Co-ordinate System - View Reference Point yW • In our world co-ordinate system, we need to specify where the camera is • We call this the view reference point... • … and transform the world so that camera is at the origin • We call this the view co-ordinate system P0 xW zW
Viewing Co-ordinate System - View Plane Normal • Next we need to specify the direction in which the camera is pointing - the normal from the view plane to camera is the view plane normal, N • Some graphics systems require you to specify N ... • ... others (including OpenGL) allow you to specify a ‘look at’ point, Q, from which N is calculated as direction from the ‘look at’ point to the view reference point N yW P0 xW zW Q yW P0 xW zW
Viewing Co-ordinate System - View Up Direction • Next we need to specify the ‘up-direction’ of the camera - this is known as the upward direction, UP • Often we take UP=(0,1,0), the positive y-axis • Both N and UP are normalised - ie unit vectors UP yW N P0 xW zW
Viewing Co-ordinate System • This gives us a view reference point P0, and vectors N (view plane normal) and UP (upwards direction) • We now construct a right-handed viewing co-ordinate system as follows: • construct U = UPxN to create a vector orthogonal to N and to UP • construct V = N x U to complete the viewing co-ordinate system: U, V, N V UP V U yW N P0 xW zW
Interlude: Today’s Puzzle • Why does a mirror reflect left-right and not up-down?
Transformation from World to Viewing Co-ordinates • Given an object with positions defined in world co-ordinates, we need to calculate the transformation to viewing co-ordinates • The view reference point must be transformed to the origin, and lines along the U, V, N directions must be transformed to lie along the x, y, z directions
T = 1 0 0 -x0 0 1 0 -y0 0 0 1 -z0 0 0 0 1 Transformation from World to Viewing Co-ordinates • Translate so that P0 lies at the origin V U yW P0 N (x0, y0, z0) xW zW - apply translation by (-x0, -y0, -z0)
Transformation from World to Viewing Co-ordinates • Apply rotations so that the U, V and N axes are aligned with the xW, yW and zW directions • This involves three rotations Rx, then Ry, then Rz • first rotate around xW to bring N into the xW-zW plane • second, rotate around yW to align N with zW • third, rotate around zW to align V with yW • Composite rotation R = Rz. Ry. Rx
Rotation Matrix • Fortunately there is an easy way to calculate R, from U, V and N: R = u1 u2 u3 0 v1 v2 v3 0 n1 n2 n3 0 0 0 0 1 where U = (u1 u2 u3 )T etc
Viewing Transformation • Thus the viewing transformation is: ViewMatrix = R T • This transforms object positions in world co-ordinates to positions in the viewing co-ordinate system.. .. with camera pointing along negative z-axis at a view plane parallel to x-y plane • We can then apply the projection transformation - to be described later
Exercises • Convince yourself that the rotation matrix on ‘Rotation Matrix’ slide will have the required effect • Build the view matrix that results from: • camera at (1,2,3) • looking at (3,2,1) • with up direction (0,1,0)
What is OpenGL? • OpenGL provides a set of routines (API) for advanced 3D graphics • derived from Silicon Graphics GL • acknowledged industry standard, even on PCs (OpenGL graphics cards available) • integrates 3D drawing into X (and other window systems such as Windows NT) • draws simple primitives (points, lines, polygons) but NOT complex primitives such as spheres • provides control over transformations, lighting, etc • Mesa is publically available equivalent
Geometric Primitives • Defined by a group of vertices - for example to draw a triangle: glBegin (GL_POLYGON); glVertex3i (0, 0, 0); glVertex3i (0, 1, 0); glVertex3i (1, 0, 1); glEnd(); • See Chapter 2 of the OpenGL Programming Guide
Modelling, Viewing and Projection • OpenGL maintains two matrix transformation modes • MODELVIEW to specify modelling transformations, and transformations to align camera • PROJECTION to specify the type of projection (parallel or perspective) and clipping planes • See Chapter 3 of OpenGL Programming Guide
Modelling • For modelling… set the matrix mode, and create the transformation... • Thus to set a scaling on each axis... glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glScaled(sx,sy,sz);
Viewing • For viewing, use gluLookAt()to create a view transformation matrix gluLookAt(eyex,eyey,eyez, lookx,looky,lookz, upx,upy,upz) • Thus glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glScaled(sx,sy,sz); gluLookAt(eyex,eyey,eyez, lookx,looky,lookz, upx,upy,upz); creates a model-view matrix
OpenGL Utility Library (GLU)OpenGL Utility Toolkit (GLUT) • GLU: • useful set of utility routines written in terms of OpenGL … such as gluLookAt() • GLUT (Appendix D of OpenGL Guide): • Set of routines to provide an interface to the underlying windowing system - plus many useful high-level primitives (even a teapot - glutSolidTeapot()!) • Allows you to write ‘event driven’ applications • you specify call back functions which are executed when an event (eg window resize) occurs
How to Get Started • Look at the SI31/AGR resources page: • http://www.comp.leeds.ac.uk/kwb/si31/ resources.html • Points you to: • example programs • information about GLUT • information about OpenGL • information about Mesa 3D • a simple exercise
viewing co-ords Projection Transform’n mod’g co-ords world co-ords Viewing Pipeline So Far • We now should understand the viewing pipeline Viewing Transform’n Modelling Transform’n The next stage is the projection transformation….
For further reading see: www.illusionworks.com/ html/ames_room.html www.psych.uleth.ca/People/Douglas/Winter99/01/ Ames.html Ames Room
Perspective Projections • There are two types of projection: perspective and parallel • In a perspective projection, object positions are projected onto the view plane along lines which converge at the observer P1 P1’ camera P2 P2’ view plane
Parallel Projection • In a parallel projection, the observer position is at an infinite distance, so the projection lines are parallel P1 P2 view plane
Perspective and Parallel Projection • Parallel projection preserves the relative proportions of objects, but does not give a realistic view • Perspective projection gives realistic views, but does not preserve proportions • Projections of distant objects are smaller than projections of objects of the same size which are closer to the view plane
Perspective and Parallel Projection parallel perspective