CP411 polygon Lecture 6 1. Scan conversion algorithm for circle 2. Draw polygons 3. Antialiasing.

Slides:



Advertisements
Similar presentations
Polygon Scan Conversion – 11b
Advertisements

Computer Graphics Lecture 3 Modeling and Structures.
Computer Graphics Inf4/MSc 1 Computer Graphics Lecture 7 Scanline algorithm and polygon clipping Taku Komura.
Compositing and Blending Ed Angel Professor Emeritus of Computer Science University of New Mexico 1 E. Angel and D. Shreiner: Interactive Computer Graphics.
COMP 175 | COMPUTER GRAPHICS Remco Chang1/6103b – Shapes Lecture 03b: Shapes COMP 175: Computer Graphics February 3, 2015.
CS 4731: Computer Graphics Lecture 20: Raster Graphics Part 1 Emmanuel Agu.
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.
CHAPTER 3 2D GRAPHICS ALGORITHMS
OUTPUT PRIMITIVES Screen vs. World coordinate systems ● Objects positions are specified in a Cartesian coordinate system called World Coordinate.
CMPE 466 COMPUTER GRAPHICS
CS 450: COMPUTER GRAPHICS FILLING POLYGONS SPRING 2015 DR. MICHAEL J. REALE.
CS 352: Computer Graphics Chapter 7: The Rendering Pipeline.
2 COEN Computer Graphics I Evening’s Goals n Discuss the fundamentals of lighting in computer graphics n Analyze OpenGL’s lighting model n Show.
CS 4363/6353 BASIC RENDERING. THE GRAPHICS PIPELINE OVERVIEW Vertex Processing Coordinate transformations Compute color for each vertex Clipping and Primitive.
CHAPTER 12 Height Maps, Hidden Surface Removal, Clipping and Level of Detail Algorithms © 2008 Cengage Learning EMEA.
CPCS 391 Computer Graphics 1 Lecture 5: Polygon Filling
CS 376 Introduction to Computer Graphics 02 / 05 / 2007 Instructor: Michael Eckmann.
Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters  Hill, Chapter 10.
University of British Columbia CPSC 414 Computer Graphics © Tamara Munzner 1 Scan Conversion Week 6, Mon 6 Oct 2003 recap: Bresenham line algorithm scan.
CS 4731: Computer Graphics Lecture 22: Raster Graphics Part 3 Emmanuel Agu.
Part I: Basics of Computer Graphics Rendering Polygonal Objects (Read Chapter 1 of Advanced Animation and Rendering Techniques) Chapter
TOPIC 4 PART III FILL AREA PRIMITIVES POLYGON FILL AREAS CGMB214: Introduction to Computer Graphics.
March Shape Drawing Algorithms Shmuel Wimer Bar Ilan Univ., Engineering Faculty.
CGMB 314 Intro to Computer Graphics Fill Area Primitives.
Polygon Scan Conversion and Z-Buffering
Basics of Rendering Pipeline Based Rendering –Objects in the scene are rendered in a sequence of steps that form the Rendering Pipeline. Ray-Tracing –A.
CS 551 / 645: Introductory Computer Graphics
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.
Informationsteknologi Monday, November 26, 2007Computer Graphics - Class 121 Today’s class Drawing lines Bresenham’s algorithm Compositing Polygon filling.
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.
Part 6: Graphics Output Primitives (4) 1.  Another useful construct,besides points, straight line segments, and curves for describing components of a.
Graphics Pipeline Rasterization CMSC 435/634. Drawing Terms Primitive – Basic shape, drawn directly – Compare to building from simpler shapes Rasterization.
10/26/04© University of Wisconsin, CS559 Fall 2004 Last Time Drawing lines Polygon fill rules Midterm Oct 28.
10/15/02 (c) 2002 University of Wisconsin, CS559 Last Time Clipping.
2D Output Primitives Points Lines Circles Ellipses Other curves Filling areas Text Patterns Polymarkers.
CAP4730: Computational Structures in Computer Graphics
Drawing Triangles CS 445/645 Introduction to Computer Graphics David Luebke, Spring 2003.
Computer Graphics Rendering 2D Geometry CO2409 Computer Graphics Week 2.
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.
Attributes of Graphics Primitives Hearn & Baker Chapter 4 Some slides are taken from Robert Thomsons notes.
University of British Columbia CPSC 314 Computer Graphics Jan-Apr 2013 Tamara Munzner Rasterization.
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.
2D Output Primitives Points Lines Circles Ellipses Other curves Filling areas Text Patterns Polymarkers.
Lecture 15: Raster Graphics and Scan Conversion
University of British Columbia CPSC 314 Computer Graphics Jan-Apr 2010 Tamara Munzner Rasterization II Week.
CS552: Computer Graphics Lecture 17: Scan Conversion (Special Cases)
Computer Graphics Lecture 08 Taqdees A. Siddiqi Computer Graphics Filled Area Primitives I Lecture 08 Taqdees A. Siddiqi
Rasterization, or “What is glBegin(GL_LINES) really doing?” Course web page: February 23, 2012  Lecture 4.
OUTPUT PRIMITIVES CEng 477 Computer Graphics METU, 2004.
Attributes of Graphics Primitives Hearn & Baker Chapter 4
Computer Graphics Filling.
Introduction to Polygons
Polygons.
(c) 2002 University of Wisconsin, CS559
Computer Graphics Filled Area Primitives II Lecture 09 Taqdees A
Agenda Polygon Terminology Types of polygons Inside Test
CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai
Prepared by Narendra V G CSE MIT
Agenda Polygon Terminology Types of polygons Inside Test
Lecture 13 Clipping & Scan Conversion
Rasterization Week 6, Mon Feb 12
Primitive Drawing Algorithm
Primitive Drawing Algorithm
Polygons.
Presentation transcript:

