CS 4731: Computer Graphics Lecture 21: Raster Graphics: Drawing Lines. Emmanuel Agu

Similar documents
Computer Graphics (CS 543) Lecture 10: Rasterization and Antialiasing

CS 543: Computer Graphics. Rasterization

? Which intermediate. Recall: Line drawing algorithm. Programmer specifies (x,y) of end pixels Need algorithm to determine pixels on line path

Painter s HSR Algorithm

Rasterization: Geometric Primitives

Rasterization, or What is glbegin(gl_lines) really doing?

From Ver(ces to Fragments: Rasteriza(on

OpenGL Graphics System. 2D Graphics Primitives. Drawing 2D Graphics Primitives. 2D Graphics Primitives. Mathematical 2D Primitives.

1 Introduction to Graphics

From Vertices to Fragments: Rasterization. Reading Assignment: Chapter 7. Special memory where pixel colors are stored.

Rendering. A simple X program to illustrate rendering

Chapter - 2: Geometry and Line Generations

Line Drawing Week 6, Lecture 9

Rendering. A simple X program to illustrate rendering

CS 450: COMPUTER GRAPHICS REVIEW: DRAWING LINES AND CIRCLES SPRING 2015 DR. MICHAEL J. REALE

Scan Converting Circles

Rasterization. CS4620/5620: Lecture 12. Announcements. Turn in HW 1. PPA 1 out. Friday lecture. History of graphics PPA 1 in 4621.

Einführung in Visual Computing

CS Rasterization. Junqiao Zhao 赵君峤

Chapter 8: Implementation- Clipping and Rasterization

Line Drawing. Foundations of Computer Graphics Torsten Möller

Computer Graphics. - Rasterization - Philipp Slusallek

Tópicos de Computação Gráfica Topics in Computer Graphics 10509: Doutoramento em Engenharia Informática. Chap. 2 Rasterization.

Raster Displays and Scan Conversion. Computer Graphics, CSCD18 Fall 2008 Instructor: Leonid Sigal

Scan Converting Lines

Line Drawing. Introduction to Computer Graphics Torsten Möller / Mike Phillips. Machiraju/Zhang/Möller

Rasterization. CS 4620 Lecture Kavita Bala w/ prior instructor Steve Marschner. Cornell CS4620 Fall 2015 Lecture 16

Rasterization. CS4620 Lecture 13

Scan Conversion. Drawing Lines Drawing Circles

Computer Graphics: Graphics Output Primitives Line Drawing Algorithms

COMP371 COMPUTER GRAPHICS

Pipeline implementation II

Digital Differential Analyzer Bresenhams Line Drawing Algorithm

The graphics pipeline. Pipeline and Rasterization. Primitives. Pipeline

Rendering approaches. 1.image-oriented. 2.object-oriented. foreach pixel... 3D rendering pipeline. foreach object...

Output Primitives Lecture: 3. Lecture 3. Output Primitives. Assuming we have a raster display, a picture is completely specified by:

Output Primitives. Dr. S.M. Malaek. Assistant: M. Younesi

EF432. Introduction to spagetti and meatballs

CS 130 Final. Fall 2015

Incremental Form. Idea. More efficient if we look at d k, the value of the decision variable at x = k

Efficient Plotting Algorithm

Implementation III. Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts University of New Mexico

Surface shading: lights and rasterization. Computer Graphics CSE 167 Lecture 6

Overview of Computer Graphics

Topic #1: Rasterization (Scan Conversion)

Rasterization. COMP 575/770 Spring 2013

Pipeline and Rasterization. COMP770 Fall 2011

Scan Conversion. CMP 477 Computer Graphics S. A. Arekete

Scan Conversion. Lines and Circles

CS 130. Scan Conversion. Raster Graphics

Computer Graphics D Graphics Algorithms

Graphics (Output) Primitives. Chapters 3 & 4

CS4620/5620: Lecture 14 Pipeline

EF432. Introduction to spagetti and meatballs

UNIT -8 IMPLEMENTATION

CS488 2D Graphics. Luc RENAMBOT

Clipping and Scan Conversion

Announcements. Midterms graded back at the end of class Help session on Assignment 3 for last ~20 minutes of class. Computer Graphics

Fall CSCI 420: Computer Graphics. 7.1 Rasterization. Hao Li.

Last class. A vertex (w x, w y, w z, w) - clipping is in the - windowing and viewport normalized view volume if: - scan conversion/ rasterization

Points and lines. x x 1 + y 1. y = mx + b

Computer Graphics. Lecture 2. Doç. Dr. Mehmet Gokturk

FROM VERTICES TO FRAGMENTS. Lecture 5 Comp3080 Computer Graphics HKBU

Rendering. Basic Math Review. Rasterizing Lines and Polygons Hidden Surface Remove Multi-pass Rendering with Accumulation Buffers.

2D Image Synthesis. 2D image synthesis. Raster graphics systems. Modeling transformation. Vectorization. u x u y 0. o x o y 1

Lecture 6 of 41. Scan Conversion 1 of 2: Midpoint Algorithm for Lines and Ellipses

Lecture 6 of 41. Scan Conversion 1 of 2: Midpoint Algorithm for Lines and Ellipses

RASTERIZING POLYGONS IN IMAGE SPACE

Topic 0. Introduction: What Is Computer Graphics? CSC 418/2504: Computer Graphics EF432. Today s Topics. What is Computer Graphics?

Realtime 3D Computer Graphics Virtual Reality

Computer Graphics Prof. Sukhendu Das Dept. of Computer Science and Engineering Indian Institute of Technology, Madras Lecture - 14

CSCI 420 Computer Graphics Lecture 14. Rasterization. Scan Conversion Antialiasing [Angel Ch. 6] Jernej Barbic University of Southern California

0. Introduction: What is Computer Graphics? 1. Basics of scan conversion (line drawing) 2. Representing 2D curves

Raster Scan Displays. Framebuffer (Black and White)

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Computer Graphics

COMP30019 Graphics and Interaction Scan Converting Polygons and Lines

Rasterization. Rasterization (scan conversion) Digital Differential Analyzer (DDA) Rasterizing a line. Digital Differential Analyzer (DDA)

Department of Computer Sciences Graphics Fall 2003 (Lecture 2) Pixels

Computer Graphics Lecture Notes

Renderer Implementation: Basics and Clipping. Overview. Preliminaries. David Carr Virtual Environments, Fundamentals Spring 2005

UNIT 2 GRAPHIC PRIMITIVES

An Improved Algorithm for Scan-converting a Line

CPSC / Scan Conversion

From 3D World to 2D Screen. Hendrik Speleers

Introduction to Computer Graphics (CS602) Lecture 05 Line Drawing Techniques

CS452/552; EE465/505. Clipping & Scan Conversion

Display Technologies: CRTs Raster Displays

CS 130 Exam I. Fall 2015

CS 450: COMPUTER GRAPHICS RASTERIZING LINES SPRING 2016 DR. MICHAEL J. REALE

Unit 2 Output Primitives and their Attributes

MODULE - 4. e-pg Pathshala

CS 4300 Computer Graphics. Prof. Harriet Fell Fall 2012 Lecture 5 September 13, 2012

I Internal Examination (Model Paper) B.Tech III Year VI semester, Computer Science & Engineering

Computer Graphics D Graphics Algorithms

graphics pipeline computer graphics graphics pipeline 2009 fabio pellacini 1

CS 4731/543: Computer Graphics Lecture 7 (Part I): Raster Graphics: Polygons & Antialiasing. Emmanuel Agu

graphics pipeline computer graphics graphics pipeline 2009 fabio pellacini 1

CSCI 4620/8626. Coordinate Reference Frames

Graphics System. Processor. Output Display. Input Devices. Frame Buffer. Memory. Array of pixels. Resolution: # of pixels Depth: # of bits/pixel

Shading Techniques Denbigh Starkey

Transcription:

CS 4731: Computer Graphics Lecture 21: Raster Graphics: Drawing Lines Emmanuel Agu

2D Graphics Pipeline Clipping Object World Coordinates Applying world window Object subset window to viewport mapping Simple 2D Drawing Pipeline Display Rasterization Object Screen coordinates

Rasterization (Scan Conversion) Convert high-level geometry description to pixel colors in the frame buffer Example: given vertex x,y coordinates determine pixel colors to draw line Two ways to create an image: Scan existing photograph Procedurally compute values (rendering) Viewport Transformation Rasterization

Rasterization A fundamental computer graphics function Determine the pixels colors, illuminations, textures, etc. Implemented by graphics hardware Rasterization algorithms Lines Circles Triangles Polygons

Rasterization Operations Drawing lines on the screen Manipulating pixel maps (pixmaps): copying, scaling, rotating, etc Compositing images, defining and modifying regions Drawing and filling polygons Previously glbegin(gl_polygon), etc Aliasing and antialiasing methods

Line drawing algorithm Programmer specifies (x,y) values of end pixels Need algorithm to figure out which intermediate pixels are on line path Pixel (x,y) values constrained to integer values Actual computed intermediate line values may be floats Rounding may be required. E.g. computed point (10.48, 20.51) rounded to (10, 21) Rounded pixel value is off actual line path (jaggy!!) Sloped lines end up having jaggies Vertical, horizontal lines, no jaggies

Line Drawing Algorithm 8 7 6 5 4 3 2 1 Line: (3,2) -> (9,6)? Which intermediate pixels to turn on? 0 1 2 3 4 5 6 7 8 9 10 11 12

Line Drawing Algorithm Slope-intercept line equation y = mx + b Given two end points (x0,y0), (x1, y1), how to compute m and b? m dy y1 y0 = = b = y0 m * x0 dx x1 x0 (x1,y1) dy (x0,y0) dx

Line Drawing Algorithm Numerical example of finding slope m: (Ax, Ay) = (23, 41), (Bx, By) = (125, 96) m = By Bx Ay Ax = 96 41 125 23 = 55 102 = 0.5392

Digital Differential Analyzer (DDA): Line Drawing Algorithm Walk through the line, starting at (x0,y0) Constrain x, y increments to values in [0,1] range Case a: x is incrementing faster (m < 1) Step in x=1 increments, compute and round y Case b: y is incrementing faster (m > 1) Step in y=1 increments, compute and round x m>1 (x1,y1) m=1 (x0,y0) dx dy m<1

DDA Line Drawing Algorithm (Case a: m < 1) yk +1 = yk + m (x1,y1) x = x0 y = y0 Illuminate pixel (x, round(y)) x = x0 + 1 y = y0 + 1 * m Illuminate pixel (x, round(y)) x = x + 1 y = y + 1 * m Illuminate pixel (x, round(y)) (x0, y0) Until x == x1

DDA Line Drawing Algorithm (Case b: m > 1) x x k + 1 = k + 1 m (x1,y1) x = x0 y = y0 Illuminate pixel (round(x), y) y = y0 + 1 x = x0 + 1 * 1/m Illuminate pixel (round(x), y) y = y + 1 x = x + 1 /m Illuminate pixel (round(x), y) (x0,y0) Until y == y1

DDA Line Drawing Algorithm Pseudocode compute m; if m < 1: { float y = y0; // initial value for(int x = x0;x <= x1; x++, y += m) setpixel(x, round(y)); } else // m > 1 { float x = x0; // initial value for(int y = y0;y <= y1; y++, x += 1/m) setpixel(round(x), y); } Note: setpixel(x, y) writes current color into pixel in column x and row y in frame buffer

Line Drawing Algorithm Drawbacks DDA is the simplest line drawing algorithm Not very efficient Round operation is expensive Optimized algorithms typically used. Integer DDA E.g.Bresenham algorithm (Hill, 10.4.1) Bresenham algorithm Incremental algorithm: current value uses previous value Integers only: avoid floating point arithmetic Several versions of algorithm: we ll describe midpoint version of algorithm

Bresenham s Line-Drawing Algorithm Problem: Given endpoints (Ax, Ay) and (Bx, By) of a line, want to determine best sequence of intervening pixels First make two simplifying assumptions (remove later): (Ax < Bx) and (0 < m < 1) Define Width W = Bx Ax Height H = By - Ay (Bx,By) (Ax,Ay)

Bresenham s Line-Drawing Algorithm Based on assumptions: W, H are +ve H < W As x steps in +1 increments, y incr/decr by <= +/ 1 y value sometimes stays same, sometimes increases by 1 Midpoint algorithm determines which happens

Bresenham s Line-Drawing Algorithm (x1,y1) What Pixels to turn on or off? Consider pixel midpoint M(Mx, My) M = (x0 + 1, Y0 + ½) M(Mx,My) Build equation of line through and compare to midpoint If midpoint is above line, y stays same If midpoint is below line, y increases + 1 (x0, y0)

Bresenham s Line-Drawing Algorithm Using similar triangles: y x Ay Ax = H W (x,y) (Ax,Ay) (Bx,By) H(x Ax) = W(y Ay) -W(y Ay) + H(x Ax) = 0 Above is ideal equation of line through (Ax, Ay) and (Bx, By) Thus, any point (x,y) that lies on ideal line makes eqn = 0 Double expression (to avoid floats later), and give it a name, F(x,y) = -2W(y Ay) + 2H(x Ax)

Bresenham s Line-Drawing Algorithm So, F(x,y) = -2W(y Ay) + 2H(x Ax) Algorithm, If: F(x, y) < 0, (x, y) above line F(x, y) > 0, (x, y) below line Hint: F(x, y) = 0 is on line Increase y keeping x constant, F(x, y) becomes more negative

Bresenham s Line-Drawing Algorithm Example: to find line segment between (3, 7) and (9, 11) F(x,y) = -2W(y Ay) + 2H(x Ax) = (-12)(y 7) + (8)(x 3) For points on line. E.g. (7, 29/3), F(x, y) = 0 A = (4, 4) lies below line since F = 44 B = (5, 9) lies above line since F = -8

Bresenham s Line-Drawing Algorithm (x1,y1) What Pixels to turn on or off? Consider pixel midpoint M(Mx, My) M = (x0 + 1, Y0 + ½) M(Mx,My) If F(Mx,My) < 0, M lies above line, shade lower pixel (same y as before) If F(Mx,My) > 0, M lies below line, shade upper pixel (x0, y0)

Can compute F(x,y) incrementally Initially, midpoint M = (Ax + 1, Ay + ½) F(Mx, My) = -2W(y Ay) + 2H(x Ax) = 2H W Can compute F(x,y) for next midpoint incrementally If we increment x + 1, y stays same, compute new F(Mx,My) F(Mx, My) += 2H If we increment x +1, y + 1 F(Mx, My) -= 2(W H)

Bresenham s Line-Drawing Algorithm Bresenham(IntPoint a, InPoint b) { // restriction: a.x < b.x and 0 < H/W < 1 int y = a.y, W = b.x a.x, H = b.y a.y; int F = 2 * H W; // current error term for(int x = a.x; x <= b.x; x++) { setpixel at (x, y); // to desired color value if F < 0 F = F + 2H; else{ Y++, F = F + 2(H W) } } } Recall: F is equation of line

Bresenham s Line-Drawing Algorithm Final words: we developed algorithm with restrictions 0 < m < 1 and Ax < Bx Can add code to remove restrictions To get the same line when Ax > Bx (swap and draw) Lines having m > 1 (interchange x with y) Lines with m < 0 (step x++, decrement y not incr) Horizontal and vertical lines (pretest a.x = b.x and skip tests) Important: Read Hill 9.4.1

References Hill, chapter 9