170 likes | 182 Views
This article explores the problem of rasterization, specifically how idealized primitives are mapped to discrete display space. It discusses scan converting lines and presents the DDA and Bresenham's algorithms as solutions.
E N D
The Rasterization Problem: Idealized PrimitivesMap to Discrete Display Space Edited from http://www.comp.hkbu.edu.hk/~sci3750/lect8-04.ppt 更新时间2019年10月31日星期四7时38分11秒
Solution Involves Selection of DiscreteRepresentation Values Edited from http://www.comp.hkbu.edu.hk/~sci3750/lect8-04.ppt
Scan Converting Lines:Characterizing the Problem ideal line i.e. for each x, choose y i.e. for each y, choose x Edited from http://www.comp.hkbu.edu.hk/~sci3750/lect8-04.ppt
Scan Converting Lines:The Strategy • Pick pixels closest to endpoints • Select in between pixels “closest” to ideal line • Objective: To minimize the required calculations. • Pixels can be displayed in multiple shapes and sizes. • In OpenGL, the centers of pixels are located at values halfway between intergers. Edited from http://www.comp.hkbu.edu.hk/~sci3750/lect8-04.ppt
Calculate ideal line equation Starting at leftmost point: for each xi Calculate Select pixel at Scan Converting Lines:DDA (Digital Differential Analyzer) Algorithm Selected Not Selected Edited from http://www.comp.hkbu.edu.hk/~sci3750/lect8-04.ppt
Scan Converting Lines:DDA Algorithm, Incremental Form Therefore, rather than recomputing y in each step, simply add m. The Algorithm: void Line(int x0, int y0, int xn, int yn) { int x; float dy, dx, y, m; dy = yn - y0; dx = xn - x0; m = dy/dx; y = y0; for (x = x0; x<=xn,x++){ WritePixel(x, round(y)); y += m; } } Edited from http://www.comp.hkbu.edu.hk/~sci3750/lect8-04.ppt
We assume m ≤1 in the above. • For larger slope, the separation between pixels can be large, generating an unacceptable rendering of the line segment. • We swap the roles of x and y for line segments with larger slopes. Edited from http://www.comp.hkbu.edu.hk/~sci3750/lect8-04.ppt
Not Allowed Option NE 0 <= Slope <= 1 Option E Last Selection Bresenham’s Algorithm:Allowable Pixel Selections • DDA requires a floating-point addition. • Bresenham derived an algorithm that avoids all floating-point claculations. Edited from http://www.comp.hkbu.edu.hk/~sci3750/lect8-04.ppt
Bresenham’s Algorithm:Iterating 0 <= Slope <= 1 Select E Select NE Edited from http://www.comp.hkbu.edu.hk/~sci3750/lect8-04.ppt
Bresenham’s AlgorithmDecision Function: (implicit equation of the line) Edited from http://www.comp.hkbu.edu.hk/~sci3750/lect8-04.ppt
Bresenham’s Algorithm:Calculating the Decision Function Edited from http://www.comp.hkbu.edu.hk/~sci3750/lect8-04.ppt
Option NE Option E Bresenham’s Algorithm:Incremental Calculation of di Edited from http://www.comp.hkbu.edu.hk/~sci3750/lect8-04.ppt
Bresenham’s Algorithm:The Code void BresenhamLine(int x0, int y0, int xn, int yn) { int dx,dy,incrE,incrNE,d,x,y; dx=xn-x0; dy=yn-y0; d=2*dy-dx; /* initial value of d */ incrE=2*dy; /* decision funct incr for E */ incrNE=2*dy-2*dx; /* decision funct incr for NE */ x=x0; y=y0; DrawPixel(x,y) /* draw the first pixel */ while (x<xn){ if (d<=0){ /* choose E */ d+=incrE; x++; /* move E */ }else{ /* choose NE */ d+=incrNE; x++; y++; /* move NE */ } DrawPixel (x,y); } } Edited from http://www.comp.hkbu.edu.hk/~sci3750/lect8-04.ppt
Bresenham’s AlgorithmAn example 12 11 10 9 8 7 4 5 6 7 8 9 Edited from http://www.comp.hkbu.edu.hk/~sci3750/lect8-04.ppt
Bresenham’s AlgorithmAn example 12 11 10 9 8 7 4 5 6 7 8 9 Edited from http://www.comp.hkbu.edu.hk/~sci3750/lect8-04.ppt
Bresenham’s AlgorithmAn example 12 11 10 9 8 7 4 5 6 7 8 9 Edited from http://www.comp.hkbu.edu.hk/~sci3750/lect8-04.ppt
Another derivation: comparing d1 and d2 Not Allowed Option NE 0 <= Slope <= 1 d1 d2 Last Selection Option E Ref. [HA], Section 3.2.2 Edited from http://www.comp.hkbu.edu.hk/~sci3750/lect8-04.ppt