38
Introduction to Computer Graphics CS 445 / 645 Lecture 9 Chapter 4: Rasterization L. McMillan MIT 6.837 notes (Fall ’98)

Introduction to Computer Graphics CS 445 / 645 Lecture 9 Chapter 4: Rasterization L. McMillan MIT 6.837 notes (Fall ’98)

Embed Size (px)

Citation preview

Page 1: Introduction to Computer Graphics CS 445 / 645 Lecture 9 Chapter 4: Rasterization L. McMillan MIT 6.837 notes (Fall ’98)

Introduction to Computer Graphics

CS 445 / 645

Lecture 9Chapter 4: Rasterization

L. McMillanMIT 6.837 notes (Fall ’98)

Page 2: Introduction to Computer Graphics CS 445 / 645 Lecture 9 Chapter 4: Rasterization L. McMillan MIT 6.837 notes (Fall ’98)

What made line drawing faster?

Symmetry and Differential AnalyzerSymmetry and Differential AnalyzerSymmetry and Differential AnalyzerSymmetry and Differential Analyzer

Page 3: Introduction to Computer Graphics CS 445 / 645 Lecture 9 Chapter 4: Rasterization L. McMillan MIT 6.837 notes (Fall ’98)

The Rendering Pipeline

Transform

Illuminate

Transform

Clip

Project

Rasterize

Model & CameraModel & CameraParametersParameters Rendering PipelineRendering Pipeline FramebufferFramebuffer DisplayDisplay

ModelWorld

WorldCamera

Page 4: Introduction to Computer Graphics CS 445 / 645 Lecture 9 Chapter 4: Rasterization L. McMillan MIT 6.837 notes (Fall ’98)

Switch to MIT Slides

Course slides from Leonard McMillan’s MIT Course slides from Leonard McMillan’s MIT Course (6.837) from Fall ‘98Course (6.837) from Fall ‘98Course slides from Leonard McMillan’s MIT Course slides from Leonard McMillan’s MIT Course (6.837) from Fall ‘98Course (6.837) from Fall ‘98

Page 5: Introduction to Computer Graphics CS 445 / 645 Lecture 9 Chapter 4: Rasterization L. McMillan MIT 6.837 notes (Fall ’98)

Rasterizing Polygons

In interactive graphics, polygons rule the worldIn interactive graphics, polygons rule the world

Two main reasons:Two main reasons:

• Lowest common denominator for surfacesLowest common denominator for surfaces

– Can represent any surface Can represent any surface with arbitrary accuracywith arbitrary accuracy

– Splines, mathematical functions, volumetric isosurfaces…Splines, mathematical functions, volumetric isosurfaces…

• Mathematical simplicity lends itself to simple, regular rendering Mathematical simplicity lends itself to simple, regular rendering algorithmsalgorithms

– Like those we’re about to discuss… Like those we’re about to discuss…

– Such algorithms embed well in hardwareSuch algorithms embed well in hardware

In interactive graphics, polygons rule the worldIn interactive graphics, polygons rule the world

Two main reasons:Two main reasons:

• Lowest common denominator for surfacesLowest common denominator for surfaces

– Can represent any surface Can represent any surface with arbitrary accuracywith arbitrary accuracy

– Splines, mathematical functions, volumetric isosurfaces…Splines, mathematical functions, volumetric isosurfaces…

• Mathematical simplicity lends itself to simple, regular rendering Mathematical simplicity lends itself to simple, regular rendering algorithmsalgorithms

– Like those we’re about to discuss… Like those we’re about to discuss…

– Such algorithms embed well in hardwareSuch algorithms embed well in hardware

Page 6: Introduction to Computer Graphics CS 445 / 645 Lecture 9 Chapter 4: Rasterization L. McMillan MIT 6.837 notes (Fall ’98)

Rasterizing Polygons

Triangle is the Triangle is the minimal unitminimal unit of a polygonof a polygon

• All polygons can be broken up into All polygons can be broken up into trianglestriangles

• Triangles are guaranteed to be:Triangles are guaranteed to be:

