0% found this document useful (0 votes)
118 views48 pages

Transformation

The document discusses 3D transformations including: - Transformations are represented by coordinate matrices - Transformations include translation, scaling, and rotation about axes - Homogeneous coordinates allow affine transformations to be represented by single matrices - Rotation in 3D involves rotating about axes (x, y, z) using rotation matrices - Arbitrary axis rotation is achieved by aligning the axis with z then rotating, and undoing the alignment rotations
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
118 views48 pages

Transformation

The document discusses 3D transformations including: - Transformations are represented by coordinate matrices - Transformations include translation, scaling, and rotation about axes - Homogeneous coordinates allow affine transformations to be represented by single matrices - Rotation in 3D involves rotating about axes (x, y, z) using rotation matrices - Arbitrary axis rotation is achieved by aligning the axis with z then rotating, and undoing the alignment rotations
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 48

3D Transformation

o Instead of creating every o Transformations are


new object from scratch, measured in a coordinate
we may transform some matrix
existing objects o There are two possible ways
of specifying the Z-axis, which
o We also need to transform gives rise to a left-handed or
objects from one space to a right-handed system
another, e.g., world space
to view space/camera
space
o Instead of performing each
transformation alone, we
accumulate the
transformations in a matrix
before applying them to
objects
2D Transformation
o Scaling so
o X’ = aX x' = x cos f - y sin f
o Y’ = aY y' = y cos f + x sin f
o Translation
o X’=x+b  x'   cos  sin   x 
o Y’=y+b
     
 y'   cos sin   y 
o Rotation
x' = r cos (q + f ) = y
r cos q cos f - r sin q sin f

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) w0 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

o Rotation matrix: cos q  sin q 0


 sin q cos q 0

 0 0 1
3D Translation and Scaling

o In 3D four elements can be used to represent a


point so the translation and scaling
transformations may be represented as:

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

o In 2D, rotation is about a point


o In 3D, rotation is about a vector, which can be
done through rotations about x, y or z axes
o Positive rotations are anti-clockwise, negative
rotations are clockwise, when looking down a
positive axis towards the origin
y y
y

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

o about Z axis cosq  sinq 0 0


 sinq cosq 0 0
Rz   
 0 0 1 0
 
 0 0 0 1
Rotation Axis

o In general rotation vector does not pass through


coordinate origin

The idea is to turn rotation about an arbitrary axis to


rotations about coordinate axes
Rotation about an Arbitrary Axis
o We will first rotate about x then about y to align the arbitrary
axis with z axis, then do the required rotation about z, then
undo rotations about y and x axes a
sin  
k
c
cos  
k
k is length of Uxz
c

 Uyz is proj. of U on yz plane xz=(a,0,c)


 d is length of Uyz k is length of Uxz

(1)

Reverse translation and rotations abut x and y to move the arbitrary


vector U back to its initial position (leave only required rotation about U)
Rotation about an Arbitrary Axis

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

1 0 0 0  cosq 0 sinq 0 cosq  sinq 0 0


 0  0
0 cosq  sinq 0 1 0 0 R   sinq cosq 0

Rx    Ry   
 0 0
z
0 sinq cosq 0  sinq 0 cosq 0 0 1
     
0 0 0 1  0 0 0 1  0 0 0 1
Rotation about an Arbitrary Axis
o Alternatively, we can first rotate about y then about x to align
the arbitrary axis with z axis, then do the required rotation
about z, then undo unwanted rotations
- rotate about y axis by a to bring vector n to YoZ plane
Y
- rotate about x axis by  to align n with Z axis

nx ny
a  tan 1
  tan 1

nz d
o n(nx, ny, nz) X
  d  nx + nz
2 2

a d Final transformation matrix can be


obtained by changing x to y and y to
Z x in equation (1)
(1)
Transformation in OpenGL

o Because of the way OpenGL applies the


