13D ViewingCamera TransformationsCS148: Intro to CGInstructor: Dan MorrisTA: Sean WalkerJuly 7, 2005Outline for today{ Overview: 3D Æ 2D{ Viewing{ Video break{ Projection{ The depth bufferOpenGL in a nutshell{ We can build primitives from vertices{ We can build objects from primitives{ We usually model objects in a “convenient coordinate frame”z E.g. drawSqare(), glutSolidSphere()OpenGL magicpixelsobject coordinatesWhat have we done so far?{ We know how to take all points in our object and put them in a global frame{ Objects may have their own reference frames, but using GL transformations, every vertex ends up in the global framemodelingtransformationworld coordinatesobject coordinatesWhat have we done so far?{ We know how to turn primitives into pixels if we have their pixel locationsrasterizationpixelswindow coordinatesWorking backwards{ We learned (almost) how to convert OpenGL coordinates into window coordinates:glViewport(x,y,w,h);{ What does glViewport() really do?{ From the documentation:Specifies the affine transformation of x and y from normalized device coordinates to window coordinates.{ What the $*%^ are normalized device coordinates?2Normalized Device Coordinates{ On any computer in the world, with any window size, OpenGL maps the range[-1,1] to the window{ At the end of all my transformations:z (0,0) will be the center of the window,z (1,1) will be at the upper-right{ If I want a vertex to show up at the center of the screen, my transformations had better transform it to (0,0,z).The Viewport Transformation{ We already know how to turn NDC into window coordinates{ Just call glViewPort()What is the transformation set up by glViewport(x,y,w,h)?viewporttransformationwindow coordinates11-1-1normalized device coordinatesWhat’s left?modelingtransformationworld coordinatesobject coordinatesrasterizationpixelsviewporttransformationwindow coordinates11-1-1normalized device coordinates?Outline for today{ Overview: 3D Æ 2D{ Viewing{ Video break{ Projection{ The depth bufferViewing{ I have a world with objects in it{ Useful to think of a virtual camera somewhere in that world, looking at whatever I want the user to seeEye Coordinates{ …but GL doesn’t have a setCameraPosition() function*{ The OpenGL camera is always sitting at the origin and looking straight down the –z axis, with y pointed up{ We’ll call this coordinate system “eye coordinates”3The Viewing Transformation{ In order to define our own camera, we’ll apply a transformation that moves everything into view{ In other words, we’ll translate our world coordinates into eye coordinates{ This is the “viewing transformation”, and it’s equivalent to placing our virtual camera in our virtual worldThe ModelView Matrix Revisited{ To do this in OpenGL, the very first transform to go onto our modelviewstack will be the viewing transform{ When you fire up project 2, the current coordinate frame is already world coordinates, because we defined a viewing transform for you{ The modelview matrix encompasses the modeling and viewing transformationsDefining a camera{ Let’s assume we want the camera to bez Sitting at point E (eye) in world coordsz Looking at point L (look) in world coords{ Does this define a unique coordinate system?{ Also need an ‘up vector’ that tells us how the camera is oriented around its “look axis”Building a viewing matrix{ So let’s think of our job as implementing the function:matrix buildViewMatrix(point E,point L,vector up);E, L, and up are in the world frame…How do we build a viewing matrix?{ The viewing matrix maps points in the world frame to points in the camera frame{ Two steps:z Apply a translation to put their origins at the same placez Apply a rotation to make their axes line upTranslation First{ We’re good at translation…What translation do I apply to put the camera at point (ex, ey, ez)?{ To “move the camera backward”, we would move the whole world forward…⎥⎥⎥⎥⎦⎤⎢⎢⎢⎢⎣⎡−−−1000100010001ezeyexOpenGL, please translate all future vertices by(-ex,-ey,-ez)4Now rotation{ Now the current origin is at the camera frame origin, but I need to align the axes…Now rotationWhat vector in the world frame should match the zaxis in the camera frame?What vector in the world frame should match the y axis in the camera frame?What vector in the world frame should match the x axis in the camera frame?matrix buildViewMatrix(point E,point L,vector up);The magic vectors{ The z axis: E - Lz The camera always looks down its -z axis, and we want to look from E to L{ The y axis: upz The camera is always oriented with y pointing up{ The x axis: up x (E-L){ All three vectors are perpendicular…The secrets of rotation matrices{ Rotation matrices have some amazing properties…z All columns are unit vectors!z The columns of a rotation matrix define the vectors to which it rotates the coordinate axes!z RT= R-1⎥⎥⎥⎦⎤⎢⎢⎢⎣⎡⎥⎥⎥⎦⎤⎢⎢⎢⎣⎡=⎥⎥⎥⎦⎤⎢⎢⎢⎣⎡001333231232221131211312111rrrrrrrrrrrrWOW!Hand-made rotation matrices{ If I want a rotation matrix that maps the x axis to the unit vector(a,b,c), what should the first column look like?⎥⎥⎥⎦⎤⎢⎢⎢⎣⎡⎥⎥⎥⎦⎤⎢⎢⎢⎣⎡=⎥⎥⎥⎦⎤⎢⎢⎢⎣⎡001333223221312rrcrrbrracbaHand-made rotation matrices{ If I wanted to…z Rotate the z axis to the vector E-Lz Rotate the y axis to the vector upz Rotate the x axis to the vector up x (E-L) { Define unit vectors:z f = (E-L) / |E-L|z u = up / |up|z s = u x f⎥⎥⎥⎦⎤⎢⎢⎢⎣⎡⎥⎥⎥⎦⎤⎢⎢⎢⎣⎡=⎥⎥⎥⎦⎤⎢⎢⎢⎣⎡zyxfusfusfuszyxzzzyyyxxx'''5But really that’s not what I want…{ I have three unit vectors (f, u, and s) and I want to rotate them into the x, y, and z axes{ What matrix will rotate f, u, and s into the x, y, and z axes?⎥⎥⎥⎦⎤⎢⎢⎢⎣⎡=⎥⎥⎥⎦⎤⎢⎢⎢⎣⎡==−zyxzyxzyxTzzzyyyxxxTfffuuusssfusfusfusRR1Finally, the viewing matrix…⎥⎥⎥⎥⎦⎤⎢⎢⎢⎢⎣⎡⎥⎥⎥⎥⎦⎤⎢⎢⎢⎢⎣⎡−−−⎥⎥⎥⎥⎦⎤⎢⎢⎢⎢⎣⎡=⎥⎥⎥⎥⎦⎤⎢⎢⎢⎢⎣⎡1100010001000110000001zyxzyxzyxzyxzyxzyxpworldpworldpworldeeefffuuussspeyepeyepeye{ Reminder: all this work was to transform points in worldcoordinates to points in eyecoordinatesViewing in OpenGL [cube.cpp]{ gluLookAt() does everything we just talked aboutz Multiplies the current transformation by the matrix we just builtz Usually the
View Full Document