CSC418 - Notes
Topic 2: Basic Geometric Modeling

Navigation: back up next notes exercises

Key Concepts & Readings

OpenGL

Transformation Matrices

glMatrixMode(GL_MODELVIEW)

selects the model-view matrix for subsequent matrix operations

glTranslate(x,y,z)

constructs translation matrix and multiply the currently selected matrix with the translation matrix

glRotate(th_rad,vx,vy,vz)

constructs rotation matrix and multiply the currently selected matrix with the rotation matrix

glScale(x,y,z)

constructs scaling matrix and multiply the currently selected matrix with the scaling matrix

glLoadIdentity()

sets the currenlty selected matrix to identity

glPushMatrix()

saves the currently selected matrix to the matrix stack

glPopMatrix()

sets the currently selected matrix equal to the top entry of the matrix stack and pops the stack

glLoadMatrix(... m)

sets the currently selected matrix equal to the t

glMultMatrix(... m)

replaces the current matrix with m * current matrix

Matrix M can be setup as follows:

  glMatrixMode( GL_MODELVIEW );
  glLoadIdentity();
  glTranslatef(2.0, 1.0, 0.0);
  glRotatef( -3.14/2.0, 0.0, 0.0, 1.0); 
  glScalef(2.0, 2.0, 2.0);
  ...

Which produces the matrix M = trans(2,1,0) rot(z,-90) scale(2,2,2) ....
Another way of loading M is to use:

  glMatrixMode( GL_MODELVIEW );
  glLoadMatrixf( A );
...

Mere, M = A
Yet another way of constructing a model-view matrix is:

  glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glMultMatrix(A);
glMultMatrix(B);

Here, M = B A I

Using Transformations

The following opengl code draws four instances of a square using geometric transformations. DrawSquare() draws a unit square centered at (0,0), i.e, a square with unit area.

  ...   
  glMatrixMode(GL_MODELVIEW);     /* select the model-view matrix for subsequent matrix operations */ 
  glLoadIdentity(); /* make the model-view matrix identity */
  DrawSquare(); /*  draw the square 1 */
  glTranslatef(5.0, 0.0, 0.0); /* translate along z-axis by -5 units*/
  DrawSquare; /*  draw the square 2 */
  glScalef (2.0, 5.0, 1.0); /* scale. notice that scaling can be different along each dimension */
  DrawSquare(); /* draw square 3. notice how it is affected by previous transformations (translation & rotation), so one can combine transformations. */ 
  glLoadIdentity (); /* we need to clear the matrix; otherwise the translation will also apply to the second square */  
  glScalef (3.0, 2.0, 1.0); /* scale. notice that scaling is different along each dimension */
  DrawSquare(); /* draw the square 4 */
  ... 
 
 Four  instances of a square are drawn using the same primitive "square drawing" function by using transformations.

Scene Graph

The following code generates the above figure.

  ...
  glMatrixMode(GL_MODELVIEW); /* select the model-view matrix for subsequent matrix operations */ 
  glLoadIdentity(); /* model-view matrix = identity */
  glMultMatrix(m); /* model-view matrix = m * model-view matrix */ 
  DrawLink(); /* draws Link 1 */
  glPush(); /* saves the model-view matrix */
  glTranslate(x1,y1,z1); /* model-view = translation * model_view */
  DrawLink(); /* draws Link 2 */
  glRotation(th_rad,vx1,vy1,vz1);    /* model-view = rotation * model-view */
  glTranslation(x2,y2,z2); /* model-view = translation * model_view */
  DrawLink(); /* draws Link 4 */ 
  glPop(); /* model-view matrix = identity */
  glPush(); /* again save model-view matrix*/
  glRotation(th_rad,vx2,vy2,vz2);    /* model-view = rotation * model-view */
  glTranslation(x3,y3,z3); /* model-view = translation * model_view */
  DrawLink(); /* draws Link 3 */
  glPop(); /* model-view matrix = identity */ 
  ...   

Notes

Coordinate-Free Geometry

pdf file

2D Geometric Transforms

Also see 3D Geometric Transforms

Translation, rotation, scaling, and shearing are all examples of geometric transformations. Multiple copies of an object can be created by drawing the same object using a series of different transformation matrices.


(image courtesy of Robert Lansdale)

Affine Transformation

A transformation that preserves the parallelism of lines, but not necessarily angles or lengths.

Derivations

Suppose we transform an object (by translation, scale, rotation, shear) then a point p(x,y) of the object will be moved another point p'(x',y') of the transformed object. We want to derive the relationship between p' and p.

