Syllabus  Blank  Homework  Quizzes  
Notes  Labs  Scores  Blank 
A series of three computer operations convert an object's threedimensional coordinates to pixel positions on the screen.
We use a 4 x 4 matrix M to specify viewing, modeling or projection transformations.
//cube.cpp #include <GL/gl.h> #include <GL/glu.h> #include <GL/glut.h> void init(void) { glClearColor (0.0, 0.0, 0.0, 0.0); glShadeModel (GL_FLAT); } void display(void) { glClear (GL_COLOR_BUFFER_BIT); glColor3f (1.0, 1.0, 1.0); glLoadIdentity (); /* clear the matrix */ /* viewing transformation */ gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); glScalef (1.0, 2.0, 1.0); /* modeling transformation */ glutWireCube (1.0); glFlush (); } void reshape (int w, int h) { glViewport (0, 0, (GLsizei) w, (GLsizei) h); glMatrixMode (GL_PROJECTION); glLoadIdentity (); glFrustum (1.0, 1.0, 1.0, 1.0, 1.5, 20.0); glMatrixMode (GL_MODELVIEW); } int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode (GLUT_SINGLE  GLUT_RGB); glutInitWindowSize (500, 500); glutInitWindowPosition (100, 100); glutCreateWindow (argv[0]); init (); glutDisplayFunc(display); glutReshapeFunc(reshape); glutMainLoop(); return 0; } 
gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);:
glFrustum()  defines the projection transformation
(
a more commonly used alternative routine is gluPerspective() )
Theorem. If the vectors V_{1},V_{2}, ..., V_{n} form an orthonormal set, then the n x n matrix M constructed by setting the jth column equal to V_{j} for all 1 ≤ j ≤ n is orthogonal.
Proof. Since V_{j}'s are orthonormal, ( M^{T}M ) _{ij} = V_{i}.V_{j} = d_{ij} where d_{ij} is the Kronecker delta symbol. So M^{T} M = I. Therefore, M^{T} = M ^{1} .
Theorem. If the n x n matrix M is orthogonal, then M preserves lengths and angles.
Proof. Let M be orthogonal and P, Q are two vectors. Then
Example

