Docsity

Scan Converting Triangles, Lecture Notes - Animation, Study notes of Computer Graphics and Animation

Toggle sidebar
bg1
6.837 LECTURE 6
1. Scan Converting Triangles 2. Triangles are Minimal:
3. Triangles are Always Convex Polygons 4. Triangles Can Approximate Arbitrary Shapes
5. Any Polygon can be Decomposed into Triangles 6. Rasterizing Triangles
7. Edge-Walking Triangle Rasterizer 8. Fractional Offsets
9. Rasterizing Triangles using Edge Equations 10. Notes on using Edge Equations
11. Example Implementation 12. Edge Equation Coefficients
13. Numerical Precision of the C Coefficient 13a. Loss of Precision
14. EdgeEquation Object 15. Triangle Object
16. The Draw Method 17. Explain Two Speed Hacks
18. Triangle SetUp Method 19. Tricks Explained
20. Why Positive Area Implies a Positive Interior 21. Exercise #2
22. Triangle Rasterizer Demonstration 23. Interpolating Parameters Within a Triangle
24. This equation should appear familiar. 25. Smooth Triangle Object
26. Computing Plane Equations 27. Modified Draw( ) Method
28. More SmoothTri.Draw( ) 29. Even More SmoothTri.Draw( )
30. Smooth Triangle Results 31. A Post-Triangle World
32. Point-Cloud Rendering 33. Next Time
Lecture 6 Outline 6.837 Fall '01
Lecture 6 --- 6.837 Fall '01
http://graphics/classes/6.837/F01/Lecture06/index.html [9/27/2001 5:09:01 PM]
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23

Partial preview of the text

Download Scan Converting Triangles, Lecture Notes - Animation and more Study notes Computer Graphics and Animation in PDF only on Docsity!

6.837 LECTURE 6

  1. Scan Converting Triangles^ 2. Triangles are Minimal:
  2. Triangles are Always^ Convex^ Polygons^ 4. Triangles Can Approximate Arbitrary Shapes
  3. Any Polygon can be Decomposed into Triangles^ 6. Rasterizing Triangles
  4. Edge-Walking Triangle Rasterizer^ 8. Fractional Offsets
  5. Rasterizing Triangles using Edge Equations 10. Notes on using Edge Equations
  6. Example Implementation^ 12. Edge Equation Coefficients
  7. Numerical Precision of the^ C^ Coefficient^ 13a. Loss of Precision
  8. EdgeEquation^ Object^ 15. Triangle Object
  9. The Draw Method^ 17. Explain Two Speed Hacks
  10. Triangle SetUp Method^ 19. Tricks Explained
  11. Why Positive Area Implies a Positive Interior^ 21. Exercise #
  12. Triangle Rasterizer Demonstration 23. Interpolating Parameters Within a Triangle
  13. This equation should appear familiar.^ 25. Smooth Triangle Object
  14. Computing Plane Equations 27. Modified Draw( ) Method
  15. More SmoothTri.Draw( )^ 29. Even More SmoothTri.Draw( )
  16. Smooth Triangle Results^ 31. A Post-Triangle World
  17. Point-Cloud Rendering^ 33. Next Time

Lecture 6 (^) Outline 6.837 Fall '

http://graphics/classes/6.837/F01/Lecture06/index.html [9/27/2001 5:09:01 PM]

Scan Converting Triangles

l Why triangles?

l Rasterizing triangles

l Interpolating parameters

l Post-triangle rendering

Lecture 6 (^) Slide 1 6.837 Fall '

Triangles are Always Convex Polygons

What does it mean to be a convex?

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 boundries.

Why is being convex important?

Because no matter how a triangle is oriented on the screen a given scan line will contain

only a single segment or span of that triangle.

Lecture 6 (^) Slide 3 6.837 Fall '

Triangles Can Approximate Arbitrary Shapes

Any 2-dimensional shape (or 3D surface) can be approximated by a

polygon using a locally linear (planar) approximation. To improve

the quality of fit we need only increase the number edges.

Lecture 6 (^) Slide 4 6.837 Fall '

Rasterizing Triangles

The two most common strategies for scan-converting a triangle are

edge walking and edge equations

There are, however, other techniques including:

l Recursive subdivision of primitive (micropolygons)

