<<

The graphics

• The standard approach to object-order graphics • Many versions exist – Software, e.g., Pixar’s REYES architecture Pipeline and Rasterization • many options for quality and flexibility – Hardware, e.g., graphics cards in PCs • amazing performance: millions of triangles per frame CS4620 • We’ll focus on an abstract version of hardware pipeline (Shirley & Marschner, Chapter 8) • “Pipeline” because of the many stages – very parallelizable – leads to remarkable performance of graphics cards (many times the flops of the CPU at ~1/5 the clock speed)

Cornell CS4620 Fall 2010 © 2010 Doug James • 1 Cornell CS4620 Fall 2010 © 2010 Doug James • 2

APPLICATION Primitives Pipeline you are here COMMAND STREAM • Points 3D transformations; VERTEX PROCESSING • Line segments

TRANSFORMED GEOMETRY – and chains of connected line segments • Triangles conversion of primitives to pixels RASTERIZATION • And that’s all! FRAGMENTS – Curves? Approximate them with chains of line segments

blending, compositing, shading FRAGMENT PROCESSING – Polygons? Break them up into triangles – Curved regions? Approximate them with triangles IMAGE • Trend has been toward minimal primitives user sees this DISPLAY – Simple, uniform, repetitive: good for parallelism

Cornell CS4620 Fall 2010 © 2010 Doug James • 3 Cornell CS4620 Fall 2010 © 2010 Doug James • 4 Rasterization Rasterizing lines

• First job: enumerate the pixels covered by a primitive • Define line as a – Simple, aliased definition: pixels whose centers fall inside rectangle • Second job: interpolate values across the primitive • Specify by two – e.g., colors computed at vertices endpoints – e.g., normals at vertices • Ideal image: black – will see applications later on inside, white outside

Cornell CS4620 Fall 2010 © 2010 Doug James • 5 Cornell CS4620 Fall 2010 © 2010 Doug James • 6

Point sampling Point sampling in action • Approximate rectangle by drawing all pixels whose centers fall within the line • Problem: sometimes turns on adjacent pixels

Cornell CS4620 Fall 2010 © 2010 Doug James • 7 Cornell CS4620 Fall 2010 © 2010 Doug James • 8 Bresenham lines (midpoint alg.) Midpoint algorithm in action • Point sampling unit width rectangle leads to uneven line width • Define line width parallel to pixel grid • That is, turn on the single nearest pixel in each column • Note that 45º lines are now thinner

Cornell CS4620 Fall 2010 © 2010 Doug James • 9 Cornell CS4620 Fall 2010 © 2010 Doug James • 10

Algorithms for drawing lines Optimizing line drawing

• line equation: • Multiplying and y = b + m x rounding is slow • Simple algorithm: • At each pixel the evaluate line only options are E equation per column and NE

• W.l.o.g. x0 < x1; • d = m(x + 1) + b – y 0 ! m ! 1 • d > 0.5 decides between E and NE for x = ceil(x0) to floor(x1) y = b + m*x output(x, round(y)) y = 1.91 + 0.37 x

Cornell CS4620 Fall 2010 © 2010 Doug James • 11 Cornell CS4620 Fall 2010 © 2010 Doug James • 12 Optimizing line drawing Midpoint line algorithm

• d = m(x + 1) + b – y x = ceil(x0) • Only need to update y = round(m*x + b) d for integer steps in d = m*(x + 1) + b – y while x < floor(x1) x and y if d > 0.5 y += 1 • Do that with d –= 1 addition x += 1 d += m • Known as output(x, y) “DDA” (digital differential analyzer)

Cornell CS4620 Fall 2010 © 2010 Doug James • 13 Cornell CS4620 Fall 2010 © 2010 Doug James • 14

Linear interpolation Linear interpolation

• We often attach attributes to vertices • Pixels are not – e.g. computed diffuse color of a hair being drawn using lines exactly on the line – want color to vary smoothly along a chain of line segments • Define 2D function • Recall basic definition by projection on line – 1D: f(x) = (1 – !) y + " y 0 1 – this is linear in 2D – where ! = (x – x0) / (x1 – x0) – therefore can use • In the 2D case of a line segment, alpha is just the DDA to interpolate

fraction of the distance from (x0, y0) to (x1, y1)

Cornell CS4620 Fall 2010 © 2010 Doug James • 15 Cornell CS4620 Fall 2010 © 2010 Doug James • 16 Alternate interpretation Alternate interpretation

• We are updating d and ! as we step from pixel to pixel • View loop as visiting – d tells us how far from the line we are all pixels the line ! tells us how far along the line we are passes through • So d and ! are coordinates in a coordinate system Interpolate d and ! for each pixel oriented to the line Only output frag. if pixel is in band • This makes linear interpolation the primary operation

Cornell CS4620 Fall 2010 © 2010 Doug James • 17 Cornell CS4620 Fall 2010 © 2010 Doug James • 18

Pixel-walk line rasterization Rasterizing triangles

• The most common case in most applications x = ceil(x0) y = round(m*x + b) – with good antialiasing can be the only case d = m*x + b – y – some systems render a line as two skinny triangles while x < floor(x1) if d > 0.5 • Triangle represented by three vertices y += 1; d –= 1; else • Simple way to think of algorithm follows the pixel-walk x += 1; d += m; interpretation of line rasterization if –0.5 < d ! 0.5 output(x, y) – 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

Cornell CS4620 Fall 2010 © 2010 Doug James • 19 Cornell CS4620 Fall 2010 © 2010 Doug James • 20 Rasterizing triangles Rasterizing triangles