– PlanarPlanar

– ConvexConvex

Triangle is the Triangle is the minimal unitminimal unit of a polygonof a polygon

• All polygons can be broken up into All polygons can be broken up into trianglestriangles

• Triangles are guaranteed to be:Triangles are guaranteed to be:

– PlanarPlanar

– ConvexConvex

Page 7: Introduction to Computer Graphics CS 445 / 645 Lecture 9 Chapter 4: Rasterization L. McMillan MIT 6.837 notes (Fall ’98)

Triangulation

Convex polygons easily Convex polygons easily triangulated (Delaunay)triangulated (Delaunay)

Concave polygons present Concave polygons present a challengea challenge

Convex polygons easily Convex polygons easily triangulated (Delaunay)triangulated (Delaunay)

Concave polygons present Concave polygons present a challengea challenge

Page 8: Introduction to Computer Graphics CS 445 / 645 Lecture 9 Chapter 4: Rasterization L. McMillan MIT 6.837 notes (Fall ’98)

Triangle Rasterization Issues

SliverSliverSliverSliver

Page 9: Introduction to Computer Graphics CS 445 / 645 Lecture 9 Chapter 4: Rasterization L. McMillan MIT 6.837 notes (Fall ’98)

Moving SliversMoving SliversMoving SliversMoving Slivers

Triangle Rasterization Issues

Page 10: Introduction to Computer Graphics CS 445 / 645 Lecture 9 Chapter 4: Rasterization L. McMillan MIT 6.837 notes (Fall ’98)

Triangle Rasterization Issues

Shared Edge OrderingShared Edge OrderingShared Edge OrderingShared Edge Ordering

Page 11: Introduction to Computer Graphics CS 445 / 645 Lecture 9 Chapter 4: Rasterization L. McMillan MIT 6.837 notes (Fall ’98)

Rasterizing Triangles

Interactive graphics hardware commonly uses Interactive graphics hardware commonly uses edge walkingedge walking or or edge equationedge equation techniques for techniques for rasterizing trianglesrasterizing triangles

Interactive graphics hardware commonly uses Interactive graphics hardware commonly uses edge walkingedge walking or or edge equationedge equation techniques for techniques for rasterizing trianglesrasterizing triangles

Page 12: Introduction to Computer Graphics CS 445 / 645 Lecture 9 Chapter 4: Rasterization L. McMillan MIT 6.837 notes (Fall ’98)

Edge Walking

Basic idea: Basic idea:

• Draw edges verticallyDraw edges vertically

– Interpolate colors down edgesInterpolate colors down edges

• Fill in horizontal spans for each Fill in horizontal spans for each scanlinescanline

– At each scanline, interpolate At each scanline, interpolate edge colors across spanedge colors across span

Basic idea: Basic idea:

• Draw edges verticallyDraw edges vertically

– Interpolate colors down edgesInterpolate colors down edges

• Fill in horizontal spans for each Fill in horizontal spans for each scanlinescanline

– At each scanline, interpolate At each scanline, interpolate edge colors across spanedge colors across span

Page 13: Introduction to Computer Graphics CS 445 / 645 Lecture 9 Chapter 4: Rasterization L. McMillan MIT 6.837 notes (Fall ’98)

Edge Walking: Notes

Order three triangle vertices in x and yOrder three triangle vertices in x and y

• Find middle point in y dimension and compute if it is to the left or right Find middle point in y dimension and compute if it is to the left or right of polygon. Also could be flat top or flat bottom triangleof polygon. Also could be flat top or flat bottom triangle

We know where left and right edges are.We know where left and right edges are.

• Proceed from top scanline downwardsProceed from top scanline downwards

• Fill each spanFill each span

• Until breakpoint or bottom vertex is reachedUntil breakpoint or bottom vertex is reached

Advantage: can be made very fastAdvantage: can be made very fast

Disadvantages: Disadvantages:

• Lots of finicky special casesLots of finicky special cases

Order three triangle vertices in x and yOrder three triangle vertices in x and y

• Find middle point in y dimension and compute if it is to the left or right Find middle point in y dimension and compute if it is to the left or right of polygon. Also could be flat top or flat bottom triangleof polygon. Also could be flat top or flat bottom triangle

We know where left and right edges are.We know where left and right edges are.

• Proceed from top scanline downwardsProceed from top scanline downwards

• Fill each spanFill each span

• Until breakpoint or bottom vertex is reachedUntil breakpoint or bottom vertex is reached

Advantage: can be made very fastAdvantage: can be made very fast

Disadvantages: Disadvantages:

• Lots of finicky special casesLots of finicky special cases

Page 14: Introduction to Computer Graphics CS 445 / 645 Lecture 9 Chapter 4: Rasterization L. McMillan MIT 6.837 notes (Fall ’98)

Edge Walking: Disadvantages

Fractional offsets:Fractional offsets:

Be careful when interpolating color values!Be careful when interpolating color values!

Beware of gaps between adjacent edgesBeware of gaps between adjacent edges

Beware of duplicating shared edgesBeware of duplicating shared edges

Fractional offsets:Fractional offsets:

Be careful when interpolating color values!Be careful when interpolating color values!

Beware of gaps between adjacent edgesBeware of gaps between adjacent edges

Beware of duplicating shared edgesBeware of duplicating shared edges

Page 15: Introduction to Computer Graphics CS 445 / 645 Lecture 9 Chapter 4: Rasterization L. McMillan MIT 6.837 notes (Fall ’98)

Triangle Rasterization Issues

Color InterpolationColor InterpolationColor InterpolationColor Interpolation

Page 16: Introduction to Computer Graphics CS 445 / 645 Lecture 9 Chapter 4: Rasterization L. McMillan MIT 6.837 notes (Fall ’98)

Triangle Rasterization Issues

Shared Edge OrderingShared Edge OrderingShared Edge OrderingShared Edge Ordering

Page 17: Introduction to Computer Graphics CS 445 / 645 Lecture 9 Chapter 4: Rasterization L. McMillan MIT 6.837 notes (Fall ’98)

Edge Equations

An edge equation is simply the equation of the line An edge equation is simply the equation of the line defining that edgedefining that edge

• Q: Q: What is the implicit equation of a line?What is the implicit equation of a line?

• A: A: Ax + By + C = 0Ax + By + C = 0

• Q: Q: Given a point (Given a point (xx,,yy), what does plugging ), what does plugging xx & & yy into this equation tell into this equation tell us?us?

• A: Whether the point is:A: Whether the point is:

– On the line: On the line: Ax + By + C = 0 Ax + By + C = 0

– ““Above” the line: Above” the line: Ax + By + C > 0 Ax + By + C > 0

– ““Below” the line: Below” the line: Ax + By + C < 0 Ax + By + C < 0

An edge equation is simply the equation of the line An edge equation is simply the equation of the line defining that edgedefining that edge

• Q: Q: What is the implicit equation of a line?What is the implicit equation of a line?

• A: A: Ax + By + C = 0Ax + By + C = 0

• Q: Q: Given a point (Given a point (xx,,yy), what does plugging ), what does plugging xx & & yy into this equation tell into this equation tell us?us?

• A: Whether the point is:A: Whether the point is:

– On the line: On the line: Ax + By + C = 0 Ax + By + C = 0

– ““Above” the line: Above” the line: Ax + By + C > 0 Ax + By + C > 0

– ““Below” the line: Below” the line: Ax + By + C < 0 Ax + By + C < 0

Page 18: Introduction to Computer Graphics CS 445 / 645 Lecture 9 Chapter 4: Rasterization L. McMillan MIT 6.837 notes (Fall ’98)

Edge Equations

Edge equations thus define two Edge equations thus define two half-spaceshalf-spaces::Edge equations thus define two Edge equations thus define two half-spaceshalf-spaces::

Page 19: Introduction to Computer Graphics CS 445 / 645 Lecture 9 Chapter 4: Rasterization L. McMillan MIT 6.837 notes (Fall ’98)

