1. Trang chủ
  2. » Giáo án - Bài giảng

Bài giảng Đồ họa và hiện thực ảo - Bài 3: Các giải thuật cơ sở

7 9 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

[r]

(1)

1

Các gii thut cơ s

Le Tan Hung hunglt@it-hut.edu.vn 0913030731

Bài 3

2

Ni dung

zCác giải thuật xén tỉa - Clipping zCác thuật tốn tơ miền kín zPhép xửlý Antialiasing

3

Xén ta - Clipping z Khái niệm

Xén tỉa tiến trình tựđộng xác định điểm đối tượng nằm hay 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 ngồi cửa sổhiển thị

z Clipping điểm

xmin x xmax ymin y ymax

xmin xmax

ymax

ymin

4

Clipping đon thng

zTiến trình, giải thuật kiểm tra chấp nhận

đoạn thẳng nằm loại bỏ đoạn thẳng nằm dựa điểm đầu cuối

zLý do:

zKhông kiểm tra mọi điểm đoạn thẳng zHầu hết đoạn thẳng với hình hiển thị

đều được chấp nhận hoặc loại bỏ

zRất đợ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 nhanh với trương hợp đoạn thẳng nằm hay cửa sổ thị

z Mỗi điểm đầu cuối gán mã code phụ thuộc vào vị trí 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 P1.code OR P2.code == 0000

Chấp nhận toàn đoạn thẳng

z If P1.code AND P2.code != 0000

Loại

(2)

7

Liabarsky

z x = x1+ (x2- x1)u = x1+ uDx z y = y1+ (y2- y1)u = y1+ uDy

z xminx1+ Dx.uxmaxx [xm, xM] z yminy1+ Dy.uymaxy [ym, yM]

zPk u ≤qk k= 1, 2, 3, 4

⎪ ⎪ ⎩ ⎪ ⎪ ⎨ ⎧ = − = = − = Dy P Dy P Dx P Dx P ⎪ ⎪ ⎩ ⎪ ⎪ ⎨ ⎧ − = − = − = − = 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 xét song song với cạnh thứ k hình chữ nhật clipping

z a)Nếu qk< ⇒Đường thẳng nằm cửa sổ(hệbất phương trình vơ nghiệm)

z b)Nếu qk>= thìđoạn thẳng nằm nằm

cạnh cửa sổclipping

z Hệbất phương trình ln 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 sổclipping vịtrí trênđoạn thẳng uk= qk/Pk

– Pk< đoạn thẳng có dạngđi từngồi vào

zbất phương trình sẽcó dạng u≥qk/PkÙu≥uk – Pk>

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 giao củađoạn thẳng với cạnh k cửa sổclipping

zđoạn thẳng có dạngđi từtrong ngồi so với cạnh

k

10

z Pk< uk<

– cạnh k cửa sổclipping cắtđoạn thẳng phần mởrộng nằm ngoàiđoạn thẳng

– uk≤u< 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 uk<0

z Pk> uk>

– => uk tươngứng sẽnhận giá trị1

z điểm nằm cửa sổclipping sẽcó dạng nhưsau:

– U1≤u≤U2

{ } ⎟⎟ ⎠ ⎞ ⎜ ⎜ ⎝ ⎛ ⎭ ⎬ ⎫ ⎩ ⎨ ⎧ < = ∪

=max 0 : , 0

1 k k k k k P P q u u U {} ⎟⎟ ⎠ ⎞ ⎜ ⎜ ⎝ ⎛ ⎭ ⎬ ⎫ ⎩ ⎨ ⎧ > = ∪

=min 1 : , 0

2 k k k k k P P q u u U Sutherland-Hodgman Clipping zBasic idea:

– Consider each edge of the viewport individually

– Clip the polygon against the edge equation

(3)

13

Sutherland-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 sto ptakes 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 zFour cases:

sinside plane and pinside plane zAdd pto output

zNote: shas already been added – sinside plane and poutside plane

zFind intersection point i zAdd ito output

soutside plane and poutside plane zAdd nothing

soutside plane and pinside plane zFind intersection point i

zAdd ito output, followed by p

17

Gii thut Cyrus-Beck Liang Barsky

z Giải Cohen-Sutherland yêu cầu cửa sổ hình chữ nhật, cạnh cạnh hình

z Vấn đề nảy sinh cửa sổ clip đa giác hình chữ nhật quay góc

z Giải thuật Liang-Barsky tối ưu tìm giao điểm đ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

(4)

19

Gii thutđư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 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 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

– 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

Gii thutđường quét Scan-Line Algorithm

z The scan-line algorithm uses edge-coherenceand

incremental integer calculationsfor maximum efficiency:

– Tạo bảngedge table(ET) tập cạnhđa giác theo thứtự giá trịymincủa chúng

– Tạo bảngactive edge table(AET) tập cạnh giao vớIđoạn

thẳng quét scan-line

z Trong tiến trình quét cạnh sẽchuyển từ ET AET

z Các cạnh AET chođến giá trịymaxcủa

cạnhđạt tới = scanline

z Lúc cạnh sẽbịloại khỏi AET

Edge Table (ET)

Note: line (8,6) →(13,6) has been deleted according to the scan rules ymax xmin

numerator denominator

scan-line (0,0)

(15,15)

5 1=− ⇒

(5)

25

Giải thuật dịng qt-Scanline cho việc tơ mầu vùng

AET = yma x

current x denominator current numerator round up

round down

26

Active Edge Table (AET)

ymax 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 xmin fill spans using pairs of AET entries for all AET members

if ymax= 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 walkingor edge equationtechniques 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& yinto this equation tell us?

(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:

A1x + B1 y + C1< 0

A2 x + B

2y + C 2< 0

Ax3 + B

3 y + C3

< 0

A1x + B1 y + C1> 0

Ax3 + B

3 y + C3

> 0 A2x + B 2y + 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 you suppose edge equations are implemented in hardware?

zHow would you implement an edge-equation

rasterizer in software? – Which pixels you consider?

How you compute the edge equations?How 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 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?)

(7)

37

Signal Processing zSampling a 1-D function:

38

Signal Processing zSampling a 1-D function:

39

Signal Processing zSampling a 1-D function:

– What you notice?

40

Signal Processing

zSampling a 1-D function: what you notice?

– Jagged, not smooth

41

Signal Processing

zSampling a 1-D function: what you notice?

– Jagged, not smooth

– Loses information!

42

Antialiasing

zMéo thơng tin q 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ừ

Ngày đăng: 09/03/2021, 05:10

Xem thêm: