Notes on Assignment
Notes on Assignment Hw2 is dependent on hw1 so hw1 and hw2 will be graded together i.e. You have time to finish both by next monday 11:59p Email list issues - please cc: elif@cs.nyu.edu when sending messages to the mailing list
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 Hierarchical Design + stacks All matrices are 4x4-3d points + 1 homogeneous coordinate Use gl/glu/glut commands for debugging Camera motion which is needed for hw2- will be covered today Additional questions?
Lecture 4 Viewing, Projection and Viewport Transformations
List of Transformations 3D Object Coordinates Modeling Transformation 3D World Coordinates Viewing Transformation Projection Transformation Window-to-Viewport Transformation E.T. 06 p(x,y,z) p (x,y ) 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
E.T. 06 OpenGL Transformation Pipeline Model = position of objects View = position and aim camera
Coordinate systems z x x Eye coordinates 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 2005, Denis Zorin
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 Viewing transformation is the inverse of the camera positioning transformation Viewing transformation should be rigid: rotation + translation Steps to get the right transform: first, orient the camera correctly, then translate it 2005, Denis Zorin
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 Viewing transformation is the inverse of the camera positioning transformation Viewing transformation should be rigid: rotation + translation Steps to get the right transform: first, orient the camera correctly, then translate it 2005, Denis Zorin
Initial Configuration z zw w0 xw xe ze x
E.T. 06 Translate camera by (tx,tz) z zw w0 tz tx z e x e x xw
E.T. 06 Initial Configuration z zw w0 xw xe ze x
E.T. 06 Viewing Transformation = Translate object by (-tx,-tz) z x e tx tz z w w0 x w z e x x w = xw - tx z w = zw - tz
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 Viewing transformation is the inverse of the camera positioning transformation Viewing transformation should be rigid: rotation + translation Steps to get the right transform: first, orient the camera correctly, then translate it 2005, Denis Zorin
OpenGL Viewing Transformation -- Set in display function glmatrixmode(gl_modelview); glloadidentity(); default is modelview and equals identity default = (0,0,-1) position camera is aimed at glulookat(cx,cy,cx,lx,ly,lz,ux,uy,uz) E.T. 06 Eye ( Camera ) Coordinates default = (0,0,0) Up Direction default = (0,1,0)
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
Look-at Positioning Convert eye point to the direction vector of camera # Eye coords E.T. 06 $ " # " # "
Look-at Positioning Convert eye point to the direction vector of camera Assume up vector to be perpendicular to dir vec # Eye coords E.T. 06 $ " # " # "
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 E.T. 06 $ " # " # "
Look-at positioning Problem: given two pairs of perpendicular unit vectors, find the transformation mapping the first pair into the second # $ " # " # Eye coords " World coords 2005, Denis Zorin
Look-at positioning Determine rotation first, looking how coord vectors change: # # # " " $ # " " " $ # " # " " $ # # " " " " # $ # # $" #,#, "% 2005, Denis Zorin
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
Look-at viewing transformation " # " # # " # " # " " # # " " # # # # " $ % & $# # " # # "% $ % " # && '' $ && ( ') ( "' ' $ &' ( "' & $ && ( "' $#) #) #% " $ % & 2005, Denis Zorin
Implementation Notes for the Viewing Positioning the camera in OpenGL Text Transformation 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 (inverse) 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 2005, Denis Zorin
E.T. 06 p(x,y,z) 3D Object Coordinates Modeling Transformation 3D World Coordinates Viewing Transformation 3D Camera Coordinates Projection Transformation Window-to-Viewport Transformation p (x,y ) 2D Screen Coordinates 2D Image Coordinates
E.T. 06 OpenGL Transformation Pipeline Map viewing volume to cube [-1,1]x[-1,1]x[-1,1] (a.k.a. canonical view volume)
E.T. 06 Projection Transformations Think of this as selecting the lens (i.e. wide-angle, telephoto, fisheye, etc) Describe the viewing volume How an object is projected onto the screen Which objects or portions of objects are get clipped
Projection Transformations -Set in reshape func ( since relevant to screen ) glmatrixmode(gl_projection); glloadidentity(); Orthographic Projection Perspective Projection E.T. 06 box pyramid/frustum
E.T. 06 Orthographic Projection viewing volume - rectangular parallelepiped(=box) preserves parallelism 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 up glortho(left, right, bottom, top, near, far); gluortho2d(left, right, bottom, top); E.T. 06 project 2D image to 2D screen(e.g. text)
Orthogonal Projection 2 Steps: Translate to center the parallelepiped at 0. 1 0 0 r+l 2 0 1 0 t+b 2 0 0 1 f+n 2 0 0 0 1 Scale in all directions s.t. it ends up as a cube of size 2 centered at 0. ST = 2 r l 0 0 r+l r l 0 2 t b 0 t+b t b 2 0 0 f n 0 0 0 1 2 r l 0 0 0 2 0 t b 0 0 2 0 0 f n 1 0 0 0 1 f+n f n
E.T. 06 Perspective Projection Farther the object is from camera, the smaller it appears in image. ==> Realism Parallelism not preserved 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)
Viewing Frustum Volume in space that will be visible in the image not necessarily symmetric glfrustum(left, right, bottom, top, near, far ); note: near>far
E.T. 06 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) 3 steps: canonical view volume 2005, Denis Zorin
Projection transformation Behind the Scenes 3 steps: perspective transformation orthogonal projection 2005, Denis Zorin 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 2005, Denis Zorin
Space to plane projection In eye coordinate system z y v Image plane # " " " " " # $ " " " " # projecting to the plane z = -1 # $ $ % & ' ( # ) " # $ # # # " $ # # # # $ $%&'(#) % " # " " " " # " " " " # " " " # " # & % $ " # # " # # # # & $ 2005, Denis Zorin
Space to plane projection In eye coordinate system z y v Image plane # " " " " " # $ " " " " # projecting to the plane z = -1 # $ $ % & ' ( # ) " # $ # # # " $ # # # # $ $%&'(#) % " # " " " " # " " " " # " " " # " # & % $ " # # " # # # # & $ 2005, Denis Zorin
Projection transformation $ $ " " " $%&'() # " " " " % " " change % # " " " & # # & so that we keep z: " " " " # " " " " " " " " " " " " $ % & " # " # $ % & # " # " # the homogeneous result corresponds to $ % & # " # "" # " # $ & the last component increases monotonically with z 2005, Denis Zorin
Projection transformation " # " " " " " " " " " " " " $ % & maps the frustum to an axis-aligned parallelepiped 1/" 1/ #$% & z # #$% & already centered in (x,y), center in z-direction and scale: $ ' " # 2005, Denis Zorin " " " " " " " " & " ( " " " " $ % & % ' " # # #$% " " " " #$% " " & " " # $ " " " " " " " $ % &
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
E.T. 06 -z eye position z-axis viewing frustum -y Cross section at x=0
E.T. 06 Points (0,yi,zi,1) Plot of (zi,yi) Multiply each point by projection transformation to get
E.T. 06 Points (0,yi,zi,1) Plot of (zi,yi) Multiply each point by projection transformation to get Points (0, yi,1,-zi) Plot of (-1/zi, -yi/zi)
E.T. 06 already centered in (x,y), center in z-direction and scale: $ ' " # 2005, Denis Zorin " " " " " " " " & " ( " " " " $ % & = # #$% " " " 1 0 0 0 0 1 " 0 % ' #$% 0 " " 0 & 0 " 1.2667 " # $ " " # " 0 0 0 1 " " " " " $ % & mult each point by T
E.T. 06 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 mult each point by S
Remember the Pipeline:
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)
Additional Clipping Can define up to 6 other clipping planes Then points will pass iff intersection of viewing volume and all half spaces define clipping planes as Ax+By+Cz+D =0 (A B C D) M -1 (x y z 1) T >= 0 glclipplane(gl_clip_planei) glenable(gl_clip_planei) This is done in eye coordinates
Remember the Pipeline: p x p y p z p z homogenize then discard z component 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)
E.T. 06 p(x,y,z) 3D Object Coordinates Modeling Transformation 3D World Coordinates Viewing Transformation 3D Camera Coordinates Projection Transformation 2D Screen Coordinates Window-to-Viewport Transformation p (x,y ) 2D Image Coordinates
E.T. 06 OpenGL Transformation Pipeline map square [-1,1]x[-1,1] to image
E.T. 06 Viewport Transformation Set in reshape ( screen dependent ) Allows scales/stretches of image Refers to size and location 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 Transformation 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 Transformation Aspect Ratio Issues
E.T. 06 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
Visibility One option: 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
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
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 hidden surface removal 2005, Denis Zorin
2005, Denis Zorin Z-buffering idea 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
Z buffer Assumptions: can have other comparison funcs here defined by gldepthfunc 2005, Denis Zorin 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)
Z buffer 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
Z buffer 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
Z Buffer - brief analysis Pros 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... );