Edge Equations

And a triangle can be defined as the intersection And a triangle can be defined as the intersection of three positive half-spaces:of three positive half-spaces:

And a triangle can be defined as the intersection And a triangle can be defined as the intersection of three positive half-spaces:of three positive half-spaces:

A1x + B1y + C1 < 0

A2 x + B

2 y + C2 < 0

A 3x

+ B 3

y +

C 3 <

0

A1x + B1y + C1 > 0

A 3x

+ B 3

y +

C 3 >

0 A2 x + B

2 y + C2 > 0

Page 20: Introduction to Computer Graphics CS 445 / 645 Lecture 9 Chapter 4: Rasterization L. McMillan MIT 6.837 notes (Fall ’98)

Edge Equations

So…simply turn on those pixels for which all edge So…simply turn on those pixels for which all edge equations evaluate to > 0:equations evaluate to > 0:

So…simply turn on those pixels for which all edge So…simply turn on those pixels for which all edge equations evaluate to > 0:equations evaluate to > 0:

+++-

--

Page 21: Introduction to Computer Graphics CS 445 / 645 Lecture 9 Chapter 4: Rasterization L. McMillan MIT 6.837 notes (Fall ’98)

Using Edge Equations

Which pixels: compute min,max bounding boxWhich pixels: compute min,max bounding box

Edge equations: compute from verticesEdge equations: compute from vertices

Which pixels: compute min,max bounding boxWhich pixels: compute min,max bounding box

Edge equations: compute from verticesEdge equations: compute from vertices

Page 22: Introduction to Computer Graphics CS 445 / 645 Lecture 9 Chapter 4: Rasterization L. McMillan MIT 6.837 notes (Fall ’98)

Computing Edge Equations

Want to calculate A, B, C for each edge from (Want to calculate A, B, C for each edge from (xx11, , yy11) and ) and

((xx22, , yy22))

Treat it as a linear system:Treat it as a linear system:

AxAx11 + By + By11 + C = 0 + C = 0

AxAx22 + By + By22 + C = 0 + C = 0

Notice: two equations, three unknownsNotice: two equations, three unknowns

What can we solve?What can we solve?

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

Want to calculate A, B, C for each edge from (Want to calculate A, B, C for each edge from (xx11, , yy11) and ) and

((xx22, , yy22))

Treat it as a linear system:Treat it as a linear system:

AxAx11 + By + By11 + C = 0 + C = 0

AxAx22 + By + By22 + C = 0 + C = 0

Notice: two equations, three unknownsNotice: two equations, three unknowns

What can we solve?What can we solve?

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

Page 23: Introduction to Computer Graphics CS 445 / 645 Lecture 9 Chapter 4: Rasterization L. McMillan MIT 6.837 notes (Fall ’98)

Computing Edge Equations

Set up the linear system:Set up the linear system:

Multiply both sidesMultiply both sidesby matrix inverse:by matrix inverse:

Let Let C = xC = x0 0 yy11 -- x x1 1 yy00 for convenience for convenience

• Then Then A = yA = y00 - y - y11 and and B = xB = x00 – x – x11

Set up the linear system:Set up the linear system:

Multiply both sidesMultiply both sidesby matrix inverse:by matrix inverse:

Let Let C = xC = x0 0 yy11 -- x x1 1 yy00 for convenience for convenience

• Then Then A = yA = y00 - y - y11 and and B = xB = x00 – x – x11

1

1

11

00C

B

A

yx

yx

01

01

0110 xx

yy

yxyx

C

B

A

Page 24: Introduction to Computer Graphics CS 445 / 645 Lecture 9 Chapter 4: Rasterization L. McMillan MIT 6.837 notes (Fall ’98)

Edge Equations

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

Given three corners Given three corners PP00, P, P11, P, P22 of a triangle, what are our of a triangle, what are our

three edges?three edges?

How do we make sure the half-spaces defined by the How do we make sure the half-spaces defined by the edge equations all share the same sign on the edge equations all share the same sign on the interior of the triangle?interior of the triangle?

