Tải bản đầy đủ (.pdf) (9 trang)

slike bài giảng đồ hoạ và hiện thực ảo - lê tấn hùng chương 3 các giải thuật sinh các thực thể cơ sở

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (489.28 KB, 9 trang )

1
Các giảithuật sinh các thực thể
cơ sở
Le Tan Hung

0913030731
Rendering Pipeline: 3-D
Transform
Illuminate
Transform
Clip
Project
Rasterize
Model & Camera
Model & Camera
Parameters
Parameters
Rendering Pipeline
Rendering Pipeline
Framebuffer
Framebuffer
Display
Display
The Rendering Pipeline: 3-D
Scene graph
Object geometry
Lighting
Calculations
Clipping



Các điểm của hệ thống tọa độ 3D thế giới thực
Các điểm của hệ thống tọa độ 3D thế giới thực


Các điểm bóng theo mô hình chiếu sáng
Các điểm bóng theo mô hình chiếu sáng


Các điểm trong mô hình hệ tọa độ Camera hay tọa độ điểm nhìn
Các điểm trong mô hình hệ tọa độ Camera hay tọa độ
điểm nhìn


Các tọa độ điểm của vùng hình chóp cụt với điểm nhìn xác định
Các tọa độ điểm của vùng hình chóp cụt với điểm nhìn xác định


Đ
Đ
iểm 2
iểm 2
-
-
D
D
theo tọa độ màn hình sau phép chiếu được xén tỉa
theo tọa độ màn hình sau phép chiếu được xén tỉa
Modeling
Transforms
Viewing

Transform
Projection
Transform
Phép biến đổi Transformations
z screen space- không gian màn hình
z model space Không gian mô hình
(a.k.a. object space or world space)
z 3 loại phép biến đổi:
– Modeling transforms
– Viewing transforms
– Projection transforms
Rendering: Transformations
z Modeling transforms
– Size, place, scale, and rotate objects parts of the
model w.r.t. each other
– Object coordinates Æ world coordinates
Z
X
Y
X
Z
Y
Rendering: Transformations
z Viewing transform
– Rotate & translate the world to lie directly in front of
the camera
z Typically place camera at origin
z Typically looking down -Z axis
– World coordinates Æ view coordinates
2

Rendering: Transformations
z Projection transform
– Apply perspective foreshortening
z Distant = small: the pinhole camera model
– View coordinates Æ screen coordinates
Rendering: Transformations
z All these transformations involve shifting
coordinate systems (i.e., basis sets)
z Oh yeah, that’s what matrices do…
z Represent coordinates as vectors, transforms
as matrices
z Multiply matrices = concatenate transforms!













=









Y
X
Y
X
θθ
θθ
cossin
sincos
Rendering: Transformations
z Homogeneous coordinates: represent
coordinates in 3 dimensions with a 4-vector
– Denoted [x, y, z, w]
T
z Note that w = 1 in model coordinates
– To get 3-D coordinates, divide by w:
[x’, y’, z’]
T
= [x/w, y/w, z/w]
T
z Transformations are 4x4 matrices
z Why? To handle translation and projection
The Rendering Pipeline: 3-D
Modeling
Transforms
Scene graph
Object geometry

Lighting
Calculations
Viewing
Transform
Clipping
Projection
Transform
Result:
Result:


All vertices of scene in shared 3
All vertices of scene in shared 3
-
-
D “world” coordinate system
D “world” coordinate system


Vertices shaded according to lighting model
Vertices shaded according to lighting model


Scene vertices in 3
Scene vertices in 3
-
-
D “view” or “camera” coordinate system
D “view” or “camera” coordinate system



Exactly those vertices & portions of polygons in view frustum
Exactly those vertices & portions of polygons in view frustum