CP411 polygon Lecture 6 1. Scan conversion algorithm for circle 2. Draw polygons 3. Antialiasing

CP411 Computer Graphics 2 Draw a circle_ straight algorithm #include const float DEG2RAD = /180; void drawCircle(float radius, float x, float y) { glBegin(GL_LINE_LOOP); for (int i=0; i < 360; i++) { float degInRad = i*DEG2RAD; glVertex2f(x+cos(degInRad)*radius, y+sin(degInRad)*radius); } glEnd }

Midpoint point algorithm ● See class note CP411 polygon

Polygon ● What is a polygon? ■ A geometric shape bounded by a closed polyline formed by a sequence of vertices. ● Why choose polygon as a primitive ? ■ In interactive graphics, polygons rule the CG world ■ Two main reasons: ○ Can represent any surface with arbitrary accuracy  Splines, mathematical functions, etc. ○ Mathematical simplicity lends itself to simple, regular rendering algorithms  Such algorithms embed well in hardware

CP411 polygon Convex / Concave Polygons ● A two-dimensional shape is convex if and only if every line segment connecting two points on the boundary is entirely contained ● OpenGL can only draw convex polygon ■ How to recognize a convex polygon ?

CP411 polygon OpenGL polygon command ● glBegin (GL_POLYGON) specify vertices of the polygon glend ● glPolygonMode( GLenum face, GLenum mode ) controls the interpretation of polygons for rasterization ■ face Specifies the polygons that mode applies to. Must be GL_FRONT for front-facing polygons, GL_BACK for back-facing polygons, or GL_FRONT_AND_BACK for front- and back-facing polygons. ■ mode Specifies the way polygons will be rasterized. Accepted values are GL_POINT, GL_LINE, and GL_FILL. The default is GL_FILL for both front- and back-facing polygons.

CP411 polygon Rasterizing Polygons ● Triangle is the minimal unit of a polygon ■ All polygons can be broken up into triangles ○ Convex, concave, complex ■ Triangles are guaranteed to be: ○ Planar ○ Convex ● We often draw polygons by breaking them into triangles