A: Be consistent (Ex: A: Be consistent (Ex: [P[P0 0 PP11], [P], [P1 1 PP22], [P], [P2 2 PP00]]))

How do we make sure that sign is positive?How do we make sure that sign is positive?

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

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

Given three corners Given three corners PP00, P, P11, P, P22 of a triangle, what are our of a triangle, what are our

three edges?three edges?

How do we make sure the half-spaces defined by the How do we make sure the half-spaces defined by the edge equations all share the same sign on the edge equations all share the same sign on the interior of the triangle?interior of the triangle?

A: Be consistent (Ex: A: Be consistent (Ex: [P[P0 0 PP11], [P], [P1 1 PP22], [P], [P2 2 PP00]]))

How do we make sure that sign is positive?How do we make sure that sign is positive?

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

Page 25: Introduction to Computer Graphics CS 445 / 645 Lecture 9 Chapter 4: Rasterization L. McMillan MIT 6.837 notes (Fall ’98)

Edge Equations: Code

Basic structure of code:Basic structure of code:

• Setup: compute edge equations, bounding boxSetup: compute edge equations, bounding box

• (Outer loop) For each scanline in bounding box... (Outer loop) For each scanline in bounding box...

• (Inner loop) …check each pixel on scanline, (Inner loop) …check each pixel on scanline, evaluating edge equations and drawing the pixel if all evaluating edge equations and drawing the pixel if all three are positivethree are positive

Basic structure of code:Basic structure of code:

• Setup: compute edge equations, bounding boxSetup: compute edge equations, bounding box

• (Outer loop) For each scanline in bounding box... (Outer loop) For each scanline in bounding box...

• (Inner loop) …check each pixel on scanline, (Inner loop) …check each pixel on scanline, evaluating edge equations and drawing the pixel if all evaluating edge equations and drawing the pixel if all three are positivethree are positive

Page 26: Introduction to Computer Graphics CS 445 / 645 Lecture 9 Chapter 4: Rasterization L. McMillan MIT 6.837 notes (Fall ’98)

Optimize This!findBoundingBox(&xmin, &xmax, &ymin, &ymax);findBoundingBox(&xmin, &xmax, &ymin, &ymax);

setupEdges (&a0,&b0,&c0,&a1,&b1,&c1,&a2,&b2,&c2);setupEdges (&a0,&b0,&c0,&a1,&b1,&c1,&a2,&b2,&c2);

/* Optimize this: *//* Optimize this: */

for (int y = yMin; y <= yMax; y++) {for (int y = yMin; y <= yMax; y++) {

for (int x = xMin; x <= xMax; x++) {for (int x = xMin; x <= xMax; x++) {

float e0 = a0*x + b0*y + c0;float e0 = a0*x + b0*y + c0;

float e1 = a1*x + b1*y + c1;float e1 = a1*x + b1*y + c1;

float e2 = a2*x + b2*y + c2;float e2 = a2*x + b2*y + c2;

if (e0 > 0 && e1 > 0 && e2 > 0)if (e0 > 0 && e1 > 0 && e2 > 0)setPixel(x,y);setPixel(x,y);

}}}}

findBoundingBox(&xmin, &xmax, &ymin, &ymax);findBoundingBox(&xmin, &xmax, &ymin, &ymax);

setupEdges (&a0,&b0,&c0,&a1,&b1,&c1,&a2,&b2,&c2);setupEdges (&a0,&b0,&c0,&a1,&b1,&c1,&a2,&b2,&c2);

/* Optimize this: *//* Optimize this: */

for (int y = yMin; y <= yMax; y++) {for (int y = yMin; y <= yMax; y++) {

for (int x = xMin; x <= xMax; x++) {for (int x = xMin; x <= xMax; x++) {

float e0 = a0*x + b0*y + c0;float e0 = a0*x + b0*y + c0;

float e1 = a1*x + b1*y + c1;float e1 = a1*x + b1*y + c1;

float e2 = a2*x + b2*y + c2;float e2 = a2*x + b2*y + c2;

if (e0 > 0 && e1 > 0 && e2 > 0)if (e0 > 0 && e1 > 0 && e2 > 0)setPixel(x,y);setPixel(x,y);

}}}}

