Professional Documents
Culture Documents
L05 Transformation 2D 3D - VE
L05 Transformation 2D 3D - VE
L05 Transformation 2D 3D - VE
WHAT IS TRANSFORMATIONS?
(ALTERNATE INTERPRETATION)
2
COORDINATE SYSTEMS
•The use of coordinate systems is •Việc sử dụng các hệ tọa độ là cơ bản cho đồ
fundamental to computer graphics họa máy tính
•Coordinate systems are used to •Hệ tọa độ được sử dụng để mô tả vị trí của
describe the locations of points in
space các điểm trong không gian
4
WHY TRANSFORMATIONS?
y Scale
Translate World Coordinates
x
Modeling
Coordinates
Scale
Rotate
Translate
5
GRAPHICS PIPELINE
Transform
Illuminate
Transform
Clip
Project
Rasterize
Shear
Reflection
11
x s x 0 x 0
2D SCALING y = 0 +
s y y 0
Resizes an object in each dimension
x = a xx x + a xy y + bx
y y'
y = a yx x + a yy y + by
syy
y
x sxx
x' = x.S x
x x'
y ' = y.S y
12
ROTATION (2D)
Consider rotation about the origin by q degrees
radius stays the same, angle increases by q
14
2D TRANSLATION
Moves an object y y
by
x 1 0 x bx
y = 0 1 y + b x bx x
y
x = a xx x + a xy y + bx x' = x + bx
y = a yx x + a yy y + by
y ' = y + by
15
X-AXIS SHEAR
Shear along x axis (What is the matrix for y axis shear?)
y
x 1 shx x 0
y = 0 + y
1 y 0
x
x
x' = x + yshx
y ' = y + xshy
16
REFLECT ABOUT X AXIS
x x
x 1 0 x 0
y = 0 +
− 1 y 0
x' = x
y' = − y
17
ROTATING ABOUT AN ARBITRARY POINT
What happens when you apply a rotation
transformation to an object that is not at
the origin?
Solution:
Translate the center of rotation to the
origin
Rotate the object
Translate back to the original location
18
ROTATING ABOUT AN ARBITRARY POINT
y y
Say R is the rotation matrix to apply, and p
is the point about which to rotate
Translation to Origin: x = x − p x x
Rotation: x = Rx = R( x − p) = Rx − Rp
Translate back: x = x + p = Rx − Rp + p y y
The translation component of the composite
transformation involves the rotation matrix.
x x
What a mess!
19
SCALING AN OBJECT NOT AT THE ORIGIN
What also happens if you Điều gì cũng xảy ra nếu bạn áp dụng
apply the scaling transformation phép biến đổi tỷ lệ cho một đối tượng
không ở gốc?
to an object not at the origin?
Dựa trên sự xoay vòng về một thành
Based on the rotating about a phần điểm,
point composition,
Bạn nên làm gì để thay đổi kích thước
what should you do to resize an một đối tượng về trung tâm của chính
object about its own center? nó?
20
HOMOGENEOUS COORDINATES
21
HOMOGENEOUS COORDINATES
•If we use homogeneous coordinates, the
geometric transformations given above
can be represented using only a matrix Cartesian Homogeneous
pre-multiplication.
( x, y ) ⎯
⎯→( xh, yh, h), h 0
• A composite transformation can then be
represented by a product of the a b
corresponding matrices. , ⎯⎯ ( a, b, c), c 0
•Nếu chúng ta sử dụng tọa độ đồng nhất, các c c
phép biến đổi hình học được đưa ra ở trên có
thể được biểu diễn chỉ bằng cách nhân ma Examples: (5, 8) (15, 24, 3)
trận. (x, y) (x, y, 1)
•Một phép biến đổi tổng hợp sau đó có thể
được biểu diễn bằng một sản phẩm của các ma
trận tương ứng. [6]-22
HOMOGENEOUS COORDINATES
Use three numbers to represent a point
(x,y)=(wx,wy,w) for any constant w0 x a xx a xy bx x
y = a a yy
by y
Typically, (x,y) becomes (x,y,1) yx
Translation can now be done with matrix 1 0 0 1 1
multiplication!
23
BASIC TRANSFORMATION
Specifying a 2D-Translation:
Translation:
1 0 bx glTranslatef(tx, ty, 0.0);
0 1 b
y (The z component is set to 0 for 2D translation).
0 0 1
x 1 0 bx x
y = 0 1 by y
1 0 0 1 1
24
Specifying a 2D-Scaling with respect to the origin:
SCALE glScalef(sx, sy, 1.0);
x S x 0 0 x
y = 0 Sy 0 y
1 0 0 1 1
25
ROTATION ABOUT PIVOT POINT
cosq − sin q 0
Rotation: sin q cosq 0
0 0 1
Specifying a 2D-Rotation about a pivot point (xp,yp):
x cos q − sin q 0 x
y = sin q cos q 0 y
glTranslatef(xp, yp, 0);
glRotatef(theta, 0, 0, 1.0);
glTranslatef(-xp, -yp, 0);
26
x 1 0 0 x
y = 0 0 y
REFLECTIONS 1
0
−1
0
1 1
x − 1 0 0 x
Reflection about y
y = 0 1 0 y
x = − x y Initial 1
1
Object
0 0 1
x − 1 0 0 x
y = 0 −1 0 y
x 1
1 1
0 0
Reflection about
origin
Reflection about x
x = − x
y = − y
y = − y Reflection about x: glScalef(1, -1, 1);
Reflection about y: glScalef(-1, 1, 1);
Reflection about origin: glScalef(-1, -1, 1);
[6]-27
•A shear transformation in the x-direction (along
x)
SHEARING shifts the points in the x-direction proportional
to the y-coordinate.
•The y-coordinate of each point is unaffected.
•Shear along x, y
x 1 h 0 x
y = 0 1 0 y
1
1 1
0 0
x 1 0 0 x
y = h 1 0 y
x = x + hx y 1
0 0 1 1
y = y
[6]-28
INVERSE OF TRANSFORMATIONS
If: Examples:
x x −1
y = [T ] y T (t x , t y ) = T ( −t x ,−t y )
1
1 R −1
(q ) = R ( −q )
Then:
1 1
x x
−1
S (s x , s y ) = S ,
s
y = [T ] −1 y x sy
1
1 H x−1 ( h) = H x ( − h)
[6]-29
COMPOSITE TRANSFORMATIONS
Transformation T followed by x x
Transformation Q followed by y = [ R][Q][T ] y
Transformation R: 1 1
Order of Transformations
30
ORDER OF TRANSFORMATIONS
In composite transformations, the order of transformations is very important.
[6]-31
ORDER OF TRANSFORMATIONS (OPENGL)
OpenGL postmultiplies the current matrix with the new transformation matrix
glMatrixMode(GL_MODELVIEW); Current Matrix
glLoadIdentity(); [I]
glVertex2f(x,y); [T][R]P
Rotation followed by Translation !!
32
HOMOGENEOUS TRANSFORM ADVANTAGES
Unified view of transformation as Quan điểm thống nhất về phép biến
matrix multiplication đổi như phép nhân ma trận
Easier in hardware and software
Dễ dàng hơn trong phần cứng và phần mềm
To compose transformations, simply
multiply matrices Để soạn thảo các phép biến đổi, chỉ
Order matters: AB is generally not the cần nhân ma trận
same as BA Vấn đề đặt hàng: AB thường không giống
như BA
Allows for non-affine
transformations: Cho phép biến đổi không affine:
Perspective projections! Phối cảnh chiếu!
Bends, tapers, many others
Bẻ cong, chỉnh sửa, nhiều ứng dụng
33
khác
3D TRANSFORMATIONS
34
3D TRANSFORMATIONS
Same idea as 2D transformations a b c tx
Homogeneous coordinates: (x,y,z,w) d e f
ty
4x4 transformation matrices
g h i tz
Typical graphics packages allow for specification
of translation, rotation, scaling and arbitrary 0 0 0 1
matrices
OpenGL: glTranslate[fd], glRotate[fd],
glScale[fd], glMultMatrix[fd]
35
3D TRANSLATION
➢ Moving of object is called translation.
➢ In 3 dimensional homogeneous
coordinate representation, a point is
transformed from position P = ( x, y, z)
to P’=( x’, y’, z’) x 1 0 0 tx x
➢ This can be written as:- y 0 1 0 ty y
=
Using P’ = T . P z 0 0 1 tz z
1 0 0 0 1 1
36
SCALING (3D)
Expand or contract along each axis (fixed point of origin)
x’=sxx
y’=syy
z’=szz
p’=Sp
sx 0 0 0
0 sy 0 0
S = S(sx, sy, sz) =
0 0 sz 0
0 0 0 1
37
3D ROTATION
Rotation in 3D is about an axis in 3D space Xoay trong 3D là về một trục trong không
passing through the origin gian 3D đi qua điểm gốc
Using a matrix representation, any matrix Sử dụng biểu diễn ma trận, bất kỳ ma trận
with an orthonormal top-left 3x3 sub-matrix nào có ma trận con 3x3 trên cùng bên trái
is a rotation đều là một phép quay
Rows are mutually orthogonal (0 dot Các hàng là trực giao lẫn nhau (sản phẩm
product) 0 chấm)
Determinant is 1 Xác định là 1
Implies columns are also orthogonal, and Các cột ngụ ý cũng trực giao và chuyển vị
that the transpose is equal to the inverse ngang với nghịch đảo
38
3D ROTATION
Rotation in 3D is about an axis in 3D space x rxx rxy rxz 0 x
passing through the origin
y r ryy ryz 0 y
Using a matrix representation, any matrix = yx
with an orthonormal top-left 3x3 sub-matrix
is a rotation z rzx rzy rzz 0 z
Rows are mutually orthogonal (0 dot
product) 1 0 0 0 1 1
Determinant is 1 and for example :
Implies columns are also orthogonal, and
that the transpose is equal to the inverse rxx ryx + rxy ryy + rxz ryz = 0
39
3D ROTATION
In 2D, the only rotation possible was about Trong 2D, phép quay duy nhất có thể là về
the origin. gốc.
In 3D, there are 3 possible rotations, one Trong 3D, có 3 góc quay có thể, theo từng
about each of the x, y and z axes trục x, y và z
Positive rotations are anti-clockwise, Các phép quay dương là ngược chiều kim
negative rotations are clockwise, when đồng hồ, các phép quay âm theo chiều kim
looking down a positive axis towards the đồng hồ, khi nhìn xuống một trục dương
origin về phía gốc
y y y
x x x
z z z
40
Z-AXIS ROTATION
The equation for Z-axis rotaion
x’ = x cosθ – y sinθ
y’ = x sinθ + y cosθ
z’ = z
cos q − sin q 0 0
R = Rz(q) = sin q cos q 0
0
0 0 1 0
0 0 0 1
41
ROTATION ABOUT X AND Y AXES
Same argument as for rotation 1 0 0 0
0 cos q - sin q 0
about z axis R = Rx(q) =
For rotation about x axis, x is 0 sin q cos q 0
unchanged
For rotation about y axis, y is 0 0 0 1
unchanged
cos q 0 sin q 0
R = Ry(q) = 0 1 0 0
- sin q 0 cos q 0
0 0 0 1
42
OTHER ROTATION ISSUES
Exp: 1. Rotation is about an axis at the origin
Use the same trick as in 2D: Translate to origin, rotate, and translate back
again
Rotation is not commutative
Rotation order matters
Experiment to convince yourself of this
43
GENERAL ROTATION ABOUT THE ORIGIN
44
EG: ROTATION ABOUT ARBITRARY AXIS
HOW MANY MATRIXES ?
45
3D SHEARING
➢ Modify object shapes
➢ Useful for perspective projections
➢ When an object is viewed from different ➢ Matrix for 3d shearing
directions and at different distances, the
appearance of the object will be different. ➢ Where a and b can Be assigned any
real Value.
➢ Such view is called perspective
view. Perspective projections mimic what the
human eyes see.
46
3D REFLECTION
(REFLECTIONS-SECONDARY TRANSLATION)
47
3D REFLECTION
(REFLECTIONS-SECONDARY TRANSLATION)
48
OPENGL EXAMPLE
Rotation about z axis by 30 degrees with a
fixed point of (1.0, 2.0, 3.0)
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(1.0, 2.0, 3.0);
glRotatef(30.0, 0.0, 0.0, 1.0);
glTranslatef(-1.0, -2.0, -3.0);
glBegin(GL_TRIANGLES);
...
49
SUMMARY ScreenCoordinates
Coordinate systems
World vs. modeling coordinates Transform ation
W orld->Screen
2-D and 3-D transformations
Trigonometry and geometry
Matrix representations WorldCoordinates
Linear vs. affine transformations
Matrix operations Transform ation Transform ation Transform ation
Matrix composition O bject #1-> O bject #2-> O bject #3->
W orld W orld W orld
Hướng dẫn code:
https://www.youtube.com/watch?v=
_QajrabyTJc&t=500s O bject #1 O bject #2 O bject #3
Object Coordinates Object Coordinates Object Coordinates
50
HỆ TỌA ĐỘ THỰC
(WCS-WORLD COORDINATE SYSTEM)
51
HỆ TỌA ĐỘ THIẾT BỊ
(DCS-DEVICE COORDINATE SYSTEM)
Là hệ thống tọa độ của thiết bị nơi hiển thị hình ảnh và không
gian của đối tượng mà ứng dụng mô tả.
subselect.me
52
HỆ TỌA ĐỘ CHUẨN
(NCS - NORMALIZED COORDINATE SYSTEM)
NCS Dcs
Wcs chuyÓn ®æi 2
chuyÓn ®æi 1
53
BASIC VIEWING TRANSFORM
Phép biến đổi góc nhìn cơ bản là chuyển đổi từ hệ tọa độ mô hình sang hệ tọa độ
mà hình (Modelling co-ordinates to Screen Coordinates)
Vấn đề:
Thay đổi tỷ lệ hình ảnh
Hiệu ứng về tỉ lệ khi Zoom hình ảnh
Phần nào của hình vẽ sẽ hiển thị ?
Hiển thị tại đâu trên màn hình ?
Chuyển đổi sẽ được thực hiện ra sao?
54
2-DIMENSIONAL VIEWS (wxmax,wymax) (vxmax,vymax)
A Window : Cửa sổ, Phần hình ảnh của thế (wx,wy) (vx,vy)
giới thực sẽ được hiển thị
(vxmin,vymin)
A Viewport : Cổng nhìn, vùng màn hình nơi (wxmin,wymin)
hình ảnh thế giới thực sẽ hiển thị
55
PHÉP CHUYỂN ĐỔI
56
PHÉP BIẾN ĐỔI THEO MA TRẬN
Ma trận chuyển vị theo Window Ma trận biến đổi tỉ lệ Xvmax - Xvmin
Xwmax - Xwmin 0 0
Yv max − Yv min
1 0 0 [ S1] = 0 0
[T 1] = 0 1 0 Yw max − Yw min
0 0 1
− Xw − Yw 1
[T ] = [T 1]x[S1]x[T 2]
Xvmax - Xvmin
Ma trận chuyển vị theo tọa độ viewport 0 0
Xwmax - Xwmin
1 0 0 Yv max− Yv min
[𝑇2] = 0 1 0 [T ] = 0 0
𝑋𝑣 𝑌𝑣 1 Yw max− Yw min
Xvmax - Xvmin Yv max− Yv min
Xv min− Xw min Yv min− Yw min 1
Xwmax - Xwmin Yw max− Yw min
57
VÍ DỤ OPENGL - WINDOWS AND VIEWPORTS
58
HTTPS://LEARN.UNITY.COM/TUTORIAL/TRANSLAT
E-AND-ROTATE#5C8A44C2EDBC2A001F47CE1E
Transform and Vector –
https://www.youtube.com/watch?v=FeheZqu85WI
Lien he cac doi tuong
https://www.youtube.com/watch?v=2Ariq8vc5Vc
https://www.youtube.com/watch?v=rezoUiwgPrI&feature=youtu.be
https://www.youtube.com/watch?v=tNtOcDryKv4
Demo
Transformation models Unity3d
https://youtu.be/4RLbsJD-xqs
59
Tài liệu phục vụ
học tập sinh viên
CNTT&TT ĐHBK -
HN
Tài liệu sử dụng Hình ảnh , Text , Video trên mạng theo
nguyên tắc FU – CC và tuân thủ theo quy tắc đó
Sinh viên, người học hoàn toàn có quyền chỉnh sửa,
copy và tái suất bản theo CC & FU