• Input: • Summary – three 2D points (the triangle’s vertices in pixel space) 1! evaluation of linear • (x , y ); (x , y ); (x , y ) functions on pixel 0 0 1 1 2 2 grid – parameter values at each vertex 2! functions defined by • q00, …, q0n; q10, …, q1n; q20, …, q2n parameter values at vertices • Output: a list of fragments, each with 3! using extra – the integer pixel coordinates (x, y) parameters to determine – interpolated parameter values q0, …, qn fragment set

Cornell CS4620 Fall 2010 © 2010 Doug James • 21 Cornell CS4620 Fall 2010 © 2010 Doug James • 22

Incremental linear evaluation Incremental linear evaluation

• A linear (affine, really) function on the is: linEval(xl, xh, yl, yh, cx, cy, ck) {

// setup • Linear functions are efficient to evaluate on a grid: 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)

Cornell CS4620 Fall 2010 © 2010 Doug James • 23 Cornell CS4620 Fall 2010 © 2010 Doug James • 24 Rasterizing triangles Defining parameter functions

• Summary • To interpolate parameters across a triangle we need to 1! evaluation of linear find the cx, cy, and ck that define the (unique) linear functions on pixel grid function that matches the given values at all 3 vertices – this is 3 constraints on 3 unknown coefficients: 2! functions defined by (each states that the function parameter values agrees with the given value at vertices at one vertex) 3! using extra parameters – leading to a 3x3 matrix equation for the coefficients: to determine fragment set (singular iff triangle is degenerate)

Cornell CS4620 Fall 2010 © 2010 Doug James • 25 Cornell CS4620 Fall 2010 © 2010 Doug James • 26

Defining parameter functions Defining parameter functions

• More efficient version: shift origin to (x , y ) linInterp(xl, xh, yl, yh, x0, y0, q0, 0 0 x1, y1, q1, x2, y2, q2) { q(x, y) = cx(x x0) + cy(y y0) + q0 − − // setup q = 0 q = 1 q(x1, y1) = cx(x1 x0) + cy(y1 y0) + q0 = q1 det = (x1-x0)*(y2-y0) - (x2-x0)*(y1-y0); − − cx = ((q1-q0)*(y2-y0) - (q2-q0)*(y1-y0)) / det; q(x2, y2) = cx(x2 x0) + cy(y2 y0) + q0 = q2 cy = ((q2-q0)*(x1-x0) - (q1-q0)*(x2-x0)) / det; − − qRow = cx*(xl-x0) + cy*(yl-y0) + q0; – now this is a 2x2 linear system (since q0 falls out): // traversal (same as before) for y = yl to yh { qPix = qRow; for x = xl to xh { output(x, y, qPix); – solve using Cramer’s rule (see Shirley): qPix += cx; q = 0 } qRow += cy; } }

Cornell CS4620 Fall 2010 © 2010 Doug James • 27 Cornell CS4620 Fall 2010 © 2010 Doug James • 28 Interpolating several parameters Rasterizing triangles

linInterp(xl, xh, yl, yh, n, x0, y0, q0[], • Summary x1, y1, q1[], x2, y2, q2[]) { 1! evaluation of linear // setup functions on pixel for k = 0 to n-1 // compute cx[k], cy[k], qRow[k] grid // from q0[k], q1[k], q2[k] 2! functions defined by // traversal parameter values for y = yl to yh { for k = 1 to n, qPix[k] = qRow[k]; at vertices for x = xl to xh { 3! using extra output(x, y, qPix); for k = 1 to n, qPix[k] += cx[k]; parameters } to determine for k = 1 to n, qRow[k] += cy[k]; } fragment set }

Cornell CS4620 Fall 2010 © 2010 Doug James • 29 Cornell CS4620 Fall 2010 © 2010 Doug James • 30

Clipping to the triangle Barycentric coordinates

• Interpolate three barycentric • A coordinate system for triangles coordinates across the – algebraic viewpoint: plane – each barycentric coord is 1 at one vert. and 0 at – geometric viewpoint (areas): the other two • Triangle interior test: • Output fragments only when all three are > 0. [Shirley 2000]

Cornell CS4620 Fall 2010 © 2010 Doug James • 31 Cornell CS4620 Fall 2010 © 2010 Doug James • 32 Barycentric coordinates Barycentric coordinates

• A coordinate system for triangles • Linear viewpoint: basis for the plane – geometric viewpoint: distances [Shirley 2000] – linear viewpoint: basis of edges – in this view, the triangle interior test is just

Cornell CS4620 Fall 2010 © 2010 Doug James • 33 Cornell CS4620 Fall 2010 © 2010 Doug James • 34

Walking edge equations Pixel-walk (Pineda) rasterization

• We need to update values of the three edge equations • Conservatively with single-pixel steps in x and y visit a superset of • Edge equation already in form of the pixels you want • Components of vector are the increments • Interpolate linear functions • Use those functions to determine when to emit a fragment

Cornell CS4620 Fall 2010 © 2010 Doug James • 35 Cornell CS4620 Fall 2010 © 2010 Doug James • 36 Rasterizing triangles Clipping

• Exercise caution • Rasterizer tends to assume triangles are on screen with rounding and – particularly problematic to have triangles crossing arbitrary decisions the plane z = 0 – need to visit these • After projection, before perspective divide pixels once – clip against the planes x, y, z = 1, –1 (6 planes) – but it’s important not – primitive operation: clip triangle against axis-aligned plane to visit them twice!

Cornell CS4620 Fall 2010 © 2010 Doug James • 37 Cornell CS4620 Fall 2010 © 2010 Doug James • 38

Clipping a triangle against a plane

• 4 cases, based on sidedness of vertices – all in (keep) – all out (discard) – one in, two out (one clipped triangle) – two in, one out (two clipped triangles)

Cornell CS4620 Fall 2010 © 2010 Doug James • 39