Chapter 3: Modeling Transformation Graphics Programming, 8th Sep. Graphics and Media Lab. Seoul National University 2011 Fall
OpenGL Steps Every step in the graphics pipeline is related to the transformation. 2 Graphics and Media Lab. at Seoul National University
Related Transformations (1/2) Euclidean (Rigid) motion Preserves distance & angle. Similarity motion Preserves angle. Linear motion Preserves linearity. 3 Graphics and Media Lab. at Seoul National University
Related Transformations (2/2) Affine motion Preserves parallel line. Projective motion Preserves line. How to represent? 4 Graphics and Media Lab. at Seoul National University
Transformation Classes Euclidean transformation(se3) Preserves distance & angle.(translation, Rotation) Similarity transformation Preserves angles.(rotation, Isotropic scaling) Linear transformation(gl3) Preserves linearity.(rotation,scaling,reflection,shear) Affine transformation Preserves parallel lines. Projective transformation Preserves lines. 4x4 Matrix! How about 3D points? 5 Graphics and Media Lab. at Seoul National University
Homogeneous Coordinates We augment a point in R 3 for the transformation matrices. a 00 Modelview matrix has the form a a 0 10 20 a a a 0 01 11 21 a a a 02 12 22 0 t t t 1 2 3 1 Projection matrix has the form (The next class!) Point [x y z 1] T The result of transformation needs to be divided by w to give the 3D position. (homogeneous) Vector [x y z 0] T w=0 represents a point at infinity. (Only direction differentiates.) 6 Graphics and Media Lab. at Seoul National University
Modelview & Projection matrix OpenGL helps us to change the two most important transformation matrices: Modelview Matrix The relative transformation between object and camera Projection Matrix Clipping volume (viewing frustum) Projection to screen Vertices(primitives) are transformed by P*M. P * M * 7 Graphics and Media Lab. at Seoul National University
Code Example from Ch.2 Draw a transformed box in 3d. 8 Graphics and Media Lab. at Seoul National University
Code Example 1 Resize() function was added. A few experiments with modeling transform 9 Graphics and Media Lab. at Seoul National University
Result 10 Graphics and Media Lab. at Seoul National University
Code Example 1 - Explanation glutmainloop() instructs the program to enter the event processing loop. glutreshapefunc() registers the resizing function as the callback function. glclear( ) clears video buffers. glviewport(x,y,w,h) sets the screen space. glmatrixmode(gl_projection) tells that we modify the projection matrix. glmatrixmode(gl_modelview) tells that we modify the modelview matrix. glloadidentity() replaces the current matrix with the identity matrix. glortho(l,r,b,t,zn,zf) sets the clipping space orthographically. This changes the projection matrix. gltranslatef(),glrotatef(), and glscalef() change the modelview matrix. 11 Graphics and Media Lab. at Seoul National University
The Order of Transformations Application glloadidentity(); glmultmatrixf(n); glmultmatrixf(m); glmultmatrixf(l); glbegin(gl_points); glvertex3f(v); glend(); // C = I // C = N // C = NM // C = NML // NMLv 12 Graphics and Media Lab. at Seoul National University
The Order is Important 45 deg rotation around z-axis then 10 unit translation along +x, and vice versa. 13 Graphics and Media Lab. at Seoul National University
Thinking in Terms of Fixed Coords Code for rot then trans wrt fixed coords. glmatrixmode(gl_modelview); glloadidentity(); glmultmatrixf(t); glmultmatrixf(r); draw_the_object(); 14 Graphics and Media Lab. at Seoul National University
Thinking in Terms of Local Coords Code for trans then rot wrt local coords. glmatrixmode(gl_modelview); glloadidentity(); glmultmatrixf(t); glmultmatrixf(r); draw_the_object(); 15 Graphics and Media Lab. at Seoul National University
Thinking in Terms of Local Coords Code for rot then trans wrt local coords. glmatrixmode(gl_modelview); glloadidentity(); glmultmatrixf(r); glmultmatrixf(t); draw_the_object(); 16 Graphics and Media Lab. at Seoul National University
Push & Pop We can manage the hierarchy by glpushmatrix(), glpopmatrix(). P * M Push M1 Pop Push Pop M2 * * * Vertices of arm Vertices of shoulder Vertices of body 17 Graphics and Media Lab. at Seoul National University
Code Example 2 Robot arm unit cube: [-0.5,0.5]^3 18 Graphics and Media Lab. at Seoul National University
Result 19 Graphics and Media Lab. at Seoul National University
Code Example 2 - Explanation glutkeyboardfunc() registers the key function as the callback function: If s is pressed, rotate shoulder If S is pressed, rotate shoulder reversely If e is pressed, rotate elbow If E is pressed, rotate elbow reversely glpushmatrix() saves the current matrix. glpopmatrix() loads the saved matrix. gluperpective(fov,ratio,zn,zf) sets the view frustum. This changes the projection matrix. gltranslatef(0,0,-1); glrotatef(-angle,0,0,1); gltranslatef(0,0,1); The rotation about a point 20 Graphics and Media Lab. at Seoul National University