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

slike bài giảng đồ hoạ và hiện thực ảo - lê tấn hùng chương 4 các giải thuật 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 (502.62 KB, 10 trang )

Khoa CNTT-DDHBK Hà nội
Email:
0913030731
1
1
Các giảithuậtcơ sở
Le Tan Hung

0913030731
Bài 3
2
Nội dung
z Các giảithuậtxéntỉa - Clipping
z Các thuật toán tô miềnkín
z Phép xử lý Antialiasing
3
Xén tỉa - Clipping
z Khái niệm
Xén tỉa là tiến trình tự động xác định
các điểm của 1 đối tượng nằm trong
hay ngoài cửa sổ hiển thị
z Tiết kiệm thời gian tiến trình rasterize
bỏ qua phần nằm ngoài cửa sổ hiển
thị
z Clipping điểm
xmin ≤ x ≤ xmax
ymin ≤ y ≤ ymax
x
min
x
max


y
max
y
min
4
Clipping đoạnthẳng
z Tiến trình, giải thuật kiểm tra chấp nhận các
đoạn thẳng nằm trong và loại bỏ các đoạn thẳng
nằm ngoài dựa trên 2 điểm đầu cuối
z Lý do:
z Không kiểm tra mọi điểm trên đoạn thẳng
z Hầu hết các đoạn thẳng với 1 màn hình hiển thị
đều được chấp nhận hoặc loại bỏ
z Rất ít các đợn thẳng cắt cửa sổ hiển thị
5
Giảithuật Cohen Sutherland Outcode
z Giải thuật Cohen-Sutherland
thực hiện nhanh với các trương
hợp đoạn thẳng nằm trong hay
ngoài cửa sổ hiện thị
z Mỗi điểm đầu cuối được gán mã
code phụ thuộc vào vị trí trong
mặt phẳng mã
z p.code = 0000
z If p.x <= xmin >> P.code or 0001
z If p.y <= ymin >> P.code or 0100
z If p.x >= xmax >> P.code or 0010
z If p.y >= ymax >> P.code or 1000
6
z If P

1
.code OR P
2
.code == 0000
– Chấp nhận toàn đoạn thẳng
z If P
1
.code AND P
2
.code != 0000
– Loại
z Với truờng hợp cắt, giải thuật xác định lại
điểm đầu cuối là giao của đoạn thẳng và
khung bao của cửa sổ hiển thị
Khoa CNTT-DDHBK Hà nội
Email:
0913030731
2
7
Liabarsky
z x = x
1
+ (x
2
-x
1
)u = x
1
+ uDx
z y = y

1
+ (y
2
-y
1
)u = y
1
+ uDy
z xmin ≤ x
1
+ Dx.u ≤ xmax ⇔ x ∈ [xm, xM]
z ymin ≤ y
1
+ Dy.u ≤ ymax ⇔ y ∈ [ym, yM]
z Pk u ≤ qk k = 1, 2, 3, 4







=
−=
=
−=
DyP
DyP
DxP
DxP

4
3
2
1







−=
−=
−=
−=
14
13
12
11
yyq
yyq
xxq
xxq
M
m
M
m
8
z NếuPk= 0 : điều đótương đương vớiviệc đoạnthẳng
đang xét song song vớicạnh thứ kcủahìnhchữ nhật

clipping.
z a) Nếuqk< 0 ⇒ Đường thẳng nằm ngoài cửasổ (hệ bất
phương trình trên vô nghiệm)
z b)Nếuqk>= 0 thì đoạnthẳng nằm trong hoặcnằmtrên
cạnh củacửasổ clipping.
z Hệ bấtphương trình luôn thoả mãn.
9
z NếuPk≠ 0 : đoạnthẳng đang xét sẽ cắtcạnh k tương ứng
củacửasổ clipping tạivị trí trên đoạnthẳng uk = qk/Pk.
– Pk < 0 đoạnthẳng có dạng đitừ ngoài vào trong
z bấtphương trình sẽ có dạng u ≥ qk/Pk Ù u ≥ uk.
– Pk > 0
z u ≥ uk sẽ thuộccửasổ hiểnthị.
z bấtphương trình sẽ có dạng u ≤ qk/Pk
z u ≤ uk vớiuk= qk/Pk là giao của đoạnthẳng với
cạnh k củacửasổ clipping
z đoạnthẳng có dạng đitừ trong ra ngoài so vớicạnh
k.
10
z Pk < 0 và uk < 0
– cạnh k củacửasổ clipping cắt đoạnthẳng tạiphầnmở rộng
nằm ngoài đoạnthẳng.
– uk ≤ u< 0 thoả mãn bấtphương trình sẽ không nằmtrênđoạn
thẳng cầnxét.
– => uk sẽ nhậnlà0 khi uk<0
z Pk > 0 và uk > 1
– => uk tương ứng sẽ nhậngiátrị 1.
z điểmnằm trong cửasổ clipping sẽ có dạng như sau:
– U
1

