24
CAP4730: Computational Structures in Computer Graphics Triangle Scan Conversion

CAP4730: Computational Structures in Computer Graphics Triangle Scan Conversion

Embed Size (px)

Citation preview

Page 1: CAP4730: Computational Structures in Computer Graphics Triangle Scan Conversion

CAP4730: Computational Structures in Computer Graphics

Triangle Scan Conversion

Page 2: CAP4730: Computational Structures in Computer Graphics Triangle Scan Conversion

Triangle Area Filling Algorithms

• Why do we care about triangles?

• Edge Equations

• Edge Walking

Page 3: CAP4730: Computational Structures in Computer Graphics Triangle Scan Conversion
Page 4: CAP4730: Computational Structures in Computer Graphics Triangle Scan Conversion

We want something easier

• It is easier to do 1 thing VERY fast than 2 things pretty fast.

• Why? Think about how you code.– Scan conversion

• Polygon

• Circle

– Clipping

Page 5: CAP4730: Computational Structures in Computer Graphics Triangle Scan Conversion

Do something easier!

• Instead of polygons, let’s do something easy!

• TRIANGLES! Why? 1) All polygons can be broken into triangles

2) Easy to specify

3) Always convex

4) Going to 3D is MUCH easier

Page 6: CAP4730: Computational Structures in Computer Graphics Triangle Scan Conversion

Polygons can be broken down

Triangulate - Dividing a polygon into triangles.

Is it always possible? Why?

Page 7: CAP4730: Computational Structures in Computer Graphics Triangle Scan Conversion

Any object can be broken down into polygons

Page 8: CAP4730: Computational Structures in Computer Graphics Triangle Scan Conversion

Specifying a model

• For polygons, we had to worry about connectivity AND vertices.

• How would you specify a triangle? (What is the minimum you need to draw one?)– Only vertices

(x1,y1) (x2,y2) (x3,y3)

– No ambiguity– Line equations

A1x1+B1y1+C1=0 A2x2+B2y2+C2=0 A3x3+B3y3+C3=0

Page 9: CAP4730: Computational Structures in Computer Graphics Triangle Scan Conversion

Triangles are always convex

• What is a convex shape?

An object is convex if and only if any line segment connecting two points on its boundary is contained entirely within the object or one of its boundaries. Think about scan lines again!

Page 10: CAP4730: Computational Structures in Computer Graphics Triangle Scan Conversion

Scan Converting a Triangle

• Recap what we are trying to do

• Two main ways to rasterize a triangle– Edge Equations

• A1x1+B1y1+C1=0

• A2x2+B2y2+C2=0

• A3x3+B3y3+C3=0

– Edge Walking

Page 11: CAP4730: Computational Structures in Computer Graphics Triangle Scan Conversion

Types of Triangles

What determines the spans? Can you think of an easy way to compute spans?

What is the special vertex here?

Page 12: CAP4730: Computational Structures in Computer Graphics Triangle Scan Conversion

Edge Walking

• 1. Sort vertices in y and then x

• 2. Determine the middle vertex

• 3. Walk down edges from P0

• 4. Compute spans

P0

P1

P2

Page 13: CAP4730: Computational Structures in Computer Graphics Triangle Scan Conversion

Edge Walking Pros and Cons

Pros• Fast• Easy to implement in

hardware

Cons• Special Cases• Interpolation can be

tricky

Page 14: CAP4730: Computational Structures in Computer Graphics Triangle Scan Conversion

Color Interpolating

P0

P1

P2

(?, ?, ?)

(?, ?, ?)

Page 15: CAP4730: Computational Structures in Computer Graphics Triangle Scan Conversion

Edge Equations

• A1x1+B1y1+C1=0

• A2x2+B2y2+C2=0

• A3x3+B3y3+C3=0

• How do you go from:

x1, y1 - x2, y2 toA1x1+B1y1+C1?

P0

P1P2

Page 16: CAP4730: Computational Structures in Computer Graphics Triangle Scan Conversion

Given 2 points, compute A,B,C

0

0

11

00

CByAx

CByAx

0

0

11

00

C

C

B

A

yx

yx

C

C

B

A

yx

yx

11

00

1

1

11

00 CB

A

yx

yx

01

01

0110 xx

yy

yxyx

C

B

A

C = x0y1 – x1y0

A = y0 – y1

B = x1 – x0

Page 17: CAP4730: Computational Structures in Computer Graphics Triangle Scan Conversion

Edge Equations• What does the edge equation

mean?

• A1x1+B1y1+C1=0

• Pt1[2,1], Pt2[6,11]• A=-10, B=4, C=16• What is the value of the

equation for the: – gray part

– yellow part

– the boundary line

• What happens when we reverse P0 and P1?

P0

P1

Page 18: CAP4730: Computational Structures in Computer Graphics Triangle Scan Conversion

Positive Interior

• We add the C element from each edge– area = edge0.C + edge1.C + edge2.C– if (area>0) then inside points are in the positive

half spaces– if (area<0) then what should we do?– What happens if area=0?

Page 19: CAP4730: Computational Structures in Computer Graphics Triangle Scan Conversion

Combining all edge equations

1) Determine edge equations for all three edges

2) Find out if we should reverse the edges

3) Create a bounding box

4) Test all pixels in the bounding box whether they too reside on the same side

P2

P1

P0

Page 20: CAP4730: Computational Structures in Computer Graphics Triangle Scan Conversion

Edge Equations: Interpolating Color

• Given colors (and later, other parameters) at the vertices, how to interpolate across?

• Idea: triangles are planar in any space:– This is the “redness”

parameter space– Note:plane follows form

z = Ax + By + C– Look familiar?

Taken w/ permission from: David Luebke, UVAhttp://www.cs.virginia.edu/~gfx/Courses/2003/Intro.spring.03/lecture17.ppt

Page 21: CAP4730: Computational Structures in Computer Graphics Triangle Scan Conversion

Edge Equations: Interpolating Color

• Given redness at the 3 vertices, set up the linear system of equations:

• The solution works out to:

Page 22: CAP4730: Computational Structures in Computer Graphics Triangle Scan Conversion

Edge Equations:Interpolating Color

• Notice that the columns in the matrix are exactly the coefficients of the edge equations!

• So the setup cost per parameter is basically a matrix multiply

• Per-pixel cost (the inner loop) cost equates to tracking another edge equation value (which is?)– A: 1 add

Page 23: CAP4730: Computational Structures in Computer Graphics Triangle Scan Conversion

Pros and Cons of Edge Equations

• Pros• If you have the right

hardware (PixelPlanes) then it is very fast

• Fast tests• Easy to interpolate

colors

• Cons• Can be expensive if

you don’t have hardware

• 50% efficient

Page 24: CAP4730: Computational Structures in Computer Graphics Triangle Scan Conversion

Recap

P0

P1

P2

P1

P2

P0