CP411 polygon Triangles ● Two basic methods for drawing triangles: ■ Edge walking ○ Use a line drawing algorithm to compute edges (edge setup) ○ Fill in pixels between edges ○ Finicky, lots of special cases ○ Touches only pixels involved in triangle ■ Edge equations

CP411 polygon Edge Walking ● Basic idea: ■ Draw edges vertically ■ Fill in horizontal spans for each scanline ■ Interpolate colors down edges ■ At each scanline, interpolate edge colors across span

CP411 polygon Edge Walking: Notes ● Order vertices in x and y ■ 3 cases: break left, break right, no break ● Walk down left and right edges ■ Fill each span ■ Until breakpoint or bottom vertex is reached ● Advantage: can be made very fast ● Disadvantages: ■ Lots of finicky special cases ■ Need to pay attention to fractional offsets

CP411 polygon Edge Walking: Notes ● Fractional offsets: ● Also: beware gaps between adjacent edges

CP411 polygon Edge Equations ● An edge equation is simply the equation of the line containing that edge ■ What is the equation of a 2D line? ■ Ax + By + C = 0 ■ Given a point (x,y), what does plugging x & y into this equation tell us? ■ Whether the point is: ○ On the line: Ax + By + C = 0 ○ “Above” the line: Ax + By + C > 0 ○ “Below” the line: Ax + By + C < 0

Edge Equations ● Edge equations thus define two half-spaces: CP411 polygon

Edge Equations ● And a triangle can be defined as the intersection of three positive half-spaces: A 1 x + B 1 y + C 1 < 0 A 2 x + B 2 y + C 2 < 0 A 3 x + B 3 y + C 3 < 0 A 1 x + B 1 y + C 1 > 0 A 3 x + B 3 y + C 3 > 0 A 2 x + B 2 y + C 2 > 0 CP411 polygon

Edge Equations ● So…simply turn on those pixels for which all edge equations evaluate to > 0: CP411 polygon

Using Edge Equations ● Which pixels: compute min,max bounding box ● Edge equations: compute from vertices ● Orientation: ensure area is positive (why?) CP411 polygon

Computing Edge Equations ● Want to calculate A, B, C for each edge from (x i, y i ) and (x j, y j ) ● Treat it as a linear system: Ax 1 + By 1 + C = 0 Ax 2 + By 2 + C = 0 ● Notice: two equations, three unknowns ● Does this make sense? What can we solve? ● Goal: solve for A & B in terms of C CP411 polygon

Computing Edge Equations ● Set up the linear system: ● Multiply both sides by matrix inverse: ● Let C = x 0 y 1 - x 1 y 0 for convenience ■ Then A = y 0 - y 1 and B = x 1 - x 0 CP411 polygon

Computing Edge Equations: Numerical Issues ● We can avoid the problem in this case by using our definitions of A and B: A = y 0 - y 1 B = x 1 - x 0 C = x 0 y 1 - x 1 y 0 Thus: C = -Ax 0 - By 0 or C = -Ax 1 - By 1 CP411 polygon

Edge Equations ● So…we can find edge equation from two verts. ● Given three corners C 0, C 1, C 0 of a triangle, what are our three edges? ● How do we make sure the half-spaces defined by the edge equations all share the same sign on the interior of the triangle? ● Be consistent (Ex: [C 0 C 1 ], [C 1 C 2 ], [C 2 C 0 ]) ● How do we make sure that sign is positive? ● A: Test, and flip if needed (A= -A, B= -B, C= -C) CP411 polygon

Edge Equations: Code ● Basic structure of code: ■ Setup: compute edge equations, bounding box ■ (Outer loop) For each scanline in bounding box... ■ (Inner loop) …check each pixel on scanline, evaluating edge equations and drawing the pixel if all three are positive CP411 polygon

