rasterizing triangles cs4620/5620: lecture 16 · cornell cs4620/5620 fall 2011 •!lecture 16...

4
© 2011 Kavita Bala • (with previous instructors James/Marschner) Cornell CS4620/5620 Fall 2011 • Lecture 16 1 CS4620/5620: Lecture 16 Rasterization © 2011 Kavita Bala (with previous instructors James/Marschner) Cornell CS4620/5620 Fall 2011 • Lecture 16 Rasterizing triangles • The most common case in most applications – with good antialiasing can be the only case – some systems render a line as two skinny triangles • Triangle represented by three vertices • Simple way to think of algorithm follows the pixel-walk interpretation of line rasterization –walk from pixel to pixel over (at least) the polygon’s area – evaluate linear functions as you go – use those functions to decide which pixels are inside 2 © 2011 Kavita Bala (with previous instructors James/Marschner) Cornell CS4620/5620 Fall 2011 • Lecture 16 Rasterizing triangles • Input: –three 2D points (the triangle’s vertices in pixel space) (x 0 , y 0 ); (x 1 , y 1 ); (x 2 , y 2 ) – parameter values at each vertex q 00 , …, q 0n ; q 10 , …, q 1n ; q 20 , …, q 2n • Output: a list of fragments, each with – the integer pixel coordinates (x, y) – interpolated parameter values q 0 , …, q n 3 © 2011 Kavita Bala (with previous instructors James/Marschner) Cornell CS4620/5620 Fall 2011 • Lecture 16 Rasterizing triangles • Summary 1 evaluation of linear functions on pixel grid 2 functions defined by parameter values at vertices 3 using extra parameters to determine fragment set 4 © 2011 Kavita Bala (with previous instructors James/Marschner) Cornell CS4620/5620 Fall 2011 • Lecture 16 Incremental linear evaluation • A linear (affine, really) function on the plane is: • Linear functions are efficient to evaluate on a grid: 5 © 2011 Kavita Bala (with previous instructors James/Marschner) Cornell CS4620/5620 Fall 2011 • Lecture 16 Incremental linear evaluation linEval(xl, xh, yl, yh, cx, cy, ck) { // setup qRow = cx*xl + cy*yl + ck; // traversal for y = yl to yh { qPix = qRow; for x = xl to xh { output(x, y, qPix); qPix += cx; } qRow += cy; } } c x = .005; c y = .005; c k = 0 (image size 100x100) 6

Upload: others

Post on 11-Jul-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Rasterizing triangles CS4620/5620: Lecture 16 · Cornell CS4620/5620 Fall 2011 •!Lecture 16 Walking edge equations •We need to update values of the three edge equations with single-pixel

© 2011 Kavita Bala •(with previous instructors James/Marschner)

Cornell CS4620/5620 Fall 2011 •!Lecture 16 1

CS4620/5620: Lecture 16

Rasterization

© 2011 Kavita Bala •(with previous instructors James/Marschner)

Cornell CS4620/5620 Fall 2011 •!Lecture 16

Rasterizing triangles

• The most common case in most applications– with good antialiasing can be the only case– some systems render a line as two skinny triangles

• Triangle represented by three vertices• Simple way to think of algorithm follows the pixel-walk

interpretation of line rasterization– walk from pixel to pixel over (at least) the polygon’s area– evaluate linear functions as you go– use those functions to decide which pixels are inside

2

© 2011 Kavita Bala •(with previous instructors James/Marschner)

Cornell CS4620/5620 Fall 2011 •!Lecture 16

Rasterizing triangles

• Input:– three 2D points (the triangle’s vertices in pixel space)

• (x0, y0); (x1, y1); (x2, y2)– parameter values at each vertex

• q00, …, q0n; q10, …, q1n; q20, …, q2n

• Output: a list of fragments, each with– the integer pixel coordinates (x, y)

– interpolated parameter values q0, …, qn

3 © 2011 Kavita Bala •(with previous instructors James/Marschner)

Cornell CS4620/5620 Fall 2011 •!Lecture 16

Rasterizing triangles

• Summary1" evaluation of linear

functions on pixelgrid

2" functions defined byparameter values at vertices

3" using extraparametersto determinefragment set

4

© 2011 Kavita Bala •(with previous instructors James/Marschner)

Cornell CS4620/5620 Fall 2011 •!Lecture 16

Incremental linear evaluation

• A linear (affine, really) function on the plane is:

• Linear functions are efficient to evaluate on a grid:

5 © 2011 Kavita Bala •(with previous instructors James/Marschner)

Cornell CS4620/5620 Fall 2011 •!Lecture 16

Incremental linear evaluation

linEval(xl, xh, yl, yh, cx, cy, ck) {

// setup qRow = cx*xl + cy*yl + ck;

// traversal for y = yl to yh { qPix = qRow; for x = xl to xh { output(x, y, qPix); qPix += cx; } qRow += cy; }}

cx = .005; cy = .005; ck = 0(image size 100x100)

6

Page 2: Rasterizing triangles CS4620/5620: Lecture 16 · Cornell CS4620/5620 Fall 2011 •!Lecture 16 Walking edge equations •We need to update values of the three edge equations with single-pixel

© 2011 Kavita Bala •(with previous instructors James/Marschner)

Cornell CS4620/5620 Fall 2011 •!Lecture 16

Defining parameter functions

• To interpolate parameters across a triangle we need to

find the cx, cy, and ck that define the (unique) linear function that matches the given values at all 3 vertices– this is 3 constraints on 3 unknown coefficients:

– leading to a 3x3 matrix equation for the coefficients:

(singular iff triangleis degenerate)

(each states that the functionagrees with the given valueat one vertex)

7 © 2011 Kavita Bala •(with previous instructors James/Marschner)

Cornell CS4620/5620 Fall 2011 •!Lecture 16

Defining parameter functions

• More efficient version: shift origin to (x0, y0)

– now this is a 2x2 linear system (since q0 falls out):

– solve using Cramer’s rule (see Shirley):

8

q(x, y) = cx(x− x0) + cy(y − y0) + q0

q(x1, y1) = cx(x1 − x0) + cy(y1 − y0) + q0 = q1

q(x2, y2) = cx(x2 − x0) + cy(y2 − y0) + q0 = q2

© 2011 Kavita Bala •(with previous instructors James/Marschner)

Cornell CS4620/5620 Fall 2011 •!Lecture 16

Defining parameter functions

linInterp(xl, xh, yl, yh, x0, y0, q0,x1, y1, q1, x2, y2, q2) {

// setup det = (x1-x0)*(y2-y0) - (x2-x0)*(y1-y0); cx = ((q1-q0)*(y2-y0) - (q2-q0)*(y1-y0)) / det; cy = ((q2-q0)*(x1-x0) - (q1-q0)*(x2-x0)) / det; qRow = cx*(xl-x0) + cy*(yl-y0) + q0;

// traversal (same as before) for y = yl to yh { qPix = qRow; for x = xl to xh { output(x, y, qPix); qPix += cx; } qRow += cy; }}

q = 0 q = 1

q = 0

9 © 2011 Kavita Bala •(with previous instructors James/Marschner)

Cornell CS4620/5620 Fall 2011 •!Lecture 16

Interpolating several parameters

linInterp(xl, xh, yl, yh, n, x0, y0, q0[],x1, y1, q1[], x2, y2, q2[]) {

// setup for k = 0 to n-1 // compute cx[k], cy[k], qRow[k] // from q0[k], q1[k], q2[k]

// traversal for y = yl to yh { for k = 1 to n, qPix[k] = qRow[k]; for x = xl to xh { output(x, y, qPix); for k = 1 to n, qPix[k] += cx[k]; } for k = 1 to n, qRow[k] += cy[k]; }}

10

© 2011 Kavita Bala •(with previous instructors James/Marschner)

Cornell CS4620/5620 Fall 2011 •!Lecture 16

Rasterizing triangles

• Summary1" evaluation of linear

functions on pixelgrid

2" functions defined byparameter values at vertices

3" using extraparametersto determinefragment set

11 © 2011 Kavita Bala •(with previous instructors James/Marschner)

Cornell CS4620/5620 Fall 2011 •!Lecture 16

Clipping to the triangle

• Interpolate three barycentriccoordinates across the plane– each barycentric coord is

1 at one vert. and 0 atthe other two

• Output fragments onlywhen all three are > 0.

12

Page 3: Rasterizing triangles CS4620/5620: Lecture 16 · Cornell CS4620/5620 Fall 2011 •!Lecture 16 Walking edge equations •We need to update values of the three edge equations with single-pixel

© 2011 Kavita Bala •(with previous instructors James/Marschner)

Cornell CS4620/5620 Fall 2011 •!Lecture 16

Barycentric coordinates

• Basis: a coordinate system for trinagles

– in this view, the triangle interior test is just

[Shi

rley

2000

]

13 © 2011 Kavita Bala •(with previous instructors James/Marschner)

Cornell CS4620/5620 Fall 2011 •!Lecture 16

Barycentric coordinates

• Geometric viewpoint– algebraic viewpoint:

– geometric viewpoint (areas):

• Triangle interior test:

[Shi

rley

2000

]

14

© 2011 Kavita Bala •(with previous instructors James/Marschner)

Cornell CS4620/5620 Fall 2011 •!Lecture 16

Barycentric coordinates

• A coordinate system for triangles– geometric viewpoint: distances

– linear viewpoint: basis of edges

15 © 2011 Kavita Bala •(with previous instructors James/Marschner)

Cornell CS4620/5620 Fall 2011 •!Lecture 16

Edge equations

• In plane, triangle is the intersection of 3 half spaces

16

© 2011 Kavita Bala •(with previous instructors James/Marschner)

Cornell CS4620/5620 Fall 2011 •!Lecture 16

Walking edge equations

• We need to update values of the three edge equations with single-pixel steps in x and y

• Edge equation already in form of dot product• components of vector are the increments

17 © 2011 Kavita Bala •(with previous instructors James/Marschner)

Cornell CS4620/5620 Fall 2011 •!Lecture 16

Pixel-walk (Pineda) rasterization

• Conservativelyvisit a superset ofthe pixels you want

• Interpolate linearfunctions

• Use those functionsto determine whento emit a fragment

18

Page 4: Rasterizing triangles CS4620/5620: Lecture 16 · Cornell CS4620/5620 Fall 2011 •!Lecture 16 Walking edge equations •We need to update values of the three edge equations with single-pixel

© 2011 Kavita Bala •(with previous instructors James/Marschner)

Cornell CS4620/5620 Fall 2011 •!Lecture 16

Rasterizing triangles

• Exercise caution with rounding and arbitrary decisions– need to visit these

pixels once– but it’s important not

to visit them twice!

19