Page 27: Introduction to Computer Graphics CS 445 / 645 Lecture 9 Chapter 4: Rasterization L. McMillan MIT 6.837 notes (Fall ’98)

Edge Equations: Speed HacksSome speed hacks for the inner loop:Some speed hacks for the inner loop:

int xflag = 0;int xflag = 0;

for (int x = xMin; x <= xMax; x++) {for (int x = xMin; x <= xMax; x++) {

if (e0|e1|e2 > 0) {if (e0|e1|e2 > 0) {

setPixel(x,y);setPixel(x,y);

xflag++;xflag++;

} else if (xflag != 0) break;} else if (xflag != 0) break;

e0 += a0; e1 += a1; e2 += a2;e0 += a0; e1 += a1; e2 += a2;

}}

• Incremental update of edge equation valuesIncremental update of edge equation values (think DDA)(think DDA)

• Early termination (Early termination (why does this work?why does this work?))

• Faster test of equation valuesFaster test of equation values

Some speed hacks for the inner loop:Some speed hacks for the inner loop:

int xflag = 0;int xflag = 0;

for (int x = xMin; x <= xMax; x++) {for (int x = xMin; x <= xMax; x++) {

if (e0|e1|e2 > 0) {if (e0|e1|e2 > 0) {

setPixel(x,y);setPixel(x,y);

xflag++;xflag++;

} else if (xflag != 0) break;} else if (xflag != 0) break;

e0 += a0; e1 += a1; e2 += a2;e0 += a0; e1 += a1; e2 += a2;

}}

• Incremental update of edge equation valuesIncremental update of edge equation values (think DDA)(think DDA)

• Early termination (Early termination (why does this work?why does this work?))

• Faster test of equation valuesFaster test of equation values

Page 28: Introduction to Computer Graphics CS 445 / 645 Lecture 9 Chapter 4: Rasterization L. McMillan MIT 6.837 notes (Fall ’98)

Triangle Rasterization Issues

Exactly which pixels should be lit?Exactly which pixels should be lit?

A: Those pixels inside the triangle edgesA: Those pixels inside the triangle edges

What about pixels exactly on the edge?What about pixels exactly on the edge?

• Draw them: order of triangles matters (it shouldn’t)Draw them: order of triangles matters (it shouldn’t)

• Don’t draw them: gaps possible between trianglesDon’t draw them: gaps possible between triangles

We need a consistent (if arbitrary) rule We need a consistent (if arbitrary) rule

• Example: draw pixels on left or top edge, but not on right or Example: draw pixels on left or top edge, but not on right or bottom edgebottom edge

Exactly which pixels should be lit?Exactly which pixels should be lit?

A: Those pixels inside the triangle edgesA: Those pixels inside the triangle edges

What about pixels exactly on the edge?What about pixels exactly on the edge?

• Draw them: order of triangles matters (it shouldn’t)Draw them: order of triangles matters (it shouldn’t)

• Don’t draw them: gaps possible between trianglesDon’t draw them: gaps possible between triangles

We need a consistent (if arbitrary) rule We need a consistent (if arbitrary) rule

• Example: draw pixels on left or top edge, but not on right or Example: draw pixels on left or top edge, but not on right or bottom edgebottom edge

Page 29: Introduction to Computer Graphics CS 445 / 645 Lecture 9 Chapter 4: Rasterization L. McMillan MIT 6.837 notes (Fall ’98)

General Polygon Rasterization

Now that we can rasterize triangles, what about Now that we can rasterize triangles, what about general polygons?general polygons?

We’ll take an edge-walking approachWe’ll take an edge-walking approach

Now that we can rasterize triangles, what about Now that we can rasterize triangles, what about general polygons?general polygons?

We’ll take an edge-walking approachWe’ll take an edge-walking approach

Page 30: Introduction to Computer Graphics CS 445 / 645 Lecture 9 Chapter 4: Rasterization L. McMillan MIT 6.837 notes (Fall ’98)