transformations to our matrices, the last
transformation command in your program is the
first one applied to the vertices
o The order of transformations is critical. If you do
transformation A and then transformation B, you
almost certainly get something different than if
you do them in the opposite order
Exercise - Rotate by 45 about x axis

o To rotate by 45 degrees about x-axis, use the


following matrix:

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)

o Translation by (tx, ty, tz) is o So translation by (1, 1, 1) is


achieved using the the following:
following matrix:

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)

o Rotation by 45 degrees about (1, 1, 1) can be


achieved by a few successive rotations about the
major axes, which can be represented as a single
composite transformation
1 n x n
Y a  tan   tan 1 y
nz d
d  nx + nz
2 2

o n(nx, ny, nz) X d  nx + nz  2  1.414


2 2

 
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 )

 cos( 45) 0 sin( 45) 0 1 0 0 0 cos( 45)  sin( 45) 0 0


 0 0 cos(35.2)  sin( 35.2)
 0 1 0 0  sin( 45) cos( 45) 0 0
=  sin( 45) 0 cos(45) 0

0 sin( 35.2) cos(35.2) 0  0 0 1 0
    
 0 0 0 1 0 0 0 1  0 0 0 1

1 0 0 0  cos( 45) 0 sin( 45) 0  x


0 cos(35.2)  sin( 35.2) 0  0 1 0 0   
    y
0 sin( 35.2) cos(35.2) 
0  sin( 45) 0 cos( 45) 0 z
 
0 0 0 1  0 0 0 1

 
1
neg. rotation about Y
Exercise
o Derive an expression for the o Question: What’s q , and
matrix transformation that rotates
rotation around which
an object by 45 degrees about a
vector, originating from P1(0,1,0) axis?
on the XoY plane and passing
through point P2(1,0,0). You do
NOT need to calculate matrix y
multiplication, but show the
components of the transformation P1(0,1,0)
matrix
o P1 and P2 are on XoY plane, so
vector P1P2 is on XoY plane P2(1,0,0)
o x
o After translation to the origin q
vector P1’P2’ is still on the XoY
plane, and can be rotated by q z
to align with X axis
Exercise

1 0 0 0  cosq 0 sinq 0 cosq  sinq 0 0


0 cosq 0  0 0  sinq
 sinq 1 0 cosq 0 0
Rx    Ry    Rz   
0 sinq cosq 0  sinq 0 cosq 0  0 0 1 0
     
0 0 0 1  0 0 0 1  0 0 0 1

The rotation transformation around P1P2 is

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

1 0 0 0  cos 45 sin 45 0 0 1 0 0 0cos 45  sin 45 0 0 1 0 0 0


0 1 0 1

 sin 45 cos 45 0 0 0 cos 45  sin 45 0 sin 45 cos 45 0 0

0 1 0  1
      
0 0 1 0  0 0 1 0 0 sin 45 cos 45 0 0 0 1 0 0 0 1 0
    0    
0 0 0 1  0 0 0 1  0 0 1 0 0 0 1 0 0 0 1
A
B C D is the rotation matrix
Inverse Matrix
o Inverse matrix: if AB=I (identity matrix), then B is called A’s
inverse matrix, written as

B  A , AA  I
1 1

o The inverse matrix of a rotation matrix is the transpose (by


flipping the matrix around diagonal) of the rotation matrix
o The inverse matrix of a (invertible) square matrix M can be
obtained by aligning side by side M and an identity matrix I,
and perform the same row operations on both M and I until
M is reduced to an identity matrix, and I is changed and
becomes the inverse matrix of M
Inverse of a Square Matrix

o Find the inverse of a 3 x 3 o Multiply row(1) by - 2 and add