2
2
-
-
D screen coordinates of clipped vertices
D screen coordinates of clipped vertices
Rendering: Ánh sáng - Lighting
z Illuminating a scene: coloring pixels according to
some approximation of lighting
– Global illumination: solves for lighting of the whole
scene at once
– Local illumination: local approximation, typically
lighting each polygon separately
z Interactive graphics (e.g., hardware) does only
local illumination at run time
The Rendering Pipeline: 3-D
Modeling
Transforms
Scene graph
Object geometry
Lighting
Calculations
Viewing
Transform
Clipping

Projection
Transform
Result:
Result:


All vertices of scene in shared 3
All vertices of scene in shared 3
-
-
D “world” coordinate
D “world” coordinate
system
system


Vertices shaded according to lighting model
Vertices shaded according to lighting model


Scene vertices in 3
Scene vertices in 3
-
-
D “view” or “camera” coordinate
D “view” or “camera” coordinate
system
system



Exactly those vertices & portions of polygons in view
Exactly those vertices & portions of polygons in view
frustum
frustum


2
2
-
-
D screen coordinates of clipped vertices
D screen coordinates of clipped vertices
3
Rendering: Clipping
z Clipping a 3-D primitive returns its intersection
with the view frustum:
Rendering: Xén tỉa - Clipping
z Clipping is tricky!
– We will have a whole assignment on clipping
In: 3 vertices
In: 3 vertices
Out: 6 vertices
Out: 6 vertices
Clip
Clip
In: 1 polygon
In: 1 polygon
Out: 2 polygons
Out: 2 polygons
The Rendering Pipeline: 3-D

Transform
Illuminate
Transform
Clip
Project
Rasterize
Model & Camera
Model & Camera
Parameters
Parameters
Rendering Pipeline
Rendering Pipeline
Framebuffer
Framebuffer
Display
Display
Modeling: The Basics
z Common interactive 3-D primitives: points,
lines, polygons (i.e., triangles)
z Organized into objects
– Collection of primitives, other objects
– Associated matrix for transformations
z Instancing: using same geometry for multiple
objects
– 4 wheels on a car, 2 arms on a robot
Modeling: The Scene Graph
z Đồ thị cảnh scene graph : cây đồ thị lưu trữ đối
tượng, quan hệ giũa các đối tượng và các phép
biến đổi trên đối tượng đó
z Nút là đối tượng;

z Cành là các thực thể biến đổi
– Tương ứng là các ma trận
Robot
BodyHead
ArmTrunkLegEyeMouth
Modeling: The Scene Graph
z Traverse the scene graph in depth-first order,
concatenating transformations
z Maintain a matrix stack of transformations
ArmTrunk
Leg
EyeMouth
Head Body
Robot
Foot
Matrix
Matrix
Stack
Stack
Visited
Visited
Unvisited
Unvisited
Active
Active
4
Modeling: The Camera
z Finally: need a model of the virtual camera
– Can be very sophisticated
z Field of view, depth of field, distortion, chromatic aberration…

– Interactive graphics (OpenGL):
z Camera pose: position & orientation
– Captured in viewing transform (i.e., modelview matrix)
z Pinhole camera model
– Field of view
– Aspect ratio
– Near & far clipping planes
Modeling: The Camera
z Camera parameters (FOV, etc) are encapsulated
in a projection matrix
– Homogeneous coordinates Æ 4x4 matrix!
– See OpenGL Appendix F for the matrix
z The projection matrix premultiplies the viewing
matrix, which premultiplies the modeling matrices
– Actually, OpenGL lumps viewing and modeling
transforms into modelview matrix
Rờirạc hoá điểm ảnh
(Scan Conversion rasterization)
z Là tiến trình sinh các đốitượng hình họccơ sở bằng
phương pháp xấpxỉ dựatrênlưới phân giảicủamàn
hình
z Tính chấtcácđốitượng cần đảmbảo:
– smooth
– continuous
– pass through specified points
– uniform brightness
– efficient
Biểudiễn đoạnthẳng
z Biểudiễntường minh
(y-y1)/( x-x1) = ( y2-y1)/( x2-x1)1

