Unformatted text preview: Introduction to Computer Graphics
Lecture 7
Rasterization
Rasterization The Rendering Pipeline
Transform
Illuminate
Transform
Clip
Project
Rasterize Model & Camera
Parameters Rendering Pipeline Framebuffer Display Does rasterization need programming?
For earlier 2D graphics system based on Dos
For
system, programming may be needed.
system,
In win32 system or later, programming for
In
raterization is included in GL. The normal 2D or
3D graphics applications do not need
programming for this.
programming Rasterizing
1. Lines
1. Fill Areas Towards the Ideal Line
We can only do a discrete approximation Illuminate pixels as close to the true path as
Illuminate
possible, consider bilevel display only
possible,
• Pixels are either lit or not lit What is an ideal line
Must interpolate both defining end points
Must appear straight and continuous
• Only possible axisaligned and 45o lines
lines Must have uniform density and intensity
• Consistent within a line and over all lines Must be efficient, drawn quickly
• Lots of them are required!!! Lines
1. DDA Algorithm
1. Bresenham Algorithm (midpoint) Simple Line
Based on slopeintercept
algorithm from algebra:
y = mx + b
Simple approach:
increment x, solve for y
Floating point arithmetic
required Does it Work? It seems to work okay for lines with a
slope of 1 or less,
but doesn’t work well for lines with slope
greater than 1 – lines become more
discontinuous in appearance and we must
add more than 1 pixel per column to
make it work.
Solution?  use symmetry. Modify algorithm per octant OR, increment along xaxis if dy<dx else increment along yaxis DDA algorithm
DDA = Digital Differential Analyser
• finite differences Treat line as parametric equation in t : x(t ) = x1 + t ( x2 − x1 )
Start point End point  ( x1 , y1 )
( x2 , y 2 ) y (t ) = y1 + t ( y2 − y1 )
0 ≤ t ≤1 DDA Algorithm
Start at t = 0
Start
At each step, increment t by dt
At x new = xold Choose appropriate value for n y = y
Choose
new
old
Ensure no pixels are missed: x2 − x1
dx =
dt ≤ 1
n ( x 2 − x1 )
+
dt
n
( y 2 − y1 )
+
dt
n y2 − y1
dy =
dt ≤ 1
n Set dt=1 and set n to maximum of (x2x1) and (y2y1)
Set
(x
(y DDA algorithm
line(int x1, int y1, int x2, int y2)
{
float x,y;
int dx = x2x1, dy = y2y1;
int n = max(abs(dx),abs(dy));
float dx = dx/n, dy = dy/n;
x = x1;
y = y1;
while( n )
{
point(round(x),round(y));
x += dx;
y += dy;
}
} n  range of t. DDA algorithm
Still need a lot of floating point arithmetic.
• 2 ‘round’s and 2 adds per pixel. Is there a simpler way ?
Can we use only integer arithmetic ?
• Easier to implement in hardware. Lines
1. DDA Algorithm
1. Bresenham Algorithm (midpoint) Observation on lines.
while( n )
{
draw(x,y);
move right;
if( below line )
move up;
} Testing for the side of a line.
Need a test to determine which side of a line a
Need
pixel lies.
pixel
Write the line in implicit form: F ( x, y ) = ax + by + c = 0
• Easy to prove F<0 for points above the line, F>0 for
points below. Testing for the side of a line.
y c
(a,b) F(x,y)=0
x Testing for the side of a line.
Need to find coefficients a,b,c.
Recall explicit, slopeintercept form : F ( x, y ) = ax + by + c = 0
dy
y = mx + b and so y =
x +b
dx So: F ( x, y ) = dy ⋅ x − dx ⋅ y + c = 0 Decision variable.
Evaluate F at point M
Referred to as decision variable 1
d = F ( x p +1, y p + )
2 NE M
E
Previous
Pixel
(xp,yp) Choices for
Current pixel Choices for
Next pixel Decision variable.
Evaluated for next pixel, Depends on whether E or NE Is chosen :
If E chosen : d new 1
1
= F ( x p + 2, y p + ) = a ( x p + 2) + b( y p + ) + c
2
2
But recall :
NE
M
E
Previous
Pixel
(xp,yp) Choices for
Current pixel Choices for
Next pixel 1
d old = F ( x p +1, y p + )
2
1
= a ( x p +1) + b( y p + ) + c
2
So : d new = d old + a
= d old + dy Decision variable.
If NE was chosen : 3
3
d new = F ( x p + 2, y p + ) = a ( x p + 2) + b( y p + ) + c
2
2
M
NE d new = d old + a + b
= d old + dy − dx E
Previous
Pixel
(xp,yp) Choices for
Current pixel So : Choices for
Next pixel Summary of midpoint algorithm
Choose between 2 pixels at each step based upon
Choose
sign of decision variable.
sign
Update decision variable based upon which pixel
Update
is chosen.
is
Start point is simply first endpoint (x1,y1).
Need to calculate initial value for d Initial value of d.
Start point is (x1,y1)
d start 1
1
= F ( x1 +1, y1 + ) = a ( x1 +1) + b( y1 + ) + c
2
2
b
= ax1 + by1 + c + a +
2
b
= F ( x1 , y1 ) + a +
2 But (x1,y1) is a point on the line, so F(x1,y1) =0 d start = dy − dx / 2
Conventional to multiply by 2 to remove fraction ⇒ doesn’t effect sign. Bresenham algorithm
void MidpointLine(int
void
x1,y1,x2,y2)
x1,y1,x2,y2)
{
int dx=x2x1;
int dy=y2y1; while (x < x2) {
if (d<= 0) {
d+=incrE;
x++
} else {
d+=incrNE;
x++;
y++;
}
WritePixel(x,y); int d=2*dydx;
int increE=2*dy;
int incrNE=2*(dydx);
x=x1;
y=y1;
WritePixel(x,y); }
} Bresenham was not the end!
2step algorithm by Xiaolin Wu:
(see Graphics Gems 1, by Brian Wyvill)
Treat line drawing as an automaton , or finite state
machine, ie. looking at next two pixels of a line, easy
to see that only a finite set of possibilities exist.
The 2step algorithm exploits symmetry by
simultaneously drawing from both ends towards the
midpoint. Twostep Algorithm
Possible positions of next two pixels dependent on slope – current pixel
in blue:
Slope between 0 and ½
Slope between ½ and 1
Slope between 1 and 2
Slope greater than 2 Circle drawing.
Can also use Bresenham to draw circles.
Use 8fold symmetry
E
M
SE Previous
Pixel Choices for
Current pixel Choices for
Next pixel Circle drawing.
Implicit form for a circle is: f ( x, y ) = ( x − xc ) + ( y − yc ) − r 2 = 0
2 2 If SE is chosen d new = d old + ( 2 x p − 2 y p + 5) + (2 yc − xc )
If E is chosen d new = d old + ( 2 x p + 3) • Functions are linear equations in terms of (xp,yp)
–Termed point of evaluation Circle drawing.
d old 1
= f ( x p + 1, y p − )
2 E:
1
1
2
d new = d ( x p + 2, y p − ) = ( x p + 2 − xc ) + ( y p − − yc ) 2 − r 2
2
2
1
2
= ( x p + 1 − xc ) + 2( x p + 1 − xc ) + 1 + ( y p − − yc ) 2 − r 2
2
= d old + 2( x p − xc ) + 3 Circle drawing.
SE :
d new 3
= ( x p + 2 − xc ) + ( y p − − yc ) 2 − r 2
2
2 1
1
2
= ( x p + 1 − xc ) + 2( x p + 1 − xc ) + 1 + ( y p − − yc ) − 2( y p − − yc ) + 1 − r 2
2
2
= d old + 2 x p + 2 − xc + 1 − 2 y p + 1 + 2 yc + 1
2 = d old + (2 x p − 2 y p + 5) + (2 yc − xc ) Problems with Bresenham algorithm Pixels are drawn as a single line ⇒ unequal line
Pixels
intensity with change in angle.
intensity Pixel density = n/√2 pixels/mm Can draw lines in darker colours
according to line direction.
 Better solution : antialiasing !
Pixel density = n pixels/mm Summary of line drawing so far.
Explicit form of line
• Inefficient, difficult to control. Parametric form of line.
• Express line in terms of parameter t
Express
• DDA algorithm Implicit form of line
• Only need to test for ‘side’ of line.
• Bresenham algorithm.
• Can also draw circles. Rasterizing
1. Lines
1. Fill Areas Region Filling
Seed Fill Approaches
Seed
• works at the pixel level
• suitable for interactive painting apllications Scanline Fill Approaches
• works at the polygon level
• better performance Seed Fill Algorithms: Connectedness
4connected region: From a given pixel, the region that
4connected
you can get to by a series of 4 way moves (N, S, E and W)
you
8connected region: From a given pixel, the region that
8connected
you can get to by a series of 8 way moves (N, S, E, W, NE,
NW, SE, and SW) 4connected 8connected Boundary Fill Algorithm
Start at a point inside a region
Paint the interior outward to the
Paint
edge
edge
The edge must be specified in a
The
single color
single
Fill the 4connected or 8connected region
4connected fill is faster, but can
4connected
have problems:
have Boundary Fill Algorithm (cont.)
void BoundaryFill4(int x, int y, color newcolor, color edgecolor)
{
int current;
current = ReadPixel(x, y);
if(current != edgecolor && current != newcolor)
{
WritePixel (x, y, newcolor);
BoundaryFill4(x+1, y, newcolor, edgecolor);
BoundaryFill4(x1, y, newcolor, edgecolor);
BoundaryFill4(x, y+1, newcolor, edgecolor);
BoundaryFill4(x, y1, newcolor, edgecolor);
} Polygon Representation Scanline Fill Algorithm
Intersect scanline with polygon edges
Fill between pairs of intersections
Basic algorithm:
For y = ymin to ymax
1) intersect scanline y with each edge
2) sort intersections by increasing x [p0,p1,p2,p3]
3) fill pairwise (p0>p1, p2>p3, …) Scanline Fill Algorithm Spacial Handling (cont.)
Intersection is an edge end point
Intersection points: (p0, p1, p2) ???
>(p0,p1,p1,p2) so we can still fill pairwise
Case 1 Spacial Handling (cont.)
>In fact, if we compute the intersection of the
scanline with edge e1 and e2 separately, we will get
the intersection point p1 twice. Keep both of the p1. Spacial Handling (cont.)
However, in this case we don’t want to count p1 twice
(p0,p1,p1,p2,p3), otherwise we will fill pixels between
p1 and p2, which is wrong.
Case 2 Spacial Handling (cont.)
Summary: If the intersection is the ymin of the edge’s
endpoint, count it. Otherwise, don’t. ...
View
Full Document
 Spring '10
 Professor
 Computer Graphics, Trigraph, Pixel, Raster graphics, DDA algorithm, Rasterisation

Click to edit the document details