square matrix A given by it to row(2). also multiply
row(1) by - 3 and add it to
[1 1 2] row(3).
A = [2 4 -3]
[3 6 -5] [1 1 2 | 1 0 0] row(1)
[0 2 -7 | -2 1 0] row(2)
o Augment the given matrix by [0 3 -11 | -3 0 1] row(3)
the identity matrix then use
basic row operations so that o Multiply row(2) by 3 and
the matrix on the left row(3) by -2, add them and
becomes identify matrix. put the row obtained in
row(3).
[1 1 2 | 1 0 0] row(1)
[2 4 -3 | 0 1 0] row(2) [1 1 2 | 1 0 0] row(1)
[3 6 -5 | 0 0 1] row(3) [0 2 -7 | -2 1 0] row(2)
[0 0 1 | 0 3 -2] row(3)
Inverse of a Square Matrix
o Multiply row(1) by -2 and add it to o Mow multiply all terms in row(1)
row(2) and put the row obtained by -1/2 and multiply all terms in
in row(1). row(2) by 1/2.
[-2 0 -11 | -4 1 0] row(1)
[ 0 2 -7 | -2 1 0] row(2) [1 0 0 | 2 -17 11] row(1)
[ 0 0 1 | 0 3 -2] row(3) [0 1 0 | -1 11 -7] row(2)
[0 0 1 | 0 3 -2] row(3)
o Multiply row(3) by 11 and add it to
row(1) and put the row obtained o Now that we have the identity
in row(1). matrix on the left, the 3 by 3
matrix on the right is the inverse
[-2 0 0 | -4 34 -22] row(1) matrix:
[0 2 -7 | -2 1 0] row(2)
[0 0 1 | 0 3 -2] row(3) [ 2 -17 11]
[-1 11 -7]
o Multiply row(3) by 7 and add it to [ 0 3 -2]
row(2) and put the row obtained
in row(2).
o A similar method based on row
operations can in principle be
[-2 0 0 | -4 34 -22] row(1) used to find the inverse of any
[ 0 2 0 | -2 22 -14] row(2) invertible (that has an inverse)
[ 0 0 1 | 0 3 -2 ] row(3) square matrix.
there are other ways to invert a matrix..

A = [a b] We now need to solve the first and second


equations simultaneously to find a' and c' in
[c d] terms of a, b, c and d.

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

a a' + b c' = 1 (equation 1) B = 1 / (a d - b c) [d -b]


[-c a]
c a' + d c' = 0 (equation 2)
a b' + b d' = 0 (equation 3)
c b' + d d' = 1 (equation 4)
Inverse of a 2 by 2 Square Matrix

o In solving the question in o Use the above formula for


the above example, we the inverse of a 2 x 2
have found a formula for square matrix to find the
the inverse of any inverse of
invertible 2 x 2 matrix.
o A 2 x 2 matrix will have an A = [3 5]
inverse if its determinant [2 3]
D=ad-bc
is not equal to zero since o According to formula
division by zero is not above the inverse of A is
allowed 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

OpenGL Matrix Stacks


I*T
I*T

o The modelling stack in action:


o glLoadIdentity(); I*T*T
o glTranslatef(0,10,0); 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 The scene is made of a tree (3


Cones), a pot (a Torus), a star (a
Triangle), and some presents (3
Cubes)
o OpenGL glutSolidCone creates a
cone at the coordinate origin
o We need to rotate the cone so
that it stands up in the positive Z
direction
o We need to translate the cones to
make the tree
o We need translations to draw the
pot using OpenGL glutSolidTorus
and the star using
GL_TRIANGLES
o We need translations to draw the
presents using OpenGL
glutSolidCube
Christmas Tree Example (3 cones+1 pot)

