CS 543: Computer Graphics
Lecture 4 (Part I): 3D Affine transforms
Emmanuel Agu
Introduction to Transformations
n Introduce 3D affine transformation:
n Position (translation)
n Size (scaling)
n Orientation (rotation)
n Shapes (shear)
n Previously developed 2D (x,y)
n Now, extend to 3D or (x,y,z) case
n Extend transform matrices to 3D
n Enable transformation of points by multiplication
Point Representation
n Previously, point in 2D as column matrix
x
x
y
y 1
n Now, extending to 3D, add z-component:
x Px
y or Py
z P=
P
1 z
1
Transforms in 3D
n 2D: 3x3 matrix multiplication
n 3D: 4x4 matrix multiplication: homogenous coordinates
n Recall: transform object = transform each vertice
n General form:
m11 m12 m13 m14 Qx Px
Xform of P
m21 m22 m23 m24 Qy Py
M = Q = M P
m m32 m33 m34
31 z z
0 1 1 1
0 0
Recall: 3x3 2D Translation Matrix
§Previously, 2D :
x' x tx
= +
y' y t
y
x' 1 0 tx x
y' = 0 1 ty * y
1 0 0 1 1
4x4 3D Translation Matrix
§Now, 3D : x' x tx
+
y' = y ty
z' z t
OpenGL: z
gltranslated(tx,ty,tz)
x' 1 0 0 tx x
y' 0 1 0 ty y
z' = 0 * z
0 1 tz
1 0 1
0 0 1
§Where: x’= x.1 + y.0 + z.0 + tx.1 = x + tx, … etc
2D Scaling
§Scale: Alter object size by scaling factor (sx, sy). i.e
x’ = x . Sx x' Sx 0 x
=
y’ = y . Sy y ' 0 Sy y
(4,4)
(2,2) Sx = 2, Sy = 2
(2,2)
(1,1)
Recall: 3x3 2D Scaling Matrix
x' Sx 0 x
=
y ' 0 Sy y
x' Sx 0 0 x
y ' = 0 Sy 0 ∗ y
1 0 0 1 1
4x4 3D Scaling Matrix
x' Sx 0 0 x
•Example:
y ' = 0 Sy 0 ∗ y •If Sx = Sy = Sz = 0.5
1 0 0 1 1
•Can scale:
• big cube (sides = 1) to
small cube ( sides = 0.5)
•2D: square, 3D cube
x' S x 0 0 0 x
y' 0 Sy 0 0 y
z' = 0 0 Sz 0 ∗
z OpenGL:
1 0 1 1 glScaled(Sx,Sy,Sz)
0 0
Example: OpenGL Table Leg
// define table leg
//--------------------------------------------------------------------------
------
void tableLeg(double thick, double len){
glPushMatrix();
glTranslated(0, len/2, 0);
glScaled(thick, len, thick);
glutSolidCube(1.0);
glPopMatrix();
}
Recall: 3x3 2D Rotation Matrix
x' cos(θ ) − sin(θ ) x
= (x’,y’)
y ' sin(θ ) cos(θ ) y
θ (x,y)
r
φ
x' cos(θ ) − sin(θ ) 0 x
y ' = sin(θ ) cos(θ ) 0 y
1 0 1
0 1
Rotating in 3D
n Cannot do mindless conversion like before
n Why?
n Rotate about what axis?
n 3D rotation: about a defined axis
n Different Xform matrix for:
• Rotation about x-axis
• Rotation about y-axis
• Rotation about z-axis
n New terminology
n X-roll: rotation about x-axis
n Y-roll: rotation about y-axis
n Z-roll: rotation about z-axis
Rotating in 3D
n New terminology
n X-roll: rotation about x-axis
n Y-roll: rotation about y-axis
n Z-roll: rotation about z-axis
n Which way is +ve rotation
n Look in –ve direction (into +ve arrow)
n CCW is +ve rotation
+ x
z
Rotating in 3D
Rotating in 3D
n For a rotation angle, β about an axis
n Define:
c = cos(β ) s = sin (β )
A x-roll:
1 0 0 0
0 c − s 0
Rx ( β ) = OpenGL:
0 s c 0
glrotated(θ, 1,0,0)
0 0 0 1
Rotating in 3D
A y-roll: c 0 s 0
Rules:
0 1 0 0
R y (β ) =
•Rotate row,
OpenGL:
0 c 0
column int. is 1
−s
glrotated(θ, 0,1,0) •Rest of row/col is 0
0 0 0 1
•c,s in rect pattern
A z-roll:
c − s 0 0
s c 0 0
Rz ( β ) =
OpenGL: 0 0 1 0
glrotated(θ, 0,0,1) 0 0 0 1
Example: Rotating in 3D
Q: Using y-roll equation, rotate P = (3,1,4) by 30 degrees:
A: c = cos(30) = 0.866, s = sin(30) = 0.5, and
c 0 s 0 3 4.6
0 1 0 0 1 1
Q= =
−s 0 c 0 4 1.964
0 0 0 1 1 1
E.g. first line: 3.c + 1.0 + 4.s + 1.0 = 4.6
Matrix Multiplication Code
Q: Write C code to Multiply point P = (Px, Py, Pz, 1) by a 4x4
matrix shown below to give new point Q = (Qx,Qy,Qz, 1). i.e.
where
m11 m12 m13 m14
Qx Px
m21 m22 m23 m24
Qy Py M =
Q = M P m31 m32 m33 m34
z z 0 1
1 1 0 0
Matrix Multiplication Code
n Outline of solution:
n Declare P,Q as array:
• Double P[4], Q[4];
n Declare transform matrix as 2-dimensional array
• Double M[4][4];
n Remember: C indexes from 0, not 1
n Long way:
• Write out equations line by line expression for Q[i]
• E.g. Q[0] = P[0]*M[0][0] + P[1]*M[0][1] + P[2]*M[0][2] +
P[3]*M[0][3]
n Cute way:
• Use indexing, say i for outer loop, j for inner loop
Matrix Multiplication Code
n Using loops looks like:
n for(i=0;i<4;i++)
{
temp = 0;
for(j=0;j<4;j++)
{
temp += P[j]*M[i][j];
}
Q[i] = temp;
}
n Test matrice code rigorously
n Use known results (or by hand) and plug into your code
3D Rotation About Arbitrary Axis
n Arbitrary rotation axis (rx, ry, rz)
n openGL: rotate(θ, rx, ry, rz)
n Without openGL: a little hairy!!
n Important: read Hill and Kelley, pg 220 - 223
y (rx, ry, rz)
z
3D Rotation About Arbitrary Axis
n Can compose arbitrary rotation as combination of:
n X-roll
n Y-roll
n Z-roll
M = Rz ( β 3 ) R y ( β 2 ) Rx ( β 1 )
3D Rotation About Arbitrary Axis
n Classic: use Euler’s theorem
n Euler’s theorem: any sequence of rotations = one rotation
about some axis
n Our approach:
n Want to rotate β about the axis u through origin and arbitrary
point
n Use two rotations to align u and x-axis
n Do x-roll through angle β
n Negate two previous rotations to de-align u and x-axis
3D Rotation About Arbitrary Axis
Ru ( β ) = R y (−θ ) Rz (φ ) Rx ( β ) Rz (−φ ) R y (θ )
Composing Transformation
n Composing transformation – applying several transforms
in succession to form one overall transformation
n Example:
M1 X M2 X M3 X P
where M1, M2, M3 are transform matrices applied to P
n Be careful with the order
n Matrix multiplication is not commutative
References
n Hill, chapter 5.3