Notes on Assignment Notes on Assignment Objects on screen - made of primitives Primitives are points, lines, polygons - watch vertex ordering The main object you need is a box When the MODELVIEW matrix is changed it applies to all vertices of the object that is drawn next Notes on Assignment Extensions - 4 days before (Thu by midnight) not guaranteed Ask questions IN CLASS. Notes on Assignment Hierarchical Design + stacks Camera motion - will be covered today All matrices are 4x4-3d points + 1 homogeneous coordinate Use gl/glu/glut commands for debugging
OpenGL Pipeline Lecture 4 Viewing, Projection and Viewport s Model = position of objects View = position and aim camera p(x,y,z) 3D Object Coordinates Modeling 3D World Coordinates Viewing Projection Window-to-Viewport p (x,y ) List of s 3D Camera Coordinates 2D Screen Coordinates 2D Image Coordinates Done last week Make the monitor a free moving video camera perspective, parallel screen coords to viewport resolution 2005, Denis Zorin Coordinate systems z x Eye coordinates x z World coordinates World coordinates - fixed initial coord system; everything is defined with respect to it Eye coordinates - coordinate system attached to the camera; in this system camera looks down negative Z-axis
Positioning the camera Modeling transformation: reshape the object, orient the object, position the object with respect to the world coordinate system Viewing transformation: transform world coordinates to eye coordinates Translate camera by (tx,tz) z zw w0 xw Viewing transformation is the inverse of the camera positioning transformation Viewing transformation should be rigid: rotation + translation tz tx x e z e x Steps to get the right transform: first, orient the camera correctly, then translate it 2005, Denis Zorin Initial Configuration Initial Configuration z zw z zw w0 w0 xw xw xe xe ze x ze x
Viewing = z x e Translate object by (-tx,-tz) z e tx tz z w w0 x x w x w = xw - tx z w = zw - tz Look-at positioning Find the viewing transform given the eye (camera) position, point to look at, and the up vector Need to specify two transforms: rotation and translation. translation is easy natural rotation: define implicitly using a point at which we want to look and a vector indicating the vertical in the image (up vector) can easily convert the eye point to the direction vector of the camera axis; can assume up vector perpendicular to view vector 2005, Denis Zorin OpenGL Viewing glmatrixmode(gl_modelview); Look-at Positioning Convert eye point to the direction vector of camera glloadidentity(); position camera is aimed at glulookat(cx,cy,cx,lx,ly,lz,ux,uy,uz) Eye coords Eye ( Camera ) Coordinates Up Direction
Look-at Positioning Convert eye point to the direction vector of camera Assume up vector to be perpendicular to dir vec Eye coords Look-at Positioning Convert eye point to the direction vector of camera Assume up vector to be perpendicular to dir vec This gives you a pair of perpendicular vectors Eye coords Look-at positioning Problem: given two pairs of perpendicular unit vectors, find the transformation mapping the first pair into the second 2005, Denis Zorin 2005, Denis Zorin Eye coords World coords Look-at positioning Determine rotation first, looking how coord vectors change:,,
Look-at positioning Recall the matrix for translation: Now we have the camera positioning matrix, To get the viewing transform, invert: ' For rotation the inverse is the transpose ' 2005, Denis Zorin 2005, Denis Zorin Implementation Notes for the Viewing Text Positioning the camera in OpenGL imagine that the camera is an object and write a sequence of rotations and translations positioning it change each transformation in the sequence to the opposite reverse the sequence Camera positioning is done in the code before modeling transformations OpenGL does not distinguish between viewing and modeling transformation and joins them into the modelview matrix Look-at viewing transformation p(x,y,z) 3D Object Coordinates Modeling 3D World Coordinates Viewing 3D Camera Coordinates '' ' ( ' ' ' ( ' ( ' ) ) Projection Window-to-Viewport p (x,y ) 2D Screen Coordinates 2D Image Coordinates 2005, Denis Zorin
OpenGL Pipeline Projection s glmatrixmode(gl_projection); glloadidentity(); Orthographic Projection Perspective Projection Map viewing volume to cube [-1,1]x[-1,1]x[-1,1] Projection s Think of this as selecting the lens Describe the viewing volume How an object is projected onto the screen Which objects or portions of objects are get clipped Orthographic Projection viewing volume - rectangular parallelepiped(=box) translating camera doesn t change size but may change visibility no foreshortening - less realistic default in opengl - cube centered at (0,0,0), side = 2 but useful in architecture and cad where actual angles and sizes important
Orthographic Projection Viewing frustum Volume in space that will be visible in the image is the aspect ratio of the image width/height y z glortho(left, right, bottom, top, near, far); gluortho2d(left, right, bottom, top); 2005, Denis Zorin glfrustum(left, right, bottom, top, near, far); Perspective Projection Farther the object is from camera, the smaller it appears in image. ==> Realism Translating camera may change size of objects Not good for measurements. Achieved by specifying the volume as a frustum (truncated pyramid, with apex at camera) Perspective Projection gluperspective(fovy,aspect,near far); (in degrees) symmetric in x and y
Projection transformation Behind the Scenes Maps the viewing frustum into a standard cube extending from -1 to 1 in each coordinate (normalized device coordinates) Space to plane projection In eye coordinate system z y v Image plane projecting to the plane z = -1 3 steps: a.k.a canonical view volume '() '() 2005, Denis Zorin 2005, Denis Zorin orthogonal projection 3 steps: perspective transformation 2005, Denis Zorin Projection transformation Behind the Scenes change the matrix of projection to keep z: result is a parallelepiped translate: parallelepiped centered at 0 scale in all directions: cube of of size 2 centered at 0 Space to plane projection In eye coordinate system '() z y Image plane v '() projecting to the plane z = -1 2005, Denis Zorin 2005, Denis Zorin
Projection transformation '() change so that we keep z: the homogeneous result corresponds to the last component increases monotonically with z -z -y viewing frustum eye position Cross section at x=0 z-axis 2005, Denis Zorin Projection transformation maps the frustum to an axis-aligned parallelepiped 1/ 1/ z Points (0,yi,zi,1) Plot of (zi,yi) already centered in (x,y), center in z-direction and scale: ' ( ' Multiply each point by projection transformation to get 2005, Denis Zorin
Points (0,yi,zi,1) Plot of (zi,yi) S = 1 r tan(α/2) 0 0 0 1 0 tan(α/2) 0 0 2 0 0 0 ( 1 f 1 n ) 0 0 0 1 = 2 0 0 0 0 2 0 0 0 0 15 0 0 0 0 1 Multiply each point by projection transformation to get Points (0, yi,1,-zi) mult each point by S Plot of (-1/zi, -yi/zi) already centered in (x,y), center in z-direction and scale: ' ( 2005, Denis Zorin = mult each point by T 1 0 0 0 0 1 0 ' 0 0 0 1.2667 0 0 0 1 Projection transformation Combined matrix, mapping frustum to a cube: ' ' ( To get normalized image plane coordinates (valid range [-1,1] both), just drop z in the result and convert from homogeneous to regular. To get pixel coordinates, translate Perspective by 1, Division and scale x and y (Viewport transformation) 2005, Denis Zorin
Remember the Pipeline: Remember the Pipeline: homogenize then discard z component p x p y p z p z p x /p z p y /p z p z /p z 1 p x/p z p y /p z 1 = normalized image plane coordinates ([-1,1] in x and y) Clipping Removal or modification of primitives based on whether they fall within a viewing volume. Done against Canonical View Volume. Why? Parameter free. Only values you need to know for clipping volume are -1 and 1 Aligned with coordinate axes - inside outside trivial (and efficient - no need for dot products) p(x,y,z) Modeling Viewing Projection Window-to-Viewport p (x,y ) 3D Object Coordinates 3D World Coordinates 3D Camera Coordinates 2D Screen Coordinates 2D Image Coordinates
OpenGL Pipeline map square [-1,1]x[-1,1] to image Viewport Idea is to transform [-1,1]x[-1,1] to [0,w]x[0,h] Behind the scenes Translate by (1,1) Scale x by w and y by x y -1 1 w Viewport Refers to size of image you see on screen Defines the rectangular region of the window where the image is drawn OpenGL: glviewport(x, y, width, height); Default - entire window: (0,0, w,h) Lower left corner Viewport Aspect Ratio Issues
Visibility Objects close to the camera occlude those that are further away. Should be the case in the final image that we draw Default - latest drawn object covers older ones Another option: Visibility Sort objects in the scene from farthest to closest : Painters Algorithm not practical adding/removing objects in the scene in the front? back? not very efficient need to compute distances from eye draw pixels that will be covered sorting - usually BSP Trees One option: Visibility Object-Space Approach For all pairs of polygons compare against each other choose from display/no display/partial display For each polygon compare against n-1 others Visibility Objects that are closer to the camera occlude the objects that are further away All objects are made of planar polygons A polygon typically projects 1 to 1 idea: project polygons in turn ; for each pixel, record distance to the projected polygon when writing pixels, replace the old color with the new one only if the new distance to camera for this pixel is less then the recorded one 2005, Denis Zorin
Z-buffering idea Z buffer Problem: need to compare distances for each projected point Solution: convert all points to a coordinate system in which (x,y) are image plane coords and the distance to the image plane increases when the z coordinate increases decreases In OpenGL, this is done by the projection matrix Red text refers to the negative z case What are z values? Z values are obtained by applying the projection transform, that is, mapping the viewing volume frustum to the standard cube. Z value increases with th distance to the camera. Remember the discarded z values Z values for each pixel are computed for each pixel covered by a polygon during using perspective linear interpolation division? of z values at vertices. Typical Z buffer size: 24 bits (same as RGB combined). 2005, Denis Zorin 2005, Denis Zorin Z buffer Z buffer Assumptions: each pixel has storage for a z-value, in addition to RGB all objects are scanconvertible (typically are polygons, lines or points) Algorithm: initilize zbuf to maximal value for each object for each pixel (i,j) obtained by scan conversion if znew(i,j) < zbuf(i,j) if znew(i,j) > zbuf(i,j) zbuf(i,j) = znew(i,j) ; write pixel(i,j) What are z values? Z values are obtained by applying the projection transform, that is, mapping the viewing volume frustum to the standard cube. Z value increases with th distance to the camera. decreases Z values for each pixel are computed for each pixel covered by a polygon using linear interpolation of z values at vertices. Typical Z buffer size: 24 bits (same as RGB combined). 2005, Denis Zorin 2005, Denis Zorin
Pros Z Buffer - brief analysis simple to implement(hw/sw) loops over polygons compatible with pipeline architectures Cons Large amount of memory Often waste of rendering resources for obscured objects Z Buffer - OpenGL Notes Depth Buffer glutinitdisplaymode(glut_depth... ); glenable(gl_depth_test); glclear( GL_DEPTH_BUFFER_BIT... );