≤ u ≤ U
2
11
{}














<=∪= 0,:0max
1 k
k
k
kk
P
P
q
uuU
{}















>=∪= 0,:1min
2 k
k
k
kk
P
P
q
uuU
12
Sutherland-Hodgman Clipping
z Basic idea:
– Consider each edge of the viewport individually
– Clip the polygon against the edge equation
– After doing all planes, the polygon is fully clipped
Khoa CNTT-DDHBK Hà nội
Email:
0913030731

3
13
Sutherland-Hodgman Clipping
z Input/output for algorithm:
– Input: list of polygon vertices in order
– Output: list of clipped poygon vertices consisting of
old vertices (maybe) and new vertices (maybe)
z Note: this is exactly what we expect from the
clipping operation against each edge
14
Sutherland-Hodgman Clipping
z Sutherland-Hodgman basic routine:
– Go around polygon one vertex at a time
– Current vertex has position p
– Previous vertex had position s, and it has been added
to the output if appropriate
15
Sutherland-Hodgman Clipping
z Edge from s to p takes one of four cases:
(Purple line can be a line or a plane)
inside outside
s
p
p output
inside outside
s
p
no output
inside outside
s

p
i output
inside outside
s
p
i output
p output
16
Sutherland-Hodgman Clipping
z Four cases:
– s inside plane and p inside plane
z Add p to output
z Note: s has already been added
– s inside plane and p outside plane
z Find intersection point i
z Add i to output
– s outside plane and p outside plane
z Add nothing
– s outside plane and p inside plane
z Find intersection point i
z Add i to output, followed by p
17
GiảithuậtCyrus-Beck
Liang Barsky
z Giải Cohen-Sutherland yêu cầu cửa sổ là hình chữ
nhật, các cạnh là cạnh của màn hình
z Vấn đề nảy sinh khi cửa sổ clip là 1 đa giác bất kỳ
hoặc hình chữ nhật quay đi 1 góc
z Giải thuật Liang-Barsky tối ưu khi tìm giao điểm của
đoạn thẳng với cử sổ hiển thị

z Nicholl-Lee-Nicholl reducing redundant boundary
clipping by identifying edge and corner regions
18
3-D Clipping
z Before actually drawing on the screen, we have
to clip (Why?)
z Can we transform to screen coordinates first,
then clip in 2D?
– Correctness: shouldn’t draw objects behind viewer
(what will an object with negative z coordinates do in
our perspective matrix?) (draw it…)
Khoa CNTT-DDHBK Hà nội
Email:
0913030731
4
19
Giảithuật đường biên (Boundary - File
Algorithm)
z Giải_thuật_đường_biên ( x, y )
Color : biếnmầu
Begin
Color = Readpixel ( x, y );
If ( Color = mầutô) or ( Color = mầu đường biên )
Kếtthúcvìchạmbiên
hoặcchạmphần đãtô
Else
Putcolor(x,y, mauto)
Giải_thuật_đường_biên ( x+1, y );
Giải_thuật_đường_biên ( x-1, y );
Giải_thuật_đường_biên ( x, y+1 );

Giải_thuật_đường_biên ( x, y-1 );
// Thựchiệnlạigiảithuậtvớicácđiểmlâncận
End.
20
Edge Walking
z Basic idea:
– Draw edges vertically
– Fill in horizontal spans for each scanline
– Interpolate colors down edges
– At each scanline, interpolate
edge colors across span
21
Edge Walking: Notes
z Order vertices in x and y
– 3 cases: break left, break right, no break
z Walk down left and right edges
– Fill each span
– Until breakpoint or bottom vertex is reached
z Advantage: can be made very fast
z Disadvantages:
– Lots of finicky special cases
– Tough to get right
– Need to pay attention to fractional offsets
22
Edge Walking: Notes
z Fractional offsets:
z Be careful when interpolating color values!
z Also: beware gaps between adjacent edges
23
Giảithuật đường quét

Scan-Line Algorithm
z The scan-line algorithm uses edge-coherence and
incremental integer calculations for maximum efficiency:
– Tạobảng edge table (ET) tậpcủacáccạnh đa giác theo thứ tự
giá trị y
min
của chúng
– Tạobảng active edge table (AET) tậpcáccạnh giao vớI đoạn
thẳng quét scan-line
z Trong tiến trình quét các cạnh sẽ chuyểntừ ET ra AET.
z Các cạnh sẽở trong AET cho đến khi giá trị y
max
của
cạnh đạttới = scanline
z Lúc nay cạnh sẽ bị loạirakhỏiAET.
24
Edge Table (ET)
Note: line (8,6) → (13,6) has been deleted according to the scan rules
y
max
x
min
numerator
denominator
scan-line
(0,0)
(15,15)
5
31


=⇒
m
Khoa CNTT-DDHBK Hà nội
Email:
0913030731
5
25
Giảithuật dòng quét-Scanline cho việctômầu vùng
AET =
y
ma
x
current x denominator current numerator
round up
round down
26
Active Edge Table (AET)
y
max
current x denominator
AET =
current numerator
round up
round down
27
Scan-Line Algorithm
y = y of first non empty entry in ET
AET = null
repeat
move all ET entries in slot y to AET

sort AET entries according to x
min
fill spans using pairs of AET entries
for all AET members
if y
max
= y then remove from AET
y = y+1
for all AET members
update numerator
if numerator>denominator
numerator=numerator-denominator
x = x+1
until AET and ET empty
28
Rasterizing Triangles
z Interactive graphics hardware commonly uses
edge walking or edge equation techniques for
rasterizing triangles
z Two techniques we won’t talk about much:
– Recursive subdivision of primitive into micropolygons
(REYES, Renderman)
– Recursive subdivision of screen (Warnock)
29
Recursive Triangle Subdivision
30
Edge Equations
z An edge equation is simply the equation of the
line containing that edge
– Q: What is the equation of a 2D line?

– A: Ax + By + C = 0
– Q: Given a point (x,y), what does plugging x & y into
this equation tell us?
– A: Whether the point is:
z On the line: Ax + By + C = 0
z “Above” the line: Ax + By + C > 0
z “Below” the line: Ax + By + C < 0
Khoa CNTT-DDHBK Hà nội
Email:
0913030731
6
31
Edge Equations
z Edge equations thus define two half-spaces:
z And a triangle can be defined as the intersection of
three positive half-spaces:
A
1
x

+

B
1
y

+

C
1

<

0
A
2
x

+

B
2
y

+

C
2
<

0
A
3
x

+

B
3
y


+

C
3
<

0
A
1
x

+

B
1
y

+

C
1
>

0
A
3
x

+


B
3
y

+

C
3
>

0
A
2
x

+

B
2
y

+

C
2
>

0
32
Edge Equations

z So…simply turn on those pixels for which all
edge equations evaluate to > 0:
+
+
+
-
-
-
33
Using Edge Equations
z An aside: How do you suppose edge equations
are implemented in hardware?
z How would you implement an edge-equation
rasterizer in software?
– Which pixels do you consider?
– How do you compute the edge equations?
– How do you orient them correctly?
34
Using Edge Equations
z Which pixels: compute min,max bounding box
z Edge equations: compute from vertices
z Orientation: ensure area is positive (why?)
35
Hiệu ứng răng cưa
Aliasing - Antialiasing
z Aliasing: signal processing term
with very specific meaning
z Aliasing: computer graphics term
for any unwanted visual artifact
z Antialiasing: computer graphics

term for avoiding unwanted
artifacts
36
Signal Processing
z Raster display: regular sampling of a continuous
function (Really?)
z Think about sampling a 1-D function:
Khoa CNTT-DDHBK Hà nội
Email:
0913030731
7
37
Signal Processing
z Sampling a 1-D function:
38
Signal Processing
z Sampling a 1-D function:
39
Signal Processing
z Sampling a 1-D function:
– What do you notice?
40
Signal Processing
z Sampling a 1-D function: what do you notice?
– Jagged, not smooth
41
Signal Processing
z Sampling a 1-D function: what do you notice?
– Jagged, not smooth
– Loses information!

42
Antialiasing
z Méo thông tin trong quá trình lấymẫutầnsố
thấp
z In raster images – leads to jagged edges with
hiệu ứng bậc thang – staircase effect
z Việc làm giảm hiệu ứng méo thông tin bằng
phương pháp bù trừ
sampling frequency
Khoa CNTT-DDHBK Hà nội
Email:
0913030731
8
43
Phương pháp khử hiệu ứng răng cưa
Antialiasing Methods
1. Cốđịnh tín hiệubằng phương pháp lọc-prefiltering:
Giảm độ rộng dảitần tín hiệubỏibộ lọcthấphơntrướckhi
lấymẫu.
Highest quality method, but often impractical.
2. Cốđịnh mẫubằng siêu mẫu supersampling:
Use more samples to raise the Nyquist frequency.
Simple and widely used.
3. Cốđịnh mẫubằng phương pháp mẫubấtkỳ
- stochastic sampling:
Sample randomly, not uniformly.
Relatively simple, usually used in combination with
supersampling.
44
Prefiltering – Lọc

z Eliminate high frequencies before sampling
(Foley & van Dam p. 630)
– Convert I(x) to F(u)
– Apply a low-pass filter (e.g., multiply F(u) by a box
function)
– Then sample. Result: no aliasing!
z Problem: most rendering algorithms generate
sampled function directly
– e.g., Z-buffer, ray tracing
45
Antialiasing in the
Continuous Domain
z Problem with prefiltering:
– Sampling and image generation inextricably linked in
most renderers
z Z-buffer algorithm
z Ray tracing
– Why?
z Sự cuốn méo với các miền liên tục do hiệu ứng
xấp xỉ của phương pháp tiền lọc
46
Phương pháp siêu mẫu
z Supersampling cons
– Doesn’t eliminate aliasing, just shifts the Nyquist limit
higher
z Can’t fix some scenes (e.g., checkerboard)
– Tăng bộ nhớ cho việc lưu trữ
z Supersampling pros
– Relatively easy
– Often works all right in practice

– Can be added to a standard renderer
47
Antialiasing by
supersampling
48
Khoa CNTT-DDHBK Hà nội
Email:
0913030731
9
49
anti aliasing (1)
50
Antialiasing (2)
51
The A-Buffer
z Idea: approximate continuous filtering by
subpixel sampling
z Summing areas now becomes simple
52
The A-Buffer
z Advantages:
– Incorporating into scanline renderer reduces storage costs
dramatically
– Processing per pixel depends only on number of visible
fragments
– Can be implemented efficiently using bitwise logical ops on
subpixel masks
z Disadvantages
– Still basically a supersampling algorithm
– Not a hardware-friendly algorithm

z Lists of potentially visible polygons can
grow without limit
z Work per-pixel non-deterministic
53
Recap:
Antialiasing Strategies
z Supersampling: sample at higher resolution, then
filter down
– Pros:
z Conceptually simple
z Easy to retrofit existing renderers
z Works well most of the time
– Cons:
z High storage costs
z Doesn’t eliminate aliasing, just shifts Nyquist limit upwards
54
Antialiasing Strategies
z A-Buffer: approximate prefiltering of continuous
signal by sampling
– Pros:
z Integrating with scan-line renderer keeps storage costs low
z Can be efficiently implemented with clever bitwise operations
– Cons:
z Still basically a supersampling approach
z Doesn’t integrate with ray-tracing
Khoa CNTT-DDHBK Hà nội
Email:
0913030731
10
55

Stochastic Sampling
z An intuitive argument:
– In stochastic sampling, every region of the image has a finite
probability of being sampled
– Thus small features that fall between uniform sample points tend
to be detected by non-uniform samples
z Integrating with different renderers:
– Ray tracing:
z It is just as easy to fire a ray one direction as another
– Z-buffer: hard, but possible
z Notable example: REYES system (?)
z Using image jittering is easier (more later)
– A-buffer: nope
z Totally built around square pixel filter and primitive-to-sample
coherence
56
Stochastic Sampling
z Idea: randomizing distribution of samples
scatters aliases into noise
z Problem: what type of random distribution
to adopt?
z Reason: type of randomness used affects
spectral characteristics of noise into which high
frequencies are converted
z Problem: given a pixel, how to distribute points
(samples) within it?

×