Pof. Feng Liu Fall 26 http://www.cs.pdx.edu/~fliu/couses/cs447/ /7/26
Last time Compositing NPR 3D Gaphics Toolkits Tansfomations 2
Today 3D Tansfomations The Viewing Pipeline Mid-tem: in class, Nov. 2 Homewok 3 available, due Octobe 3, in class 3
Homogeneous Coodinates Use thee numbes to epesent a 2D point (x,y)=(wx,wy,w) fo any constant w Typically, (x,y) becomes (x,y,) To go backwads, divide by w Tanslation can now be done with matix multiplication! 4
Basic Tansfomations Tanslation: Rotation: Scaling: 5 y x b b y x s s cos sin sin cos
Homogeneous Tansfom Advantages Unified view of tansfomation as matix multiplication Easie in hadwae and softwae To compose tansfomations, simply multiply matices Ode mattes: AB is geneally not the same as BA Allows fo non-affine tansfomations: Pespective pojections! 6
Diections vs. Points We have been talking about tansfoming points Diections ae also impotant in gaphics Viewing diections (,) Nomal vectos (-2,-) Ray diections Diections ae epesented by vectos, like points, and can be tansfomed, but not like points 7
Tansfoming Diections Say I define a diection as the diffeence of two points: d=a b This epesents the diection of the line between two points Now I tanslate the points by the same amount: a =a+t, b =b+t How should I tansfom d? 8
Homogeneous Diections Tanslation does not affect diections! Homogeneous coodinates give us a vey clean way of handling this The diection (x,y) becomes the homogeneous diection (x,y,) The coect thing happens fo otation and scaling also Unifom scaling changes the length of the vecto, but not the diection 9 y x y x b b y x
3D Tansfomations Homogeneous coodinates: (x,y,z)=(wx,wy,wz,w) Tansfomations ae now epesented as 4x4 matices Typical gaphics packages allow fo specification of tanslation, otation, scaling and abitay matices OpenGL: gltanslate[fd], glrotate[fd], glscale[fd], glmultmatix[fd]
3D Tanslation z y x t t t z y x z y x
3D Rotation Rotation in 3D is about an axis in 3D space passing though the oigin Using a matix epesentation, any matix with an othonomal top-left 3x3 sub-matix is a otation Rows ae mutually othogonal ( dot poduct) Deteminant is Implies columns ae also othogonal, and that the tanspose is equal to the invese 2
3D Rotation 3.,,,, then and if 3 2 3 2 3 2 etc z y x R z y x z y x zz zy zx yz yy yx xz xy xx
Poblems with Rotation Matices Specifying a otation eally only equies 3 numbes Axis is a unit vecto, so equies 2 numbes Angle to otate is thid numbe Rotation matix has a lage amount of edundancy Othonomal constaints educe degees of feedom back down to 3 Rotations ae a vey complex subject, and a detailed discussion is way beyond the scope of this couse 4
Altenative Repesentations Specify the axis and the angle (OpenGL method) Eule angles: Specify how much to otate about X, then how much about Y, then how much about Z Had to think about, and had to compose Any thee axes will do e.g. X,Y,Z Specify the axis, scaled by the angle Only 3 numbes, called the exponential map Quatenions 5
Quatenions 4-vecto elated to axis and angle, unit magnitude Rotation about axis (n x,n y,n z ) by angle : cos / 2,sin / 2 nx cos / 2, ny cos / 2, nz Reasonably easy to compose Reasonably easy to go to/fom otation matix Only nomalized quatenions epesent otations, but you can nomalize them just like vectos, so it isn t a poblem Easy to pefom spheical intepolation 6
Othe Rotation Issues Rotation is about an axis at the oigin Fo otation about an abitay axis, use the same tick as in 2D: Tanslate the axis to the oigin, otate, and tanslate back again Rotation is not commutative Rotation ode mattes Expeiment to convince youself of this 7
Tansfomation Leftoves Scale, shea etc extend natually fom 2D to 3D Rotation and Tanslation ae the igid-body tansfomations: Do not change lengths o angles, so a body does not defom when tansfomed 8
Modeling Fo the moment assume that all geomety consists of points, lines and faces Line: A segment between two endpoints Face: A plana aea bounded by line segments Any face can be tiangulated (boken into tiangles) 9
Image ul: http://www.capcom-cental.com/concept/e4/leonwiefame.jpg 2
Modeling and OpenGL In OpenGL, all geomety is specified by stating which type of object and then giving the vetices that define it glbegin() glend() glvetex[34][fdv] Thee o fou components (egula o homogeneous) Float, double o vecto (eg float[3]) Chapte 2 of the OpenGL ed book 2
Rendeing Geneate an image showing the contents of some egion of space The egion is called the view volume, and it is defined by the use Detemine whee each object should go in the image Viewing, Pojection Detemine which pixels should be filled Rasteization Detemine which object is in font at each pixel Hidden suface elimination, Hidden suface emoval, Visibility Detemine what colo it is Lighting, Shading 22
Gaphics Pipeline Gaphics hadwae employs a sequence of coodinate systems The location of the geomety is expessed in each coodinate system in tun, and modified along the way The movement of geomety though these spaces is consideed a pipeline Local Coodinate Space Wold Coodinate Space View Space Canonical View Volume Display Space 23
Local Coodinate Space It is easiest to define individual objects in a local coodinate system Fo instance, a cube is easiest to define with faces paallel to the coodinate axes Key idea: Object instantiation Define an object in a local coodinate system Use it multiple times by copying it and tansfoming it into the global system This is the only effective way to have libaies of 3D objects 24
Wold Coodinate System Eveything in the wold is tansfomed into one coodinate system - the wold coodinate system It has an oigin, and thee coodinate diections, x, y, and z Lighting is defined in this space The locations, bightness and types of lights The camea is defined with espect to this space Some highe level opeations, such as advanced visibility computations, can be done hee 25
View Space Define a coodinate system based on the eye and image plane the camea The eye is the cente of pojection, like the apetue in a camea The image plane is the oientation of the plane on which the image should appea, like the film plane of a camea Some camea paametes ae easiest to define in this space Focal length, image size Relative depth is captued by a single numbe in this space 26
Canonical View Volume Canonical View Space: A cube, with the oigin at the cente, the viewe looking down z, x to the ight, and y up Canonical View Volume is the cube: [-,] [-,] [-,] Vaiants (late) with viewe looking down +z and z fom - Only things that end up inside the canonical volume can appea in the window Tasks: Paallel sides and unit dimensions make many opeations easie Clipping decide what is in the window Rasteization - decide which pixels ae coveed Hidden suface emoval - decide what is in font Shading - decide what colo things ae 27
Window Space Window Space: Oigin in one cone of the window on the sceen, x and y match sceen x and y Windows appea somewhee on the sceen Typically you want the thing you ae dawing to appea in you window But you may have no contol ove whee the window appeas You want to be able to wok in a standad coodinate system you code should not depend on whee the window is You taget Window Space, and the windowing system takes cae of putting it on the sceen 28
Gaphics Pipeline Local Coodinate Space Wold Coodinate Space View Space Canonical View Volume Display Space 29
Canonical Window Tansfom Poblem: Tansfom the Canonical View Volume into Window Space (eal sceen coodinates) Dop the depth coodinate and tanslate The gaphics hadwae and windowing system typically take cae of this but we ll do the math to get you wamed up The windowing system adds one final tansfomation to get you window on the sceen in the ight place 3
Canonical Window Tansfom Typically, windows ae specified by a cone, width and height Cone expessed in tems of sceen location This epesentation can be conveted to (x min,y min ) and (x max,y max ) We want to map points in Canonical View Space into the window Canonical View Space goes fom (-,-,-) to (,,) Lets say we want to leave z unchanged What basic tansfomations will be involved in the total tansfomation fom 3D sceen to window coodinates? 3
Canonical Window Tansfom (,) (x max,y max ) (x min,y min ) (-,-) Canonical view volume Window space 32
Canonical Window Tansfom (,) (x max,y max ) (x min,y min ) (-,-) x y z pixel pixel pixel xmax xmin 2 xmax xmin y y 2 y y max min max min 2x 2 y z canonical canonical canonical 33
Canonical Window Tansfom You almost neve have to woy about the canonical to window tansfom In OpenGL, you tell it which pat of you window to daw in elative to the window s coodinates That is, you tell it whee to put the canonical view volume You must do this wheneve the window changes size Window (not the sceen) has oigin at bottom left glviewpot(minx, miny, maxx, maxy) Typically: glviewpot(,, width, height)fills the entie window with the image Some textbook deives a diffeent tansfom, but the same idea 34
glviewpot(,, width, height) 35
glviewpot(,, width, height) 36
Gaphics Pipeline Local Coodinate Space Wold Coodinate Space View Space Canonical View Volume Display Space 37
View Volumes Only stuff inside the Canonical View Volume gets dawn Points too close o too fa away will not be dawn But, it is inconvenient to model the wold as a unit box A view volume is the egion of space we wish to tansfom into the Canonical View Volume fo dawing Only stuff inside the view volume gets dawn Descibing the view volume is a majo pat of defining the view 38
Othogaphic Pojection Othogaphic pojection pojects all the points in the wold along paallel lines onto the image plane Pojection lines ae pependicula to the image plane Like a camea with infinite focal length The esult is that paallel lines in the wold poject to paallel lines in the image, and atios of lengths ae peseved This is impotant in some applications, like medical imaging and some compute aided design tasks
Othogaphic View Space View Space: a coodinate system with the viewe looking in the z diection, with x hoizontal to the ight and y up A ight-handed coodinate system! All ous will be The view volume is a ectilinea box fo othogaphic pojection The view volume has: a nea plane at z=n a fa plane at z=f, (f < n) a left plane at x=l a ight plane at x=, (>l) a top plane at y=t and a bottom plane at y=b, (b<t) z y x (,b,n) (l,t,f) 4
Rendeing the Volume To find out whee points end up on the sceen, we must tansfom View Space into Canonical View Space We know how to daw Canonical View Space on the sceen This tansfomation is pojection The mapping looks simila to the one fo Canonical to Window 4
Othogaphic Pojection Matix (Othogaphic View to Canonical Matix) 42 view canonical view canonical view view view canonical canonical canonical z y x f n f n f n b t b t b t l l l f n b t l f n b t l z y x x M x 2 2 2 2 2 2 2 2 2
Gaphics Pipeline Local Coodinate Space Wold Coodinate Space View Space Canonical View Volume Display Space 43
Next Time Pespective Pojection Clipping 44