y = kx + m
– k = (y2-y1)/( x2-x1)
– m = y1- kx1
– Δy = k Δx
z Biểudiễn không tường minh
(y2-y1)x - (x2-x1)y + x2y1 - x1y2 = 0
hay rx + sy + t = 0
– s = -(x2-x1 )
– r = (y2-y1) và t = x2y1 - x1y2
z Biểudiễnthambiến
P(u) = P1 + u(P2 - P1)
u [0,1]
X = x1 + u( x2 - x1 )
Y = y1 + u( y2 - y1 )
m
P(x
1
, y
1
)
P(x
2
, y
2
)
u
Sinh đường tròn
Scan Converting Circles
z Implicit: f(x) = x
2

+y
2
-R
2
z Explicit: y = f(x)
z Parametric:
22
yRx=± −
cos
sin
xR
yR
θ
θ
=
=
If f(x,y) = 0 then it is on the circle.
f(x,y) > 0 then it is outside the circle.
f(x,y) < 0 then it is inside the circle.
Usually, we draw a quarter circle by
incrementing x from 0 to R in unit steps
and solving for +y for each step.
- by stepping the angle from 0 to 90
- avoids large gaps but still insufficient.
Thuật toán DDA
(Digital Differential Analizer)
Giảithuật DDA
z Với 0 < k < 1
x
i+1

= x
i
+ 1
y
i+1
= y
i
+ k
với i=1,2,3
Thuậttoán ddaline (x1, y1, x2, y2)
x1, y1, x2, y2 : tọa độ 2 điểm đầu
cuối
k : hệ số góc
x,y,m :biến
begin
m =(x2-x1)/(y2-y1);
x = x1;
y = y1;
k = 1/m;
putpixel(x,y);
while x<x2
begin
x = x+1;
y = y+k;
putpixel(round(x),round(y));
end; end;
Giảithuật thông thường
DrawLine(int x1,int y1, int x2,int y2,
int color)
{

float y;
int x;
for (x=x1; x<=x2; x++)
{
y = y1 + (x-x1)*(y2-y1)/(x2-x1)
WritePixel(x, Round(y), color );
}}
5
Giảithuật Bresenham
z 1960 Bresenham thuộc
IBM
z điểmgầnvới đường thẳng
dựatrênđộ phân giai hưu
hạn
z loạibỏ đượccácphéptoán
chia và phéptoánlàmtròn
như ta đãthấy trong gỉai
thuật DDA
z Xét đoạnthẳng với 0 < k < 1
012
0
1
2
d2
d1
Giảithuật Bresenham
d
2
= y - yi = k(xi +1) + b - yi
d

1
= yi+1 - y = yi + 1 - k(xi + 1) -
b
z If d
1
≤ d
2
=> y
i+1
= yi + 1
else d
1
> d
2
=> y
i+1
= yi
z D = d
1
-d
2
= -2k(xi + 1) + 2yi - 2b + 1
z Pi = ΔxD = Δx (d
1
-d
2
)
d1
d2
x

i
x
i
+1
y
i
y
i
+1
Pi = -2Δyxi + 2Δxyi + c
P
i+1
-P
i
= -2Δy(x
i+1
-xi) + 2Δx(yi
+1
-yi)
z NếuPi ≤ 0 ⇒ yi
+1
= yi + 1
Pi
+1
= Pi - 2Δy + 2Δx
z NếuPi> 0 ⇒ yi
+1
= yi
Pi
+1

= Pi - 2Δy
P
1
= Δx(d
1
-d
2
)
P
1
= -2Δy + Δx
Giảithuật Bresenham
y
i+1
M
( x
i
, y
i
)
x
i
x
i+1
Giảithuật trung điểm-Midpoint
z Jack Bresenham 1965 / Pitteway 1967
z VanAken ápdụng cho việcsinh các đường
thẳng và đường tròn 1985
z Cáccôngthức đơngiảnhơn, tạo đượccác
điểmtương tự như với Bresenham

z d = F (xi + 1, yi + 1/2) là trung điểmcủa đoạn
AB
z Việcso sánh, hay kiểmtraM sẽ được thay
bằng việcxétgiá trị d.
– Nếud > 0 điểmB đượcchọn, y
i+1
= y
i
– nếud < 0 điểmA đượcchọn. ⇒ y
i+1
= y
i
+
1
– Trong trường hợpd = 0 chúng ta có thể
chọn điểmbấtkỳ hoặcA, hoặcB.
A
M
B
Bresenham’s Algorithm: Midpoint
Algorithm
z Sử dụng phương pháp biểudiễn không tường minh
z Tạimỗi trung điểmcủa đoạnthẳng giá trịđượctính
là:
z Chúng ta gọi d
i
là biến quyết định củabướcthứ i
0=++ cbyax
()
()

()
iiii
iiii
iiii
yxcbyax
yxcbyax
yxcbyax
,0
,0
,0
⇒>++
⇒<++
⇒=++
on line
above line
below line
()
cybxad
iii
+






+++=
2
1
1

Bresenham’s Algorithm: Midpoint
Algorithm
z If d
i
> 0 then chọn điểm A⇒ trung điểmtiếptheosẽ có dạng:
()
bad
cybxadyx
i
iiiii
++=
+






+++=⇒






++
+
2
3
2

2
3
,2
1
6
Bresenham’s Algorithm: Midpoint
Algorithm
z if d
i
< 0 then chọn điểm Bvà trung điểmmớilà
z Ta có:
z Ðiểm đầu
()
[]
2
2
1
1
2
1
,1
b
acbyax
cybxadyx
startstart
startstartstartstartstart
++++=
+







+++=⇒






++
()
ad
cybxadyx
i
iiiii
+=
+






+++=⇒







++
+
2
1
2
2
1
,2
1
Cx
x
y
y
xCc
xxxb
yyya
startend
startend
+
Δ
Δ
=





Δ=

−=Δ−=
−=Δ=
where
2
0
b
a ++=
Midpoint Line Algorithm
dx = x_end-x_start
dy = y_end-y_start
d = 2*dy-dx
x = x_start
y = y_start
while x < x_end
if d <= 0 then
d = d+(2*dy)
x = x+1
else
d = d+2*(dy-dx)
x = x+1
y = y+1
endif
SetPixel(x,y)
endwhile
initialisation
choose B
choose A
Giảithuật
Bresenham's Midpoint
z d = a(xi + 1) + b(yi + 1/2) + c