translation

trans(a,b):
x'=x+a
y'=y+b

scaling

scale(a,b):
x'=a x
y'=b y

shear

shearx(a):
x'=x+a y
y'=y

sheary(a):
x'=x
y'=y+a x

rotation

Imagine z-axis coming out of the paper. We define counter clock-wise rotation about z-axis as positive rotation and clock-wise rotation about z-axis as negative, i.e., we are following the right hand convention. If you align your right thumb with the positive z-axis (coming out of the page), the direction of the curl of your fingers is positive rotation.

rot(zaxis,angle_rad):
x'=x cos(a) - y sin(a)
y'=x sin(a) + y cos(a)

Homogeneous Coordinates

Transformation Matrices

The transformations defined above can be rewritten using 3x3 matrices and homogeneous coordinates. The general form of the matrix is:

The expressions derived earlier correspond to the following transformation matrices.

Translation Matrix
Scaling Matrix
Rotation Matrix

Composition and Inversion of Transformations

A series of transformations can be accumulated into a single transformation matrix. Suppose our object is defined as follows:

Suppose we wish to place the object in our scene like this:

This can be accomplished in several ways, one of which is:

Substituting (1) in (2) gives:

Comments on Matrix Composition

Hiearchical Models

Scene Graphs OR Transformation Hierarchies

Consider building the following model of a hand with one finger:

This can be constructed using a transformation hierarchy. In the following scene graph, circles represent transformations and squares represent geometry. The pseudocode on the left can be used to draw the scene.

f1: trans(d_hand,0) rot(z,th1) 
f2: trans(d1,0) rot(z,th2) 
f3: trans(d2,0) rot(z,th3) 

M=M*Thand 
draw_hand 
M=M*Tf1;
draw f1;
M=M*Tf2;
draw f2;
M=M*Tf3;
draw f3

Now consider drawing a hand with three identical fingers. We can create a more complex scene graph which uses multiple instances of a finger scene graph, as shown below. Because each of the fingers is defined relative to the hand coordinate system, a way is needed to restore the hand coordinate system before beginning to draw each finger. This is done through the pushMatrix() and popMatrix() function calls.

M=M*Thand 
draw_hand 
pushMatrix() 
M=M*Tf1a 
draw_finger() 
popMatrix() 
pushMatrix() 
M=M*Tf1b 
draw_finger() 
popMatrix() 
pushMatrix() 
M=M*Tf1c 
draw_finger() 
popMatrix() 

draw_finger() { 
  draw f1 
  M=M*Tf2 
  draw f2 
  M=M*Tf3 
  draw f3 

Many graphics systems maintain a stack for the current transformation matrix:

Exercises

Exercise 1

  1. Assuming 2D coordinates (3x3 homogeneous matrices), show that rotations and translations do not commute.
  2. Derive a 3x3 homogeneous matrix that scales an object by a factor S, along the line y = m x.

Exercise 2

An affine transformation can be completely specified by its action on three points. What is the matrix for the transformation that maps the points A(0,0) B(1,0) and C(1,0) to the points A'(1,2) B'(1,1) and C'(2,1) respectively?

Hint: 2D rotation, scaling, shear and translation are all affine transformations, of the form

p' = Mp,   where p=[x,y,1]T and M is a 3x3 matrix of the form:
[ a b c ]
[ d e f ]
[ 0 0 1 ]

Exercise 3

Assuming two-dimensional transformations, show that the following pairs of operations do or do not commute:

  1. Two rotations commute.
  2. A rotation and a uniform scaling (Sx=Sy) commute.
  3. A rotation and a non-uniform scaling do not commute.

Exercise 4

Reflections: Assume a two-dimensional space.

  1. Give the homogeneous (3 x 3) matrix representing a reflection about the x axis.
  2. Give the matrix for a reflection about a line which passes through the origin and makes an angle a with the x-axis (Hint: how do we rotate around non-origin points?).
  3. Given an n by n raster of pixels such that pixel a[x][y] is displayed at (x,y), write a code fragment to reflect the pixels about the line x=y.
  4. Reflect the same raster about the line x+y=n-1.

Exercise 5

Show that rot(z,theta1) rot(z,theta2) = rot(z,theta1 + theta2)

Exercise 6

Find the inverse of the following transformation matrix, assuming that the vectors A, B, and C are an orthonormal set. (Hint: there is a 'clean' solution).

[ Ax Bx Cx Tx ]
[ Ay By Cy Ty ]
[ Az Bz Cz Tz ]
[ 0  0  0  1  ]