Vectors and Points
Homogeneous Representation of a Point and a Vector
A vector
A point
Properties
Any affine combination of points is a legitimate point
Let
Then
or
which is a valid point as t + (1t) is 1. So a point plus a scaled vector is also a valid point.
Is A a point or a vector?
Answer:
Calculate the sum of coefficients
We use the preceeding ideas to show that the three medians of the above triangle meet at a point that lies twothirds of the way along each median. This point is the centroid.
Proof:
The point that is twothirds of the way from A to D lies at
which yields
Since this result is symmetrical in A, B and C, it must also be twothirds of the way along the median from B and twothirds of the way along the median from C. Hence the three medians meet there, and G is the centroid.
This result generalizes nicely for a regular polygon with N sides: the centroid is simply the average of the location of the N vertices, another affine combination
Linear interpolation between points R and Q:
In one dimension, it can be implemented as:
float lerp ( float q, float r, float t ) { return q + ( r  q ) * t; //same as ( 1  t ) * q + t * r; } 
Point P(t) is tween ( inbetween ) at t of points Q and R if t is a fraction and P(t) is computed the way along the straight line QR.
Point2 tween( Point2 P1, Point2 P2, float t ) { Point2 P; P.x = P1.x + ( P2.x  P1.x ) * t; P.y = P1.y + ( P2.y  P1.y ) * t; return P; } 
F( x, y ) = 0
e.g. Straight line through points P and Q:
e.g Circle with radius R centered at origin:
F(x, y ) = x^{2} + y^{2}  R^{2}
Advantages:
F(x, y) > 0 ( x, y ) outside curve
F(x, y) < 0 ( x, y ) inside curve
parametric Form:
e.g. A line passing through points P at t = 0, and Q at t = 1
x(t) = P_{x} + ( Q_{x}  P_{x} )t
y(t) = P_{y} + ( Q_{y}  P_{y} )t
e.g. ellipse with half width W, half height H
y(t) = H sin ( t ) for 0 ≤ t ≤ 2π
Advantages:
//draw the curve ( x(t), y(t) ) using // the array t[0] .... t[n1] of "sampletimes" glBegin( GL_LINES ) for ( int i = 0; i < n; ++i ) glVertex2f( x(t[i]), y(t[i]) ); glEnd(); 
P(t) = ( x(t), y(t), z(t) )
e.g circular helix
y(t) = sin ( t )
z(t) = bt
affinis ~ connected with
In general, an affine transformation is composed of linear transformations
(rotation, scaling or shear) and
a translation (or "shift"):
Geometrically, an affine transformation in Euclidean space is one that preserves:
Consider two points P and Q


An affine transformation can transform point P to point Q and the transformation can be represented by a matrix M
M = 

Q can be found by

= 


If m_{ii} = 1 and m_{ij} = 0 for i ≠ j, we have the identity matrix:
I = 

void glLoadIdentity( void ); 
Sets the current matrix to the 4 x 4 identity matrix 
void glLoadMatrix{fd}(const TYPE *m); 
Sets the sixteen values of the current matrix to those specified by m. 
void glMultMatrix{fd}(const TYPE *m); 
Multiplies the matrix specified by the sixteen values pointed to by m by the current matrix and stores the result as the current matrix. 
void glPushMatrix( void ); 
'Pushes current Matrix' onto stack. The current stack is determined by glMatrixMode(). 
void glPopMatrix( void ); 
'Pops matrix at top of stack' onto current stack. The current stack is determined by glMatrixMode(). 
Translation

void glTranslate{fd}(TYPE Tx, TYPE Ty, TYPE Tz); 
Multiplies the current matrix by a matrix that moves (translates) an object by the given Tx, Ty, and Tz values (or moves the local coordinate system by the same amounts). 
Scaling

Scaling and reflection: glScalef(2.0, 0.5, 1.0); 
void glScale{fd}(TYPE Sx, TYPE Sy, TYPE Sz); 
Multiplies the current matrix by a matrix that stretches, shrinks, or reflects an object along the axes. Each x, y, and z coordinate of every point in the object is multiplied by the corresponding argument Sx, Sy, or Sz. With the local coordinate system approach, the local coordinate axes are stretched, shrunk, or reflected by the Sx, Sy, and Sz factors, and the associated object is transformed with them. 
Rotations
Rotation about an axis
Elementary rotations about a coordinate axis:
Suppose

Example: Where will be the point P = ( 3, 1, 4 ) after a rotation of 30^{o} about the yaxis.
Solution:

Here θ = 30^{o}, cos θ = 0.866, and sin θ = 0.5
The new position is given by
Q = M.P = R_{y}(30^{o}).P = 

3D affine transformations can be composed and the result is another 3D affine transformation
Example: What is the matrix associated with an xroll of 45^{o}, followed by a yroll of 30^{o}, followed by a zroll of 60^{o},
Solution:
Euler's Theorem: Any rotation ( or sequence of rotations ) about a point is equivalent to a single rotation about some axis through that point. 
void glRotate{fd}(TYPE angle, TYPE x, TYPE y, TYPE z); 
Multiplies the current matrix by a matrix that rotates an object (or the local coordinate system) in a counterclockwise direction about the ray from the origin through the point (x, y, z). The angle parameter specifies the angle of rotation in degrees. 
glLoadIdentity(); glColor3f(1.0, 1.0, 1.0); draw_triangle(); /* solid lines */ glEnable(GL_LINE_STIPPLE); /* dashed lines */ glLineStipple(1, 0xF0F0); glLoadIdentity(); glTranslatef(20.0, 0.0, 0.0); draw_triangle(); glLineStipple(1, 0xF00F); /*long dashed lines */ glLoadIdentity(); glScalef(1.5, 0.5, 1.0); draw_triangle(); glLineStipple(1, 0x8888); /* dotted lines */ glLoadIdentity(); glRotatef (90.0, 0.0, 0.0, 1.0); draw_triangle (); glDisable (GL_LINE_STIPPLE); 
void draw_trangle( void ) { glBegin ( GL_LINE_LOOP ); glVertex2f( 0.0, 25.0 ); glVertex2f( 25.0, 25.0 ); glVertex2f( 25.0, 25.0 ); glEnd(); } 
Mesh msh; msh.readFile( DATA_FILE ); msh.drawMesh(); glFlush(); SDL_Delay ( 1000 ); glPushMatrix(); //save current matrix M glTranslatef ( 2.0, 0, 0 ); //move in xdirection msh.drawMesh(); glFlush(); SDL_Delay ( 1000 ); glRotatef ( 90, 0, 1, 0 ); //rotate about yaxis for 90 msh.drawMesh(); //? "translate then rotate" or "rotate translate" ? glFlush(); SDL_Delay ( 1000 ); glPopMatrix(); //restore maxtrix M glRotatef ( 90, 0, 1, 0 ); glTranslatef ( 2.0, 0, 0 ); //? "translate rotate" or "rotate translate" ? msh.drawMesh(); glFlush(); 
Suppose C is an affine combination of two points A and B:
i.e. lines remain lines, planes remain plane after transform
A straight line passing through points A, B is:
The image of L(t) is the same affine combination of the images of A and B:
A plane can be written as an affine combination of 3 points, A, B, and C:
Consider two lines with same direction b:
L_{2}(t) = B + bt
Under affine transformation M, they become
L'_{2}(t) = M.B + (Mb)t
Two planes with same directional normal can be described by
P_{2}(s,t) = B + as + bt
Under affine transformation
P'_{2}(t) = M.B + (Ma)s + (Mb)t
Volume of enclosed objects:
Note again: M_{2}M_{1} ≠ M_{1}M_{2}
glLoadIdentity(); //M = I glTranslatef(); //M = I.T = T glRotate(); //M = M.R = T.R glScale(); //M = M.S = T.R.S draw_a_point( P ); //Q = M.P = T.R.S.P
Note that the equivalent order of operation on P is : scale, rotate, translate
Suppose we want to rotate an object about a 'z' axis through ( 1, 0, 0 ). We can achieve this by moving everything by 1, do the rotation, and move things back by 1.
Question: Which of the following is correct?
glTranslatef ( 1.0, 0.0, 0.0 ); glRotatef( degrees, 0.0, 0.0, 1.0 ); glTranslatef ( 1.0, 0.0, 0.0 ); draw_object(); 
Or
glTranslatef ( 1.0, 0.0, 0.0 ); glRotatef( degrees, 0.0, 0.0, 1.0 ); glTranslatef ( 1.0, 0.0, 0.0 ); draw_object(); 
Example: Building an Articulated Robot Arm
glTranslatef (1.0, 0.0, 0.0); glRotatef ((GLfloat) shoulder, 0.0, 0.0, 1.0); glTranslatef (1.0, 0.0, 0.0); glPushMatrix(); glScalef (2.0, 0.4, 1.0); glutWireCube (1.0); glPopMatrix(); 
glTranslatef (1.0, 0.0, 0.0); glRotatef ((GLfloat) elbow, 0.0, 0.0, 1.0); glTranslatef (1.0, 0.0, 0.0); glPushMatrix(); glScalef (2.0, 0.4, 1.0); glutWireCube (1.0); glPopMatrix(); 

Robot Arm with fingers: