z a Nếu qk < 0 ⇒ Đường thẳng nằm ngoài cửa sổ hệ bất phương trình trên vô nghiệm z bNếu qk >= 0 thì đoạn thẳng nằm trong hoặc nằm trên cạnh của cửa sổ clipping.. zbất phương trình sẽ có
Trang 1Các giải thuật cơ sở
Le Tan Hung hunglt@it-hut.edu.vn 0913030731
Bài 3
2
Nội dung
zCác giải thuật xén tỉa - Clipping
zCác thuật toán tô miền kín
zPhé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
y max
y min
4
Clipping đoạn thẳng
zTiế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
zLý do:
zKhông kiểm tra mọi điểm trên đoạn thẳng
zHầ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ỏ
zRất ít các đợn thẳng cắt cửa sổ hiển thị
5
Giải thuậ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ị
Trang 2Liabarsky
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]
zPk u ≤ qk k = 1, 2, 3, 4
⎪
⎪
⎩
⎪
⎧
=
−
=
=
−
=
Dy P Dy P
Dx P Dx P
4 3 2 1
⎪
⎪
⎩
⎪
⎧
−
=
−
=
−
=
−
=
1 4
1
3
1 2
1
1
y y
q
y y
q
x x
q
x x
q
M m M m
8
z Nếu Pk = 0 : điều đó tương đương với việc đoạn thẳng
đang xét song song với cạnh thứ k của hình chữ nhật clipping
z a) Nếu qk < 0 ⇒ Đường thẳng nằm ngoài cửa sổ (hệ bất phương trình trên vô nghiệm)
z b)Nếu qk >= 0 thì đoạn thẳng nằm trong hoặc nằm trên cạnh của cửa sổ clipping
z Hệ bất phương trình luôn thoả mãn
9
z Nếu Pk≠ 0 : đoạn thẳng đang xét sẽ cắt cạnh k tương ứng
của cửa sổ clipping tại vị trí trên đoạn thẳng uk = qk/Pk
– Pk < 0 đoạn thẳng có dạng đi từ ngoài vào trong
zbất phương trình sẽ có dạng u≥ qk/Pk Ù u ≥ uk
– Pk > 0
zu≥ uk sẽ thuộc cửa sổ hiển thị
zbất phương trình sẽ có dạng u≤ qk/Pk
zu≤ uk với uk = qk/Pk là giao của đoạn thẳng với
cạnh k của cửa sổ clipping
zđoạn thẳng có dạng đi từ trong ra ngoài so với cạnh
k
10
z Pk < 0 và uk < 0 – cạnh k của cửa sổ clipping cắt đoạn thẳng tại phần mở rộng nằm ngoài đoạn thẳng
– uk ≤ u< 0 thoả mãn bất phương trình sẽ không nằm trên đoạn thẳng cần xét
– => uk sẽ nhận là 0 khi uk<0
z Pk > 0 và uk > 1 – => uk tương ứng sẽ nhận giá trị 1
z điểm nằm trong cửa sổ clipping sẽ có dạng như sau:
– U1≤ u ≤ U2
⎠
⎞
⎜
⎜
⎝
⎛
⎭
⎬
⎫
⎩
⎨
⎧
<
=
∪
k
k k
P
q u u U
⎠
⎞
⎜
⎜
⎝
⎛
⎭
⎬
⎫
⎩
⎨
⎧
>
=
∪
k
k k
P
q u u U
Sutherland-Hodgman Clipping
zBasic idea:
– Consider each edge of the viewport individually
– Clip the polygon against the edge equation
– After doing all planes, the polygon is fully clipped
Trang 3Sutherland-Hodgman Clipping
zInput/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)
zNote: this is exactly what we expect from the
clipping operation against each edge
14
Sutherland-Hodgman Clipping
zSutherland-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
zEdge 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
i output
inside outside
s p
i output
p output
16
Sutherland-Hodgman Clipping
zFour cases:
– sinside plane and pinside plane
z Add pto output
z Note: shas already been added
– sinside plane and poutside plane
z Find intersection point i
z Add ito output
– soutside plane and poutside plane
z Add nothing
– soutside plane and pinside plane
z Find intersection point i
z Add ito output, followed by p
17
Giải thuật Cyrus-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
zBefore actually drawing on the screen, we have
to clip ( Why? )
zCan 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…)
Trang 4Giải thuật đường biên (Boundary - File
Algorithm)
z Giải_thuật_đường_biên ( x, y )
Color : biến mầu
Begin
Color = Readpixel ( x, y );
If ( Color = mầu tô ) or ( Color = mầu đường biên )
Kết thúc vì chạm biên
hoặc chạm phầ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ực hiện lại giải thuật với các điểm lân cận
End
20
Edge Walking
zBasic 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
zOrder vertices in x and y
– 3 cases: break left, break right, no break
zWalk down left and right edges
– Fill each span
– Until breakpoint or bottom vertex is reached
zAdvantage: can be made very fast
zDisadvantages:
– Lots of finicky special cases
– Tough to get right
– Need to pay attention to fractional offsets
22
Edge Walking: Notes
zFractional offsets:
zBe careful when interpolating color values!
zAlso: beware gaps between adjacent edges
Giải thuật đường quét
Scan-Line Algorithm
z The scan-line algorithm uses edge-coherence and
incremental integer calculations for maximum efficiency:
– Tạo bảng edge table (ET) tập của các cạnh đa giác theo thứ tự
giá trị ymincủa chúng
– Tạo bảng active edge table (AET) tập các cạ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ển từ ET ra AET
z Các cạnh sẽ ở trong AET cho đến khi giá trị y maxcủa
cạnh đạt tới = scanline
z Lúc nay cạnh sẽ bị loại ra khỏi AET
Edge Table (ET)
y max x min
numerator denominator
scan-line
(0,0)
(15,15)
5 3
⇒
m
Trang 5Giải thuật dòng quét-Scanline cho việc tô 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
zInteractive graphics hardware commonly uses
edge walking or edge equation techniques for rasterizing triangles
zTwo 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
zAn 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:
zOn the line: Ax + By + C = 0
z“Above” the line: Ax + By + C > 0
z“Below” the line: Ax + By + C < 0
Trang 6Edge 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 x 3 +
3
y +
C 3
< 0
A 1 x + B 1 y + C 1 > 0
A x 3 +
3
y +
C 3
> 0 A 2 x + B 2
y + C
2 > 0
32
Edge Equations
zSo…simply turn on those pixels for which all edge equations evaluate to > 0:
+ +
-33
Using Edge Equations
zAn aside: How do you suppose edge equations
are implemented in hardware?
zHow 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
zWhich pixels: compute min,max bounding box
zEdge equations: compute from vertices
zOrientation: ensure area is positive ( why? )
Hiệu ứng răng cưa
Aliasing - Antialiasing
zAliasing: signal processing term
with very specific meaning
zAliasing: computer graphics term
for any unwanted visual artifact
zAntialiasing: computer graphics
term for avoiding unwanted
artifacts
Signal Processing
zRaster display: regular sampling of a continuous function ( Really? )
zThink about sampling a 1-D function:
Trang 7Signal Processing
zSampling a 1-D function:
38
Signal Processing
zSampling a 1-D function:
39
Signal Processing
zSampling a 1-D function:
– What do you notice?
40
Signal Processing
zSampling a 1-D function: what do you notice?
– Jagged, not smooth
41
Signal Processing
zSampling a 1-D function: what do you notice?
– Jagged, not smooth
– Loses information!
42
Antialiasing
zMéo thông tin trong quá trình lấy mẫu tần số thấp
zIn raster images – leads to jagged edges with hiệu ứng bậc thang – staircase effect
zViệc làm giảm hiệu ứng méo thông tin bằng phương pháp bù trừ
sampling frequency
Trang 8Phương pháp khử hiệu ứng răng cưa
Antialiasing Methods
1 Cố định tín hiệu bằng phương pháp lọc-prefiltering:
Giảm độ rộng dải tần tín hiệu bỏi bộ lọc thấphơn trước khi
lấy mẫu.
Highest quality method, but often impractical.
2 Cố định mẫu bằng siêu mẫu supersampling:
Use more samples to raise the Nyquist frequency
Simple and widely used.
3 Cố định mẫu bằng phương pháp mẫu bất kỳ
- stochastic sampling:
Sample randomly, not uniformly
Relatively simple, usually used in combination with
supersampling.
44
Prefiltering – Lọc
zEliminate 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!
zProblem: most rendering algorithms generate sampled function directly
– e.g., Z-buffer, ray tracing
45
Antialiasing in the
Continuous Domain
zProblem with prefiltering:
– Sampling and image generation inextricably linked in
most renderers
z Z-buffer algorithm
z Ray tracing
zSự 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
zSupersampling 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ữ
zSupersampling pros
– Relatively easy
– Often works all right in practice
– Can be added to a standard renderer
Antialiasing by
supersampling
Trang 9anti 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
zSupersampling: 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
zA-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
Trang 10Stochastic 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: REYESsystem ( ? )
z Using image jitteringis easier (more later)
– A-buffer: nope
z Totally built around square pixel filter and primitive-to-sample
Stochastic Sampling
zIdea: randomizing distribution of samples scatters aliases into noise
zProblem: what type of random distribution
to adopt?
zReason: type of randomness used affects spectral characteristics of noise into which high frequencies are converted
zProblem: given a pixel, how to distribute points (samples) within it?