Khoa CNTT - DDHBK Hà nội hunglt@it-hut.edu.vn 8682595
Phép biến đổi - Transformations
Bài 4 Các phép biến đổi Đồ hoạ Transformations
(cid:122) Trong kỹ thuật đồ hoạ 3 bước: Mô hình, Tô trát và Hiên thị (modeling, rendering, displaying)
(cid:122) Với Modeling ( Mô hình hóa) :
world coordinate
modeling coordinate Modeling
viewing coordinate (eye
transformation
coordinate)
Viewing transformation
(cid:122) Transformation: là phép ánh xạ tọa độ điểm hay
vector thành tọa độ hay vector khác
Le Tan Hung Email: hunglt@it-hut.edu.vn
2 1
Transformations - Modeling
Phép biến đổi Transformations
world
(cid:122) Biến đổi mô hình hoá - Modeling transformations
– build complex models by positioning simple
components
(cid:122) Biến đổi tạo góc nhìn - Viewing transformations
– placing virtual camera in the world – transformation from world coordinates to camera
coordinates
(cid:122) Biến Phép chiếu – Projection Transform
3 4
Modeling Transformations
Phép biến đổi Affine Affine Transformations?
Transform coordinate system
(cid:122) Phép biến đổi Affine là phép biến đổi tọa độ
Transform objects/points
đ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.
(cid:122) 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.
1
5 6
Khoa CNTT - DDHBK Hà nội hunglt@it-hut.edu.vn 8682595
Biểu diễn Ma trận
Matrix Representation
(cid:122) Phép biến đổi đồ họa - affine transformation T ánh xạ
(cid:122) 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
t
=
+
+
tập P sang tập Q: Q aP – x x
bP x
x
–
(cid:122) Việc biến đổi P sử dụng tọa độ của P (Px,Py) ánh
t
=
+
+
Q y
cP y
dP y
y
xạ thành các tọa độ mới Q (Qx,Qy)
– where a, b, c, d, tx and ty là các hệ số
(cid:122) Việc biến đổi có thể biểu diễn thông qua hàm T,
(cid:122) Biểu diễn ma trận:
Q
P
t
x
x
x
Q
P
t
a c
b d
y
y
y
⎞ ⎟⎟ ⎠
⎛ ⎜⎜ ⎝
⎛ ⎜ ⎜ ⎝
⎛ ⎜ ⎜ ⎝
⎞ ⎟ =⎟ ⎠
⎞ ⎟ +⎟ ⎠
⎛ ⎜ ⎜ ⎝
⎞ ⎟ ⎟ ⎠
(cid:122) i.e.
hàm ánh xạ của điểm: – T(Px,Py) = (Qx,Qy) – or: – T(P) = Q
Tr
MP Q =
+
7 8
y
Các phép biến đổi hình học hai chiều
Phép biến đổi
(cid:122) Phép bất biến
T
=
(cid:122) Phương pháp biểu diễn đối tượng P = [ x y ] (cid:122) Phép biến đổi vị trí điểm
01 10
⎡ ⎢ ⎣
⎤ ⎥ ⎦
T
=
a b dc
⎡ ⎢ ⎣
⎤ ⎥ ⎦
(cid:122) Thực thi phép biến đổi đúng trên 1 điểm ảnh sẽ đúng
(cid:122) Phép biến đổi tỉ lệ - Scaling (cid:122) A scaling changes the size of an object with two scale
b
cy
bx
dy
' y
X
x
+
+
=
=
) (
[ ( ax
] )
] [ ] T *
] *y
[
[
a
0
[ x
]'
c
d
y
y
x
'
y
=
=
)
factors, Sx and Sy ] [ ] X T *
[
[ x
] *
[ ( ax
]
[
]'
trên toàn bộ đối tượng a ⎡ ⎢ ⎣
⎤ =⎥ ⎦
x z
y
10
⎡ ⎢ ⎣
b
⎤ =⎥ ⎦ 1
X
y
dy
'
y
=
+
=
[
] [ ] T *
[ x
] *
[ bx
]
[ x
]'
10
⎡ ⎢ ⎣
⎤ =⎥ ⎦
pW
pM
(cid:122) Phép biến dạng (cid:122) A shearing shears an object in a particular direction, (in
x
z
2D, it’s either in the x or in the y direction
9 10
Thuộc tính cơ bản của phép biến đổi Affine Transformations
Phép quay- Rotation
(cid:122) Preservation of lines:
y
– They preserve lines, so the image of a straight line is
x = ρ cos α, y = ρ sin α ;
( x’, y’ )
another straight line.
– This vastly simplifies drawing transformed line
segments.
ρ
( x, y )
– We need only compute the image of the two endpoints
ρ
θ
α
x
of the original line and then draw a straight line between them
– Preservation of collinearity guarantees that polygons will
[T
]
=
transform into polygons
sin cos
cos sin −
θ θ
⎡ ⎢ ⎣
θ ⎤ ⎥ θ ⎦
– Affine transformations map lines to lines;
x’ = ρ cos (θ +α ), y’ = ρ sin (θ +α ) ; x’ = ρ ( cosθ cosα - sinθ sinα ) = x cosθ - y sinθ y’ = ρ ( sinθ cosα + cosθ sinα ) = x sinθ + y cosθ [x' y']= [xcosθ - ysinθ xsinθ + ycosθ]
2
11 12
Khoa CNTT - DDHBK Hà nội hunglt@it-hut.edu.vn 8682595
Thuộc tính
Kết hợp các phép biến đổi Composition of Affine Transforms
(cid:122) Preservation of parallelism
– Preservation of parallelism guarantees that
(cid:122) Any affine transformation can be decomposed into elementary transformations.
parallelograms will transform into parallelograms
(cid:122) Mọi phép biến đổi phức tạp đều
(cid:122) Preservation of proportional distances
– Preservation of proportional distances means that mid-
points of lines remain mid-points
(cid:122) Affine transformations change volume by |
Det(M) |;
có thể tạo thành từ các phép biến đổi cơ sở như: – Dịch chuyển - Translation – Tỉ lệ - Scaling – Quay- Rotation – Biến dạng - Shearing
13 14
Thuộc tính
Affine transformations preserve affine combinations
T
(cid:122) It is rare that we want to perform just one elementary
transformation.
(cid:122) 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
(cid:122) 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 (cid:122) We have defined each transformation by their effects on
(cid:122) These individual transformations combine into one
single points
overall transformation
(cid:122) This is called the composition of transformations. (cid:122) The composition of two or more affine transformations
(cid:122) In practice these will be applied to multiple points to transfer entire scenes or objects made up of many defining points
is also an affine transformation
15 16
Pivotal points
Điểm gốc - Pivotal points Cho phép quay và tỉ lệ Rotation and Scaling
(cid:122) The simple versions of rotation and scaling have been based around the
(cid:122) Often we wish to rotate or scale with respect to some
origin.
pivotal point, not the origin
(cid:122) This means that when we rotate or scale, the object will also move, with
(cid:122) Most significantly, we often wish to rotate or scale an
respect to the origin
object about its centre, or midpoint
(cid:122) Translate all points through (-c1,-c2) (cid:122) Rotate all points about the origin by
(cid:122) In this way, the object’s location does not change (cid:122) To do this, we relate the rotation or scaling about the
(cid:122) Translate all points back through (c1,c2)
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 – then all points are translated back, so that V is restored to its
(c1,c2) original location
(0,0) 17
3
18
Khoa CNTT - DDHBK Hà nội hunglt@it-hut.edu.vn 8682595
Hệ toạ độ đồng nhất
Tọa độ đồng nhất Homogeneous Transform
– x' = ax + by + n – y' = cx + dy + m
(cid:122) Vấn đề gặp phải: (cid:122) An affine transformation is composed of a linear
transformation followed by a translation
(cid:122) Phương pháp biểu diễn mở rộng thông qua
(cid:122) Unfortunately, the translation portion is not a
tọa độ đồng nhất của các vector vị trí
matrix multiplication but must instead be added as an extra term, or vector
(cid:122) Với ứng dụng của phép chiếu hình học mà
(cid:122) What we need is a “trick”, so that translations can
be represented in matrix multiplication form
(cid:122) This then means that they can be easily
ở đó tọa độ điểm được mô tả dưới ma trận [ x* y* h] – với x = x*/h, y = y*/h, z = z*/h và h là một số
composed with other transformations, by simply multiplying the matrices together
thực tuỳ ý
20 19
Phép biến đổi với tọa độ đồng nhất
Ưu điểm của Hệ tọa độ đồng nhất Homogeneous Transform
b
a
0
(cid:122) Ma trận biến đổi đồng nhất T ][
d
c
0
=
(cid:122) Ðưa ra cái nhìn hợp nhất của các phép biến đổi dưới phép nhân ma trận, hỗ trợ cho việc xử lý bằng cả phần cứng và phần mềm
nm
1
⎤ ⎥ ⎥ ⎥ ⎦
⎡ ⎢ ⎢ ⎢ ⎣
(cid:122) Phép tịnh tiến
x '[
y
]1'
[
x
y
[
nymx
]1
=
+
+
=
1 0 nm
00 01 1
⎤ ⎥ ⎥ ⎥ ⎦
⎡ ⎢ ]1 ⎢ ⎢ ⎣
(cid:122) Kết hợp các các phép biến đổi tạo thành ma trận tích đơn giản duy nhất. Tránh nhầm lẫn về thứ tự của các phép nhân khi sử dụng. – Order matters: AB is generally not the same as BA (cid:122) 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
(tx, ty, tz)
22 21
Phép tỉ lệ
Phép quay
y
y
x
y
x '[
]1'
[
]1
sin cos
0 0
φ φ
φ φ
=
cos sin −
x '[
y
]1'
[
x
y
Sx 1.[
Sy ]12.
S 1 0
0 0 S 02
=
=
0
0
1
⎤ ⎥ ⎥ ⎥ ⎦
⎡ ⎢ ⎢ ⎢ ⎣
0
0
1
( x’, y’ )
⎡ ⎢ ]1 ⎢ ⎢ ⎣
⎤ ⎥ ⎥ ⎥ ⎦
x .[
cos
y sin.
x sin.
y .
cos
]1
=
φ
−
φ
φ
+
φ
ρ
( x, y )
ρ
θ
α
x
4
24 23
Khoa CNTT - DDHBK Hà nội hunglt@it-hut.edu.vn 8682595
Phép biến đổi tổng hợp
Ma trận biến đổi 3 chiều 3D Matrix Transformations
(cid:122) 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
(cid:122) Again, using homogeneous coordinates it is
possible to represent each type of transformation in a matrix form
(cid:122) In 3D, each transformation is represented by a
4x4 matrix
26 25
Các phép biến đổi hình học 3 chiều
Phép tịnh tiến
(cid:122) Biểu diễn điểm trong không gian 3 chiều
1
0
0
0
(cid:122) [ x* y* z* h ] = [ x y z 1 ]. [ T ] (cid:122) [x' y' z' 1 ]= [ x*/h y*/h z*/h 1 ][ T ]
0
1
0
0
dxT ([
,
dy
,
dz
)]
=
(cid:122) Ma trận biến đổi
0
0
1
0
b
c
a
p
1
dx
dy
dz
⎤ ⎥ ⎥ ⎥ ⎥ ⎦
⎡ ⎢ ⎢ ⎢ ⎢ ⎣
][T
=
e i
f j
d g
q r
nml
s
⎤ ⎥ ⎥ ⎥ ⎥ ⎦
⎡ ⎢ ⎢ ⎢ ⎢ ⎣
– [X'] = [ X ] . [ T(dx,dy,dz) ] – [ x' y' z' 1 ] = – [ x y z 1 ].[ T(dx,dy,dz) ] (cid:122) = [ x+dx y+dy z+dz 1 ]
28 27
Phép tỉ lệ
Rotation
(cid:122) In 2D, the only rotation possible was about the
origin.
01 s 0
'[ x
y
'
z
'
1 ]
[
x
y
z
1 ]
=
(cid:122) In 3D, there are 3 possible rotations, one about
each of the x, y and z axes
0 0
00 ⎤ ⎥ 002 s ⎥ ⎥ 0 03 s ⎥ 10 0 ⎦
⎡ ⎢ ⎢ ⎢ ⎢ ⎣
sx=
sy
sz
1.[
2.
]13.
(cid:122) Positive rotations are anti-clockwise, negative rotations are clockwise, when looking down a positive axis towards the origin
y
y
y
(cid:122) s1, s2, s3 là các hệ số tỉ lệ
x
x
tương ứng trên các trục toạ độ
x
z
z
z
5
30 29
Khoa CNTT - DDHBK Hà nội hunglt@it-hut.edu.vn 8682595
Quay quanh trục y
Phép quay 3 chiều
cos
0
sin
0
θ
θ
−
(cid:122) Quay quanh các trục toạ độ
(cid:122) Quay quanh trục x
0
0
]
[ Ty
=
1 0
0 cos
0 0
0 sin θ
θ
cos
sin
φ
φ
[Tx
]
=
0
0
0
1
⎤ ⎥ ⎥ ⎥ ⎥ ⎦
⎡ ⎢ ⎢ ⎢ ⎢ ⎣
−
φ
φ
sin 0
cos 0
1 ⎡ ⎢ 0 ⎢ ⎢ 0 ⎢ 0 ⎣
0 ⎤ ⎥ 0 ⎥ ⎥ 0 ⎥ 1 ⎦
(cid:122) Quay quanh trục z
cos
sin
00
ϕ
ϕ
ϕ
ϕ
−
Tz [
]
=
sin 0 0
cos 0 0
00 01 10
⎤ ⎥ ⎥ ⎥ ⎥ ⎦
⎡ ⎢ ⎢ ⎢ ⎢ ⎣
32 31
Phép biến dạng (secondary translation)
Phép lấy đối xứng (reflections-secondary translation)
1
c
b 1
d
'[ x
y
'
z
'
1 ]
[
x
y
z
1 ]
=
i
0 ⎤ ⎥ 0 f ⎥ ⎥ 01 g ⎥ 1000 ⎦
=
x
+
yd
+
gz
bx
++ y
iz
cx
fy
+
z
⎡ ⎢ ⎢ ⎢ ⎢ ⎣ +
[
]1
34 33
Quay quanh một trục bất kỳ
Quay quanh một trục bất kỳ song song với các trục tọa độ
0
0
1
0
0
0
([T
)]
φ
=
[
Tr
]
=
φ φ
φ φ
−
0 0
1 0
0 1
0 0
,
cos sin 0
sin cos 0
0
y
z
1
,
0 ⎤ ⎥ 0 ⎥ ⎥ 0 ⎥ 1 ⎦
1 ⎡ ⎢ 0 ⎢ ⎢ 0 ⎢ 0 ⎣
⎤ ⎥ ⎥ ⎥ ⎥ ⎦
⎡ ⎢ ⎢ ⎢ ⎢ ⎣
0
0
0
1
[ Tr]
1 =−
0
sin φ cos φ y sin φφ −
z 1( )cos −
1 ⎡ ⎢ 0 cos φ ⎢ [ ] Tth = ⎢ sin 0 φ − ⎢ y z 1(0 )cos sin φφ + − ⎢ ⎣
0 ⎤ ⎥ 0 ⎥ ⎥ 0 ⎥ 1 ⎥ ⎦
y
z
−
−
1 ⎡ ⎢ 0 ⎢ ⎢ 0 ⎢ 0 ⎣
00 ⎤ ⎥ 00 ⎥ ⎥ 01 ⎥ 1 ⎦
6
36 35
Khoa CNTT - DDHBK Hà nội hunglt@it-hut.edu.vn 8682595
Solution
Biến đổi góc nhìn Viewing Transformation
(cid:122) Chuyển P1 về gốc tọa độ. (cid:122) Quay quanh trục y sao cho P1P2 nằm trên mặt phẳng
(y, z)
(cid:122) Quay quanh trục x sao cho P1P2 trùng với trục z. (cid:122) Quay quanh trục z sao cho P1P3 nằm trên mặt phẳng
(y, z)
(cid:122) 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.
Lê Tấn Hùng
37 38
Nội dung
Hệ tọa độ thực (WCS-World Coordinate System)
(cid:122) Phép biến đổi nhìn cơ bản (cid:122) Biến đổi hệ thống tọa độ (cid:122) Camera (cid:122) Biến đổi 3D viewing (cid:122) Ví dụ
(cid:122) 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. (cid:122) Đơ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 ...
(cid:122) Modeling Coordinate
39 40
Hệ tọa độ thiết bị (DCS-Device Coordinate System)
Hệ tọa độ chuẩn (NCS - Normalized Coordinate System)
(cid:122) Chuyển đổi hệ tọa độ (cid:122) Giải quyết vấn đề khi ứng dụng chạy trên các
(cid:122) 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ị khác nhau (cid:122) Có kích thước 1x1
NCS
Dcs
chuyÓn ®æi 2
Wcs
chuyÓn ®æi 1
subselect.me
Vïng täa ®é thiÕt bÞ VGA=640x480
ThiÕt bÞ hiÓn thÞ
7
41 42
Khoa CNTT - DDHBK Hà nội hunglt@it-hut.edu.vn 8682595
2-Dimensional Views
Phép biến đổi nhìn cơ bản Basic Viewing Transform
(cid:122) Phép biến đổi góc nhìn cơ bản là chuyển đổi từ
(cid:122) A Window : Cửa sổ, Phần hình ảnh của thế giới
thực sẽ được hiển thị
hệ tọa độ mô hình sang hệ tọa độ mà hình (Modelling co-ordinates to Screen Coordinates)
(cid:122) A Viewport : Cổng nhìn, vùng màn hình nơi hình
ảnh thế giới thực sẽ hiển thị
(wxmax,wymax) (vxmax,vymax)
(cid:122) Vấn đề: (cid:122) Thay đổi tỷ lệ hình ảnh (cid:122) Hiệu ứng về tỉ lệ khi Zoom hình ảnh (cid:122) Phần nào của hình vẽ sẽ hiển thị ? (cid:122) Hiển thị tại đâu trên màn hình ? (cid:122) Chuyển đổi sẽ được thực hiện ra sao?
(vx,vy) (wx,wy)
(vxmin,vymin) (wxmin,wymin) 44 43
Phép biến đổi theo ma trận
Phép chuyển đổi
(cid:122) Ma trận chuyển vị theo Window
1
0
0
1
1 [ T ]
=
Xw
Yw
−
−
⎡ ⎢ ⎢ ⎢ ⎣
0 ⎤ ⎥ 0 ⎥ ⎥ 1 ⎦
0
Xvmax - Xwmax -
Xvmin Xwmin
S ]1[
0
=
(cid:122) Ma trận biến đổi tỉ lệ
Yv Yw
Yv Yw
min min
0
max − max − 0
⎤ 0 ⎥ ⎥ 0 ⎥ ⎥ 1 ⎥ ⎥ ⎦
⎡ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎣
1
0
0
T ]2[
0
1
0
=
1
Xv
Yy
⎡ ⎢ ⎢ ⎢ ⎣
⎤ ⎥ ⎥ ⎥ ⎦
(cid:122) Ma trận chuyển vị theo tọa độ viewport
46 45
Ví dụ OpenGL - Windows and Viewports
Ma trận biến đổi tổng hợp của phép chuyển đổi tọa độ
(cid:122) Plotting a function revisted..
= ][ T ]2[]1[]1[ TxSxT Xvmax - Xvmin //set the viewing coordinates setWindow(xmin, xmax, ymin, 0 ymax); Xwmax - Xwmin − max min Yv Yv setViewport(0,640,0,480); glBegin(GL_POINTS); =
//--------------- setWindow --------------------- void setWindow(GLdouble left, Gldouble right, GLdouble bottom, GLdouble top) { glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(left, right, bottom, top); }
0 ][ T for(GLdouble x = xmin; x Yw < xmax; x+=0.005 ) Xvmax - Xvmin min − max min − max Yw Yv Yv − min min − min min Xv Xw Yv Yw { Xwmax - Xwmin − max min Yw Yw ⎤ 0 ⎥ ⎥ ⎥ 0 ⎥ ⎥ 1 ⎥ ⎦ ⎡ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎣ glVertex2d(x, pow(2.7183,-x) *cos(2*3.14*x));
//---------------- setViewport ------------------ void setViewport(GLdouble left, Gldouble right, GLdouble bottom, GLdouble top) { glViewport(left, bottom, right – left, top - bottom);}
8
} glEnd(); 48 47
Khoa CNTT - DDHBK Hà nội hunglt@it-hut.edu.vn 8682595
Các phép biến đổi
Biến đổi hệ tọa độ Coordinate Transforms
x
x
u
u’
(cid:122) Là phép biến đổi trên hệ tọa độ của đối tượng. Việc thay đổi hệ tọa độ của đối tượng với hệ thống tọa độ chung x tao nên sự thay đổi toàn cảnh
(1,1)
u
(1,1)
u’
(1,1)
v
y
y
v’
(1,1)
001
Q
P
=
v
v’
010 100
⎤ ⎥ ⎥ ⎥ ⎦
⎡ ⎢ ⎢ ⎢ ⎣
Hệ tọa độ cục bộ
49 50
y Đối tượng sau biến đổi trên tđ toàn cục
Translation
Rotation
O
=
x
x
x
u’
x
0 0 1
u
⎡ ⎢ ⎢ ⎢ ⎣
⎤ ⎥ ⎥ ⎥ ⎦
u
u’
Q
P
=
(1+tx,1+ty)
(1,1)
(1,1)
01 10 00
tx ty 1
v’
⎡ ⎢ ⎢ ⎢ ⎣
⎤ ⎥ ⎥ ⎥ ⎦
v
y
v
y
y
tx
v’ y tx
=
tx ty
=
=
0 0 1
tx ty 1
00
1
1
01 10 00
tx ty 1
0 1 1
ty + 1
01 ⎡ ⎢ 10 ⎢ ⎢ 00 ⎣
⎤ ⎥ ⎥ ⎥ ⎦
⎡ ⎢ ⎢ ⎢ ⎣
⎤ ⎥ ⎥ ⎥ ⎦
tx ⎡ ⎢ ty ⎢ ⎢ 1 ⎣
⎤ ⎥ ⎥ ⎥ ⎦
01 ⎡ ⎢ 10 ⎢ ⎢ ⎣
⎤ ⎥ ⎥ ⎥ ⎦
1 ⎡ ⎢ 0 ⎢ ⎢ 1 ⎣
⎤ ⎥ ⎥ ⎥ ⎦
1 ⎡ + ⎢ ty ⎢ ⎢ ⎣
⎤ ⎥ ⎥ ⎥ ⎦
⎤ ⎥ ⎥ ⎥ ⎦
⎡ ⎢ 1 ⎢ ⎢ ⎣
⎤ ⎥ ⎥ ⎥ ⎦
⎡ ⎢ ⎢ ⎢ ⎣
⎤ ⎥ ⎥ ⎥ ⎦
⎡ ⎢ ⎢ ⎢ ⎣
v
u
=
=
Q
P
=
θ θ
tx
sin cos 1
0 0 1
cos θ sin θ 0
sin − cos 0
⎡− ⎢ ⎢ ⎢ ⎣
θ ⎤ ⎥ θ ⎥ ⎥ ⎦
⎡ ⎢ ⎢ ⎢ ⎣
cos θ ⎤ ⎥ sin θ ⎥ ⎥ 1 ⎦
⎤ ⎥ ⎥ ⎥ ⎦
⎡ ⎢ ⎢ ⎢ ⎣
O
=
u (0, 1, 0)
origin
v (1, 0, 0)
ty 1
⎡ ⎢ ⎢ ⎢ ⎣
⎤ ⎥ ⎥ ⎥ ⎦
51 52
Scaling
Composite Transformations
u
1(
cos
sin
−
+
x
x
x
u
u
1(
cos
sin
−
−
x 1 y 1
y 1 y 1
x
(1,1)
) θ ) θ 1
u’
⎡ ⎢ O = ⎢ ⎢ ⎣
θ ⎤ ⎥ θ ⎥ ⎥ ⎦
(sx*1,sy*1)
(1,1)
v
v
y
1( 1(
cos cos
sin sin
) θ ) θ
θ + θ +
− −
+ −
x 1 y 1
y 1 y 1
’ y
1
cos ⎡ ⎢ sin v = ⎢ ⎢ ⎣
θ ⎤ ⎥ θ ⎥ ⎥ ⎦
v
y
y
v
) θ ) θ
x 1( θ + 1 y 1( θ + 1
y sin + 1 y sin − 1
sx
0
0
0
sx
0
cos − cos − 1
θ ⎤ ⎥ θ ⎥ ⎥ ⎦
sin − ⎡ ⎢ u = cos ⎢ ⎢ ⎣
M
1( 1(
cos cos
sin sin
θ θ
=
− −
+ −
x 1 y 1
y 1 x 1
u
Q
P
=
=
v
O
=
=
cos θ sin θ 0
sin − cos 0
) θ ) θ 1
⎛ ⎜ ⎜ ⎜ ⎝
θ ⎞ ⎟ θ ⎟ ⎟ ⎠
sy 1
0 1
0 0
sy 0
0 1
⎡ ⎢ ⎢ ⎢ ⎣
⎤ ⎥ ⎥ ⎥ ⎦
⎡ ⎢ ⎢ ⎢ ⎣
⎤ ⎥ ⎥ ⎥ ⎦
⎤ ⎥ ⎥ ⎥ ⎦
⎤ ⎥ ⎥ ⎥ ⎦
⎡ ⎢ ⎢ ⎢ ⎣
⎡ ⎢ 0 ⎢ ⎢ 1 ⎣
9
53 54
Khoa CNTT - DDHBK Hà nội hunglt@it-hut.edu.vn 8682595
Quan sát 3D - 3D Viewing
(cid:122) Hiển thị hình ảnh 3D trên các thiết bị
+
+ transform
+ transform
+ transform
(cid:122) 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 (cid:122) 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
Obviously we want something more versatile
=
2D
55 56
Viewing
The Viewing Process
world coordinate
viewing coordinate (eye coordinate)
modeling coordinate Modeling
(cid:122) Để tạo 1 cảnh nhìn gồm các bước
Viewing transformation
transformation
– Mô tả biểu diễn hình học – Định nghĩa điểm nhìn – Camera
yV
xV
yW
-zV
pV
yM
(cid:122) Chuyển đổi từ đối tượng 3D sang 2D (cid:122) Xác định vùng hiển thị của đố tượng (cid:122) Xác định camera để có thể tạo thành phép
pW
chiếu từ 3D thành 2D.
pM
xM
xW
zM
zW
57 58
Khái quát về - Camera Analogy
Viewing Coordinates
(cid:122) Có thể sử dụng mô hình hệ
(cid:122) Khái niệm (cid:122) camera location: Vị trí trong thế
thống tọa độ quan sát trên các thông số gồm: – Điểm nhìn (VRP View Reference
giới thực
Point ): điểm gốc quan sát. Vị trí của Camera)
(cid:122) viewing direction: Hướng chiếu
của Camera.
Three mutually orthogonal basis vectors:
– Vector mặt phẳng quan sát (VPN View Plane Normal vector ): viewing direction)
(cid:122) up vector: Hướng phía trên của
n
(
,
)
=
nnn= , 1
2
3
Camera
– Vector hướng quan sát (VUP View UP vector ): establishes orientation of “camera”
(cid:122) aperture size: Vùng nhìn thấy của
(
,
)
u
=
uuu= , 1
2
3
Camera
N N NV × NV × unv
,
,
)
vvv=×= (
1
2
3
10
59 60
Khoa CNTT - DDHBK Hà nội hunglt@it-hut.edu.vn 8682595
Ví dụ: Viewing Transformation
(i) simple cases( always view along negative z-axis )
(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
Điểm nhìn trên trục Z zW -axis
Nhìn ở vị trí (a, b, c) theo trục Z và ngược hướng zW-axis
u 1
yW
v
n
u
u
v 1 v
=
=
=
yW
2
u
n 1 n 2 n 3
2 v 3
3
⎞ ⎟ ⎟ . ⎟ ⎟ ⎠
⎞ ⎟ ⎟ and , ⎟ ⎟ ⎠
⎛ ⎜ ⎜ ⎜ ⎜ ⎝
⎞ ⎟ ⎟ , ⎟ ⎟ ⎠
⎛ ⎜ ⎜ ⎜ ⎜ ⎝
⎛ ⎜ ⎜ ⎜ ⎜ ⎝
(a, b, c)
Ma trận biến đổi tọa độ thế giới sang tọa độ quan sát :
xW
xW
n
(0, 0, z)
MWC,VC = R T
zW
zW
61
62
MWC,VC = T (0, 0, z,) MWC,VC = T (-a, -b, -c )
61 62
3D Viewing Transformation
Viewing Transformation
(cid:122) Với điểm quan sát VRPoint (x0, y0, z0) the Viewing
(cid:122) We might also achieve this in terms of the following
transformation sequence: – Translate the view reference point to the origin of the world co-
– Apply Rotations to align xv, yv and zv, axes, respectively
2
3
001
−
0 0
u v
010
x 0 y
−
0
Transformation can be defined by composition of the following two transformations: ordinate system
R
=
T
=
001
x
−
0
100
0
010
0 y
−
0
u 1 v 1 n 1 0
2 n 2 0
u v 3 n 3 0
1
000
z − 1
⎛ ⎜ ⎜ ⎜ ⎜ ⎜ ⎝
⎞ ⎟ ⎟ ⎟ ⎟ ⎟ ⎠
⎛ ⎜ ⎜ ⎜ ⎜ ⎜ ⎝
⎞ ⎟ ⎟ ⎟ ⎟ ⎟ ⎠
100
z
−
0
000
1
⎛ ⎜ ⎜ ⎜ ⎜ ⎜ ⎝
⎞ ⎟ ⎟ ⎟ ⎟ ⎟ ⎠
(x0, y0, z0)
M
TR •
=VCWC ,
r x
r ,M=′ x
vcwc
This is used to determine the location of each vertex in the camera co-ordinate system
2
3
63 64
• Recall we’ve re-expressed our world geometry in eye’s frame yV reference.
0 0
u v
R
where
xV
yW
0
-zV
pV
yM
u 1 v 1 n 1 0
2 n 2 0
u v 3 n 3 0
1
⎛ ⎜ ⎜ ⎜ = ⎜ ⎜⎜ ⎝
⎞ ⎟ ⎟ ⎟ , ⎟ ⎟⎟ ⎠
pW
pM
u 1
xM
xW
zM
2
zW
etc
uR
R
=
Verification:
u 3 1
⎡ ⎢ u ⎢ ⎢ ⎢ ⎣
⎤ ⎥ ⎥ , ? = ⎥ ⎥ ⎦
• To do projection, we use a synthetic cameraby introducing a viewing frustum after the viewing transformation.
yV
yW
xV
pV
-zV
yW
pV = MWC,VC pW
pW
pW
pM
pM
xW
xW
zW
zW
65
66
11
65 66
Khoa CNTT - DDHBK Hà nội hunglt@it-hut.edu.vn 8682595
Camera trong OpenGL
(cid:122) Các hàm cơ bản
– glOrtho() and gluLookAt() – these created parallel projections
(cid:122) We now want to modify a scene to give us
Demo : Viewing from Nate Robins (Utah)
perspective projections. – lines converge as they get further away
68
68 67
The Camera
The Camera
(cid:122) Khởi tạo Camera với phép
(cid:122) The Perspective Camera glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(viewAngle, aspectRatio, N, F);
chiếu phối cảnh – Thay cho glOrtho() bằng: – gluPerspective()
(cid:122) gluPerspective – viewAngle – aspectRatio (W/H) – near plane – far plane
69 70
The Camera
The Camera
(cid:122) Positioning the Camera
– The camera is positioned using a combination
of translations and rotations.
– Think if the camera being in the same location
up(x,y,z)
as the viewers eye.
eye(x,y,z)
lookat(x,y,z)
12
71 72
Khoa CNTT - DDHBK Hà nội hunglt@it-hut.edu.vn 8682595
The Camera
The Camera
(cid:122) The camera can have an arbitrary orientation and
position.
glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(eye.x, eye.y, eye.z,
(cid:122) Therefore we can think of the camera having its own
axes.
look.x, look.y, look.z, up.x, up.y, up.z);
73 74
The Camera
Camera Movement
(cid:122) A camera has six degrees of freedom:
(cid:122) Camera Movement
– To move the camera along the u axis you would simply perform
(cid:122) eye = eye + Du
v
1. slid in 3 directions; and 2. rotated in 3 directions
(cid:122) Camera Movement
– To move slide the camera in any direction: eye.x = dU*u.x + dV*v.x + dN*n.x eye.y = dU*u.y + dV*v.y + dN*n.y eye.z = dU*u.z + dV*v.z + dN*n.z
– The camera can move along its axes. – This is called sliding the camera.
n
(cid:122) Camera Movement
u – Besides physically moving the camera to another location… – the camera can be tilted in different directions to look at
different parts of the scene.
75 76
The Camera
The Camera - Camera Movement
We use a plane analogy to describe the cameras
(cid:122) Camera Movement
movement.
– Besides physically moving the camera to another
location…
– the camera can be tilted in different directions to look
(cid:122) a rotation from the horizontal along the length is called PITCH (cid:122) a rotation from the horizontal along the width is called ROLL (cid:122) a rotation around the vertical is called YAW
at different parts of the scene.
13
77 78
Khoa CNTT - DDHBK Hà nội hunglt@it-hut.edu.vn 8682595
The Camera
The Camera
v
v
n
u
(cid:122) Pitch
(cid:122) Roll
v’ = cos(θ)v – sin(θ)n n’ = sin(θ)n + cos(θ)v u’ = cos(θ)u + sin(θ)v v’ = cos(θ)v - sin(θ)u
79 80
The Camera
A 3D Scene
n
u
(cid:122) Yaw
(cid:122) Notice the presence of
the camera, the projection plane, and the world coordinate axes
(cid:122) Viewing transformations define how to acquire the image
on the projection plane
n’ = cos(θ)n - sin(θ)u u’ = sin(θ)n + cos(θ)u
81 82
Viewing Transformations
2 Basic Steps
(cid:122) Create a camera-centered view
(cid:122) Align the two coordinate frames by rotation
(cid:122) Camera is at origin (cid:122) Camera is looking along negative z-axis (cid:122) Camera’s ‘up’ is aligned with y-axis
14
83 84
Khoa CNTT - DDHBK Hà nội hunglt@it-hut.edu.vn 8682595
Creating Camera Coordinate Space
2 Basic Steps
(cid:122) Specify a point where the camera is located in world
(cid:122) Translate to align origins
space, the eye point
(cid:122) Specify a point in world space that we wish to become
the center of view, the lookat point
(cid:122) Specify a vector in world space that we wish to point up in camera image, the up vector
(cid:122) Intuitive camera
movement
85 86
Constructing Viewing Transformation, V
Constructing Viewing Transformation, V
(cid:122) Create a vector from eye-point to lookat-point
(cid:122) Construct another important vector from the
cross product of the lookat-vector and the vup- vector
(cid:122) Normalize the vector
(cid:122) This vector, when normalized, should align with
[1, 0, 0]T Why?
(cid:122) Desired rotation matrix should map this
vector to [0, 0, -1]T Why?
87 88
Constructing Viewing Transformation, V
Compositing Vectors to Form V
(cid:122) One more vector to define…
(cid:122) This vector, when normalized, should align with [0, 1, 0]T
(cid:122) We know the three world axis vectors (x, y, z) (cid:122) We know the three camera axis vectors (r, u, l) (cid:122) Viewing transformation, V, must convert from world to
camera coordinate systems
(cid:122) Now let’s compose the results
15
89 90
Khoa CNTT - DDHBK Hà nội hunglt@it-hut.edu.vn 8682595
Compositing Vectors to Form V
Compositing Vectors to Form V
(cid:122) Remember
(cid:122) Therefore, rotation component of viewing
– Each camera axis vector is unit length. – Each camera axis vector is perpendicular to others
transformation is just transpose of computed vectors
(cid:122) Camera matrix is orthogonal and normalized
– Orthonormal
(cid:122) Therefore, M-1 = MT
91 92
Compositing Vectors to Form V
Final Viewing Transformation, V
(cid:122) Translation component too
(cid:122) To transform vertices, use this matrix:
(cid:122) Multiply it through
(cid:122) And you get this:
16
93 94