General Polygon Rasterization

Consider the following polygon:Consider the following polygon:

How do we know whether a given pixel on the How do we know whether a given pixel on the scanline is inside or outside the polygon?scanline is inside or outside the polygon?

Consider the following polygon:Consider the following polygon:

How do we know whether a given pixel on the How do we know whether a given pixel on the scanline is inside or outside the polygon?scanline is inside or outside the polygon?

A

B

C

D

E

F

Page 31: Introduction to Computer Graphics CS 445 / 645 Lecture 9 Chapter 4: Rasterization L. McMillan MIT 6.837 notes (Fall ’98)

Polygon Rasterization

Inside-Outside PointsInside-Outside PointsInside-Outside PointsInside-Outside Points

Page 32: Introduction to Computer Graphics CS 445 / 645 Lecture 9 Chapter 4: Rasterization L. McMillan MIT 6.837 notes (Fall ’98)

Polygon Rasterization

Inside-Outside PointsInside-Outside PointsInside-Outside PointsInside-Outside Points

Page 33: Introduction to Computer Graphics CS 445 / 645 Lecture 9 Chapter 4: Rasterization L. McMillan MIT 6.837 notes (Fall ’98)

General Polygon Rasterization

Basic idea: use a Basic idea: use a parity testparity test

for each scanlinefor each scanline

edgeCnt = 0;edgeCnt = 0;

for each pixel on scanline (l to r)for each pixel on scanline (l to r)

if (oldpixel->newpixel crosses edge) if (oldpixel->newpixel crosses edge)

edgeCnt ++;edgeCnt ++;

// draw the pixel if edgeCnt odd// draw the pixel if edgeCnt odd

if (edgeCnt % 2)if (edgeCnt % 2)

setPixel(pixel);setPixel(pixel);

Basic idea: use a Basic idea: use a parity testparity test

for each scanlinefor each scanline

edgeCnt = 0;edgeCnt = 0;

for each pixel on scanline (l to r)for each pixel on scanline (l to r)

if (oldpixel->newpixel crosses edge) if (oldpixel->newpixel crosses edge)

edgeCnt ++;edgeCnt ++;

// draw the pixel if edgeCnt odd// draw the pixel if edgeCnt odd

if (edgeCnt % 2)if (edgeCnt % 2)

setPixel(pixel);setPixel(pixel);

Page 34: Introduction to Computer Graphics CS 445 / 645 Lecture 9 Chapter 4: Rasterization L. McMillan MIT 6.837 notes (Fall ’98)

General Polygon Rasterization

Count your vertices carefullyCount your vertices carefully

• If exactly on pixel boundary?If exactly on pixel boundary?

• Shared vertices?Shared vertices?

• Vertices defining horizontalVertices defining horizontaledge?edge?

– Consider A-B versus I-HConsider A-B versus I-H

Count your vertices carefullyCount your vertices carefully

• If exactly on pixel boundary?If exactly on pixel boundary?

• Shared vertices?Shared vertices?

• Vertices defining horizontalVertices defining horizontaledge?edge?

– Consider A-B versus I-HConsider A-B versus I-H

B

CD

E

FG

I H

J

A

Page 35: Introduction to Computer Graphics CS 445 / 645 Lecture 9 Chapter 4: Rasterization L. McMillan MIT 6.837 notes (Fall ’98)

Faster Polygon RasterizationHow can we optimize the code?How can we optimize the code?

for each scanlinefor each scanline

edgeCnt = 0;edgeCnt = 0;

for each pixel on scanline (l to r)for each pixel on scanline (l to r)

if (oldpixel->newpixel crosses edge) if (oldpixel->newpixel crosses edge)

edgeCnt ++;edgeCnt ++;

// draw the pixel if edgeCnt odd// draw the pixel if edgeCnt odd

if (edgeCnt % 2)if (edgeCnt % 2)

setPixel(pixel);setPixel(pixel);

Big cost: testing pixels against each edgeBig cost: testing pixels against each edge