findBoundingBox(&xmin, &xmax, &ymin, &ymax); setupEdges (&a0,&b0,&c0,&a1,&b1,&c1,&a2,&b2,&c2); /* Optimize this: */ for (int y = yMin; y <= yMax; y++) { for (int x = xMin; x <= xMax; x++) { float e0 = a0*x + b0*y + c0; float e1 = a1*x + b1*y + c1; float e2 = a2*x + b2*y + c2; if (e0 > 0 && e1 > 0 && e2 > 0) setPixel(x,y); } CP411 polygon

General Polygon Rasterization ● Consider the following polygon: ● How do we know whether a given pixel on the scanline is inside or outside the polygon? A B C D E F CP411 polygon

General Polygon Rasterization ● Does it still work? A B C D E F G I H CP411 polygon

General Polygon Rasterization ● Basic idea: use a parity test for each scanline edgeCnt = 0; for each pixel on scanline (l to r) if (oldpixel->newpixel crosses edge) edgeCnt ++; // draw the pixel if edgeCnt odd if (edgeCnt % 2) setPixel(pixel); CP411 polygon

Faster Polygon Rasterization ● How can we optimize the code? for each scanline edgeCnt = 0; for each pixel on scanline (l to r) if (oldpixel->newpixel crosses edge) edgeCnt ++; // draw the pixel if edgeCnt odd if (edgeCnt % 2) setPixel(pixel); ● Big cost: testing pixels against each edge ● Solution: active edge table (AET) CP411 polygon

Active Edge Table ● Idea: ■ Edges intersecting a given scanline are likely to intersect the next scanline ■ Within a scanline, the order of edge intersections doesn’t change much from scanline to scanline CP411 polygon

Active Edge Table ● Algorithm: ■ Sort all edges by their minimum y coord ■ Starting at bottom, add edges with Y min = 0 to AET ■ For each scanline: ○ Sort edges in AET by x intersection ○ Walk from left to right, setting pixels by parity rule ○ Increment scanline ○ Retire edges with Y max < Y ○ Add edges with Y min > Y ○ Recalculate edge intersections and resort (how?) ■ Stop when Y > Y max for last edges CP411 polygon

Edge Equations: Speed Hacks ● Some speed hacks for the inner loop: int xflag = 0; for (int x = xMin; x <= xMax; x++) { if (e0|e1|e2 > 0) { setPixel(x,y); xflag++; } else if (xflag != 0) break; e0 += a0; e1 += a1; e2 += a2; } ■ Incremental update of edge equation values ■ Early termination (why does this work?) ■ Faster test of equation values CP411 polygon

Antialiasing ● What is aliasing? The jaggies appeared in the drawings of lines are called aliasing. It is due to the approximation of a continuous line with discrete pixels. ● Aliasing effects: ■ Distant objects may disappear entirely ■ Objects can blink on and off in animations ● Antialiasing techniques involve some form of blurring to reduce contrast, smoothen image ● Three antialiasing techniques: ■ Prefiltering ■ Postfiltering ■ Supersampling CP411 polygon

Prefiltering ● Basic idea: ■ compute area of polygon coverage ■ use proportional intensity value ● Example: if polygon covers ¼ of the pixel ■ use ¼ polygon color ■ add it to ¾ of adjacent region color ● Cons: computing pixel coverage can be time consuming CP411 polygon

Prefiltering ● Conceive that a line is 1 pixel wide which covers certain pixel squares. ● A simple drawing method sets the pixels in C, G, H and L to the current color. ● Suppose that the overlapping proportion of a certain pixel is , the new pixel_color is then set to   current_color + (1-  )  existing_color 

CP411 polygon glEnable(GL_BLEND); glEnable(GL_LINE_SMOOTH); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glHint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE); Without antialiasing With antialiasing

Supersampling ● Useful if we can compute color of any (x,y) value on the screen ■ Increase frequency of sampling ■ Instead of (x,y) samples in increments of 1 ■ Sample (x,y) in fractional (e.g. ½) increments ■ Find average of samples ● Example: Double sampling = increments of ½ = 9 color values averaged for each pixe CP411 polygon

Postfiltering ● Supersampling uses average ■ Gives all samples equal importance ● Post-filtering: use weighting (different levels of importance) ● Compute pixel value as weighted average ■ Samples close to pixel center given more weight CP411 polygon