o void drawScene() { o //Draw bottom cone


o glClear(GL_COLOR_BUFFER_BI o glutSolidCone(1.0, 3.0, 10, 10);
T | GL_DEPTH_BUFFER_BIT); glPushMatrix();
o glMatrixMode(GL_MODELVIEW); o glTranslatef(0.0f, 0.0f, 1.0f);
o glLoadIdentity(); o //Draw middle cone
//Move the scene down 2 and o glutSolidCone(0.75, 2.0, 10, 10);
forward 8 units glPushMatrix();
o glTranslatef(0.0f, -2.0f, -8.0f); o glTranslatef(0.0f, 0.0f, 1.0f);
o //Continually rotate scene about o //Draw top cone
the y-axis o glutSolidCone(0.5, 1.0, 10, 10);
o glRotatef(angle, 0.0f, 1.0f, 0.0f); o glPopMatrix();
o //Save the transformations o glPopMatrix();
performed thus far
o glPushMatrix(); o //Draw a pot
o //Rotate about the x-axis so the o glPushMatrix();
cones are upright
o glRotatef(-90, 1.0f, 0.0f, 0.0f); o glTranslatef(0.0f, 0.0f, -0.2f);
o glColor3f(0.0f, 0.8f, 0.0f); o glColor3f(0.5f, 0.5f, 0.5f);
o glutSolidTorus(0.2, 0.3, 10, 10);
o glPopMatrix();
o glPopMatrix(); //Cancel rotation
required for the tree to be upright
Christmas Tree Example
o //Draw a flat star on top o //Draw some presents
o glPushMatrix(); o glPushMatrix();
o glTranslatef(0.0f, 3.0f, 0.0f); o glTranslatef(1.0f, 0.0f, 0.0f);
o glColor3f(1.0f, 1.0f, 0.0f); o glColor3f(0.5f, 0.0f, 0.8f);
o glBegin(GL_TRIANGLES); o glutSolidCube(0.5f);
o glVertex3f(-0.2f, -0.1f, 0.0f); o glPopMatrix();
o glVertex3f(0.2f, -0.1f, 0.0f); o glPushMatrix();
o glVertex3f(0.0f, 0.3f, 0.0f); o glTranslatef(0.8f, 0.0f, 0.8f);
o glVertex3f(-0.2f, 0.1f, 0.0f); o glColor3f(1.0f, 0.0f, 0.0f);
o glVertex3f(0.2f, 0.1f, 0.0f); o glutSolidCube(0.7f);
o glVertex3f(0.0f, -0.3f, 0.0f);
o glEnd();
o glPopMatrix();
o }
Christmas Tree Example
o //Draw a bow on top of the large o glPopMatrix(); //Undo the
red present using 2 torus. transformations required for the
o glPushMatrix(); bow
o //Move to the top of the present o glPopMatrix();
o glTranslatef(-0.1f, 0.35f, -0.1f); o //Undo the transformations
o //Rotate about current x axis so required for the red present
the bow is flat. o glPushMatrix();
o glRotatef(90, 1.0, 0.0, 0.0); o glTranslatef(-0.8f, 0.0f, -0.8f);
o glColor3f(0.8f, 0.8f, 0.8f); o glColor3f(0.2f, 0.2f, 0.95f);
o glutSolidTorus(0.03, 0.12, 10, 10); o glutSolidCube(0.5f);
o glPushMatrix(); o glPopMatrix();
o //Centre of the other torus o glutSwapBuffers();
o glTranslatef(0.2f, 0.2f, 0.0f);
o glutSolidTorus(0.03, 0.12, 10, 10); o A timer is added to make the
o glPopMatrix(); whole scene rotate about the y
axis in world coordinates
Lecture Room Example
Lecture Room Example
o /* This method simply creates o void createTable() { o // Create table top
a black pole by scaling and o // Create table legs o glPushMatrix();
translating a solid cube. */ o createPole(0.05,1.0,0.05); o glTranslatef(0.5, 1.025,
o glPushMatrix(); 0.5);
o void createPole(float xSize, o glTranslatef(0.0, 0.0, 0.95); o glColor3f(0.4509,0.2901,0
float ySize, float zSize) { .0705);
o glPushMatrix(); o createPole(0.05,1.0,0.05);
o glPopMatrix(); o glScalef(1.0, 0.05, 1.0);
o glTranslatef(xSize / 2, ySize / o glutSolidCube(1.0);
2, zSize / 2); o glPushMatrix();
o glTranslatef(0.95, 0.0, 0.95); o glPopMatrix();
o glColor3f(0.0,0.0,0.0); o } // end of createTable
o glScalef(xSize, ySize, zSize); o createPole(0.05,1.0,0.05);
o glutSolidCube(1.0); o glPopMatrix();
o glPopMatrix(); o glPushMatrix();
o } // end of createLeg o glTranslatef(0.95, 0.0, 0.0);
o createPole(0.05,1.0,0.05);
o /* The next method creates a o glPopMatrix();
table consisting of a table top
and four poles. The table top
is simply a scaled solid brown
cube. */
Lecture Room Example
o /* This next method is used to
create a podium, using a long o /* This creates a row of tables. It creates
solid cube and a smaller and four tables, leave a gap, and then create
three more tables. Current position is
thinner cube at the top for a head where the table closest to the wall with the
board.*/ windows will appear. */
o void createPodium() { o void createRowOfTables(boolean fullRow) {
o // Create podium base o for(int i = 0; i < 4; i++) {
o glPushMatrix(); o glPushMatrix();
o glTranslatef(0.5, 0.75, 0.5); o glTranslatef(1.3 * i, 0.0, 0.0);
o createTable();
o glColor3f(0.4509,0.2901,0.0705); o glPopMatrix();
o glScalef(1.0, 1.5, 1.0); o } // end of for
o glutSolidCube(1.0); o if(fullRow) {
o glPopMatrix(); o glPushMatrix();
o // Create back board o glTranslatef(1.3 * 5, 0.0, 0.0);
o glPushMatrix(); o for(int i = 0; i < 3; i++) {
o glTranslatef(0.5, 1.51, 0.975); o glPushMatrix();
o glTranslatef(1.3 * i, 0.0, 0.0);
o glColor3f(0.4509,0.2901,0.0975); o createTable();
o glScalef(1.0, 0.2, 0.05); o glPopMatrix();
o glutSolidCube(1.0); o // end of for
o glPopMatrix(); o glPopMatrix();
o } // end of createPodium o } // end of if
o } // end of createRowOfTables()
Lecture Room Example
o /* This method creates the walls o // Create back wall
of the room. Walls slightly vary in o glBegin(GL_QUADS);
colour just so that it gives an o glColor3f(1.0,1.0,1.0);
impression of shading helping in o glVertex3f(-5.5,-1.5,10.5);
distinguishing the difference o glVertex3f(-5.5, 1.5, 10.5);
between the walls. The current o glVertex3f(5.5, 1.5, 10.5);
position being the center of the
room. */ o glVertex3f(5.5, -1.5, 10.5);
o glEnd();
o void createWalls() {
o // Create right wall (wall with door)
o // Create front wall o glBegin(GL_QUADS);
o glBegin(GL_QUADS); o glColor3f(1.0,1.0,1.0);
o glColor3f(1.0,1.0,1.0); o glVertex3f(5.5, -1.5, -10.5);
o glVertex3f(-5.5,-1.5,-10.5); o glVertex3f(5.5, 1.5, -10.5);
o glVertex3f(-5.5, 1.5, -10.5); o glVertex3f(5.5, 1.5, 10.5);
o glVertex3f(5.5, 1.5, -10.5); o glVertex3f(5.5, -1.5, 10.5);
o glVertex3f(5.5, -1.5, -10.5); o glEnd();
o glEnd();
Lecture Room Example
o // Create left wall (wall with windows) o glColor3f(0.90, 0.90, 0.90);
o glBegin(GL_QUADS); o glVertex3f(-3.5, 1.5, -10.5);
o glColor3f(1.0, 1.0, 1.0);
o glVertex3f(-3.5, 2.0, -10.5);
o glVertex3f(-5.5, -1.5, -10.5);
o glVertex3f(-5.5, 1.5, -10.5); o glVertex3f(-3.5, 2.0, 10.5);
o glVertex3f(-5.5, 1.5, 10.5); o glVertex3f(-3.5, 1.5, 10.5);
o glVertex3f(-5.5, -1.5, 10.5); o glEnd();
o glEnd(); o glBegin(GL_QUADS);
o // Create ceiling o glColor3f(0.90, 0.90, 0.90);
o glBegin(GL_QUADS);
o glVertex3f(-5.5, 2.0, -10.5);
o glColor3f(0.95, 0.95, 0.95);
o glVertex3f(-3.5, 1.5, -10.5); o glVertex3f(-5.5, 2.0, 10.5);
o glVertex3f(-3.5, 1.5, 10.5); o glVertex3f(-3.5, 2.0, 10.5);
o glVertex3f(5.5, 1.5, 10.5); o glVertex3f(-3.5, 2.0, -10.5);
o glVertex3f(5.5, 1.5, -10.5); o glEnd();
o glEnd(); o glBegin(GL_QUADS);
o // This creates the raised section in the
ceiling o glColor3f(1.0, 1.0, 1.0);
o glBegin(GL_QUADS); o glVertex3f(-5.5, 1.5, 10.5);
o glColor3f(1.0, 1.0, 1.0); o glVertex3f(-5.5, 2.0, 10.5);
o glVertex3f(-5.5, 1.5, -10.5); o glVertex3f(-3.5, 2.0, 10.5);
o glVertex3f(-5.5, 2.0, -10.5); o glVertex3f(-3.5, 1.5, 10.5);
o glVertex3f(-5.5, 2.0, 10.5);
o glEnd();
o glVertex3f(-5.5, 1.5, 10.5);
o glEnd(); o glBegin(GL_QUADS);
o glBegin(GL_QUADS); o
Lecture Room Example
o glColor3f(1.0, 1.0, 1.0); o void createWindows() {
o glVertex3f(-5.5, 1.5, -10.5); o // Enable the texture
o glVertex3f(-5.5, 2.0, -10.5); o for(int i = 1; i < 5; i++) {
o glPushMatrix();
o glVertex3f(-3.5, 2.0, -10.5);
o glTranslatef(0.0, 0.75,3.9 * i);
o glVertex3f(-3.5, 1.5, -10.5); o glEnable(GL_TEXTURE_2D);
o glEnd(); o glBindTexture(GL_TEXTURE_2D,
o // Create floor lowWindows[i - 1]);
o glBegin(GL_QUADS); o createLowWindow();
o lDisable(GL_TEXTURE_2D);
o glColor3f(0.4549, 0.7333, 0.9843);
o glPopMatrix();
o glVertex3f(-5.5, -1.5, -10.5); o glPushMatrix();
o glVertex3f(-5.5, -1.5, 10.5); o glTranslatef(0.0, 2.022,3.9 * i);
o glVertex3f(5.5, -1.5, 10.5); o glEnable(GL_TEXTURE_2D);
o glVertex3f(5.5, -1.5, -10.5); o glBindTexture(GL_TEXTURE_2D,
o lEnd(); highWindows[i - 1]);
o createHighWindow();
o } // end of createWalls
o glDisable(GL_TEXTURE_2D);
o /* The next method adds four lower o glPopMatrix();
windows and 4 higher windows o glPopMatrix();
each with a different texture
mapping (using the textures stored o } // end of for
in highWindows and lowWindows. o } // end of createWindows
add all of the windows to the wall in
the room. The current position is
center of the room. */
Lecture Room Example
o /* This creates a polygon with texture o /* This method will create the entire
mapping of an image of a projection. scene which will include the walls,
The current position is the center of windows, tables, chairs, podium,
where the image will appear. It will use doors, doorway and projection. */
the current projection (projectionNo) in
the projections array. The projection o void Display(void) {
goes in the x and y axis. */
o glLoadIdentity();
o void createProjection() { o glLightfv(GL_LIGHT1, GL_POSITION,
position);
o // Enable the texture o gluLookAt(x, y, z, x + lx,y + ly,z + lz,
o glEnable(GL_TEXTURE_2D); 0.0f,1.0f,0.0f);
o glBindTexture(GL_TEXTURE_ o glClear(GL_COLOR_BUFFER_BIT |
2D, projections[projectionNo]); GL_DEPTH_BUFFER_BIT);
o glBegin(GL_QUADS); o glEnable(GL_DEPTH_TEST)
o glTexCoord2f(0.0,0.0);
o glVertex3f(-1.568, -1.25, 0.0); o …
o glTexCoord2f(0.0,1.0); o …
o glVertex3f(-1.568, 1.25, 0.0); o } // end of Display
o glTexCoord2f(1.0,1.0);
o glVertex3f(1.568, 1.25, 0.0);
o glTexCoord2f(1.0,0.0);
o glVertex3f(1.568, -1.25, 0.0);
o glEnd();
o glDisable(GL_TEXTURE_2D);
o } // end of showProjection
Rubik’s Cube
Modelling a Cube
{
1,2,3,4, // front face
y
Vertex array
5,6,7,8 // back face 7 -1,1,-1 Vi[0][1][2]:
3,2,7,6 // top face
1,3,5,4 // bottom face V1=(-1,-1,1)
3,6,5,4 // right face -1,1,1 2 6 1,1,-1 V2=(-1,1,1)
1,2,7,8 // left face V3=(1,1,1)
} V4=(1,-1,1)
o V5=(1,-1,-1)
cube[n].face[0].v[0].x=x-0.5; -1,-1,-1 8 3 1,1,1 V6=(1,1,-1)
cube[n].face[0].v[0].y=y-0.5; V7=(-1,1,-1)
cube[n].face[0].v[0].z=z-0.5;
V8=(-1,-1,-1)
cube[n].face[0].v[1].x=x+0.5;
cube[n].face[0].v[1].y=y-0.5; 1 -1,-1,1 5 1,-1,-1 x
cube[n].face[0].v[1].z=z-0.5;
cube[n].face[0].v[2].x=x+0.5;z
cube[n].face[0].v[2].y=y+0.5;
cube[n].face[0].v[2].z=z-0.5;
cube[n].face[0].v[3].x=x-0.5; 41,-1,1
cube[n].face[0].v[3].y=y+0.5;
cube[n].face[0].v[3].z=z-0.5;
An Example Program
o #include <glut.h>
o GLfloat light_diffuse[] = {1.0, 0.0, 0.0, 1.0}; /* Red diffuse light. */
o GLfloat light_position[] = {1.0, 1.0, 1.0, 0.0}; /* Infinite light location. */

o GLfloat n[6][3] = { /* Normals for the 6 faces of a cube. */ y


o {-1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {1.0, 0.0, 0.0}, -1,1,-1
o {0.0, -1.0, 0.0}, {0.0, 0.0, 1.0}, {0.0, 0.0, -1.0} }; 1,1,-1
-1,1,1 -1,-1,-1
o GLint faces[6][4] = { /* Vertex indices for the 6 faces of a cube. */
o {0, 1, 2, 3}, {3, 2, 6, 7}, {7, 6, 5, 4}, -1,-1,1 1,1,1 1,-1,-1
o {4, 5, 1, 0}, {5, 6, 2, 1}, {7, 4, 0, 3} };
x
o
o
GLfloat v[8][3]; /* array of 3D vectors (X,Y,Z values of vertices).*/
Void drawBox(void)
z 1,-1,1

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 [email protected],
with the Subject: “G53GRA Coursework Submission”
Practical This Week

o Getting started with OpenGL


o Try to run the Cube program
o Download BesierCurve.zip from course website
and try to understand and run the program

You might also like