Lecture 8 - fkm.utm.mykasim/eng_design/vr/Lecture8-3D-transformation-in... · Lecture 8 3D...
Transcript of Lecture 8 - fkm.utm.mykasim/eng_design/vr/Lecture8-3D-transformation-in... · Lecture 8 3D...
Lecture 8
3D Geometric Transformation
Applying transformation matrices
D
nnn
D
nn
zyx
zyx
zyx
zyx
zyx
zyx
yx
yx
yx
yx
yx
yx
nnn
nn
3
222
111
'''
'''
'''
2
22
11
''
''
''
1
1
1
1
1
1
1
1
1
1
1
1
222
111
22
11
Translation
1
010
001
yx
TR
1
0100
0010
0001
zyx
TR
2D Translation
3D Translation
•Component-wise addition of vectors
x’ = x + dx y’ = y + dyz’ = z + dz
•To move polygons: translate vertices (vectors) and redraw lines between them•Preserves lengths and angles•Does not require reference point
Rotation
100
0cossin
0sincos
RRotation 2D
•Rotation of vectors through an angle
x’ = x cos Ө – y sin Өy’ = x sin Ө + y cos Ө
•Preserves lengths and angles•Point of rotation point (0,0)
(X,Y)
A
(X cos - Y sin , x sin + Y cos)A’
Center of
rotation
(0,0)
Rotation at other point than origin point
Let say that the center of rotation is (x1, y1). Translation has to be carried before and after rotation
Procedure1. Convert the (x1, y1) to (0,0) by applying translation x
= -x1 and y= -y1. 2. Apply the rotation matrix3. Convert the (0,0) to its original point (x1, y1) by
applying translation x = x1 and y = y1.
111
010
001
111
010
001
1
1
1
1
1
1
22
11
''
''
''
22
11
yxyxyx
yx
yx
yx
yx
yx
R
nnnn
3D Rotation
1000
0cossin0
0sincos0
0001
XR
1000
0cos0sin
0010
0sin0cos
YR
1000
0100
00cossin
00sincos
ZR
Axis of rotation: x
Axis of rotation: z
Axis of rotation: y
If axis of rotation other than specified axis: apply translation before and after 3D rotation is applied
Foundation of transformations
Similar method can be applied on other transformation such as shear, reflection etc.
Important, if the transformation requires point (2D), axis (2D & 3D) and plane (3D) at the origin CS. Translation must be applied before and after applying the respective transformation.
Applying transformation composite matrices
Do this transformation on screen1. Draw rectangle any size
2. Translate dx = 6
3. Rotate it by 450
1. The same rectangle
2. Rotate it 450
3. Translate dx = 6
Q: Do both rectangles overlapping each other? Why?
Y
X0
1
1
2
2
3 4 5 6 7 8 9 10
3
4
5
6
Y
X0
1
1
2
2
3 4 5 6 7 8 9 10
3
4
5
6
Translation → Rotation
Rotation → Translation
Composite Transformations
Composite Transformation
C
nC
XX
'
321 ...
Composite Transformation:
Combination of multiple transformations must be in sequence.
Example 1
A cube (1 x 1 x 1 ) as shown in Figure 2a is transformed into the solid as shown in Figure 2b. Establish the transformation matrices for this transformation.
The scaling matrix transformation based on reference point of (0, 0, 0) is as in the following.
1000
000
000
000
Z
y
x
SS
S
S
Example 1: Solution
1004
0100
0010
0001
1000
0100
0040
0001
[Scaling][Translation]
Rename 3Dsolidcube.cpp and try this problem• Note: 1. Matrix in openGL is applied in reverse sequence, last will be applied first and first will be applied last.2. By assign variable to the scale matrix and this variable will be controlled by the idle function, show an animation of the cube becomes bigger.
Example 2
The available API function allows only to draw cube with ( l x l x l ) size.Based on this function and the transformation matrices, model the robotic arm with arm of ( 2 x 2 x 20) and (2 x 2 x 5) at the position as shown the figure. The center bottom of the lower arm is at ( 0, 0, 0).
Discuss the methodology to model both lower and upper arm to be positioned as in the figure
Step 1: Scaling to lower and upper
1000
02000
0020
0002
*1
armLLarm
The scaling of lower arm
The scaling of the
upper arm
1000
0500
0020
0002
*1
armUU arm
Example 2
Step 2: Translate the upper arm at the end of the lower arm
12000
0100
0010
0001
*1*2
armUU arm
Step 3: Rotate lower and upper arm along +ve Y axis by 30o
1000
0)30cos(0)30sin(
0010
0)60sin(0)30cos(
*1*3
oo
oo
armarm LL
The transformation for the
lower arm.
The transformation for the upper arm
1)60sin(200)60cos(20
0100
0010
0001
1000
0)30cos(0)30sin(
0010
0)30sin(0)30cos(
1)60sin(200)60cos(20
0100
0010
0001
*2*3
oo
oo
oo
oo
armarm UU
Example 2
Step 4: Rotate upper arm along +ve Y axisby 10o
1)60sin(200)60cos(20
0100
0010
0001
1000
0)10cos(0)10sin(
0010
0)10sin(0)10cos(
1)60sin(200)60cos(20
0100
0010
0001
*3*4
oo
oo
oo
oo
armarm UU
The center rotation is at the end of the lower
arm which is (20 cos(60o), 20 sin(60o)).
Therefore, translation must be applied
before and after the rotation matrix.
Example 2
Step 5: Rotate the lower and upperarm by 15o along +ve Z axis
1000
0100
00)15cos()15sin(
00)15sin()15cos(
*3*5
oo
oo
armarm LL
The transformation for the
lower arm.
The transformation for the
upper arm.
1)60sin(200)60cos(20
0100
0010
0001
1000
0100
00)15cos()15sin(
00)15sin()15cos(
1)60sin(200)60cos(20
0100
0010
0001
*4*5
oo
oo
oo
oo
armarm UU
Example 2
Example 3: Planet and Sun
You have to do animation of planet goes around sun using two solid sphere.
Rename 3Dcubesolid.cpp and modify the program to model the sun and the planet using sphere with 5 and 2 unit diameter respectively. The planet orbit is 10 unit radius.