Professional Documents
Culture Documents
Transformation
Transformation
y' = r sin (q + f ) =
r sin q cosf + r cos q sin f x = r cos q
x y = r sin q
Homogeneous Coordinates
o In 2D, three elements can be used to represent a point, e.g.,
(x,y,1) to represent (x,y) or (wx,wy,w) w0 to represent (x,y)
o (x,y,w) is a homogeneous coordinate representing the point
(x/w, y/w)
o A point has many homogeneous representations in
homogeneous coordinates.
o Reason for homogeneous coordinates
o Allow an affine transformation to be represented by a single
matrix
o Useful for perspective transformation
o Allow bezier curves and sline curves to represent circles and
other conic sections
2D Transformation Matrices
o Translation matrix: 1 0 bx
0 1 b
y
0 0 1
o Scaling matrix: s x 0 0
0 s 0
y
0 0 1
1 0 0 trX
Translation 0 1 0 trY
matrix 0 0 1 trZ
0 0 0 1
Scaling
SX 0 0 0
matrix 0 SY 0 0
0 0 SZ 0
0 0 0 1
3D Rotation
x x
x
z z z
Major Axis Rotation Matrices
1 0 0 0
0 cosq sinq 0
o about X axis Rx
0 sinq cosq 0
0 0 0 1
cosq 0 sinq 0
o about Y axis 0 1 0 0
Ry
sinq 0 cosq 0
0 0 0 1
(1)
c a
0 0 Why?
k k Cos it’s
0 0 0 0
negative
a 0
c
0 rotation
k k
0 1
about y
0 0
about x axis about y axis
nx ny
a tan 1
tan 1
nz d
o n(nx, ny, nz) X
d nx + nz
2 2
1 0 0 0
1 0 0 0 1 1
0
cos 45 sin 45 0
0 0
R x (45)
2 2
0 sin 45 cos 45 0 0 1 1
0
0 0 0 1 2 2
0 0 0 1
Exercise - Translate by (1, 1, 1)
1 0 0 tx 1 0 0 1
0 0 t y 0
M T (t x , t y , t z )
1 1 0 1
0 1 tz M T (1,1,1)
0 0 0 1 1
0 0 0 1
0 0 0 1
Exercise - Rotate by 45 about axis (1, 1, 1)
nx = 1
a d tan 1
ny
tan 1
1
35.264
SO d
ny = 1 2
n 1
Z
nz = 1 a tan 1 x tan 1 45
nz 1
Q3 - Arbitrary Axis Rotation
o The composite
transformation can then be
represented as:
M R (1,1,1) Ry (a ) Rx ( ) Rz (q ) Rx ( ) Ry (a )
1 1
1 0 0 0 cos45 sin 45 0 0 1 0 0 0 cos45 sin 45 0 0 1 0 0 0
0 1 sin 45 cos45 0 0 0 cos45 sin 45 0 sin 45 cos45 0 0
1 0
0 1 0 1
0 0 1 0 0 0 1 0 0 sin 45 cos45 0 0 0 1 0 0 0 1 0
0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1
Exercise
1 T 1 0 0 0 1 0 0 0
cos 45 sin 45 0 0 cos 45 sin 45 0 0 cos 45 sin 45 0 0 0
sin 45 cos 45 0 1 0 0
0 sin 45 cos 45 0 sin 45 cos 45 0 0 1 1 0
0 0
0 0 1 0 0 0 1 0
0 0 1 0 0 0 1 0 0 0 1 0
0 0 0 1 0 0 0 1
0 0 0 1 0 0 0 1 0 0 0 1
1 0 0 0 1 0 0 0
cos 45 sin 45 0 0 cos 45 sin 45 0 0 1 0 0 0 0 0 0 0 1
sin 45 cos 45
1
1 0
0 0 sin 45 cos 45 0 0 0 1 0 0 0 1 0 0 0
0 0 1
TEST : 0 0 1 0 0 0 1 0 0 0 1 0
0 0 0 1 0 0 0 1
1 0 0 0 1
0 0 0 1 0 0 0
1 0 0 0
1
1 0 0 0
0 1 0 1 0 1 0 1
0 0 1 0 0 0 1 0
So, finally, the rotation transformation around P1P2 is 0 0 0 1 0 0 0 1
B A , AA I
1 1
Let B be the inverse of A given by Multiply the first equation by d and the
second equation by b and subtract the left
and right terms of the equations obtained to
B = [a' b'] find
[c' d']
a'(a d - b c) = d
From definition, we have AB = I a ' = d / (a d - b c)
[a b] [a' b'] = [1 0] Substitute a' in the second equation to obtain
[c d] [c' d'] [0 1] c'= - c / (a d - b c)
We need to find terms a', b', c' and d' To find b' and d', multiply equation (3) by d
and equation (4) by b and subtract the left
of B (inverse) in terms of a, b, c and d and right terms of the equations obtained to
which are the terms of matrix A. find
Multiply matrices A and B above.
b'(a d - b c) = - b or b' = - b / (a d - b c)
[a a' + b c' a b' + b d'] = [1 0]
[c a' + d c' c b' + d d'] [0 1] Substitute b' in equation (3) to obtain d'= a /
(a d - b c)
The above gives 4 equations Finally matrix B, the inverse of A, is given by
1 / [ 3*3 – 5*2 ] [ 3 -5 ]
[-2 3 ]
o Simplify to obtain inverse
of A:
[ -3 5 ]
[ 2 -3 ]
Transformation Exercise
o An object is defined by four vertices A (0,0,0), B (1,0,0), C
(0,1,0) and D (0,0,1). The object has to be rotated by +45
degrees about a line passing through point C (0,1,0) with a
direction V = j + k. Calculate the vertex positions after the
rotation.
z
1 1
0 0 1 0 0 0
2 2 1 1 1 0 0 0
0 0
1 1 0 1 0 1
Q 0 0 2 2
2 2 0 1 1
0 0 0 1 0
D
0 0 1 0
2 2 0
1
0 0 1
0 0 0 1 0 0 0
V
then left multiply Q with
inverse transformations that
A y move V back to its original position
C to arrive at a matrix M.
then multiply M with each of
B A, B, C, D to get vertex positions
x
after the rotation around V.
OpenGL Matrix Stacks
o OpenGL has three matrix o Each time you call any geometric
stacks: primitive function, the primitive is
o Model/View transformed by the current matrix
glMatrixMode(GL_MODELVIEW o Say you call glLoadIdentity(), and
); glTranslatef(0,0,10), the current
o Projection matrix stack is
glMatrixMode(GL_PROJECTIO 1 0 0 0
N);
o Textures 0 1 0 0
glMatrixMode(GL_TEXTURE); 0 0 1 10
o OpenGL keeps a
transformation stack, and 0 0 0 1
matrix at the top of the stack o If you now call glVertex3f(0,0,0) now,
is the current matrix C the vertex will appear at (0,0,10).
o Each matrix call, e.g., o If you need the current matrix for
glRotate, or glTranslate, or later use, you need to save a copy of
glScale creates a it on the stack using glPushMatrix().
transformation matrix M and
right multiplies it with the o When you need to use the saved
current matrix C, resulting in a matrix, you can use glPopMatrix().
new current matrix CM
I*T
o glPushMatrix();
o glTranslatef(10,0,0); I*T*T*R
I*T
o glRotatef(45,0,1,0);
o glPushMatrix();
I*T*T*R
o glRotatef(45,0,1,0);
I*T*T*R
o glPopMatrix(); I*T
o glPopMatrix();
o glPopMatrix(); I*T*T*R*R
I*T*T*R
I*T
Transformation in OpenGL
Pyramid Example
Pyramid Example
void showcube (double x, double y, o The first cube drawn uses the
double z, double angle) { call:
//Shows a cube at x,y,z rotated by
angle around y axis showcube(-9,0,0,45), which
draws a cube translated by 9 in
glPushMatrix(); the –x direction and rotated 45
glTranslatef(x,y,z); degrees around the y axis.
glRotatef(angle,0,1,0); o This results in the following
colorcube(); transformation matrix:
glPopMatrix();
}
o Before any showcube operation a
copy of the current matrix is
saved onto the matrix stack to
become current matrix.
o After the cube is drawn the
current matrix is popped and the
saved matrix becomes the
current matrix.
Pyramid Example
o void showpyramid(void)
{ o //Rear o //Rear
o glPushMatrix(); o showcube(-6,0,-3,45); o showcube(-3,3,-3,0);
o o showcube(0,0,-3,45); o showcube(3,3,-3,0);
o //Move to centre of o showcube(6,0,-3,45); o showcube(0,3,-6,0);
bottom row
o showcube(-3,0,-6,45);
o glTranslatef(0,-9,0);
o showcube(3,0,-6,45); o //3rd row
o //Bottom row o showcube(0,0,-9,45);
o //2nd row o showcube(-3,6,0,45);
o //Centre
o showcube(3,6,0,45);
o showcube(-9,0,0,45);
o showcube(-3,0,0,45); o //Centre o showcube(0,6,3,45);
o showcube(3,0,0,45); o showcube(0,6,-3,45);
o showcube(0,3,0,0);
o showcube(9,0,0,45);
o showcube(6,3,0,0); o //Top row
o //Front o showcube(-6,3,0,0); o showcube(0,9,0,0);
o showcube(-6,0,3,45);
o showcube(0,0,3,45);
o //Front o glPopMatrix();
o showcube(6,0,3,45);
o }
o showcube(-3,0,6,45); o showcube(-3,3,3,0);
o showcube(3,0,6,45);
o showcube(3,3,3,0);
o showcube(0,0,9,45);
o showcube(0,3,6,0);
Christmas Tree Example
o {
o int i; • faces[i][k] is index to kth vertex of the ith face
o for (i = 0; i < 6; i++) { • &v[faces[i][k]][0] is a pointer to kth vertex of ith face
o glBegin(GL_QUADS); • v[0][1][2]: x,y,z coordinates of a vertex
o glNormal3fv(&n[i][0]);
o glVertex3fv(&v[ faces[i][0] ] [0]);
o glVertex3fv(&v[ faces[i][1] ] [0]);
v[0][0] = v[1][0] = v[2][0] = v[3][0] = -1;
o glVertex3fv(&v[ faces[i][2] ] [0]); v[4][0] = v[5][0] = v[6][0] = v[7][0] = 1;
o glVertex3fv(&v[ faces[i][3] ] [0]); v[0][1] = v[1][1] = v[4][1] = v[5][1] = -1;
o glEnd(); v[2][1] = v[3][1] = v[6][1] = v[7][1] = 1;
o } v[0][2] = v[3][2] = v[4][2] = v[7][2] = 1;
o } v[1][2] = v[2][2] = v[5][2] = v[6][2] = -1;
An Example Program
o Void display(void) o /* Use depth buffering for hidden surface
o { elimination. */
o glClear(GL_COLOR_BUFFER_BIT |
GL_DEPTH_BUFFER_BIT); o glEnable(GL_DEPTH_TEST);
o drawBox(); o /* Setup the view of the cube. */
o glutSwapBuffers(); o glMatrixMode(GL_PROJECTION);
o }
o Void init(void) /* field of view, aspect ratio, Z near, Z far */
o { o gluPerspective(40.0, 1.0, 1.0, 10.0);
o /* Setup cube vertex data. */
o v[0][0] = v[1][0] = v[2][0] = v[3][0] = -1; o glMatrixMode(GL_MODELVIEW);
o v[4][0] = v[5][0] = v[6][0] = v[7][0] = 1;
o v[0][1] = v[1][1] = v[4][1] = v[5][1] = -1; /*eye is (0,0,5), center is (0,0,0), up is Y direction */
o v[2][1] = v[3][1] = v[6][1] = v[7][1] = 1; gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.);
o v[0][2] = v[3][2] = v[4][2] = v[7][2] = 1;
o v[1][2] = v[2][2] = v[5][2] = v[6][2] = -1; /* Adjust cube position to be aesthetic angle*/
o /* Enable a single OpenGL light. */ o glTranslatef(0.0, 0.0, -1.0);
o glLightfv(GL_LIGHT0, GL_DIFFUSE, o glRotatef(60, 1.0, 0.0, 0.0);
light_diffuse); o glRotatef(-20, 0.0, 0.0, 1.0);
o glLightfv(GL_LIGHT0, }
GL_POSITION, light_position);
o glEnable(GL_LIGHT0);
o glEnable(GL_LIGHTING);
Coursework
o Criteria
o Should not do exactly the same as the examples given.
Should be creative
o Submission deadline
o 4pm, Friday, 3rd December 2010
o Submission method
o Hardcopy of the report to the School Office on the
ground floor
o Email submission of a zip file, containing the source
code and softcopy of the report, to bai@cs.nott.ac.uk,
with the Subject: “G53GRA Coursework Submission”
Practical This Week