z Nếu điểm đượcchọnlà B thi M sẽ tang
theo x một đơnvị
– d
i+1
= F(xi +2, yi + 1/2)
= a(xi +2) + b(yi + 1/2) + c
– di = a(xi + 1) + b(yi + 1/2) + c
z Nếu điểmA đượcchọn thi` M tăng theo
2 hướng x và y vớicùng một đơnvị.
di
+ 1
= F (xi + 2, yi + 3/2)
– = a(xi + 2) + b(yi +3/2) + c
– di
+ 1
= di + a + b.
¾ Vớia+ b = dy - dx.
d <= 0
B¾t ®Çu
x = x1 ;
y = y1;
dx = x2 - x1;
dy = y2 - y1;
d = dy - dx/2;
Putpixel (x ,y);
x < x2
KÕt thóc
d = d + dy
d = d + dy - dx
y = y + 1

yes
no
No
yes
x = x + 1
Midpoint Circle Algorithm
z Sử dụng phương pháp biểudiễn
không tường minh trong giảithuật
z Thựchiệngiảithuậttrên1/8
đường tròn và lấy đốixứng xho
các góc còn lại.
z Với d
i
là giá trị của đường tròn tại
một điểmbấtkỳ ta có
(
)
(
)
0
2
22
=−−+− ryyxx
cc
(
)
()
()

circle outside is , if 0

circleon is , if 0
circle inside is , if 0





>
=
<
=
ii
ii
ii
i
yx
yx
yx
d
Midpoint Circle Algorithm
z As with the line, we determine the value of the decision variable
by substituting the mid-point of the next pixel into the implicit
form of the circle:
z If d
i
< 0 we choose pixel A otherwise we choose pixel B
– Note: we currently assume the circle is centered at the origin
()
2
2

2
2
1
1 ryxd
iii







−++=
Midpoint Circle Algorithm
z Again, as with the line algorithm, the choice of A or B can be
used to determine the new value of d
i+1
z If A chosen then next midpoint has the following decision
variable:
z Otherwise if B is chosen then the next decision variable is given
by:
()
32
2
1
2
2
1
,2
2

2
2
1
++=







−++=⇒






−+
+
ii
iiiii
xd
ryxdyx
()
522
2
3
2
2

3
,2
2
2
2
1
+−+=







−++=⇒






−+
+
iii
iiiii
yxd
ryxdyx
7
Midpoint Circle Algorithm
z If we assume that the radius is an integral value, then the first

pixel drawn is (0, r) and the initial value for the decision variable
is given by:
z Although the initial value is fractional, we note that all other
values are integers.
⇒ we can round down:
r
rrrdr
−=







+−+=⇒







4
5
4
1
1
2
1

,1
22
0
rd −=1
0
Midpoint Circle Algorithm
d = 1-r
x = 0
y = r
while y < x
if d < 0 then
d = d+2*x+3
x = x+1
else
d = d+2*(x-y)+5
x = x+1
y = y-1
endif
SetPixel(c
x
+x,c
y
+y)
endwhile
initialisation
choose B
choose A
Translate to the circle center
stop at diagonal ⇒ end of octant
Scan Converting Ellipses

z 2a is the length of the major axis along the x axis.
z 2b is the length of the minor axis along the y axis.
z The midpoint can also be applied to ellipses.
z For simplicity, we draw only the arc of the ellipse that
lies in the first quadrant, the other three quadrants can
be drawn by symmetry
22 22 22
(, ) 0Fxybxayab=+−=
Scan Converting Ellipses: Algorithm
z Firstly we divide the quadrant into two regions
z Boundary between the two regions is
– the point at which the curve has a slope of -1
– the point at which the gradient vector has the i and j components of
equal magnitude
22
(, ) / / 2 2
g
radFxy Fx Fy bx ay=∂ ∂ +∂ ∂ = +ijij
A
M tiep tuyen = -1
B gradient
B C
M
i
Ellipses: Algorithm (cont.)
z At the next midpoint, if a
2
(y
p
-0.5)<=b

2
(x
p
+1), we switch region 1=>2
z In region 1, choices are E and SE
– Initial condition: d
init
= b
2
+a
2
(-b+0.25)
– For a move to E, d
new
= d
old
+Delta
E
with Delta
E
= b
2
(2x
p
+3)
– For a move to SE, d
new
= d
old
+Delta

SE
with
Delta
SE
= b
2
(2x
p
+3)+a
2
(-2y
p
+2)
z In region 2, choices are S and SE
– Initial condition: d
init
= b
2
(x
p
+0.5)
2
+a
2
((y-1)
2
-b
2
)
– For a move to S, d

new
= d
old
+Delta
s
with Delta
s
= a
2
(-2y
p
+3)
– For a move to SE, d
new
= d
old
+Delta
SE
with
Delta
SE
= b
2
(2x
p
+2)+a
2
(-2y
p
+3)

z Stop in region 2 when the y value is zero.
Ký tự Bitmap
z Trên cơ sỏ định nghĩamỗikýtự
vớimột font chư cho trướclàmột
bitmap ch
ữ nhậtnhỏ
z Font/typeface: set of character
shapes
z fontcache
– cáckýtự theo chuỗiliêntiếpnhau
trong bộ nhớ
z Dạng cơ bản
– (thường N, nghiêng I, đậmB,
nghiêng đậmB+I)
z Thuộc tính
– Also colour, size, spacing and
orientation
ab
8
Cấutrúcfont chữ
Typedef struct
{
int leftx,
int width;
} Char location; //Vị trí của text
Typedef struct
{
CacheId;
Heiglit; // Độ rộng chữ
CharSpace; // Khoảng

cách giữacáckýtự
Charlocation Table [128];
} fontcache
Ký tự vector
z Xây dựng theo phương
pháp đ
ịnh nghĩacáckýtự
bởi đường cong mềm bao
ngoài c
ủa chúng.
z Tốn kém nhấtvề mặt tính
toán
z Chất lượngcao
So sánh
z Đơngiản trông việcsinhký
tự ( copypixel)
z Lưutrữ lớn
z Các phép biến đổi (I,B,
scale) đòi hỏilưutrữ thêm
z Kích thước không dổi
z Phứctạp (Tính toán
phương trình)
z Lưutrữ gọnnhẹ
z Các phép biến đổidựavào
các công thứcbiến đổi
z Kích thướcphụ thuôc vào
môi trường ( ko có kích
thướccốđịnh)
Giải thuật đường quét sinh đa giác
Polygon Scan Conversion

z Tồn tại rất nhiều giải thuật sinh đa giác.
z Mỗi giải thuật phục vụ cho 1 loại đa giác nhất
định:
– some algorithms allow triangular polygons only
– others require that the polygons are convex and non self-
intersecting and have no holes
triangular convex non-convex self-intersecting religious
Polygon Scan Conversion
z Polygon scan conversion là giải thuật chung kinh điển cho các
loại khác nhau
z Cho mỗi đoạn thẳng quét, chúng ta xác định các cạnh của đa
giác cắt đoạn thẳng compute spans representing the interior
portions of the polygons along this scan-line and fill the
associated pixels.
z This represents the heart of a scan-line rendering algorithm
used in many commercial products including Renderman and
3D Studio MAX.
Polygon Scan Conversion
z Dùng giảithuật(trungđiểm) để xác
định các điểmbiênchomỗi đagiác
theo thứ tự tăng củax.
z Các diểmphải:
– Không bị chia sẻ bởicácđagiác
lân cận
– Các đagiácchỉ toàn các điểm
cạnh( điểm biên)
z Đảmbảocácđagiácchiasẻđiểm
biên mà không chia sẻ các điểm
ảnh bên trong của mình.
9

Polygon Scan Conversion
z Thủ tục chung:
– Xác định giao của đường thẳng quét với cạnh đa giác
– Sắp xếp các giao điểm theo mức độ tăng dần của x value
– Điền các điểm ảnh vào giữa cặp các điểm x
z Need to handle 4 cases to prevent pixel sharing:
– if intersection has fractional x value, do we round up or down?
z if inside (on left of span) round up, if outside (on right) round down
– what happens if intersection is at an integer x value?
z if on left of span assume its interior otherwise exterior
– how do we handle shared vertices?
z ignore pixel associated with y
max
of an edge
– how do we handle horizontal edges?
z handled as a result of previous rule (lower edges not drawn)
Polygon Scan Conversion
rounded down for A
rounded up for B
integer x value is on
right = exterior
y
max
not
included
horizontal edge
removed
Polygon Scan Conversion
z Determining intersections with polygon edges is expensive
– rather than re-computing all intersections at each iteration, use

incremental calculations
– i.e. if we intersect edge e on scan-line i then it is likely we will
intersect the edge on scan-line i+1 (this is known as edge-
coherence)
z Assume slope of the edge > 1 (other edges obtained via
symmetries)
– incremental DDA calculation was:
– slope m is given by
– note that numerator and denominator are integral ⇒ we can use
integer DDA.
m
xxyy
iiii
1
,1
11
+=+=
++
(
)
()
startend
startend
xx
yy
m


=

×