CPSC 453
Transformations & OpenGL 3D Lecture VII
CPSC453
page 1
2D rotation revisited
As matrix multiplication:
It can be used for points or vectors Transforming endpoints is enough Other transformations can be expressed using matrix multiplication CPSC453
page 2
1
Transformations & Matrices CG transformations as matrix multiplications P’ = rotate( P, θ ) can be written as P’ = R(θ) P Transformation matrix
Multiple transformations single matrix
P1 = R(θ) P0 P2 = T(vx,vy,vz) P1 P3 = S(sx,sy,sz) P2 Or P3 = S(sx,sy,sz)T(vx,vy,vz)R(θ) P0 Combined transformation matrix
CPSC453
page 3
Transformations & OpenGL Current Transformation Matrix (CTM) Initially CTM = I glScalef() CTM CTM * S
glTranslatef() CTM CTM * T
… CTM is Post-multiplied Transformation specified in ‘reverse order’ Allows for hierarchical models
CPSC453
page 4
2
Transformations & OpenGL Current Transformation Matrix (CTM) Initially is set to the 4x4 identity matrix
vertices
Current Transformation
vertices
CPSC453
page 5
Example – hierarchical model void mydisplay() { glClear( GL_COLOR_BUFFER_BIT ); for( float angle = 0 ; angle < 360 ; angle += 30 ) { glLoadIdentity(); glRotatef( angle, 0, 0, -1 ); glTranslatef( 0, 0.75, 0 ); glScalef( 0.2, 0.2, 0 ); draw_square(); } glFlush(); }
CPSC453
page 6
3
Example – hierarchical model
CPSC453
page 7
Example – hierarchical model void draw_circle() { for( float angle = 0 ; angle < 360 ; angle += 30 ) { glPushMatrix(); glRotatef( angle, 0, 0, -1 ); glTranslatef( 0, 0.75, 0 ); glScalef( 0.2, 0.2, 0 ); draw_square(); glPopMatrix(); }} void mydisplay() { glClear( GL_COLOR_BUFFER_BIT ); for( float angle = 0 ; angle < 360 ; angle += 30 ) { glPushMatrix(); glRotatef( angle, 0, 0, -1 ); glTranslatef( 0, 0.75, 0 ); glScalef( 0.1, 0.1, 0.1 ); draw_circle(); glPopMatrix(); } glFlush(); } CPSC453
page 8
4
Matrix Stack glPushMatrix() glPopMatrix() glLoadIdentity() glLoadMatrix() glMultMatrix()
CPSC453
page 9
Transformations & OpenGL Transformation matrices in OpenGL are 4x4 P’ = M * P x’
A1,1 A1,2 A1,3 A1,4
x
y’
A2,1 A2,2 A2,3 A2,4
y
z’
A3,1 A3,2 A3,3 A3,4
z
w’
A4,1 A4,2 A4,3 A4,4
w
4x4 matrices better than 3x3, allow for more transformations P = ( x, y, z, w ) = homogeneous coordinates CPSC453
page 10
5
Homogeneous Coordinates
shape
point
vector
Previous notation 2D and 3D
homogeneous 2D, 3D
CPSC453
page 11
Translation glTranslate( vx, vy, vz )
CTM CTM * Tv
CPSC453
page 12
6
Scaling glScale(vx,vy,vz)
CPSC453
page 13
Rotation Rotation around x,y or z
CPSC453
page 14
7
Rotation Rotation around arbitrary vector glRotate(θ, x, y, z )
CPSC453
page 15
Homogeneous Coordinates Points ( x, y, z, 1 ) ( x, y, z, w ) ( x/w, y/w, z/w, w/w )
Translation * Vector = unchanged vector Translation * Point = new point Rotation * Vector = new vector Rotation * Point = new point
CPSC453
page 16
8