Joshua Barczak* CMSC 435 UMBC

Slides:



Advertisements
Similar presentations
Computer Graphics Inf4/MSc 1 Computer Graphics Lecture 7 Scanline algorithm and polygon clipping Taku Komura.
Advertisements

CS 4731: Computer Graphics Lecture 20: Raster Graphics Part 1 Emmanuel Agu.
CS 450: COMPUTER GRAPHICS REVIEW: DRAWING LINES AND CIRCLES SPRING 2015 DR. MICHAEL J. REALE.
Line Drawing Algorithms. Rasterization-Process of determining which pixels give the best approximation to a desired line on the screen. Scan Conversion-Digitizing.
Line clipping: Line clipping algorithm is method of eliminate lines of outside area of the object,so outside of object viewing is Removed. Typically, any.
CP411 polygon Lecture 6 1. Scan conversion algorithm for circle 2. Draw polygons 3. Antialiasing.
Introduction to Computer Graphics Chapter 6 – 2D Viewing Pt 2 1.
University of British Columbia CPSC 314 Computer Graphics Jan-Apr 2005 Tamara Munzner Interpolation Clipping.
Informationsteknologi Thursday, November 22, 2007Computer Graphics - Class 111 Today’s class Clipping Parametric and point-normal form of lines Intersecting.
CS5500 Computer Graphics © Chun-Fa Chang, Spring 2007 CS5500 Computer Graphics May 3, 2007.
University of British Columbia CPSC 414 Computer Graphics © Tamara Munzner 1 Scan Conversion Week 6, Mon 6 Oct 2003 recap: Bresenham line algorithm scan.
Rasterization Foley, Ch: 3. Pixels are represented as disjoint circles centered on uniform grid Each (x,y) of the grid has a pixel Y X (1,1) (1,2) (0,0)
Graphics Pipeline Rasterization CMSC 435/634. Drawing Terms Primitive – Basic shape, drawn directly – Compare to building from simpler shapes Rasterization.
Polygon Scan Conversion and Z-Buffering
CS 551 / 645: Introductory Computer Graphics
Scan Conversion Line and Circle
Windowing and clipping
Graphics Pipeline Rasterization CMSC 435/634. Drawing Terms Primitive – Basic shape, drawn directly – Compare to building from simpler shapes Rasterization.
3/4/04© University of Wisconsin, CS559 Spring 2004 Last Time Clipping Lines –Cohen-Sutherland: The use of outcodes and early reject/accept tests –Liang-Barsky:
CS 445 / 645 Introduction to Computer Graphics Lecture 8 Triangle Rasterization Lecture 8 Triangle Rasterization.
Rasterization Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006 (Slide set originally by David Luebke)
Triangle Scan Conversion. 2 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 Rasterization Rasterization (scan conversion) –Determine which.
Graphics Pipeline Rasterization CMSC 435/634. Drawing Terms Primitive – Basic shape, drawn directly – Compare to building from simpler shapes Rasterization.
College of Computer and Information Science, Northeastern UniversityOctober 12, CS G140 Graduate Computer Graphics Prof. Harriet Fell Spring 2006.
10/15/02 (c) 2002 University of Wisconsin, CS559 Last Time Clipping.
CAP4730: Computational Structures in Computer Graphics
Image Synthesis Rabie A. Ramadan, PhD 7. 2 Image Rasterization.
Drawing Triangles CS 445/645 Introduction to Computer Graphics David Luebke, Spring 2003.
Introduction to Computer Graphics CS 445 / 645 Lecture 9 Chapter 4: Rasterization L. McMillan MIT notes (Fall ’98)
10/15/02 (c) 2002 University of Wisconsin, CS559 Who Am I? Prof Stephen Chenney These notes will be online after the lecture – in fact they’re online already.
University of British Columbia CPSC 314 Computer Graphics Jan-Apr 2013 Tamara Munzner Rasterization.
1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai.
Chapter III Rasterization
Graphics Pipeline Rasterization CMSC 435/634. Drawing Terms Primitive – Basic shape, drawn directly – Compare to building from simpler shapes Rasterization.
Rendering.
Lecture 13: Raster Graphics and Scan Conversion
Rasterization Overview Raster Display Device. Scan Conversion / Rasterization: converting vector graphics into raster graphics (determining pixels in.
Computer Graphics Inf4/MSc Computer Graphics Lecture 4 Line & Circle Drawing.
Scan Conversion of Line Segments. What is Scan conversion? Final step of rasterization (the process of taking geometric shapes and converting them into.
Rasterization, or “What is glBegin(GL_LINES) really doing?” Course web page: February 23, 2012  Lecture 4.
Computer Graphic 2 D Viewing.
Computer Graphics Clipping.
CS G140 Graduate Computer Graphics
Computer Graphics Shading in OpenGL
Concepts, Algorithms for line clipping
(c) 2002 University of Wisconsin, CS559
Basic Raster Graphics Algorithms for Drawing 2D Primitives
CS 4731: Computer Graphics Lecture 20: Raster Graphics Part 1
Clipping Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006
Implementation III.
Graphics Pipeline Clipping
Introduction to Computer Graphics with WebGL
© University of Wisconsin, CS559 Fall 2004
3D Rendering Pipeline Hidden Surface Removal 3D Primitives
Lecture 13 Clipping & Scan Conversion
Chapter VII Rasterizer
Joshua Barczak CMSC 435 UMBC
Two Dimensional Viewing and Clipping.
Computer Graphics Implementation III
Edited from The Rasterization Problem: Idealized Primitives Map to Discrete Display Space Edited from.
(c) 2002 University of Wisconsin, CS559
(c) 2002 University of Wisconsin, CS559
© University of Wisconsin, CS559 Fall 2004
Rasterization Week 6, Mon Feb 12
Chapter 3 Graphics Output Primitives
Basic Raster Graphics Algorithms for Drawing 2D Primitives
Clipping Clipping Sutherland-Hodgman Clipping
Where We Stand At this point we know how to: Next thing:
Clipping University of British Columbia CPSC 314 Computer Graphics
Line Drawing Algorithms
Presentation transcript:

Joshua Barczak* CMSC 435 UMBC Rasterization Joshua Barczak* CMSC 435 UMBC *Featuring figures by Dr. Marc Olano

Object-Order Rendering Object-Space Primitives World-space Primitives Camera-Space Primitives Clip-Space Primitives Clip Modeling XForm Viewing XForm Projection XForm Clip-Space Primitives Rasterize Window XForm Viewport XForm Displayed Pixels Pixels Raster-Space Primitives We are here

Viewing Transform The job of the viewing transform is: Get us from these axes Onto these axes

Projection Transform The job of the projection transform is: Cram the visible universe into a unit box Project

Canonical View Volume (GL) (-1,1,1) (1,1,1) Top Edge of Screen (0,1,-1) Center of screen Left Edge of Screen (0,0,-1) (-1,0,-1) (1,0,-1) (1,-1,1) Points outside the cube are not visible… (1,-1,-1) (0,-1,-1) D3D uses z=0. I like theirs better… Bottom Edge of Screen

Rasterization “Raster”-ize Convert from geometric shape to set of covered pixels Also called: Scan conversion, filling Or just “drawing”

Our Pixel Coordinate System 0,0 Width 1 0.5,0.5 1 Height

Line Drawing Given endpoints of line, which pixels to draw? 8

Line Drawing Given endpoints of line, which pixels to draw? 9

Line Drawing Always step over Step up if midpoint is below line One pixel per row/column Step up if midpoint is below line F(x,y) < 0 Use implicit line equation x1 Midpoint x0

Line Drawing Assume one pixel per column (x index), which row (y index)? Choose based on relation of line to midpoint between candidate pixels ? ? ? ? 11

Midpoint Algorithm int xa = floor(x0); int xb = floor(x1); int y = floor(y0); if( y0 <= y1 ) { for( int x= xa; x<=xb; x++ ){ fill(x,y); if( f(x+1.5,y+1) < 0 ) y++; } } else { if( f(x+1.5,y) > 0 ) y--; Assumptions: x0 <= x1 (if not, swap endpoints) |x1-x0| > |y1-y0| (if not, reverse x and y)

Making it Fast Let’s count flops: Inside the loop, we have: 7x Fadd/Fsub 4x Fmul 2x Int_to_float(x,y) for( int x= xa; x<=xb; x++ ){ fill(x,y); if( f(x+1.5,y+1) < 0 ) y++; } for( int x= xa; x<=xb; x++ ){ fill(x,y); d = (y0-y1)*(x+1.5) + (x1-x0)*(y+1) +x0*y1-y0*x1; if( d < 0 ) y++; }

Making it Fast Take out the loop invariantsWe now have: 4x Fadd/Fsub 2x Fmul 2x Int_to_float for( int x= xa; x<=xb; x++ ){ fill(x,y); d = (y0-y1)*(x+1.5) + (x1-x0)*(y+1) +x0*y1-y0*x1; if( d < 0 ) y++; } float dy = y0-y1; float dx = x1-x0; float c = x0*y1-y0*x1; for( int x= xa; x<=xb; x++ ){ fill(x,y); d = dy*(x+1.5)+dx*(y+1)+c; if( d < 0 ) y++; } A good compiler will do this for you…

Making it Fast Now do some algebra Exposes more invariants… float dy = y0-y1; float dx = x1-x0; float c = x0*y1-y0*x1; for( int x= xa; x<=xb; x++ ){ fill(x,y); d = dy*(x+1.5)+dx*(y+1)+c; if( d < 0 ) y++; } Now do some algebra Exposes more invariants… float dy = y0-y1; float dx = x1-x0; float c = x0*y1-y0*x1; for( int x= xa; x<=xb; x++ ){ fill(x,y); d = dy*x + dx*y + 1.5*dy + dx + c; if( d < 0 ) y++; } A compiler is less likely to do this for you…

Making it Fast This ‘d’ variable depends only on x,y All its other inputs are loop-invariant x,y are only ever incremented When x changes, add dy. When y changes, add dx float dy = y0-y1; float dx = x1-x0; float c = x0*y1-y0*x1 + 1.5*dy + dx; for( int x= xa; x<=xb; x++ ){ fill(x,y); d = dy*x + dx*y + c; if( d < 0 ) y++; } dx/dy are the partial derivatives of d wrt x,y

Incremental Computation Now we’re down to one measly add This is also known as a ‘DDA’ Digital difference analyzer float dy = y0-y1; float dx = x1-x0; float d = x0*y1-y0*x1 + 1.5*dy + dx; float dxdy = dx+dy; for( int x= xa; x<=xb; x++ ){ fill(x,y); if( d < 0 ){ y++; d += dxdy; } else d += dx; I’d be very surprised if a compiler did this for you…

Incremental Interpolation Let’s add colors… Interpolate them using DDA // … float drdx = (r1-r0)/(x1-x0); float drdy = (r1-r0)/(y1-y0); // ditto for g,b for( int x= xa; x<=xb; x++ ){ fill(x,y,r,g,b); if( d < 0 ){ y++; d += dxdy; r += drdx + drdy; // … g,b … } else{ d += dx; r += drdx; // … g,b, …

Rectangles Filling Rectangles int i0 = ceil( x0 - 0.5f ); // Round endpoints int i1 = ceil( x1 - 0.5f ); int j0 = ceil( y0 - 0.5f ); int j1 = ceil(y1 - 0.5f ); for( int y= j0; y < j1; y++ ) // fill pixels for( int x= i0; x < i1; x++ ) fill(x,y);

Rectangles Filling Rectangles int i0 = ceil( x0 - 0.5f ); // Round endpoints int i1 = ceil( x1 - 0.5f ); int j0 = ceil( y0 - 0.5f ); int j1 = ceil(y1 - 0.5f ); for( int y= j0; y < j1; y++ ) // fill pixels for( int x= i0; x < i1; x++ ) fill(x,y);

Rectangles Filling Rectangles int i0 = ceil( x0 - 0.5f ); // Round endpoints int i1 = ceil( x1 - 0.5f ); int j0 = ceil( y0 - 0.5f ); int j1 = ceil(y1 - 0.5f ); for( int y= j0; y < j1; y++ ) // fill pixels for( int x= i0; x < i1; x++ ) fill(x,y);

Rectangles Filling Rectangles int i0 = ceil( x0 - 0.5f ); // Round endpoints int i1 = ceil( x1 - 0.5f ); int j0 = ceil( y0 - 0.5f ); int j1 = ceil(y1 - 0.5f ); for( int y= j0; y < j1; y++ ) // fill pixels for( int x= i0; x < i1; x++ ) fill(x,y);

Rectangles Filling Rectangles int i0 = ceil( x0 - 0.5f ); // Round endpoints int i1 = ceil( x1 - 0.5f ); int j0 = ceil( y0 - 0.5f ); int j1 = ceil(y1 - 0.5f ); for( int y= j0; y < j1; y++ ) // fill pixels for( int x= i0; x < i1; x++ ) fill(x,y);

Rounding… Rounding endpoints snaps to covered pixel grid…

Rounding…

Top-Left Rule… How we break ties is important (<, not <=)

Top-Left Rule… Right

Top-Left Rule… Wrong Overdraw!

Colored Rectangles Bilinear Interpolation: r0 r1 v v r2 r3 u

Colored Rectangles Terrible code: r0 r1 for( int y=y0; y<y1; y++ ){ for( int x=x0; x<x1; x++ ) { float u = (x+0.5)/(x1-x0); float v = (y+0.5)/(y1-y0); float r = (1-u)*( (1-v)*r0 + v*r2) + u *( (1-v)*r1 + v*r3) ); fill(x,y,r); } } v v r2 r3 u

Colored Rectangles Terrible code: r0 r1 for( int y=y0; y<y1; y++ ){ for( int x=x0; x<x1; x++ ) { float u = (x+0.5)/(x1-x0); float v = (y+0.5)/(y1-y0); float r = (1-u)*( (1-v)*r0 + v*r2) + u *( (1-v)*r1 + v*r3) ); fill(x,y,r); } } v v Move invariants… r2 r3 u

Colored Rectangles Better code: r0 r1 for( int y=y0; y<y1; y++ ){ float v = (y+0.5) / (y1-y0); float rv0 = (1-v)*r0 + v*r2; float rv1 = (1-v)*r1 + v*r3; for( int x=x0; x<x1; x++ ) { float u = (x+0.5)/(x1-x0); float r = (1-u)*rv0 + u*rv1 fill(x,y,r); } } v v r2 r3 Convert lerps to DDAs… u

Colored Rectangles Much better code: r0 r1 float drdy0 = (r2-r0)/(y1-y0); float drdy1 = (r3-r1)/(y1-y0); float rv0 = r0 + 0.5*drdy0; float rv1 = r1 + 0.5*drdy1; for( int y=y0; y<y1; y++ ){ float drdx = (rv1-rv0)/(x1-x0); float r= rv0 + 0.5*drdx; for( int x=x0; x<x1; x++ ) { fill(x,y,r); r += drdx; // inner DDA } rv0 += drdy0; rv1 += drdy1; // outer DDA } rv0 rv1 r2 r3 r

Triangles We’ll cover two algorithms: Span filling Barycentric

Span Filling Intersect scan line with triangle

Span Filling Fill pixels in each span…

Span Filling Fill pixels in each span…

Span Filling Fill pixels in each span…

Span Filling Fill pixels in each span…

Span Filling Fill pixels in each span…

Span Filling Fill pixels in each span…

Span Filling Fill pixels in each span…

Triangles Incremental span filling DDA down “active edges” Snap and fill for each line DDA vertex data as we go /// Approximate code… for( y=y0; y<y1; y++ ) { for( x = round(x0); x < round(x1); x++ ) fill(x,y); x0 += dxdy0; x1 += dxdy1; }

Barycentric Rasterization Start with bounding box…

Barycentric Rasterization Snap to grid…

Barycentric Rasterization Barycentric tests…

Barycentric Rasterization Barycentric tests…

Barycentric Rasterization Barycentric tests…

Barycentric Rasterization Triangle Edge Equations: x1,y1 x2,y2 x0,y0

Barycentric Rasterization g=1 Divide through by opposite vert: Barycentric coords. x1,y1 b=0 a=0 g=0 x2,y2 x0,y0 a=1 b=1 Point is in triangle when all three are positive

Barycentric Rasterization // conservative bounding box for triangle // (snapped to pixel grid) int ymin = floor( min(y0,y1,y2) ); int ymax = ceil( max(y0,y1,y2) ); int xmin = floor( min(x0,x1,x2) ); int xmax = ceil( max(x0,x1,x2) ); // normalized edge equation: // f01(x,y) = a01*x +b01*y + c01; float a01 = (y0-y1); float b01 = (x1-x0); float c01 = (x0*y1-y0*x1); float n01 = a01*x2 + b01*y2 + c01; a01 /= n01; b01 /= n01; c01 /= n01; // similar setup for f12, f20... // Walk bounding box and test pixels for( int y=ymin; y<ymax; y++ ){ for( int x=xmin; x<xmax; x++ ){ float a = a01*(x+0.5) + b01*(y+0.5) + c01; float b = a12*(x+0.5) + b12*(y+0.5) + c12; float c = a20*(x+0.5) + b20*(y+0.5) + c20; if( a>0 && b>0 && c>0 ) fill(x,y); } g=1 x1,y1 b=0 a=0 g=0 x2,y2 x0,y0 a=1 b=1

Barycentric Rasterization // Walk bounding box and test pixels for( int y=ymin; y<ymax; y++ ){ for( int x=xmin; x<xmax; x++ ){ float a = a01*(x+0.5) + b01*(y+0.5) + c01; float b = a12*(x+0.5) + b12*(y+0.5) + c12; float c = a20*(x+0.5) + b20*(y+0.5) + c20; if( a>0 && b>0 && c>0 ) fill(x,y); } g=1 x1,y1 b=0 a=0 // incremental version: float ya= c01 + (ymin+0.5)*b01 + (xmin+0.5)*a01; float yb= c12 + (ymin+0.5)*b12 + (xmin+0.5)*b12; float yc= c20 + (ymin+0.5)*b20 + (xmin+0.5)*b20; for( int y=ymin; y<ymax; y++ ){ float a = ya; float b = yb; float c = yc; for( int x=xmin; x<xmax; x++ ){ if( a>0 && b>0 && c>0 ) fill(x,y); a += a01; b += a12; c += a20; } ya += b01; yb += b12; yc += b20; g=0 x2,y2 x0,y0 a=1 b=1

Barycentric Interpolation Weight per-vertex values by barycentric coordinates

Barycentric Rasterization Top-Left Rule: Book says: Test an arbitrary point -1,1

Barycentric Rasterization Top-Left Rule: I say: “Down and to the right” Use inward facing edge normal… Tie goes to edge if: (X==0 && y<0) || X>0

Pros and Cons Span filling: Barycentric Less per-pixel math Hits only visible pixels Barycentric Simpler More parallelism Irregular sampling easier

Barycentric Rasterization SIMD Parallelism (NxN Stamp)

Barycentric Rasterization SIMD Parallelism (NxN Stamp)

Barycentric Rasterization SIMD Parallelism (NxN Stamp)

Barycentric Rasterization SIMD Parallelism (NxN Stamp)

Barycentric Rasterization SIMD Parallelism (NxN Stamp)

Barycentric Rasterization SIMD Parallelism (NxN Stamp)

Combining Ideas Problem: Lots of tests are wasted ½ to 2/3

Combining Ideas One Solution: Combine ideas Conservative tile walk using DDA

Combining Ideas DDA between rows, snapped to tile grid Fill tile spans with NxN stamps

Combining Ideas DDA between rows, snapped to tile grid Fill tile spans with NxN stamps

Combining Ideas DDA between rows, snapped to tile grid Fill tile spans with NxN stamps

Combining Ideas DDA between rows, snapped to tile grid Fill tile spans with NxN stamps

Combining Ideas DDA between rows, snapped to tile grid Fill tile spans with NxN stamps

Combining Ideas Could skip tests on the interior Fill complete block *Bottom ones are red because I can’t assume a horizontal edge

Recursive Descent Subdivide screen* For a real example, read: *Maybe better to subdivide triangle AABB instead, but this this box works better for illustration… For a real example, read: Abrash,M. “Rasterization on Larrabee” Dr. Dobbs, May 2009

Recursive Descent Subdivide screen For a good time, read: Abrash,M. “Rasterization on Larrabee” Dr. Dobbs, May 2009

Recursive Descent …trivial reject empty nodes… For leet skillz, read: Abrash,M. “Rasterization on Larrabee” Dr. Dobbs, May 2009

Recursive Descent …trivial accept full nodes… To learn some magic, read: Abrash,M. “Rasterization on Larrabee” Dr. Dobbs, May 2009

Recursive Descent …keep going… …switch to NxN stamps once they’re small enough DANGER! patent exposure in: Abrash,M. “Rasterization on Larrabee” Dr. Dobbs, May 2009