Docsity

Rasterizing Polygons and Triangles in Computer Graphics by David Luebke - Prof. David Lue, Study notes of Computer Science

Toggle sidebar
bg1
1
DavidLuebke 3/8/00
CS 551 / 645:
Introductory Computer Graphics
Rasterizing Triangles
DavidLuebke 3/8/00
Rasterizing Polygons
●In interactive graphics, polygons rule the world
●Two main reasons:
– Lowest comm on denominator for surfaces
■Can represent anysurface
with arbitrary accu racy
■Splines, mathematicalfunctions, volumetric isosurfaces…
– Mathematical simplicity lends it self to simple,
regular rendering algorithms
■Like those we’reabout to discuss…
■Such algorithmsembed well in hardware
DavidLuebke 3/8/00
Rasterizing Polygons
●Triangle is the
minimal unit
of a polygon
– All polygons can be broken up into triangles
■Convex, concave,complex
– Triangles are guaranteed to be:
■Planar
■Convex
–
What exactly does it mean to be convex?
DavidLuebke 3/8/00
Convex Shapes
●A two-dimensional shape is
convex
if and
only if every line segment connecting two
points on the boundary is entirely contained.
DavidLuebke 3/8/00
Convex Shapes
●
Why do we want convex shapes for
rasterization?
●One good answer: because any scan line is
guaranteed to contain at most one segment
or
span
of a triangle
– Another answer coming up later
– Note: the book has an algorithm which handles
concave polygons. It is
much
more complex than
what we’ll present here!
DavidLuebke 3/8/00
Decomposing Polys Into Tris
●Any convex polygon canbe trivially
decomposed into triangles
–Drawit
●Any concave or complex polygon can be
decomposed into triangles, too
– Non-trivial!
pf3
pf4
pf5

Partial preview of the text

Download Rasterizing Polygons and Triangles in Computer Graphics by David Luebke - Prof. David Lue and more Study notes Computer Science in PDF only on Docsity!

David Luebke 3/8/

CS 551 / 645:

Introductory Computer Graphics

Rasterizing Triangles

David Luebke 3/8/

Rasterizing Polygons

l In interactive graphics, polygons rule the world

l Two main reasons:

  • Lowest common denominator for surfaces n Can represent any surfacewith arbitrary accuracy n Splines, mathematical functions, volumetric isosurfaces…
  • Mathematical simplicity lends itself to simple,

regular rendering algorithms

n Like those we’re about to discuss… n Such algorithms embed well in hardware

David Luebke 3/8/

Rasterizing Polygons

l Triangle is theminimal unit of a polygon

  • All polygons can be broken up into triangles n Convex, concave, complex
  • Triangles are guaranteed to be: n Planar n Convex
  • What exactly does it mean to be convex?

David Luebke 3/8/

Convex Shapes

l A two-dimensional shape isconvex if and

only if every line segment connecting two

points on the boundary is entirely contained.

David Luebke 3/8/

Convex Shapes

l Why do we want convex shapes for

rasterization?

l One good answer: because any scan line is

guaranteed to contain at most one segment

orspan of a triangle

  • Another answer coming up later
  • Note: the book has an algorithm which handles

concave polygons. It ismuch more complex than

what we’ll present here!

David Luebke 3/8/

Decomposing Polys Into Tris

l Any convex polygon can be trivially

decomposed into triangles

  • Draw it

l Any concave or complex polygon can be

decomposed into triangles, too

  • Non-trivial!

David Luebke 3/8/

Rasterizing Triangles

l Interactive graphics hardware commonly

usesedge walking oredge equation

techniques for rasterizing triangles

l Two techniques we won’t talk about much:

  • Recursive subdivision of primitive into

micropolygons (REYES, Renderman)

  • Recursive subdivision of screen (Warnock)

David Luebke 3/8/

Recursive Triangle Subdivision

David Luebke 3/8/

Recursive Screen Subdivision

David Luebke 3/8/

Edge Walking

l Basic idea:

  • Draw edges vertically
  • Fill in horizontal spans for each scanline
  • Interpolate colors down edges
  • At each scanline, interpolate

edge colors across span

David Luebke 3/8/

Edge Walking: Notes

l Order vertices in x and y

  • 3 cases: break left, break right, no break

l Walk down left and right edges

  • Fill each span
  • Until breakpoint or bottom vertex is reached

l Advantage: can be made very fast

l Disadvantages:

  • Lots of finicky special cases
  • Tough to get right
  • Need to pay attention tofractional offsets

David Luebke 3/8/

Edge Walking: Notes

l Fractional offsets:

l Be careful when interpolating color values!

l Also: beware gaps between adjacent edges

David Luebke 3/8/

Computing a Bounding Box

l Easy to do

l Surprising number of speed hacks possible

  • See McMillan’s Java code for an example

David Luebke 3/8/

Computing Edge Equations

l Want to calculate A, B, C for each edge from

( x i , y i ) and ( x j , y j )

l Treat it as a linear system:

Ax 1 + By 1 + C = 0

Ax 2 + By 2 + C = 0

l Notice: two equations, three unknowns

l Does this make sense? What can we solve?

l Goal: solve for A & B in terms of C

David Luebke 3/8/

Computing Edge Equations

l Set up the linear system:

l Multiply both sides

by matrix inverse:

l Let C = x 0 y 1 - x 1 y 0 for convenience

  • Then A = y 0 - y 1 and B = x 1 - x (^0)

 

  

 =− 

  

  

  



1

1

1 1

C B

A

x y

x y

 

  



−

−

−

− = 

  



1 0

0 1 1 0 x x

y y

xy xy

C

B

A

David Luebke 3/8/

Computing Edge Equations:

Numerical Issues

l Calculating C = x 0 y 1 - x 1 y 0 involves some

numerical precision issues

  • When is it bad to subtract two floating-point

numbers?

  • A: When they are of similar magnitude n Example: 1.234x10^4 - 1.233x10 4 = 1.000x10^1 n We lose most of the significant digits in result
  • In general, ( x 0 ,y 0 ) and ( x 1 ,y 1 ) (corner vertices of a

triangle) are fairly close, so we have a problem

David Luebke 3/8/

Computing Edge Equations:

Numerical Issues

l We can avoid the problem in this case by

using our definitions ofA andB:

A = y 0 - y 1 B = x 1 - x 0 C = x 0 y 1 - x 1 y 0

Thus:

C = -Ax 0 - By 0 or C = -Ax 1 - By 1

l Why is this better?

l Which should we choose?

  • Trick question! Average the two to avoid bias:

C = - [ A ( x 0 +x 1 ) + B(y 0 +y 1 )] / 2

David Luebke 3/8/

Edge Equations

l So…we can find edge equation from two verts.

l Given three corners C 0 , C 1 , C 0 of a triangle,

what are our three edges?

l How do we make sure the half-spaces defined

by the edge equations all share the same sign

on the interior of the triangle?

l A: Be consistent (Ex: [C 0 C 1 ], [C 1 C 2 ], [C 2 C 0 ])

l How do we make sure that sign is positive?

l A: Test, and flip if needed ( A= -A, B= -B, C= -C )

David Luebke 3/8/

Edge Equations: Code

l Basic structure of code:

  • Setup: compute edge equations, bounding box
  • (Outer loop) For each scanline in bounding box...
  • (Inner loop) …check each pixel on scanline,

evaluating edge equations and drawing the pixel if

all three are positive

David Luebke 3/8/

Optimize This!

findBoundingBox(&xmin, &xmax, &ymin, &ymax); setupEdges (&a0,&b0,&c0,&a1,&b1,&c1,&a2,&b2,&c2);

/ Optimize this: / for (int y = yMin; y <= yMax; y++) { for (int x = xMin; x <= xMax; x++) { float e0 = a0x + b0y + c0; float e1 = a1x + b1y + c1; float e2 = a2x + b2y + c2; if (e0 > 0 && e1 > 0 && e2 > 0) setPixel(x,y); } }**

David Luebke 3/8/

Edge Equations: Speed Hacks

l Some speed hacks for the inner loop:

int xflag = 0; for (int x = xMin; x <= xMax; x++) { if (e0|e1|e2 > 0) { setPixel(x,y); xflag++; } else if (xflag != 0) break; e0 += a0; e1 += a1; e2 += a2; }

  • Incremental update of edge equation values

(think DDA)

  • Early termination (why does this work?)
  • Faster test of equation values David Luebke 3/8/

Edge Equations:

Interpolating Color

l Given colors (and later, other parameters) at

the vertices, how to interpolate across?

l Idea: triangles are planar in any space:

  • This is the “redness”

parameter space

  • Note:plane follows form

z = Ax + By + C

  • Look familiar?

David Luebke 3/8/

Edge Equations:

Interpolating Color

l Given redness at the 3 vertices, set up the

linear system of equations:

l The solution works out to:

David Luebke 3/8/

Edge Equations:

Interpolating Color

l Notice that the columns in the matrix are

exactly the coefficients of the edge equations!

l So the setup cost per parameter is basically a

matrix multiply

l Per-pixel cost (the inner loop) cost equates to

tracking another edge equation value