Transformations and Projections (Some slides adapted from Amitabh Varshney) Transformation of lines/normals • 2D. Line is set of points (x,y) for which (a,b,c).(x,y,1)T=0. Suppose we rotate points by R. Notice that: (a,b,c)RT R(x,y,1)T So, (a,b,c)RT is the rotation of the line (a,b,c). Surface normals are similar, except they are defined by (a,b,c).(x,y,z)T = 0 Rotation about a known axis • Suppose we want to rotate about u. • Find R so that u will be the new z axis. – u is third row of R. – Second row is anything orthogonal to u. – Third row is cross-product of first two. – Make sure matrix has determinant 1. • Then rotate about (new) z axis. • Then apply inverse of first rotation. OpenGL Transformation Support • Three matrices – GL_MODELVIEW, GL_PROJECTION, GL_TEXTURE – glMatrixMode( mode ) specifies the active matrix • glLoadIdentity( ) – Set the active matrix to identity • glLoadMatrix{fd}(TYPE *m) • m1 – Set the 16 values of the current m2 matrix to those specified by m m = m3 glMultMatrix{fd}(TYPE *m) m – Multiplies the current active matrix by m 4 m5 m6 m7 m8 m9 m10 m11 m12 m13 m14 m15 m16 Transformation Example glMatrixMode(GL_MODELVIEW); glLoadIdentity( ); // matrix = I glMultMatrix(N); // matrix = N glMultmatrix(M); // matrix = NM glMultMatrix(L); // matrix = NML glBegin(GL_POINTS); glVertex3f(v); // v will be transformed: NMLv glEnd( ); OpenGL Transformations • glTranslate{fd}(TYPE x, TYPE y, TYPE z) – Multiply the current matrix by the translation matrix • glRotate{fd}(TYPE angle, TYPE x, TYPE y, TYPE z) – Multiply the current matrix by the rotation matrix that rotates an object about the axis from (0,0,0) to (x, y, z) • glScale{fd}(TYPE x, TYPE y, TYPE z) – Multiply the current matrix by the scale matrix Examples glMatrixMode(GL_MODELVIEW); glRecti(50,100,200,150); glTranslatef(-200.0, -50.0, 0.0); glRecti(50,100,200,150); glLoadIdentity(); glRotatef(90.0, 0.0, 0.0, 1.0); glRecti(50,100,200,150); glLoadIdentity(); glscalef(-.5, 1.0, 1.0) glRecti(50,100,200,150); Hierarchical Transformations in OpenGL • Stacks for Modelview and Projection matrices • glPushMatrix( ) – push-down all the matrices in the active stack one level – the top-most matrix is copied (the top and the secondfrom-top matrices are initially the same). • glPopMatrix( ) – pop-off and discard the top matrix in the active stack • Stacks used during recursive traversal of the hierarchy. • Typical depths of matrix stacks: – Modelview stack = 32 (aggregating several transformations) – Projection Stack = 2 (eg: 3D graphics and 2D helpmenu) Viewing in 3D • World (3D) Screen (2D) • Orienting Eye coordinate system in World coordinate system – View Orientation Matrix • Specifying viewing volume and projection parameters for n d (d < n) – View Mapping Matrix World to Eye Coordinates (Images Removed) World to Eye Coordinates • We need to transform from the world coordinates to the eye coordinates • The eye coordinate system is specified by: – View reference point (VRP) • (VRPx, VRPy, VRPz) – Direction of the axes: eye coordinate system • U = (ux, uy, uz) • V = (vx, vy, vz) • N = (nx, ny, nz) World to Eye Coordinates • There are two steps in the transformation (in order) – Translation – Rotation World to Eye Coordinates • Translate World Origin to VRP a b = c 1 1 0 0 0 0 0 -VRPx 1 0 -VRPy 0 1 -VRPz 0 0 1 x y z 1 World to Eye Coordinates • Rotate World X, Y, Z to the Eye coordinate system u, v, n, also known as the View Reference Coordinate system x’ y’ = z’ 1 ux vx nx 0 uy vy ny 0 uz vz nz 0 0 0 0 1 a b c 1 Camera Analogy (Images Removed) Specifying 3D View (Camera Analogy) • • • • • Center of camera (x, y, z) : 3 parameters Direction of pointing (q,j) : 2 parameters Camera tilt (w) : 1 parameter Area of film (w, h) : 2 parameters Focus (f) : 1 parameter Specifying 3D View • Center of camera (x, y, z) : View Reference Point (VRP) • Direction of pointing (q,j) : View Plane Normal (VPN) • Camera tilt (w) : View Up (VUP) • Area of film (w, h) : Aspect Ratio (w/h), Field of view (fov) • Focus (f) : Will consider later Eye Coordinate System VUP (Y-axis) VPN (Z-axis) VRP (origin) Viewing Plane • View Reference Point (VRP) • View Plane Normal (VPN) • View Up (VUP) VUP VPN (X-axis) World to Eye Coordinates • Translate World Origin to VRP • Rotate World X, Y, Z to the Eye coordinate system, also known as the View Reference Coordinate system, VRC = (VUP VPN, VUP, VPN), respectively: ( VUP VPN ) 0 ( VUP ) 0 ( VPN ) 0 0 0 0 1 Eye Coordinate System (OpenGL/GLU library) • gluLookAt (eyex , eyey , eyez , lookatx , lookaty , lookatz , upx , upy , upz ); • In our terminology: eye = VRP lookat = VRP + VPN up = VUP • gluLookAt also works even if: – lookat is any point along the VPN – VUP is not perpendicular to VPN gluLookAt() (Images Removed) Image from: Interactive Computer Graphics by Ed Angel Eye Coordinate System (OpenGL/GLU library) • This how the gluLookAt parameters are used to generate the eye coordinate system parameters: VRP = eye VPN = (lookat - eye) / ||(lookat - eye) ||2 VUP = VPN (up VPN) • The eye coordinate system parameters are then used in translation T(VRP) and rotation R(XYZ VRC) to get the view-orientation matrix Camera Obscura "When images of illuminated objects ... penetrate through a small hole into a very dark room ... you will see [on the opposite wall] these objects in their proper form and color, reduced in size ... in a reversed position, owing to the intersection of the rays". Da Vinci http://www.acmi.net.au/AIC/CAMERA_OBSCURA.html (Russell Naughton) • Used to observe eclipses (eg., Bacon, 1214-1294) • By artists (eg., Vermeer). Jetty at Margate England, 1898. http://brightbytes.com/cosite/collection2.html (Jack and Beverly Wilgus) Pinhole cameras • Abstract camera model - box with a small hole in it • Pinhole cameras work in practice (Forsyth & Ponce) The equation of projection (Forsyth & Ponce) The equation of projection • Cartesian coordinates: – We have, by similar triangles, that (x, y, z) -> (f x/z, f y/z, f) – Ignore the third coordinate, and get x y (x, y, z) ( f , f ) z z Perspective Projection x’’ y’’ = z’’ w’’ 1 0 0 0 0 1 0 0 0 0 1 1/f 0 0 0 0 x y z 1 • Homogenize (divide by w’’= z / f) to get: – x’ = x / (z / f) = x’’/ w’’ – y’ = y / (z / f) = y’’/ w’’ – z’ = z / (z / f) = z’’/ w’’ = f Distant objects are smaller (Forsyth & Ponce) Parallel lines meet Common to draw image plane in front of the focal point. Moving the image plane merely scales the image. (Forsyth & Ponce) Vanishing points • Each set of parallel lines meets at a different point – The vanishing point for this direction • Sets of parallel lines on the same plane lead to collinear vanishing points. – The line is called the horizon for that plane Properties of Projection • • • • • Points project to points Lines project to lines Planes project to the whole image Angles are not preserved Degenerate cases – Line through focal point projects to a point. – Plane through focal point projects to line – Plane perpendicular to image plane projects to part of the image (with horizon). Weak perspective (scaled orthographic projection) • Issue – perspective effects, but not over the scale of individual objects – collect points into a group at about the same depth, then divide each point by the depth of its group (Forsyth & Ponce) The Equation of Weak Perspective ( x, y , z ) s ( x, y ) • s is constant for all points. • Parallel lines no longer converge, they remain parallel. Parallel Projection Project on the plane, z = 0 x’ y’ z’ 1 = 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 x y z 1 Cameras with Lenses (Forsyth & Ponce) OpenGL for Projection • Projection specifies clipping windows. • glMatrixMode(GL_PROJECTION) • glOrtho(xmin,xmax,ymin,ymax,dnear,dfar) • gluPerspective(theta,aspect,dnear,dfar)