Rasterization: 1107190 - Introdução À Computação Gráfica Prof. Christian Azambuja Pagot Ci / Ufpb
Rasterization: 1107190 - Introdução À Computação Gráfica Prof. Christian Azambuja Pagot Ci / Ufpb
Lecture 2
4
Universidade Federal da Paraíba
Centro de Informática
Raster Display
Gona.eu (Wikipedia) 5
Universidade Federal da Paraíba
Centro de Informática
Raster Graphics
Vídeo Onboard
RAM
7
Universidade Federal da Paraíba
Centro de Informática
Video Memory
Vídeo Offboard
Vídeo Card
How about
colors?
8
Universidade Federal da Paraíba
Centro de Informática
Colors (Quick view)
● Electromagnetic spectrum
Visible spectrum
Normally used
in computers!
11
Universidade Federal da Paraíba
Centro de Informática
Image Storage
Pixel (0,0) Pixel (1,0) i * 4+0
(4 bytes) (4 bytes) i * 4+1 Pixel (i,0)
i * 4+2 (4 bytes)
0 1 2 3 4 5 6 7 i * 4+3
R G B A R G B A ... R G B A
Color buffer
Line 0 Screen
12
Universidade Federal da Paraíba
Centro de Informática
Image Storage
w columns
(width)
# pixels = w * h
Column w-2
Column w-1
Column 0
Column 1
Column 2
Column 3
Column 4
Line 0
Line 1
Line 2
Line 3
Line 4
h lines
(height)
Line h-2
Line h-1
13
Universidade Federal da Paraíba
Centro de Informática
Image Storage
W ( # columns)
Line 0
Line 1
Line 2
Line 3
Line 4
Pixel = (x,y)
Line h-2
Line h-1
Color buffer
14
Universidade Federal da Paraíba
Centro de Informática
Image Storage
● Vídeo memory screen image footprint:
Screen Image
Color buffer
Vídeo Memory
Frame buffer
Depth buffer
Auxiliar buffer 1
Auxiliar buffer 2
...
Auxiliar buffer n
15
Universidade Federal da Paraíba
Centro de Informática
Rasterization
● “Approximation of mathematical ('ideal')
primitives, described in terms of vertices on a
Cartesian grid, by sets of pixels of the
appropriate intensity of gray or color.”
- Foley et. al
16
Universidade Federal da Paraíba
Centro de Informática
Rasterization
Screen
Pixel
17
Universidade Federal da Paraíba
Centro de Informática
Rasterization
Screen
Pixel
Pixel center
18
Universidade Federal da Paraíba
Centro de Informática
Rasterizing Lines
Since ∆x is greater ∆y:
Δy
m=
Δx
y i=m x i + b
By incrementing x by 1, we can
compute the corresponding y:
∆y 1st point: (x0, mx0+ b))
2nd point: (x1, mx1+ b))
Y 3rd point: (x2, mx2+ b))
.
.
X .
∆x nth point: (xn, mxn+ b))
19
Universidade Federal da Paraíba
Centro de Informática
Rasterizing Lines
Since ∆x is greater ∆y:
Δy
m=
Δx
y i=m x i + b
By incrementing x by 1, we can
compute the corresponding y:
∆y 1st point: (x0, Round(mx0+ b))
2nd point: (x1, Round(mx1+ b))
Y 3rd point: (x2, Round(mx2+ b))
.
.
X .
∆x nth point: (xn, Round(mxn+ b))
20
Universidade Federal da Paraíba
Centro de Informática
Rasterizing Lines
● Problems with this approach:
– At each iteration:
● A floating point multiplication.
● A floating point addition.
● A Round operation.
21
Universidade Federal da Paraíba
Centro de Informática
Rasterizing Lines
● Solution:
– Multiplication can be eliminated:
y i+ 1=m x i+1 +b
y i+ 1=m(x i + Δ x)+ b
y i+ 1= y i + m Δ x This is an incremental
algorithm.
– If ∆x = 1: Usually referred as the DDA
(digital differential analyzer)
y i+ 1= y i + m algorithm.
22
Universidade Federal da Paraíba
Centro de Informática
Bresenham Line Algorithm
● Incremental.
● Avoids multiplications and roundings.
● Can be generalized for circles.
23
Universidade Federal da Paraíba
Centro de Informática
Variation of Bresenham's Algor.
line
Y
X
24
Universidade Federal da Paraíba
Centro de Informática
Variation of Bresenham's Algor.
+ + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + Assuming that 0 ≤ m ≤ 1:
+ + + + + + + + + + + + + + +
ne
+ + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + +
y =mx+ b
m (midpoint)
+ + + + + + + + + + + + + + +
Δy
line
+ + + + + + + + + + + + + + +
+ + + + + + + + +
+
yc
c e
y= ( ) Δx
x +b α = Δy
β = −Δ x
γ = b⋅Δ x
Y Φ (x , y )=α x +β y + γ=0
X xc Φ ( x , y )=Δ y⋅x−Δ x⋅y + b⋅Δ x =0
1 d =Φ (m )→ Decision variable
c=(x c , y c )
e=( x c +1, y c ) if (d < 0)
ne=(x c +1, y c + 1) next pixel = ne
Will we have else
1 to evaluate next pixel = e
m =( x c +1, y c + )
2 a polynomial
Universidade Federal da Paraíba every pixel? 25
Centro de Informática
Variation of Bresenham's Algor.
+ + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + +
If E is choosen:
?
+ + + + + + + + + + + + + + + + + + + + + + + 1
+ + + + + + + + + + + + + + + + + + + + + + + d old =α (x c +1)+β( y c + )+ γ
mold mnew
+ + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + +
2
line + + + + + + + + +
1
+
c e ? d new =α ( x c +2)+β( y c + )+ γ
2
Y
d new −d old → d new =d old +α
X
Remembering...
1
d old =Φ (m old )=Φ(( x c +1, y c + )) Φ (x , y )=α x +β y + γ
2
1 α = Δy
d new =Φ (m new )=Φ(( x c +2, y c + )) β = −Δ x
2 γ = b⋅Δ x
26
Universidade Federal da Paraíba
Centro de Informática
Variation of Bresenham's Algor.
+ + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + +
If NE is choosen:
?
+ + + + + + + + + + + + + + + + + + + + + + + 1
+ + + + + + + + + + + + + + + + + + + + + + +
mnew d old =α ( x c +1)+β( y c + )+ γ
+ + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + +
2
+ + + + + + + + + + + + + + + + +
3
+ + + + + + + + + + + + + + +
ne ? d new =α (x c +2)+β( y c + )+ γ
+ + + + + + + + + + + +
+ + + + + + + + + + 2
+ + + + + + +
+ + + + +
mold
d new −d old → d new =d old +α +β
+ +
line
c
Remembering...
Y
Φ (x , y )=α x +β y + γ
X 1
d old =Φ (m old )=Φ(( x c +1, y c + )) α = Δy
2 β = −Δ x
3 γ = b⋅Δ x
d new =Φ (m new )=Φ(( x c +2, y c + ))
2
27
Universidade Federal da Paraíba
Centro de Informática
Variation of Bresenham's Algor.
● How about the 1st pixel (there is no Dold!)?
1
d =Φ (m )=α( x 0+ 1)+β( y 0 + )+ γ
2
β
d =Φ (c)+α+ Φ (c)=0
2
m α = Δy
line
β
d =α+ β = −Δ x
y0 2 γ = b⋅Δ x
Y Δx
d =Δ y −
X 2
x0
Φ ( x , y )=0=2⋅0=2 Φ( x , y )=2(α x +β y + γ)
d =Φ (m)
1
=Φ((x 0 +1, y 0 + )) d =2 Δ y −Δ x
2
28
Universidade Federal da Paraíba
Centro de Informática
Variation of Bresenham's Algor.
● The entire algoritm for 0 < m < 1:
MidPointLine() {
int dx = x1 – x0;
int dy = y1 – x0;
int d = 2 * dy – dx;
int incr_e = 2 * dy;
The computation of d, now,
int incr_ne = 2 * (dy – dx); involves only addition!
int x = x0;
int y = y0;
PutPixel(x, y, color)
while (x < x1) {
if (d <= 0) {
d += incr_e;
Slopes outside the range [0,1]
x++; can be handled by symmetry!
} else {
d += incr_ne;
x++;
y++;
}
PutPixel(x, y, color);
}
}
30
Universidade Federal da Paraíba
Centro de Informática