Solution: Solution: active edge table (AET)active edge table (AET)

How can we optimize the code?How can we optimize the code?for each scanlinefor each scanline

edgeCnt = 0;edgeCnt = 0;

for each pixel on scanline (l to r)for each pixel on scanline (l to r)

if (oldpixel->newpixel crosses edge) if (oldpixel->newpixel crosses edge)

edgeCnt ++;edgeCnt ++;

// draw the pixel if edgeCnt odd// draw the pixel if edgeCnt odd

if (edgeCnt % 2)if (edgeCnt % 2)

setPixel(pixel);setPixel(pixel);

Big cost: testing pixels against each edgeBig cost: testing pixels against each edge

Solution: Solution: active edge table (AET)active edge table (AET)

Page 36: Introduction to Computer Graphics CS 445 / 645 Lecture 9 Chapter 4: Rasterization L. McMillan MIT 6.837 notes (Fall ’98)

Active Edge Table

Idea: Idea:

• Edges intersecting a given scanline are likely to intersect Edges intersecting a given scanline are likely to intersect the next scanlinethe next scanline

• The order of edge intersections doesn’t change much from The order of edge intersections doesn’t change much from scanline to scanlinescanline to scanline

Idea: Idea:

• Edges intersecting a given scanline are likely to intersect Edges intersecting a given scanline are likely to intersect the next scanlinethe next scanline

• The order of edge intersections doesn’t change much from The order of edge intersections doesn’t change much from scanline to scanlinescanline to scanline

Page 37: Introduction to Computer Graphics CS 445 / 645 Lecture 9 Chapter 4: Rasterization L. McMillan MIT 6.837 notes (Fall ’98)

Active Edge TableAlgorithm: Algorithm: scanline from bottom to top…scanline from bottom to top…

• Sort all edges by their minimum y coordSort all edges by their minimum y coord

• Starting at bottom, add edges with YStarting at bottom, add edges with Yminmin= 0 to AET= 0 to AET

• For each scanline:For each scanline:

– Sort edges in AET by x intersectionSort edges in AET by x intersection

– Walk from left to right, setting pixels by parity ruleWalk from left to right, setting pixels by parity rule

– Increment scanlineIncrement scanline

– Retire edges with YRetire edges with Ymaxmax < Y < Y

– Add edges with YAdd edges with Yminmin < Y < Y

– Recalculate edge intersections (Recalculate edge intersections (how?how?))

• Stop when Y > YStop when Y > Ymaxmax for last edges for last edges

Algorithm: Algorithm: scanline from bottom to top…scanline from bottom to top…

• Sort all edges by their minimum y coordSort all edges by their minimum y coord

• Starting at bottom, add edges with YStarting at bottom, add edges with Yminmin= 0 to AET= 0 to AET

• For each scanline:For each scanline:

– Sort edges in AET by x intersectionSort edges in AET by x intersection

– Walk from left to right, setting pixels by parity ruleWalk from left to right, setting pixels by parity rule

– Increment scanlineIncrement scanline

– Retire edges with YRetire edges with Ymaxmax < Y < Y

– Add edges with YAdd edges with Yminmin < Y < Y

– Recalculate edge intersections (Recalculate edge intersections (how?how?))

• Stop when Y > YStop when Y > Ymaxmax for last edges for last edges

Page 38: Introduction to Computer Graphics CS 445 / 645 Lecture 9 Chapter 4: Rasterization L. McMillan MIT 6.837 notes (Fall ’98)

Review: Polygon Rasterization

A B

D C

F

EH G

For scanline, determine all polygon edges that intersect current scanlineSort edge intersections with scanline in least to greatest orderUse parity count to determine when pixels are drawnHorizontal lines do not contribute to parity countYmin endpoints do contribute to parity countYmax endpoints do not contribute to parity count

Bottom edge drawn because A is min of AH. AB does not contribute

Not drawn because H is max of AHAnd HG does not contribute

Not drawn because D is min of EDAnd increments counter to 2.

DC doesn’t contribute