Beruflich Dokumente
Kultur Dokumente
1
Introduction
2
3
4
5
Classification of Computer Graphics by Object and Picture Type of
Object
2D
3D
Pictorial representation
Line drawing
Gray scale image
Color image
Line drawing (or wire-frame)
Line drawing, with various affects Shaded, color image with various
effects
Interactive Graphics
6
Computer Graphics is about animation
(films)
7
Games are very important in Computer Graphics
8
Medical Imaging is another driving force
10
Scientific Visualisation
To view below and
above our visual range
11
History
12
Ivan Sutherland (1963) - SKETCHPAD
• pop-up menus
• constraint-based drawing
• hierarchical modeling
13
Display hardware
• vector displays
– 1963 – modified oscilloscope
– 1974 – Evans and Sutherland Picture System
• raster displays
– 1975 – Evans and Sutherland frame buffer
– 1980s – cheap frame buffers → bit-mapped personal computers
– 1990s – liquid-crystal displays → laptops
– 2000s – micro-mirror projectors → digital cinema
• other
– stereo, head-mounted displays
– autostereoscopic displays
– tactile, haptic, sound
14
Input hardware
• 2D
– light pen, tablet, mouse, joystick, track ball, touch
panel, etc.
– 1970s & 80s - CCD analog image sensor + frame
grabber
– 1990s & 2000’s - CMOS digital sensor + in-camera
processing
[Nayar00]
→ →
16
Input hardware
• 2D
– light pen, tablet, mouse, joystick, track ball, touch panel, etc.
– 1970s & 80s - CCD analog image sensor + frame grabber
– 1990s & 2000’s - CMOS digital sensor + in-camera processing
→ high-X imaging (dynamic range, resolution, depth of field,…)
• 3D
– 3D trackers
– multiple cameras
– active rangefinders
• other
– data gloves
– voice
17
Rendering
• 1960s - the visibility problem
– Roberts (1963), Appel (1967) - hidden-line algorithms
– Warnock (1969), Watkins (1970) - hidden-surface
algorithms
– Sutherland (1974) - visibility = sorting
18
• 1960s - the visibility problem
– Roberts (1963), Appel (1967) - hidden-line algorithms
– Warnock (1969), Watkins (1970) - hidden-surface algorithms
– Sutherland (1974) - visibility = sorting
21
• early 1980s - global illumination
– Whitted (1980) - ray tracing
– Goral, Torrance et al. (1984), Cohen (1985) - radiosity
– Kajiya (1986) - the rendering equation
23
• early 1990s - non-photorealistic rendering
– Drebin et al. (1988), Levoy (1988) - volume rendering
– Haeberli (1990) - impressionistic paint programs
– Salesin et al. (1994-) - automatic pen-and-ink
illustration
– Meier (1996) - painterly rendering
24
Basic raster graphics algorithms for drawing 2d
primitives.
How do things end up on the screen?
25
Architecture Of A Graphics
System
Display
Frame Video
Processor Monitor
Memory Buffer Controller
Display System
CPU
Processor Memory
System Bus
26
Output Devices
27
Basic Cathode Ray Tube (CRT)
• Fire an electron beam at a phosphor
coated screen
28
Raster Scan Systems
Draw one line at a time
29
Colour CRT
• An electron gun for each colour – red,
green and blue
30
Plasma-Panel Displays
Applying voltages to
crossing pairs of
conductors
causes the gas
(usually a mixture
including neon) to
break down into a
glowing plasma of
electrons and
ions
31
Liquid Crystal Displays
• Light passing
through the
liquid crystal
is twisted so it
gets through
the polarizer
• A voltage is
applied using
the
crisscrossing
conductors to
stop the
twisting and
turn pixels off
32
Scan Conversion
• A line segment in a scene is defined by
the coordinate positions of the line end-
points y
(7, 5)
(2, 2)
x
33
• But what happens when we try to draw
this on a pixel based display?
35
Line Equations
• Let’s quickly review the equations involved
in drawing lines
y Slope-intercept line
equation:
yend y = m⋅ x +b
where:
yend − y0
y0 m=
xend − x0
b = y0 − m ⋅ x0
x
x0 xend
36
• The slope of a line (m) is defined by its
start and end coordinates
• The diagram below shows some examples
of lines and their slopes
m=∞
m = -4 m=4
m = -2 m=2
m = -1 m=1
m = -1/2 m = 1/2
m = -1/3 m = 1/3
m=0 m=0 37
• Find corresponding y coordinate for each
unit x coordinate
example:
(7, 5)
5
2
(2, 2)
38
x
2 7
5
0 1 2 3 4 5 6 7 39
y
5
(7, 5) • First work out m and b:
5−2 3 3 4
m= = b = 2− ∗2 =
2
7−2 5 5 5
(2, 2)
x
2 3 4 5 6 7
3 4 3 3 4 1
y (3) = ⋅ 3 + = 2 y ( 4) = ⋅ 4 + = 3
5 5 5 5 5 5
3 4 4 3 4 2
y (5) = ⋅ 5 + = 3 y ( 6) = ⋅ 6 + = 4
5 5 5 5 5 5
40
Now just round off the results and turn on
these pixels to draw our line
3
7 y (3) = 2 ≈ 3
5
6
1
5 y ( 4) = 3 ≈ 3
5
4
4
3 y (5) = 3 ≈ 4
2
5
1
2
0
y ( 6) = 4 ≈ 4
5
0 1 2 3 4 5 6 7 8
41
However, this approach is just way too slow
In particular look out for:
The equation y = mx + b requires the multiplication of m by x
– Rounding off the resulting y coordinates
We–need a faster solution
In the previous example we chose to solve the parametric line equation to give us the y coordinate for each unit x coordinate
What if we had done it the other way around?
So this gives us:
where: and
•
y −b
x =
m
yend −y0
m= b = y0 −m ⋅ x0
xend −x0 42
Leaving out the details this gives us:
2 1
x(3) = 3 ≈ 4 x ( 4) = 5 ≈ 5
3 3
m=∞
m = -4 m=4
m = -2 m=2
m = -1 m=1
m = -1/2 m = 1/2
m = -1/3 m = 1/3
m=0 m=0 44
#include <stdio.h>
#include <graphics.h>
void main(void)
{
int graphdriver = DETECT, graphmode, color, n, m;
}
45
Initializes the graphics system
Declaration:
void far initgraph(int far *graphdriver,int far *graphmode, char far *pathtodriver);
Remarks:
To start the graphics system, you must first call initgraph.
initgraph initializes the graphics system by loading a graphics driver from disk
(or validating a registered driver) then putting the system into graphics mode.
initgraph also resets all graphics settings (color, palette, current position,
viewport, etc.) to their defaults, then resets graphresult to 0.
46
*graphmode :Integer that specifies the initial graphics mode
(unless *graphdriver = DETECT). If *graphdriver = DETECT, initgraph
sets *graphmode to the highest resolution available for the detected
driver. You can give *graphmode a value using a constant of the
graphics_modes enumeration type.
49
Simple Line
Based on slope-intercept
algorithm from algebra:
y = mx + b
Simple approach:
increment x, solve for y
Floating point arithmetic
required
50
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.
51
Modify algorithm per octant
52
DDA algorithm
53
x(t ) = x1 + t ( x2 − x1 )
DDA Algorithm y (t ) = y1 + t ( y2 − y1 )
• Start at t = 0
• At each step, increment t by dt dx
xnew = xold +
• Choose appropriate value for dt dt
dy
• Ensure no pixels are missed: ynew = yold +
dt
– Implies: and
dx dy
<1 <1
dt dt
• Set dt to maximum of dx and dy
54
DDA algorithm
line(int x1, int y1, int x2, int y2)
{ n - range of t.
float x,y;
int dx = x2-x1, dy = y2-y1;
int n = max(abs(dx),abs(dy));
float dt = n, dxdt = dx/dt, dydt = dy/dt;
x = x1;
y = y1;
while( n-- ) {
point(round(x),round(y));
x += dxdt;
y += dydt;
}
}
55
• Again the values calculated by the equations
used by the DDA algorithm must be rounded to
match pixel values
56
DDA algorithm
• Still need a lot of floating point arithmetic.
– 2 ‘round’s and 2 adds per pixel.
57
Observation on lines.
while( n-- )
{
draw(x,y);
move right;
if( below line )
move up;
}
58
Testing for the side of a line.
Need a test to determine which side of a line a pixel lies.
• 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.
59
Testing for the side of a line.
F ( x, y ) = ax + by + c = 0
dy
y = mx + b and so y = x+b
dx
F ( x, y ) = dy.x − dx. y + c = 0
60
Decision variable.
1
Evaluate F at point M d = F ( x p + 1, y p + )
2
Referred to as decision variable
NE
M
E
61
Decision variable.
If E chosen :
1 1
d new = F ( x p + 2, y p + ) = a ( x p + 2) + b( y p + ) + c
2 2
But recall :
1
d old = F ( x p + 1, y p + )
NE 2
1
M = a ( x p + 1) + b( y p + ) + c
2
E
Previous Choices for
So :
d new = d old + a
Pixel Choices for Next pixel
(xp,yp) Current pixel = d old + dy
62
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 So :
NE
d new = d old + a + b
E
= d old + dy − dx
Previous Choices for
Pixel Choices for Next pixel
(xp,yp) Current pixel
63
Summary of mid-point algorithm
• Choose between 2 pixels at each step
based upon the sign of a decision
variable.
• Update the decision variable based upon
which pixel is chosen.
• Start point is simply first endpoint (x1,y1).
• Need to calculate the initial value for d
64
Initial value of d.
Start point is (x1,y1)
1 1
d start = F ( x1 + 1, y1 + ) = a( x1 + 1) + b( y1 + ) + c
2 2
b
= ax1 + by1 + c + a +
2
b
= F ( x1 , y1 ) + a +
2
d start = dy − dx / 2
Conventional to multiply by 2 to remove fraction ⇒ doesn’t effect sign.
65
Bresenham algorithm
void MidpointLine(int
x1,y1,x2,y2)
{ while (x < x2) {
int dx=x2-x1; if (d<= 0) {
int dy=y2-y1; d+=incrE;
x++
int d=2*dy-dx; } else {
int increE=2*dy; d+=incrNE;
x++;
int incrNE=2*(dy-dx); y++;
x=x1; }
WritePixel(x,y);
y=y1; }
WritePixel(x,y); }
66
Bresenham was (not) the end!
2-step 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 2-step algorithm exploits symmetry by
simultaneously drawing from both ends towards
the midpoint.
67
Two-step Algorithm
Possible positions of next two pixels dependent on slope –
current pixel in blue:
Slope between 0 and ½
68
Circle drawing.
M
SE
69
Circle drawing.
f
(
x
,
y
)
=
(
x
x
c
2
(
y
y
c
2
r
70
Problems with Bresenham algorithm
72
73
74
75
76
77
78
79
Circle Midpoint Algorithm
F ( x, y ) = 0 on circle
(-R,0) (R,0)
F ( x, y ) < 0 inside
F ( x, y ) > 0 outside
(0,R)
80
Choosing the Next Pixel
decision variable d
d = F ( M ) = F ( x + 1, y + 1 / 2)
(x, y) (x+1, y)
E
F ( x + 1, y + 1 / 2) > 0choose E
M
F ( x + 1, y + 1 / 2) ≤ 0choose SE
SE
(x+1, y+1)
81
Change of d when E is chosen
(x, y) (x+1, y) (x+2, y)
E
Mold Mnew
SE
(x+1, y+1) (x+2, y+1)
d new = ( x + 2) + ( y + 1 / 2) − R
2 2 2
d old = ( x + 1) + ( y + 1 / 2) − R
2 2 2
∆d = d new − d old = 2 x + 3
82
Change of d when SE is chosen
d new = ( x + 2) + ( y + 3 / 2) − R
2 2 2
E
∆d = d new − d old = 2 x + 2 y + 5
Mold
SE
Mnew
M0
(1,-R+1)
d0 = F (M 0 )
d 0 = F (1,− R + 1 / 2)
d 0 = (1) + (− R + 1 / 2) − R
2 2 2
d0 = 5 / 4 − R
84
Midpoint Circle Algo
x = 0;
y = -R;
d = 5/4 – R; /* real */
setPixel(x,y);
while (y > x) {
if (d > 0) { /* E chosen */
d += 2*x + 3;
x++;
} else { /* SE chosen */
d += 2*(x+y) + 5;
x++; y++;
}
setPixel(x,y);
} 85
New Decision Variable
86
New Circle Algorithm
x = 0;
y = -R;
h = 1 – R;
setPixel(x,y);
while (y > x) {
if (h > 0) { /* E chosen */
h += 2*x + 3;
x++;
} else { /* SE chosen */
h += 2*(x+y) + 5;
x++; y++;
}
setPixel(x,y);
} 87
Second-Order Differences
• Note that d is incremented by a linear expression each
time through the loop.
– We can speed things up a bit by tracking how these linear
expressions change.
– Not a huge improvement since multiplication by 2 is just a left-
shift by 1 (e.g. 2*x = x<<1).
88
2nd Order Difference when E chosen
∆Eold = 2 x + 3 ∆SEold = 2( x + y ) + 5
∆Enew = 2( x + 1) + 3 ∆SEnew = 2( x + 1 + y ) + 5
∆Enew − ∆Eold = 2 ∆SEnew − ∆SEold = 2
89
2nd Order Difference when
SE chosen
• When SE chosen, we move from
pixel (x,y) to (x+1,y+1).
∆Eold = 2 x + 3 ∆SEold = 2( x + y ) + 5
∆Enew = 2( x + 1) + 3 ∆SEnew = 2( x + 1 + y + 1) + 5
∆Enew − ∆Eold = 2 ∆SEnew − ∆SEold = 4
90
New and Improved Circle Algorithm
x = 0; y = -R;
h = 1 – R;
dE = 3; dSE = -2*R + 5;
setPixel(x,y);
while (y > x) {
if (h > 0) { /* E chosen */
h += dE;
dE += 2; dSE += 2;
x++;
} else { /* SE chosen */
h += dSE;
dE += 2; dSE += 4;
X++; y++;
}
setPixel(x,y);
}
91
92
93
94
95
96
97
98
99
100
101
102
103