22 Implementation Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts University of New Mexico Many changes – Jeff.

Slides:



Advertisements
Similar presentations
Computer Graphics - Rasterization -
Advertisements

CS 450: COMPUTER GRAPHICS FILLING POLYGONS SPRING 2015 DR. MICHAEL J. REALE.
CS 352: Computer Graphics Chapter 7: The Rendering Pipeline.
CHAPTER 12 Height Maps, Hidden Surface Removal, Clipping and Level of Detail Algorithms © 2008 Cengage Learning EMEA.
3/2/04© University of Wisconsin, CS559 Spring 2004 Last Time General Perspective –Methods for specifying the view volume As a set of clip planes As a field.
Introduction to Computer Graphics Chapter 6 – 2D Viewing Pt 2 1.
Different types of Polygons Simple Convex Simple Concave Non-simple : self-intersecting With holes ConvexConcaveSelf-intersecting.
Informationsteknologi Thursday, November 22, 2007Computer Graphics - Class 111 Today’s class Clipping Parametric and point-normal form of lines Intersecting.
Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters  Hill, Chapter 10.
Chapter 6: Vertices to Fragments Part 2 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley Mohan Sridharan Based on Slides.
Vertices and Fragments I CS4395: Computer Graphics 1 Mohan Sridharan Based on slides created by Edward Angel.
Implementation III Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts University of New Mexico.
CS123 | INTRODUCTION TO COMPUTER GRAPHICS Andries van Dam © Scan Conversion CS123 1 of 44Scan Conversion - 10/14/2014.
1 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 Clipping.
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Implementation I Ed Angel Professor of Computer Science, Electrical and Computer Engineering,
Graphics Pipeline Clipping CMSC 435/634. Graphics Pipeline Object-order approach to rendering Sequence of operations – Vertex processing – Transforms.
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Chapter 7: From Vertices to Fragments Ed Angel Professor of Computer Science, Electrical.
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:
Informationsteknologi Monday, November 26, 2007Computer Graphics - Class 121 Today’s class Drawing lines Bresenham’s algorithm Compositing Polygon filling.
CS 325 Introduction to Computer Graphics 03 / 08 / 2010 Instructor: Michael Eckmann.
Triangle Scan Conversion. 2 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 Rasterization Rasterization (scan conversion) –Determine which.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Implementation Ed Angel Professor of Computer Science, Electrical and Computer Engineering,
Objectives Introduce basic implementation strategies Clipping Scan conversion.
From Vertices to Fragments Chapter 7. Part I Objectives Introduce basic implementation strategies Introduce basic implementation strategies Clipping Clipping.
CS 480/680 Computer Graphics Shading in OpenGL Dr. Frederick C Harris, Jr. Fall 2013.
1Computer Graphics Implementation III Lecture 17 John Shearer Culture Lab – space 2
Introduction to Computer Graphics with WebGL
CS 480/680 Computer Graphics Implementation III Dr. Frederick C Harris, Jr. Fall 2011.
10/15/02 (c) 2002 University of Wisconsin, CS559 Last Time Clipping.
1 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 Week 3 Lecture 1: Implementation Based on Interactive Computer Graphics (Angel) - Chapter.
CSE Real Time Rendering Week 9. Post Geometry Shaders Courtesy: E. Angel and D. Shreiner – Interactive Computer Graphics 6E © Addison-Wesley 2012.
2D Output Primitives Points Lines Circles Ellipses Other curves Filling areas Text Patterns Polymarkers.
COMPUTER GRAPHICS Hochiminh city University of Technology Faculty of Computer Science and Engineering CHAPTER 07: Rasterization.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
Implementation II Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts University of New Mexico.
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.
10/19/04© University of Wisconsin, CS559 Fall 2004 Last Time Clipping –Why we care –Sutherland-Hodgman –Cohen-Sutherland –Intuition for Liang-Barsky Homework.
Review on Graphics Basics. Outline Polygon rendering pipeline Affine transformations Projective transformations Lighting and shading From vertices to.
Computer Graphics Lecture 20 Fasih ur Rehman. Last Class Clipping – What is clipping – Why we do clipping – How clipping is done.
Computer Graphics I, Fall 2010 Implementation II.
1Computer Graphics Implementation 1 Lecture 15 John Shearer Culture Lab – space 2
Computer Graphics CC416 Week 14 Filling Algorithms.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
Lecture 9 From Vertices to Fragments. Objectives Introduce basic implementation strategies Clipping Rasterization hidden-surface removal.
Computer Graphics I, Fall 2010 Scan conversion algorithms.
Rasterization, or “What is glBegin(GL_LINES) really doing?” Course web page: February 23, 2012  Lecture 4.
Introduction to Computer Graphics with WebGL
Transformations contd.
Computer Graphics Shading in OpenGL
University of New Mexico
Implementation III.
Implementation I Ed Angel
Graphics Pipeline Clipping
Introduction to Computer Graphics with WebGL
Computer Graphics : Viewing In 2D
Implementation II Ed Angel Professor Emeritus of Computer Science
Course code: 10CS65 | Computer Graphics and Visualization
Introduction to Computer Graphics with WebGL
Rasterization and Antialiasing
Lecture 13 Clipping & Scan Conversion
Computer Graphics Implementation III
Rasterization and Antialiasing
Introduction to Computer Graphics with WebGL
From Vertices To Fragments
Implementation I Ed Angel Professor Emeritus of Computer Science
Implementation III Ed Angel Professor Emeritus of Computer Science
Implementation II Ed Angel Professor Emeritus of Computer Science
From Vertex to Fragment
From Vertex to Fragment
Presentation transcript:

22 Implementation Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts University of New Mexico Many changes – Jeff Parker

23 Objectives Introduce basic implementation strategies Clipping Drawing lines Anti-aliasing Drawing Circles Scan conversion Two contributions from Robert Sproull coauthor of first text on Computer Graphics

24 Overview At end of the geometric pipeline, vertices have been assembled into primitives Must clip primitives that are outside view frustum Algorithms based on representing primitives by lists of vertices Must find which pixels can be affected by each primitive Fragment generation Rasterization or scan conversion

25 Required Tasks Clipping Rasterization or scan conversion Transformations Some tasks deferred until fragment processing Hidden surface removal Antialiasing

26 Rasterization Meta Algorithms Consider two approaches to rendering a scene with opaque objects For every pixel, determine which object that projects on the pixel is closest to the viewer and compute the shade of this pixel Ray tracing paradigm For every object, determine which pixels it covers and shade these pixels Pipeline approach Must keep track of depths

27 Clipping 2D against clipping window 3D against clipping volume Easy for line segments polygons Hard for curves and text Convert to lines and polygons first

28 Clipping 2D Line Segments Brute force approach: compute intersections with all sides of clipping window Inefficient: one division per intersection

Cohen-Sutherland Algorithm

29 Cohen-Sutherland Algorithm Idea: eliminate as many cases as possible without computing intersections Start with four lines that determine the sides of the clipping window x = x max x = x min y = y max y = y min

30 The Cases Case 1: both endpoints of line segment inside all four lines Draw (accept) line segment as is Case 2: both endpoints outside all lines and on same side of a line Discard (reject) the line segment x = x max x = x min y = y max y = y min

31 The Cases Case 3: One endpoint inside, one outside Must do at least one intersection Case 4: Both outside May have part inside Must do at least one intersection x = x max x = x min y = y max

32 Defining Outcodes For each endpoint, define an outcode Outcodes divide space into 9 regions Computation of outcode requires at most 4 subtractions b0b1b2b3b0b1b2b3 b 0 = 1 if y > y max, 0 otherwise b 1 = 1 if y < y min, 0 otherwise b 2 = 1 if x > x max, 0 otherwise b 3 = 1 if x < x min, 0 otherwise

33 Using Outcodes Consider the 5 cases below AB: outcode(A) = outcode(B) = 0 Accept line segment

34 Using Outcodes CD: outcode (C) = 0, outcode(D)  0 Compute intersection Location of 1 in outcode(D) determines which edge to intersect with Note if there were a segment from A to a point in a region with 2 ones in outcode, we might have to do two interesections (see addition)

35 Using Outcodes EF: outcode(E) logically ANDed with outcode(F) (bitwise)  0 Both outcodes have a 1 bit in the same place Thus and is non-zero Line segment is outside of corresponding side of clipping window Reject

36 Using Outcodes GH and IJ: same outcodes, neither zero but logical AND yields zero – no easy reject Shorten line segment by intersecting with one of sides of window Compute outcode of intersection (new endpoint of shortened line segment) Reexecute algorithm

Cohen-Sutherland Algorithm

37 Cohen Sutherland Efficiency In many applications, the clipping window is small relative to the size of the entire data base Most line segments are outside one or more side of the window and can be eliminated based on their outcodes Inefficiency when code has to be reexecuted for line segments that must be shortened in more than one step

38 Cohen Sutherland in 3D Use 6-bit outcodes When needed, clip line segment against planes

39 Liang-Barsky Clipping Consider the parametric form of a line segment We can distinguish between the cases by looking at the ordering of the values of  where the line determined by the line segment crosses the lines that determine the window p(  ) = (1-  )p 1 +  p 2 1  0 p1p1 p2p2

40 Liang-Barsky Clipping In (a):  4 >  3 >  2 >  1 Intersect right, top, left, bottom: shorten In (b):  4 >  2 >  3 >  1 Intersect right, left, top, bottom: reject

41 Advantages Can accept/reject as easily as with Cohen- Sutherland Using values of , we do not have to use algorithm recursively as with C-S Extends to 3D

42 Clipping and Normalization General clipping in 3D requires intersection of line segments against arbitrary plane Example: oblique view

43 Plane-Line Intersections Note that any distance measure can be used e.g. Projection onto x axis

3 Rasterization Rasterization (scan conversion) Determine which pixels are inside primitive specified by a set of vertices Produces a set of fragments Fragments have a location (pixel location) and other attributes such color and texture coordinates that are determined by interpolating values at vertices Pixel colors determined later using color, texture, normal, and other vertex properties

4 Scan Conversion of Line Segments Start with line segment in window coordinates with integer values for endpoints Assume implementation has a write_pixel function y = mx + h

Draw a Line We start with the problem of drawing a line between two pixels Without loss, assume we have line with positive slope and p1 is on left of p2 We will derive Bresenham’s algorithm Derivation due to Sproull: Using Program Transformations to Derive Line-Drawing Algorithms

Goals We want to draw the right pixels (What are they?) We want to do it quickly We want to do it correctly We want to handle all cases

Take 1 float m = (y 2 -y 1 )/(x 2 -x 1 ); for (x = x 1 ; x <= x 2 ; x++) { y = m*(x - x 1 ) + y 1 draw_pixel(x, y) }

Speedup float m = (y 2 -y 1 )/(x 2 -x 1 ); y = y 1 for (x = x 1 ; x <= x 2 ; x++) draw_pixel(x, y) y = y + m Replace multiplication with addition

Problems Works well if delta y <= delta x Otherwise, we need to reverse the roles So the “right” points mean At least one in each row and column from start to finish We add assumption that delta y <= delta x for now

Problems float m = (y 2 -y 1 )/(x 2 -x 1 ); for (x = x 1 ; x <= x 2 ; x++) draw_pixel(x, y) y = y + m m is not an integer (But m is rational) Therefore values of y are not integral But we can only draw integral pixels

Draw Integeral Points m = (y 2 -y 1 )/(x 2 -x 1 ); y = y 1 for (x = x 1 ; x <= x 2 ; x++) draw_pixel(x, round(y)) y = y + m Since there is no round() in C, we must say draw_pixel(x, trunc(y + 1/2))

Error Term We want to minimize the error The best way to measure the error is the distance to the line, as measured by perpendicular Since we have fixed slope, this is proportional to the vertical distance We minimize by rounding (trunc(y+ 1/2))

9 Candidate Pixels 1  m  0 last pixel candidates Note that line could have passed through any part of this pixel

Examples Draw lines from (0, 0) to (8, n) for n = 0 through 8

Restate - move addition m = (y 2 -y 1 )/(x 2 -x 1 ); y = y 1 + 1/2 for (x = x 1 ; x <= x 2 ; x++) draw_pixel(x, trunc(y)) y = y + m x is an int, while m and y are real numbers But m is a rational number, so y is as well

Restate - move addition m = (y 2 -y 1 )/(x 2 -x 1 ); y = y 1 + 1/2 for (x = x 1 ; x <= x 2 ; x++) draw_pixel(x, trunc(y)) y = y + m We move addition of ½ outside loop to simplify draw_pixel(x, trunc(y + 1/2))

Split y m = (y 2 -y 1 )/(x 2 -x 1 ); y = y 1 + 1/2 Split y into two parts: integer and fraction y = y i + y f 0 <= y f < 1 y i = y 1 y f = 1/2 Goal: decide when y i increases

Restate with split y m = (y 2 -y 1 )/(x 2 -x 1 ); // 2/6 in our example y i = y 1; y f = 1/2 for (x = x 1 ; x <= x 2 ; x++) draw_pixel(x, y i ) if (y f + m) >= 1 y i = y i + 1; // Bump y y f = (y f + m) - 1; else y f = (y f + m) y f = y f + m x is an int: m and y f are rational numbers x yiyiyiyi yfyfyfyf yiyiyiyi yfyfyfyf 001/2 105/6 217/611/6 313/6

Introduce variable r Recall m = ∆y / ∆x = (y 2 -y 1 )/(x 2 -x 1 ) So (m * ∆x) is an integer – multiply by x To convert 1/2 to an integer, multiply by 2 Introduce new form of error term Let r = 2∆y + 2(y f - 1)∆x At start y f = 1/2, so at start r = 2∆y – 2(1/2 – 1)∆x = 2∆y + ∆x

Change in r r = 2∆y + 2(y f - 1)∆x // r is a measure of error Increasing y f by slope m is increasing r by 2∆y r = 2∆y + 2(y f + m - 1)∆x = = 2∆y + 2(y f - 1)∆x + 2(∆y/∆x) ∆x = 2∆y + 2(y f - 1)∆x + 2∆y Decreasing y f by 1 is decreasing r by 2∆x r = 2∆y + 2(y f )∆x = 2∆y + 2(y f - 1)∆x - 2∆x

Restate with r m = (y 2 -y 1 )/(x 2 -x 1 ); y i = y 1; r = 2(y 2 -y 1 ) - (x 2 -x 1 ) for (x = x 1 ; x <= x 2 ; x++) draw_pixel(x, y i ) if (r >= 0) y i = y i + 1; r = r + 2∆y - 2∆x else r = r + 2∆y x yiyiyiyir

Reconsider All the steps seem small: let’s review We made assumptions to simplify things Boiled down to the following case: As x moves from left to right, which y? We have two choices: old y or y+1 ? ?

Jaggies Our lines have visible steps We are sampling the line at too coarse a level We could make the dots smaller Effect remains We could use a "low pass filter" to smooth it out Low pass filter smears details together

20 Antialiasing by Area Averaging Color multiple pixels for each x depending on coverage by ideal line original antialiased magnified

Wu Antialiasing Xialin Wu - An Efficient Antialiasing Technique Assume we have a function with 0 ≤ m ≤ 1 Consider this example: we have sampled function f at three points

Wu Antialiasing Given the graph in the upper part We would digitize this with as below First point is rounded down to lattice Second point is rounded up to lattice This misrepresents the shape Had we moved the graph up or down, would have digitized differently

Wu Antialiasing Idea: Bracket point with 2 pixels North-South when m < 1 East-West when m > 1 Darkness is proportional to closeness to line

Wu Antialiasing Work through an example We are using the ceiling and floor functions

Example Work through an example (0, 0) to (5, 2) Compute values of f(x) at x = {0, 1, 2, 3, 4, 5}

Example Work through an example (0, 0) to (5, 2) Compute Intensity at x = {0, 1, 2, 3, 4, 5}

Example Work through an example (0, 0) to (5, 2) Compute Intensity at x = {0, 1, 2, 3, 4, 5} 1, [2/5, 3/5], [4/5, 1/5], [1/5, 4/5], [3/5, 2/5], 1

Algorithm draw_pixel(x 0,y 0 ); draw_pixel(x 1,y 1 ); // Ends D = 0; d = floor(m2 n + 0.5); x 0 = x 0 + 1; x 1 = x 1 – 1; while (x 0 < x 1 ) { D = D + d; if (overflow) y 0 = y 0 + 1; y 1 = y 1 – 1 I(x 0,y 0 ) := I(x 1,y 1 ) := D div 2 k-n I(x 0,y 0 +1) := I(x 1,y 1 -1) := 1 - I(x 0,y 0 ) }

Example Work through an example (0, 0) to (5, 2) 2/5 = …. 1, [2/5, 3/5], [4/5, 1/5], [1/5, 4/5], [3/5, 2/5], 1 xOverflowD Overflow ?

Example 2 Work through an example (0, 0) to (6, 2)

Line Width These two lines have the same amount of ink One is 40% longer Diagonal is less dense

19 Aliasing Ideal rasterized line should be 1 pixel wide Choosing best y for each x (or visa versa) produces aliased raster lines

Gupta-Sproull Wu does not address the issue of line width Gupta and Sproull provided a way to sample Earlier than Wu's algorithm Can deal with arbitrary line widths Provide lookup table to compute the samples quickly Not as fast as Wu’s algorithm Idea: a low pass filter

Gupta-Sproull Problem: Wu does not address the issue of line width Gupta and Sproull provide a way to sample Can deal with arbitrary line widths Provide a way to compute the samples quickly Sample using a cone with radius 1, volume 1

Filter: Signal strength is proportional to the volume below cone above the line Uses lookup table to speed up computation Use approximate distance to line

21 Polygon Aliasing Aliasing problems can be serious for polygons Jaggedness of edges Small polygons neglected Need compositing so that color of one polygon does not totally determine color of pixel All three polygons should contribute to color

Drawing Circles Bresenham addressed the problem of drawing circles and ellipses We deal with range shown Slope goes from 0 to -1 Generate other 7 octants at same time

Drawing Circles Decision Function D(x, y) = x 2 + y 2 - R 2 Initialize error term e = -D(x, y) At each step, we update x Do we pick y, or y-1?

Update Error Term New error term based on old D(x, y) = x 2 + y 2 - R 2 If we move to (x+1, y) D(x+1, y) = D(x, y) + 2x + 1 If the next point is (x+1, y-1) D(x+1, y-1) = D(x, y) + 2(x-y) + 2

Drawing Circles void Circle( int cx, int cy, int radius ) { int x = 0, y = radius, e = 1-radius; DrawPoint( x + cx, y + cy ); while (y > x) { if (e < 0) // Go “east” e += 2*x + 3; else // Go “south-east” e += 2*(x-y) + 5; y--; x++; DrawPoint( x + cx, y + cy ); }

12 Polygon Scan Conversion Scan Conversion = Fill How to tell inside from outside Convex easy Non-simple difficult Odd even test Count edge crossings Winding number odd-even fill

13 Winding Number winding number = 2 winding number = 1

13 Winding Number Count clockwise encirclements of point Alternate definition of inside: inside if winding number  0 winding number = 2 winding number = 1

13 Scan Lines Sort vertices by x value Interpolate between left and right endpoints of scan line Sometimes a scan line intersects an odd number Count max or min vertex twice – coming and going Count ordinary vertex once

18 Data Structure

13 Edges Sort vertices by y value (height) Keep list of current edges hitting this scan line Move to next scan line A new vertex forces us to add new edge and/or drop old edge Keep intersections sorted by x Interpolate across scan line

17 Scan Line Fill Can also fill by maintaining a data structure of all intersections of polygons with scan lines Sort by scan line Fill each span vertex order generated by vertex list desired order

14 Filling in the Frame Buffer Fill at end of pipeline Convex Polygons only Nonconvex polygons assumed to have been tessellated (see example on prior page for why) Shades (colors) have been computed for vertices (Gouraud shading) Combine with z-buffer algorithm March across scan lines interpolating shades Incremental work small

15 Using Interpolation span C1C1 C3C3 C2C2 C5C5 C4C4 scan line C 1 C 2 C 3 specified by glColor or by vertex shading C 4 determined by interpolating between C 1 and C 2 C 5 determined by interpolating between C 2 and C 3 interpolate between C 4 and C 5 along span

13 Summary Rasterizing is an important problem Thousands of vertices, millions of fragments Must be done quickly, and accurately We have seen a number of clever algorithms Focus on what we can do quickly Integer operations on simple data