300 likes | 390 Views
GR2 Advanced Computer Graphics AGR. Lecture 4 Viewing Pipeline Getting Started with OpenGL. world co-ordinates. modelling co-ordinates. MODELLING TRANSFORMATION. The Story So Far ...Lecture 2.
E N D
GR2Advanced Computer GraphicsAGR Lecture 4 Viewing Pipeline Getting Started with OpenGL
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
projection co-ordinates viewing co-ordinates PROJECTIONTRANSFORMATION The Story So Far...Lecture 3 • And we have seen how we can transform from a special viewing co-ordinate system (camera on z-axis pointing along the axis) into a projection co-ordinate system
viewing co-ordinates world co-ordinates VIEWINGTRANSFORMATION view’g co-ords proj’n co-ords mod’g co-ords world co-ords Completing the Pipeline - Lecture 4 • We now need to fill in the missing part to get
Viewing Coordinate System - View Reference Point yW P0 • In our world co-ordinate system, we need to specify a view reference point - this will become the origin of the view co-ordinate system • This can be any convenient point, along the camera direction from the camera position • indeed one possibility is the camera position itself xW zW
Viewing Coordinate System - View Plane Normal • Next we need to specify the view plane normal, N - this will give the camera direction, or z-axis direction • 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 to the ‘look at’ point from the view reference point yW P0 N xW zW yW P0 Q xW zW
Viewing Coordinate System - View Up Direction • Finally we need to specify the view-up direction, V - this will give the y-axis direction V yW P0 N xW zW
Viewing Co-ordinate System • This gives us a view reference point P0,and vectors N (corresponding to zV) and V (corresponding to yV) • We can construct a vector U perpendicular to both V and N, and this will correspond to the xV axis • How? V U yW P0 N xW zW
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 yW U N P0 (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: M = 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
view’g co-ords proj’n co-ords mod’g co-ords world co-ords Viewing Pipeline So Far • We now should understand this viewing pipeline
Clipping • Next we need to understand how the clipping to the view volume is performed • Recall that with perspective projection we defined a view frustum outside of which we wanted to clip points and lines, etc • The next slide is from lecture 3 ...
View Frustum - Perspective Projection back plane view frustum view window camera front plane zV
Clipping to View Frustum • It is quite easy to clip lines to the front and back planes (just clip in z).. • .. but it is difficult to clip to the sides because they are ‘sloping’ planes • Instead we carry out the projection first which converts the frustum to a rectangular parallelepiped (ie a cuboid)
back plane view volume view window front plane zV Clipping for Parallel Projection • In the parallel projection case, the viewing volume is already a rectangular parallelepiped
Normalized Projection Co-ordinates • Final step before clipping is to normalize the co-ordinates of the rectangular parallelepiped to some standard shape • for example, in some systems, it is the cube with limits +1 and -1 in each direction • This is just a scale transformation • Clipping is then carried out against this standard shape
view’g co-ords proj’n co-ords mod’g co-ords world co-ords normalized projection co-ordinates Viewing Pipeline So Far • Our pipeline now looks like: NORMALIZATIONTRANSFORMATION
And finally... • The last step is to position the picture on the display surface • This is done by a viewport transformation where the normalized projection co-ordinates are transformed to display co-ordinates, ie pixels on the screen
view’g co-ords proj’n co-ords mod’g co-ords world co-ords normalized projection co-ordinates Viewing Pipeline - The End • A final viewing pipeline is therefore: device co-ordinates DEVICETRANSFORMATION
Interlude • Why does a mirror reflect left-right and not up-down?
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
Viewing • 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
OpenGL Utility Library (GLU) • Useful set of higher level utility routines to make some tasks easier • written in terms of OpenGL and provided with the OpenGL implementation • for example, gluLookAt() is a way of specifying the viewing transformation • Described within the OpenGL Programming Guide • eg gluLookAt() is described in Chap 3, pp19-21
OpenGL Utility Toolkit (GLUT) • Set of routines to provide an interface to the underlying windowing system - plus many useful high-level primitives (even a teapot - glutSolidTeapot()!) • See Appendix D of OpenGL Guide • 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 GR2/AGR resources page: • http://www.scs.leeds.ac.uk/kwb/GR2/ resources.html • Points you to: • example programs • information about GLUT • information about OpenGL • a simple exercise