l Recursive subdivision of screen (Warnock's algorithm)

Lecture 6 (^) Slide 6 6.837 Fall '

Edge-Walking Triangle Rasterizer

Notes on edge walking:

l Sort the vertices in both x and y

Determine if the middle vertex, or breakpoint lies on the left or

right side of the polygon. If the triangle has an edge parallel to

the scanline direction then there is no breakpoint.

l

Determines the left and right extents for each scanline

(called spans ).

l

Walk down the left and right edges filling the pixels in-between

until either a breakpoint or the bottom vertex is reached.

l

Advantages and Disadvantages:

l Generally very fast

l Loaded with special cases (left and right breakpoints, no breakpoints)

l Difficult to get right

l Requires computing fractional offsets when interpolating parameters across the triangle

The algorithm described in the book is an edge-walking algorithm.

Lecture 6 (^) Slide 7 6.837 Fall '

Rasterizing Triangles using Edge Equations

An edge equation is simply a discriminating function like those we used in our curve and

line-drawing algorithms.

An edge equation segments a planar region

into three parts, a boundary, and two

half-spaces. The boundary is identified by

points where the edge equation is equal to

zero. The half-spaces are distiguished by

differences in the edge equation's sign. We

can choose which half-space gives a positive

sign by multiplication by -1.

We can scale all three edges so that their negative halfspaces are on

the triangle's exterior.

Lecture 6 (^) Slide 9 6.837 Fall '

Notes on using Edge Equations

l Compute edge equations from vertices

l Orient edge equations

l Compute a bounding box

l Scan through pixels in bounding box evaluating the edge equations

l When all three are positive then draw the pixel.

Lecture 6 (^) Slide 10 6.837 Fall '

Edge Equation Coefficients

The edge equation coefficients are computed using the coordinates of the two vertices. Each

point determines an equation in terms of our three unknowns, A , B , and C.

We can solve for A and B in terms of C by setting up the following linear system.

Multiplying both sides by the matrix inverse.

If we choose , then we get and.

Why could we just choose C?

Lecture 6 (^) Slide 12 6.837 Fall '

Numerical Precision of the C Coefficient

Computers represent floating-point number internally in a format similar to scientific

notation. The very worse thing that you can do with numbers represented in scientific

notation is subtract numbers of similar magnitude. We loose most of the significant digits in

our result (see next slide).

In the case of triangles, these sort of precision problems to occur frequently, because in

general the vertices of a triangle are close to each other.

and thus

Thankfully, we can avoid this subtraction of large numbers when computing an expression

for C. Given that we know A and B we can solve for C as follows:

or

To eliminate any bias toward either vertex we will average of these C values

Lecture 6 (^) Slide 13 6.837 Fall '

EdgeEquation Object

class EdgeEqn {

public final static int FRACBITS = 12; public int A, B, C; public int flag; // used to compute bounding box

public EdgeEqn(Vertex2D v0, Vertex2D v1) { double a = v0.y - v1.y; double b = v1.x - v0.x; double c = -0.5f(a(v0.x + v1.x) + b*(v0.y + v1.y)); A = (int) (a * (1<<FRACBITS)); B = (int) (b * (1<<FRACBITS)); C = (int) (c * (1<<FRACBITS)); flag = 0; if (A >= 0) flag += 8; if (B >= 0) flag += 1; }

public void flip() { A = -A; B = -B; C = -C; }

public int evaluate(int x, int y) { return (Ax + By + C); }

}

Lecture 6 (^) Slide 14 6.837 Fall '

Triangle Object

public class FlatTri implements Drawable { protected Vertex2D v[]; protected EdgeEqn edge[]; protected int color; protected int area; protected int xMin, xMax, yMin, yMax; private static byte sort[][] = {{0, 1}, {1, 2}, {0, 2}, {2, 0}, {2, 1}, {1, 0}};

public FlatTri() { // for future extension }

public FlatTri(Vertex2D v0, Vertex2D v1, Vertex2D v2) { v = new Vertex2D[3]; v[0] = v0; v[1] = v1; v[2] = v2;

// ... Paint triangle the average of it's vertex colors ... int a = ((v0.argb >> 24) & 255) + ((v1.argb >> 24) & 255) + ((v2.argb >> 24) & 255); int r = ((v0.argb >> 16) & 255) + ((v1.argb >> 16) & 255) + ((v2.argb >> 16) & 255); int g = ((v0.argb >> 8) & 255) + ((v1.argb >> 8) & 255) + ((v2.argb >> 8) & 255); int b = (v0.argb & 255) + (v1.argb & 255) + (v2.argb & 255);

a = (a + a + 3) / 6; r = (r + r + 3) / 6; g = (g + g + 3) / 6; b = (b + b + 3) / 6;

color = (a << 24) | (r << 16) | (g << 8) | b; }

Lecture 6 (^) Slide 15 6.837 Fall '

Explain Two Speed Hacks

Most everything in our Draw( ) method is straightforward, with two exceptions.

int xflag = 0;

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

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

r.pixel[y+x] = color;

xflag++;

} else if (xflag != 0) break;

e0 += A0;

e1 += A1;

e2 += A2;

All three edges are tested with a single comparison by oring together the three edges and

checking if the result is positive. If any one of the three is negative then its sign-bit will

be set to a 1, and the result of the or will be negative.

l

Since triangles are convex, we can only be inside for a single interval on any given

scanline. The xflag variable is used to keep track of when we exit the triangle's interior.

If ever we find ourselves outside of the triangle having already set some pixels on the

span then we can skip over the remainder of the scanline.

l

Lecture 6 (^) Slide 17 6.837 Fall '

Triangle SetUp Method

protected boolean triangleSetup(int width, int height) { if (edge == null) edge = new EdgeEqn[3]; // Compute the three edge equations edge[0] = new EdgeEqn(v[0], v[1]); edge[1] = new EdgeEqn(v[1], v[2]); edge[2] = new EdgeEqn(v[2], v[0]);

// Trick #1: Orient edges so that the triangle's interior lies within all of their // positive half-spaces. Assuring that the area is positive accomplishes this area = edge[0].C + edge[1].C + edge[2].C; if (area == 0) { return false; // degenerate triangle } if (area < 0) { edge[0].flip(); edge[1].flip(); edge[2].flip(); area = -area; }

// Trick #2: compute bounding box int xflag = edge[0].flag + 2edge[1].flag + 4edge[2].flag; int yflag = (xflag >> 3) - 1; xflag = (xflag & 7) - 1;

xMin = (int) (v[sort[xflag][0]].x); xMax = (int) (v[sort[xflag][1]].x + 1); yMin = (int) (v[sort[yflag][1]].y); yMax = (int) (v[sort[yflag][0]].y + 1);

// clip triangle's bounding box to raster xMin = (xMin < 0)? 0 : xMin; xMax = (xMax >= width)? width - 1 : xMax; yMin = (yMin < 0)? 0 : yMin; yMax = (yMax >= height)? height - 1 : yMax; return true; }

Lecture 6 Slide 18 6.837 Fall '