Phép biến đổi - Transformations Trong kỹ thuật đồ hoạ 3 bước: Mô hình, Tô trát và Hiên thị modeling, rendering, displaying Với Modeling Mô hình hóa : Transformation: là phép án
Trang 11
Transformations
Le Tan Hung Email: hunglt@it-hut.edu.vn
Trang 2Phép biến đổi - Transformations
Trong kỹ thuật đồ hoạ 3 bước: Mô hình, Tô trát
và Hiên thị (modeling, rendering, displaying)
Với Modeling ( Mô hình hóa) :
Transformation: là phép ánh xạ tọa độ điểm hay vector thành tọa độ hay vector khác
modeling
coordinate Modeling
transformation
Viewing transformation
world coordinate
viewing coordinate ( eye
coordinate)
Trang 33
Phép biến đổi Transformations
Biến đổi mô hình hoá - Modeling transformations
– build complex models by positioning simple components
Biến đổi tạo góc nhìn - Viewing transformations
– placing virtual camera in the world
– transformation from world coordinates to camera
coordinates
Biến Phép chiếu – Projection Transform
Trang 4Transformations - Modeling
world
Trang 55
Phép biến đổi Affine
Affine Transformations?
Ph ép biến đổi Affine l à phép biến đổi tọa độ
điểm đặc trưng của đối tượng thành tập tương
ứng các điểm mới để tạo ra các hiệu ứng cho
toàn đối tượng
– Ví dụ: phép biến đổi tọa độ với chỉ 2 điểm đầu cuối của đoạn thẳng tạo thành 2 điểm mới mà khi nối chúng với nhau tạo thành đoạn thẳng mới
Các điểm nằm trên đoạn thẳng sẽ có kết quả là điểm nằm trên đoạn thẳng mới với cùng phép biến đổi thông qua phép nội suy
Trang 6Modeling Transformations
Transform objects/points
Transform coordinate system
Trang 77
Biểu diễn Ma trận
Việc biến đối các đối tượng làm thay đổi các
điểm P thành các điểm Q theo thuật toán
Việc biến đổi P sử dụng tọa độ của P (Px,Py) ánh
Trang 8x y
x
t
t P
P d
c
b a
Q Q
x x
x
x aP bP t
y y
y
y cP dP t
Trang 99
Các phép biến đổi hình học hai
chiều
Phương pháp biểu diễn đối tượng P = [ x y ]
Phép biến đổi vị trí điểm
Thực thi phép biến đổi đúng trên 1 điểm ảnh sẽ đúng
trên toàn bộ đối tượng
b
a T
' '
y dy
bx
* y
d c
b
a x
p M
p W
Trang 10Phép biến đổi
Phép bất biến
Phép biến đổi tỉ lệ - Scaling
A scaling changes the size of an object with two scale
Trang 1111
Phép quay- Rotation
x = cos , y = sin ;
x’ = cos ( + ), y’ = sin ( + ) ;
x’ = ( cos cos - sin sin )
sin
cos]
[T
Trang 12Thuộc tính cơ bản của phép biến
đổi Affine Transformations
– We need only compute the image of the two endpoints
of the original line and then draw a straight line between them
Trang 13 Preservation of proportional distances
– Preservation of proportional distances means that points of lines remain mid-points
mid- Affine transformations change volume by | Det(M) |;
Trang 14Kết hợp các phép biến đổi
Composition of Affine Transforms
Any affine transformation can be
decomposed into elementary
Trang 15 Usually an application requires that we build a
complex transformation out of several elementary ones
– e.g translate an object, rotate it, and scale it, all in one move
These individual transformations combine into one
overall transformation
This is called the composition of transformations
The composition of two or more affine transformations
is also an affine transformation
Trang 16Thuộc tính
Tác động lên tập các điểm đặc trưng của đối
tượng tạo thành phép biến đổi cho đối tượng
We have defined each transformation by their effects on single points
In practice these will be applied to multiple points to
T
Trang 17(c 1 ,c 2 )
(0,0)
Trang 18Pivotal points
Often we wish to rotate or scale with respect to some
pivotal point, not the origin
Most significantly, we often wish to rotate or scale an
object about its centre, or midpoint
In this way, the object’s location does not change
To do this, we relate the rotation or scaling about the
pivotal point V, to an elementary rotation or scaling about the origin
– We first translate all points so that V coincides with the origin
– We then rotate or about the origin
Trang 19 Unfortunately, the translation portion is not a
matrix multiplication but must instead be added as
an extra term, or vector
What we need is a “trick”, so that translations can
be represented in matrix multiplication form
This then means that they can be easily
composed with other transformations, by simply multiplying the matrices together
Trang 20Tọa độ đồng nhất
Homogeneous Transform
tọa độ đồng nhất của các vector vị trí
ở đó tọa độ điểm được mô tả dưới ma trận [
Trang 21– Order matters: AB is generally not the same as BA
Cho phép kết hợp với cả các phép biến đổi đặc biệt không tuyến tính khác(non-affine) như:
– Phép chiếu phối cảnh - Perspective projections!
– Uốn - Bends, Vuốt tapers v.v.v
Trang 22Phép biến đổi với tọa độ đồng nhất
0 ]
[
n m
d c
b a
T
] 1 [
1
0 1 0
0 0 1
] 1 [
] 1 ' '
n m
y x
Trang 2323
Phép tỉ lệ
] 1 2 1
[ 1
0 0
0 2 0
0 0
1 ] 1 [
] 1 ' '
S y
x y
Trang 24
0
0 cos
sin
0 sin
cos ]
1 [
] 1 '
y
x
]1cos
.sin
.sin
.cos
Trang 2525
Phép biến đổi tổng hợp
Trang 26Ma trận biến đổi 3 chiều
3D Matrix Transformations
Các phép biến đổi chuyển vị - translation, tỉ
lệ-scaling và quay-rotation sử dụng trong không
gian 2D đều co thể mở rộng trong không gian 3D
Again, using homogeneous coordinates it is
possible to represent each type of transformation
in a matrix form
In 3D, each transformation is represented by a 4x4 matrix
Trang 2727
Các phép biến đổi hình học 3 chiều
m l
r j
i g
q f
e d
p c
b a
]
[T
Trang 280 0
0 0
1 0
0 0
0 1
)]
, ,
(
[
dz dy
dx
dz dy
dx
T
Trang 290 0
0 3
0 0
0 0
2 0
0 0
0 1
1
1
s s
s z
y x
z y
[
] 1 3
2
1
.
Trang 310 0
0 0
0 0
0 0
0 1
sin
cos ]
0
0 1 0
0
0 0 cos
sin
0 0 sin
cos ]
Tz
Trang 320 0
0 cos
0 sin
0 0
1 0
0 sin
0 cos
] [
Trang 330 0
0 1
0 1
0 1
1
1
i g
f d
c b
z y
x z
y
[
] 1 [ x yd gz bx y iz cx fy z
Trang 34Phép lấy đối xứng
( reflections-secondary translation)
Trang 350 1
0 0
0 0
1 0
0 0
0 1
]
[
z y
00
00
00
00
01
sin
cos)]
01
00
00
10
00
01
1
z y
)cos1(sin
)cos1(0
0cos
sin0
0sin
cos0
00
01
z y
Tth
Trang 36Quay quanh một trục bất kỳ
Trang 37 Quay quanh trục x sao cho P1P2 trùng với trục z
Quay quanh trục z sao cho P1P3 nằm trên mặt phẳng (y, z)
Euler’s Theorem: Every rotation around the origin
can be decomposed into a rotation around the x-axis followed by a rotation around the y-axis followed by a rotation around the z-axis
Trang 38Biến đổi góc nhìn Viewing Transformation
Lê Tấn Hùng
Trang 3939
Nội dung
Phép biến đổi nhìn cơ bản
Biến đổi hệ thống tọa độ
Biến đổi 3D viewing
Ví dụ
Trang 40Hệ tọa độ thực
(WCS-World Coordinate System)
Là hệ tọa độ của đối tượng
được các chương trình
ứng dụng sử dụng để mô
tả tọa độ của các đối
tượng trong thế giới thực
Đơn vị trong hệ thống tọa
độ phụ thuộc vào không
gian và kích thước của đối
tượng được mô tả, có thể
từ A0, nm, mm đến m,
km
Trang 4141
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ả
Thiết bị hiển thị
subselect.me
Vùng tọa độ thiết bị VGA=640x480
Trang 42Hệ tọa độ chuẩn
(NCS - Normalized Coordinate System)
Chuyển đổi hệ tọa độ
Giải quyết vấn đề khi ứng dụng chạy trờn cỏc thiết bị khỏc nhau
Cú kớch thước 1x1
Wcs
chuyển đổi 1
NCS chuyển đổi 2 Dcs
Trang 4343
Phép biến đổi nhìn cơ bản
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?
Trang 44max ,vy max)
Trang 4545
Phép chuyển đổi
Trang 46Phép biến đổi theo ma trận
Ma trận chuyển vị theo Window
0
0 0
1 1
Yw Xw
0
0 min max
min max
0
0
0 Xwmin
Xwmax
-Xvmin -
Xvmax
] 1
[
Yw Yw
Yv Yv
S
Trang 47max min
min Xwmin
Xwmax
-Xvmin -
Xvmax min
min
0 min
max
min
max 0
0
0 Xwmin
Xwmax
-Xvmin -
Xvmax
]
[
] 2 [ ] 1 [ ] 1 [ ]
[
Yw Yw
Yv
Yv Yw
Yv Xw
Xv
Yw Yw
Yv
Yv T
T x S x T T
Trang 48Ví dụ OpenGL - Windows and Viewports
Plotting a function revisted
//set the viewing coordinates
setWindow(xmin, xmax, ymin,
Trang 511 0
0 1
0
0
ty
tx ty
tx
1 0
1
ty
tx ty
tx
1 0 0
1 0
0 1
1
0
ty
tx ty
tx
1 0 0
1 0
0 1
Trang 52O
Trang 530
0 Q
sx v
Trang 54cos 1
(
sin )
cos 1
(
1 1
1 1
y x
cos 1
( sin
sin )
cos 1
( cos
1 1
1 1
y x
cos 1
( cos
sin )
cos 1
( sin
1 1
1 1
y x
O =
v =
u =
Trang 56Quan sát 3D - 3D Viewing
Hiển thị hình ảnh 3D trên các thiết bị
2D
Mỗi 1 quan sát 3D trên cùng 1 đối
tượng chỉ ra trạng thái chiếu của
điểm quan sát đó
– HÌnh ảnh chiếu 2D của đối tượng 3D
phụ thuộc vào người quan sát
Các quan sát khác nhau được phân
biệt bởi thông số: vị trí position, hướng
orientation, và vùng nhìn field of view
Trang 57transformation
world coordinate
viewing coordinate (eye coordinate)
Viewing transformation
Trang 58The Viewing Process
Để tạo 1 cảnh nhìn gồm các bước
– Mô tả biểu diễn hình học
– Định nghĩa điểm nhìn – Camera
Chuyển đổi từ đối tượng 3D sang 2D
Xác định vùng hiển thị của đố tượng
Xác định camera để có thể tạo thành phép chiếu từ 3D thành 2D
Trang 60Viewing Coordinates
Có thể sử dụng mô hình hệ
thống tọa độ quan sát trên các
thông số gồm:
– Điểm nhìn (VRP View Reference
Point ): điểm gốc quan sát Vị trí của Camera)
– Vector mặt phẳng quan sát (VPN
View Plane Normal vector ): viewing direction)
– Vector hướng quan sát (VUP View
UP vector ): establishes orientation of
“camera”
) , , (n1 n2 n3
N
N n
N V
Three mutually orthogonal basis vectors:
Trang 61M WC,VC = T (-a, -b, -c )
zW
xW
yW
Trang 62and
,
3 2 1
3 2 1
3 2 1
v v v
u u
u
n v
u
Ma trận biến đổi tọa độ thế giới sang tọa độ quan sát :
M = R T
(xw, yw, zw) and (u, v, n) 2 hệ tọa độ với cùng 1 gốc
Vector đơn vị (u, v, n) được định nghĩa theo WCS
Trang 6363
3D Viewing Transformation
Với điểm quan sát VRPoint (x0, y0, z0) the Viewing
Transformation can be defined by composition of the following two transformations:
0 0
1 0 0
0 1 0
0 0 1
0 0 0
z y
0
0 0 0
3 2
1
3 2
1
3 2
1
n n
n
v v
v
u u
u
R
T R
Trang 641 0 0
0 1 0
0 0 1
0 0 0
z y x
Trang 6565 65
1 0
0 0
0 0
0
3 2
1
3 2
1
3 2
n
v v
v
u u
u u
u
R u
Trang 66• Recall we’ve re-expressed our world geometry in eye’s frame
reference
• To do projection, we use a synthetic camera by introducing a
viewing frustum after the viewing transformation
Trang 6767
Camera trong OpenGL
Các hàm cơ bản
– glOrtho() and gluLookAt()
– these created parallel projections
We now want to modify a scene to give us perspective projections
– lines converge as they get further away
Trang 68Demo : Viewing
from Nate Robins
(Utah)
Trang 7171
The Camera
– The camera is positioned using a combination
of translations and rotations
– Think if the camera being in the same location
as the viewers eye
Trang 72The Camera
eye(x,y,z)
up(x,y,z)
Trang 7373
The Camera
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(eye.x, eye.y, eye.z,
Trang 7575
Camera Movement
A camera has six degrees of freedom:
1. slid in 3 directions; and
2. rotated in 3 directions
Camera Movement
– The camera can move along its axes
– This is called sliding the camera
Trang 77– the camera can be tilted in different directions to look
at different parts of the scene
Trang 78The Camera - Camera Movement
We use a plane analogy to describe the cameras
movement
a rotation from the horizontal along the length is called PITCH
a rotation from the horizontal along the width is called ROLL
a rotation around the vertical is called YAW
Trang 7979
The Camera
Pitch
v’ = cos(θ)v – sin(θ)n n’ = sin(θ)n + cos(θ)v
n
v
Trang 8181
The Camera
Yaw
n’ = cos(θ)n - sin(θ)u u’ = sin(θ)n + cos(θ)u
u
n
Trang 82A 3D Scene
Notice the presence of
the camera, the
projection plane, and
the world
coordinate axes
Viewing transformations define how to acquire the image
on the projection plane
Trang 8383
Viewing Transformations
Create a camera-centered view
Camera is at origin
Camera is looking along negative z-axis
Camera’s ‘up’ is aligned with y-axis
Trang 842 Basic Steps
Align the two coordinate frames by rotation
Trang 8585
2 Basic Steps
Translate to align origins
Trang 86Creating Camera Coordinate
Specify a vector in world
space that we wish to
point up in camera
image, the up vector
Intuitive camera
movement
Trang 8787
Constructing Viewing
Transformation, V
Create a vector from eye-point to lookat-point
Normalize the vector
Desired rotation matrix should map this
vector to [0, 0, -1]T Why?
Trang 88Constructing Viewing
Transformation, V
Construct another important vector from the
cross product of the lookat-vector and the vector
vup- This vector, when normalized, should align with [1, 0, 0]T Why?
Trang 8989
Constructing Viewing
Transformation, V
One more vector to define…
This vector, when normalized, should align with [0, 1, 0]T
Now let’s compose the results
Trang 90Compositing Vectors to Form V
We know the three world axis vectors (x, y, z)
We know the three camera axis vectors (r, u, l)
Viewing transformation, V, must convert from world to camera coordinate systems
Trang 9191
Compositing Vectors to Form V
– Each camera axis vector is unit length
– Each camera axis vector is perpendicular to others
Camera matrix is orthogonal and normalized
– Orthonormal
Therefore, M-1 = MT
Trang 92Compositing Vectors to Form V
Therefore, rotation component of viewing
transformation is just transpose of computed vectors
Trang 9393
Compositing Vectors to Form V
Translation component too
Multiply it through
Trang 94Final Viewing Transformation, V
To transform vertices, use this matrix:
And you get this: