Rasterization, or “What is glBegin(GL_LINES) really doing?” Course web page: February 23, 2012  Lecture 4.

Slides:



Advertisements
Similar presentations
CS 4731: Computer Graphics Lecture 20: Raster Graphics Part 1 Emmanuel Agu.
Advertisements

Section 3-1 to 3-2, 3-5 Drawing Lines Some of the material in these slides may have been adapted from university of Virginia, MIT, and Åbo Akademi University.
CS 376 Introduction to Computer Graphics 02 / 02 / 2007 Instructor: Michael Eckmann.
Computer Graphics 4: Bresenham Line Drawing Algorithm, Circle Drawing & Polygon Filling By:Kanwarjeet Singh.
CS 450: COMPUTER GRAPHICS REVIEW: DRAWING LINES AND CIRCLES SPRING 2015 DR. MICHAEL J. REALE.
+ CPCS 391 Computer Graphics 1 Instructor: Dr. Sahar Shabanah Lecture 3.
Line Drawing Algorithms. Rasterization-Process of determining which pixels give the best approximation to a desired line on the screen. Scan Conversion-Digitizing.
CS 450: COMPUTER GRAPHICS FILLING POLYGONS SPRING 2015 DR. MICHAEL J. REALE.
Komputer Grafik 2 (AK045206) Scan Conversion 1/31 Scan Conversion.
Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters  Hill, Chapter 10.
CS5500 Computer Graphics © Chun-Fa Chang, Spring 2007 CS5500 Computer Graphics May 3, 2007.
Implementation III Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts University of New Mexico.
C O M P U T E R G R A P H I C S Guoying Zhao 1 / 50 C O M P U T E R G R A P H I C S Guoying Zhao 1 / 45 Computer Graphics Implementation I.
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)
CS 450: COMPUTER GRAPHICS DRAWING LINES AND CIRCLES SPRING 2015 DR. MICHAEL J. REALE.
1/1/20001 Topic >>>> Scan Conversion CSE Computer Graphics.
College of Computer and Information Science, Northeastern UniversitySeptember 12, CS U540 Computer Graphics Prof. Harriet Fell Spring 2009 Lecture.
WHERE TO DRAW A LINE?? Line drawing is accomplished by calculating intermediate positions along the line path between specified end points. Precise definition.
Scan Conversion Line and Circle
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.
Informationsteknologi Monday, November 26, 2007Computer Graphics - Class 121 Today’s class Drawing lines Bresenham’s algorithm Compositing Polygon filling.
Triangle Scan Conversion. 2 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 Rasterization Rasterization (scan conversion) –Determine which.
College of Computer and Information Science, Northeastern UniversityOctober 12, CS G140 Graduate Computer Graphics Prof. Harriet Fell Spring 2006.
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.
Graphics Output Primitives
 A line segment in a scene is defined by the coordinate positions of the line end-points x y (2, 2) (7, 5)
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
Computer Graphics Drawing Line. Lines and Polylines Convex: For every pair of points in the polygon, the line between them is fully contained in the polygon.
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.
CS 325 Introduction to Computer Graphics 02 / 03 / 2010 Instructor: Michael Eckmann.
1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai.
GEOMETRY AND LINE GENERATION Geometry and Line Generation Chapter 2.
Computer Graphics Filling. Filling Polygons So we can figure out how to draw lines and circles How do we go about drawing polygons? We use an incremental.
Scan Conversion.
Rasterization Overview Raster Display Device. Scan Conversion / Rasterization: converting vector graphics into raster graphics (determining pixels in.
Computer Graphics Lecture 08 Taqdees A. Siddiqi Computer Graphics Filled Area Primitives I Lecture 08 Taqdees A. Siddiqi
Computer Graphics : Bresenham Line Drawing Algorithm, Circle Drawing
Computer Graphics CC416 Lecture 04: Bresenham Line Algorithm & Mid-point circle algorithm Dr. Manal Helal – Fall 2014.
Computer Graphics I, Fall 2010 Scan conversion algorithms.
Line Drawing Algorithms 1. A line in Computer graphics is a portion of straight line that extends indefinitely in opposite direction. 2. It is defined.
Objectives Understand Bresenhams line drawing algorithm. Apply the algorithm to plot a line with the end points specified.
Primitive graphic objects
CSCE 441 Lecture 2: Scan Conversion of Lines
Computer Graphics Filling.
CS G140 Graduate Computer Graphics
(c) 2002 University of Wisconsin, CS559
CS 4731: Computer Graphics Lecture 20: Raster Graphics Part 1
University of New Mexico
Implementation III.
Computer Graphics Implementation I.
Prof. Harriet Fell Spring 2007 Lecture 5 – January 17, 2006
Introduction to Computer Graphics with WebGL
CSCE 441 Lecture 2: Scan Conversion of Lines
© University of Wisconsin, CS559 Fall 2004
2D Scan-line Conversion
Rasterizing Lines 1 Lecture 32 Mon, Nov 12, 2007.
Rasterization and Antialiasing
Computer Graphics Implementation III
Rasterization and Antialiasing
Edited from The Rasterization Problem: Idealized Primitives Map to Discrete Display Space Edited from.
Rasterizing Polygons 2 Lecture 35 Mon, Nov 26, 2007.
Chapter 3 Graphics Output Primitives
Rasterizing Polygons Lecture 29 Wed, Dec 7, 2005.
Where We Stand At this point we know how to: Next thing:
Implementation III Ed Angel Professor Emeritus of Computer Science
Presentation transcript:

Rasterization, or “What is glBegin(GL_LINES) really doing?” Course web page: February 23, 2012  Lecture 4

Outline Rasterizing lines –DDA/parametric algorithm –Midpoint/Bresenham’s algorithm Rasterizing polygons/triangles

Rasterization: What is it? How to go from floating-point coords of geometric primitives’ vertices to integer coordinates of pixels on screen Geometric primitives –Points: Round vertex location in screen coordinates –Lines: Can do this for endpoints, but what about in between? –Polygons: How to fill area bounded by edges? from Angel

Rasterizing Lines: Goals Draw pixels as close to the ideal line as possible Use the minimum number of pixels without leaving any gaps Do it efficiently Extras –Handle different line styles Width Stippling (dotted and dashed lines) Join styles for connected lines –Minimize aliasing (“jaggies”)

DDA/Parametric Line Drawing DDA stands for Digital Differential Analyzer, the name of a class of old machines used for plotting functions Slope-intercept form of a line: y = mx + b –m = dy/dx – b is where the line intersects the Y axis DDA’s basic idea: If we increment the x coordinate by 1 pixel at each step, the slope of the line tells us how to much increment y per step –I.e., m = dy/dx, so for dx = 1, dy = m –This only works if m <= 1 —otherwise there are gaps Solution: Reverse axes and step in Y direction. Since now dy = 1, we get dx = 1/m from Angel

DDA stands for Digital Differential Analyzer, the name of a class of old machines used for plotting functions Slope-intercept form of a line: y = mx + b –m = dy/dx – b is where the line intersects the Y axis DDA’s basic idea: If we increment the x coordinate by 1 pixel at each step, the slope of the line tells us how to much increment y per step –I.e., m = dy/dx, so for dx = 1, dy = m –This only works if m <= 1 —otherwise there are gaps Solution: Reverse axes and step in Y direction. Since now dy = 1, we get dx = 1/m from Angel DDA/Parametric Line Drawing

DDA: Algorithm 1.Given endpoints (x 0, y 0 ), (x 1, y 1 ) Integer coordinates: Round if endpoints were originally real-valued Assume (x 0, y 0 ) is to the left of (x 1, y 1 ) : Swap if they aren’t 2.Then we can compute slope: m = dy/dx = (y 1 - y 0 ) / (x 1 - x 0 ) 3.Iterate If |m| <= 1 : Iterate integer x from x 0 to x 1, incrementing by 1 each step —Initialize real y = y 0 —At each step, y += m, and plot point (x, round(y)) Else |m| > 1 : Iterate integer y from y 0 to y 1, incrementing (or decrementing) by 1 —Initialize real x = x 0 —At each step, x += 1/m, and plot (round(x), y)

Midpoint/Bresenham’s line drawing DDA is somewhat slow –Floating-point calculations, rounding are relatively expensive Big idea: Avoid rounding, do everything with integer arithmetic for speed Assume slope between 0 and 1 –Again, handle lines with other slopes by using symmetry

Midpoint line drawing: Line equation Recall that the slope-intercept form of the line is y = (dy/dx)x + b Multiplying through by dx, we can rearrange this in implicit form: F(x, y) = dy x - dx y + dx b = 0 F is: –Zero for points on the line –Positive for points below the line (to (right if slope > 1) –Negative for points above the line (left if slope > 1) Examples: (0, 1), (1, 0), etc. from Angel 2x - 9y = 0 9x - 2y = 0

Midpoint line drawing: The Decision Given our assumptions about the slope, after drawing (x, y) the only choice for the next pixel is between the upper pixel U = (x+1, y+1) and the lower one L = (x+1, y) We want to draw the one closest to the line from Hill U L

Midpoint line drawing: Midpoint decision After drawing (x, y), in order to choose the next pixel to draw we consider the midpoint M = (x+1, y+0.5) –If it’s on the line, then U and L are equidistant from the line –If it’s below the line, pixel U is closer to the line than pixel L –If it’s above the line, then L is closer than U from Hill M

Midpoint line drawing: Midpoint decision So F is a decision function about which pixel to draw: –If F(M) = F(x+1, y+0.5) > 0 ( M below the line), pick U –If F(M) = F(x+1, y+0.5) <= 0 ( M above or on line), pick L from Hill

Midpoint line drawing: Implementation Key efficiency insight: F does not have to be fully evaluated every step Suppose we do the full evaluation once and get F(x+1, y+0.5) –If we choose L, next midpoint to evaluate M ' is at F(x+2, y+0.5) –If we choose U, next midpoint M '' would be at F(x+2, y+1.5) from Hill

Midpoint line drawing: Implementation from Hill Key efficiency insight: F does not have to be fully evaluated every step Suppose we do the full evaluation once and get F(x+1, y+0.5) –If we choose L, next midpoint to evaluate M ' is at F(x+2, y+0.5) –If we choose U, next midpoint M '' would be at F(x+2, y+1.5)

Midpoint line drawing: Implementation from Hill Key efficiency insight: F does not have to be fully evaluated every step Suppose we do the full evaluation once and get F(x+1, y+0.5) –If we choose L, next midpoint to evaluate M ' is at F(x+2, y+0.5) –If we choose U, next midpoint M '' would be at F(x+2, y+1.5)

Midpoint line drawing: Implementation Expanding these out using F(x, y) = dy x – dx y + dx b : – F M = F(x + 1, y + 0.5) = dy(x + 1) - dx(y + 0.5) + dx b – F M ' = F(x + 2, y + 0.5) = dy(x + 2) - dx(y + 0.5) + dx b – F M '' = F(x + 2, y + 1.5) = dy(x + 2) - dx(y + 1.5) + dx b Note that F M ' - F M = dy and F M '' - F M = dy - dx So depending on whether we choose L or U, we just have to add dy or dy - dx, respectively, to the old value of F in order to get the new value from Hill

Midpoint line drawing: Algorithm To initialize, we do a full calculation of F at the first midpoint next to the left line endpoint: F(x 0 + 1, y ) = dy(x 0 + 1) - dx(y ) + dx b = dy x 0 - dx y 0 + dx b + dy dx = F(x 0, y 0 ) + dy dx –But F(x 0, y 0 ) = 0 since it’s on the line, so our first F = dy dx –Only the sign matters for the decision, so to make it an integer value we multiply by 2 to get 2F = 2 dy - dx To update, keep current values for x and y and a running total for F : –When L is chosen: F += 2dy and x ++ –When U is chosen: F += 2(dy - dx) and x ++, y ++

Line drawing speed 100,000 random lines in 500 x 500 window (average of 5 runs) DDA: 6.8 seconds Midpoint: 2.5 seconds OpenGL using GL_LINES (in software): 1.6 seconds

Extensions How to draw thick (>1 pixel wide) lines? –Nested Bresenham’s (perpendicular to main line at each step, or series of parallel lines) Stippled/dashed lines –Add state (pen up/down) inside loop

Polygon Rasterization Given a set of vertices, want to fill the interior Basic procedure: –Iterate over scan lines between top and bottom vertex –For each scan line, find all intersections with polygon edges –Sort intersections by x -value and fill pixel runs between odd- even pairs of intersections from Hill

Polygon Rasterization: Notes Refinements –Maintain correct parity by discarding intersections with upper endpoint of any edge –Avoid double-drawing at border of abutting polygons (bad if blending/ XOR’ing): Left/bottom ownership Draw pixel on left edge of run but not right Discard intersections with any horizontal edges Efficiency –Avoid checking intersections with all poly edges for each scanline by keeping an active edge list from Hill

Rasterizing triangles Special case of polygon rasterization –Exactly two active edges at all times One method: –Fill scanline table between top and bottom vertex with leftmost and rightmost side by using DDA or midpoint algorithm to follow edges –Traverse table scanline by scanline, fill run from left to right