Computer Graphics
Transcript of Computer Graphics
1
Computer Graphics
2
Computer Graphics involves display, manipulation and storage of pictures and experimental data for proper visualization using a computer.
Typical graphics system comprises of a host computer with support of fast processor, large memory, frame buffer and
• Display devices (color monitors),• Input devices (mouse, keyboard, joystick, touch screen, trackball)• Output devices (LCD panels, laser printers, color printers. Plotters etc.)• Interfacing devices such as, video I/O,TV interface etc.
Introduction
3
4
5
6
Classification of Computer Graphics by Object and Picture Type of Object
2D3DPictorial representationLine drawingGray scale imageColor imageLine drawing (or wire-frame)Line drawing, with various affects Shaded, color image with variouseffectsInteractive Graphics
7
Computer Graphics is about animation (films)
Major driving force now
9
Medical Imaging is another driving force
Much financial support
Promotes linking of graphics with video, scans, etc.
10
Computer Aided Design too
11
Scientific Visualisation
To view below and above our visual range
12
History
13
Ivan Sutherland (1963) - SKETCHPAD
• pop-up menus
• constraint-based drawing
• hierarchical modeling
14
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
15
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]
high-X imaging (dynamic range, resolution, depth of field,…)
16
• negative film = 130:1 (7 stops)• paper prints = 46:1• [Debevec97] = 250,000:1 (18 stops)
17
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
18
Rendering
• 1960s - the visibility problem– Roberts (1963), Appel (1967) - hidden-line algorithms– Warnock (1969), Watkins (1970) - hidden-surface
algorithms– Sutherland (1974) - visibility = sorting
19
• 1960s - the visibility problem
– Roberts (1963), Appel (1967) - hidden-line algorithms
– Warnock (1969), Watkins (1970) - hidden-surface algorithms
– Sutherland (1974) - visibility = sorting
• 1970s - raster graphics
– Gouraud (1971) - diffuse lighting
– Phong (1974) - specular lighting
– Blinn (1974) - curved surfaces, texture
– Catmull (1974) - Z-buffer hidden-surface algorithm
– Crow (1977) - anti-aliasing
20
• 1960s - the visibility problem– Roberts (1963), Appel (1967) - hidden-line algorithms– Warnock (1969), Watkins (1970) - hidden-surface algorithms– Sutherland (1974) - visibility = sorting
• 1970s - raster graphics– Gouraud (1971) - diffuse lighting– Phong (1974) - specular lighting– Blinn (1974) - curved surfaces, texture– Catmull (1974) - Z-buffer hidden-surface algorithm– Crow (1977) - anti-aliasing
21
• early 1980s - global illumination– Whitted (1980) - ray tracing– Goral, Torrance et al. (1984), Cohen (1985) -
radiosity– Kajiya (1986) - the rendering equation
22
• early 1980s - global illumination– Whitted (1980) - ray tracing– Goral, Torrance et al. (1984), Cohen (1985) - radiosity– Kajiya (1986) - the rendering equation
• late 1980s - photorealism– Cook (1984) - shade trees– Perlin (1985) - shading languages– Hanrahan and Lawson (1990) - RenderMan
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
• 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
25
Basic raster graphics algorithms for drawing 2d primitives.
How do things end up on the screen?
26
System Bus
CPUDisplay
ProcessorSystem Memory
Display Processor Memory
Frame Buffer
Video Controller
MonitorMonitor
Architecture Of A Graphics System
27
Output Devices
• There are a range of output devices currently available:– Printers/plotters– Cathode ray tube displays– Plasma displays– LCD displays– 3 dimensional viewers– Virtual/augmented reality headsets
28
Basic Cathode Ray Tube (CRT)
• Fire an electron beam at a phosphor coated screen
29
Raster Scan Systems
Draw one line at a time
30
Colour CRT
• An electron gun for each colour – red, green and blue
31
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
Plasma-Panel Displays
32
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
33
Scan Conversion• A line segment in a scene is defined by
the coordinate positions of the line end-points
x
y
(2, 2)
(7, 5)
34
• But what happens when we try to draw this on a pixel based display?
• How do we choose which pixels to turn on?
35
• Considerations to keep in mind:– The line has to look good
• Avoid jaggies
– It has to be lightening fast!• How many lines need to be drawn in a typical
scene?• This is going to come back to bite us again and
again
36
Line Equations
• Let’s quickly review the equations involved in drawing lines
x
y
y0
yend
xendx0
Slope-intercept line equation:
bxmy where:
0
0
xx
yym
end
end
00 xmyb
37
• 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 = 0
m = -1/3
m = -1/2
m = -1
m = -2m = -4
m = ∞
m = 1/3
m = 1/2
m = 1
m = 2m = 4
m = 0
38
• Find corresponding y coordinate for each unit x coordinate
example:
x
y
(2, 2)
(7, 5)
2 7
2
5
39
1
2
3
4
5
0
1 2 3 4 5 60 7
40
x
y
(2, 2)
(7, 5)
2 3 4 5 6 7
2
5
5
3
27
25
m5
42
5
32 b
• First work out m and b:
• Now for each x value work out the y value:
5
32
5
43
5
3)3( y
5
13
5
44
5
3)4( y
5
43
5
45
5
3)5( y
5
24
5
46
5
3)6( y
41
Now just round off the results and turn on these pixels to draw our line
35
32)3( y
35
13)4( y
45
43)5( y
45
24)6( y
0 1 2 3 4 5 6 7 8
0
1
2
3
4
5
6
7
42
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
m
byx
0
0
xx
yym
end
end
00 xmyb
43
Leaving out the details this gives us:
We can see easily that this line doesn’t lookvery good!
We choose which way to work out the line pixels based on the slope of the line
0 1 2 3 4 5 6 7 8
0
1
2
3
4
5
6
7
43
23)3( x 5
3
15)4( x
44
If the slope of a line is between -1 and 1 then we work out the y coordinates for a line based on it’s unit x coordinates
Otherwise we do the opposite – x coordinates are computed based on unit y coordinates
m = 0
m = -1/3
m = -1/2
m = -1
m = -2m = -4
m = ∞
m = 1/3
m = 1/2
m = 1
m = 2m = 4
m = 0
45
#include <stdio.h>#include <graphics.h>
void main(void){
int graphdriver = DETECT, graphmode, color, n, m;
initgraph(&graphdriver, &graphmode, "c:\\tc");/* detect and initialize graphics system */for(n=0; n<50; n++){ putpixel(250+n,350,BLUE); /* draw a horizontal line */}
for(m=0; m<5; m++){ for(n=0; n<5; n++) {
putpixel(250+n,250+m,RED); }}
}
46
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.
*graphdriver : Integer that specifies the graphics driver to be used. You can give graphdriver a value using a constant of the graphics_drivers enumeration type.
47
*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.
pathtodriver : Specifies the directory path where initgraph looks fo graphics drivers (*.BGI) first. þ If they're not there, initgraph looks in the current directory. þ If pathtodriver is null, the driver files must be i the current directory.This is also the path settextstyle searches for the stroked character font files (*.CHR).
*graphdriver and *graphmode must be set to valid graphics_drivers andgraphics_mode values or you'll get unpredictable results. (The exception isgraphdriver = DETECT.)After a call to initgraph, *graphdriver is set to the current graphicsdriver, and *graphmode is set to the current graphics mode.Return Value:initgraph always sets the internal error code.On success, initgraph sets the code to 0 On error, initgraph sets *graphdriver to-2, -3, -4, or -5, and graphresul returns the same value.
48
Towards the Ideal Line
• We can only do a discrete approximation
• Illuminate pixels as close to the true path as possible, consider bi-level display only– Pixels are either lit or not lit
49
What is an ideal line
• Must appear straight and continuous– Only possible axis-aligned and 45o lines
• Must interpolate both defining end points• Must have uniform density and intensity
– Consistent within a line and over all lines– What about antialiasing?
• Must be efficient, drawn quickly– Lots of them are required!!!
50
Simple Line
Based on slope-intercept algorithm from algebra:
y = mx + b
Simple approach:
increment x, solve for y
Floating point arithmetic required
51
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.
52
Modify algorithm per octant
OR, increment along x-axis if dy<dx else increment along y-axis
53
DDA algorithm
• DDA = Digital Differential Analyser– finite differences
• Treat line as parametric equation in t :
)()(
)()(
121
121
yytyty
xxtxtx
),(
),(
22
11
yx
yxStart point -End point -
54
DDA Algorithm• Start at t = 0
• At each step, increment t by dt
• Choose appropriate value for dt
• Ensure no pixels are missed:– Implies: and
• Set dt to maximum of dx and dy
)()(
)()(
121
121
yytyty
xxtxtx
dt
dyyy
dt
dxxx
oldnew
oldnew
1dt
dx1
dt
dy
55
DDA algorithm
line(int x1, int y1, int x2, int y2)
{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;}
}
n - range of t.
56
• Again the values calculated by the equations used by the DDA algorithm must be rounded to match pixel values
(xk, yk) (xk+1, yk+m)
(xk, round(yk))
(xk+1, round(yk+m))
(xk, yk) (xk+ 1/m, yk+1)
(round(xk), yk)
(round(xk+ 1/m), yk+1)
57
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.
58
Observation on lines.
while( n-- ) {draw(x,y);move right;if( below line )move up;}
59
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:
0),( cbyaxyxF
• Easy to prove F<0 for points above the line, F>0 for points below.
60
Testing for the side of a line.
• Need to find coefficients a,b,c.• Recall explicit, slope-intercept form :
• So:
0),( cbyaxyxF
bxdx
dyybmxy so and
0..),( cydxxdyyxF
61
Decision variable.
PreviousPixel(xp,yp)
Choices forCurrent pixel
Choices forNext pixel
Evaluate F at point M
Referred to as decision variable
)2
1,1( pp yxFd
M
NE
E
62
Decision variable.
Evaluate d for next pixel, Depends on whether E or NE Is chosen :
If E chosen :
cybxayxFd ppppnew )2
1()2()
2
1,2(
But recall :
cybxa
yxFd
pp
ppold
)2
1()1(
)2
1,1(
So :
dyd
add
old
oldnew
M
E
NE
PreviousPixel
(xp,yp)
Choices forCurrent pixel
Choices forNext pixel
63
Decision variable.
If NE was chosen :
cybxayxFd ppppnew )2
3()2()
2
3,2(
So :
dxdyd
badd
old
oldnew
M
E
NE
PreviousPixel
(xp,yp)
Choices forCurrent pixel
Choices forNext pixel
64
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
65
Initial value of d.
2
)2
1()1()
2
1,1(
11
1111
bacbyax
cybxayxFdstart
But (x1,y1) is a point on the line, so F(x1,y1) =0
2/dxdydstart
Conventional to multiply by 2 to remove fraction doesn’t effect sign.
2),( 11
bayxF
Start point is (x1,y1)
66
Bresenham algorithm
void MidpointLine(int x1,y1,x2,y2)
{
int dx=x2-x1;
int dy=y2-y1;
int d=2*dy-dx;
int increE=2*dy;
int incrNE=2*(dy-dx);
x=x1;
y=y1;
WritePixel(x,y);
while (x < x2) {if (d<= 0) {
d+=incrE;x++
} else {d+=incrNE;x++;y++;
}WritePixel(x,y);
}}
67
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.
68
Two-step 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
69
Circle drawing.• Can also use Bresenham to draw circles.
• Use 8-fold symmetry
Choices forNext pixel
M
E
SE
PreviousPixel
Choices forCurrent pixel
70
Circle drawing.
• Implicit form for a circle is:
2 2 2) ( ) ( ) , (r y y x x y x fc c
)32(chosen is E If
)522(chosen is SE If
poldnew
ppoldnew
xdd
yxdd
Functions are linear equations in terms of (xp,yp)
–Termed point of evaluation
71
Problems with Bresenham algorithm
• Pixels are drawn as a single line unequal line intensity with change in angle.
Pixel density = n pixels/mm
Pixel density = 2.n pixels/mm
Can draw lines in darker colours according to line direction.-Better solution : antialiasing !-(eg. Gupta-Sproull algorithm)
72
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– DDA algorithm
• Implicit form of line– Only need to test for ‘side’ of line.– Bresenham algorithm.– Can also draw circles.
73
74
75
76
77
78
79
80
Circle Midpoint Algorithm
draw pixels in this octant(draw others using symmetry)
(0,R)
(0,-R)
(R,0)(-R,0)
222),( RyxyxF Implicit function for circle
0),( yxF
0),( yxF
0),( yxF
on circle
inside
outside
x+
y+
81
Choosing the Next Pixel
M
(x, y) (x+1, y)
(x+1, y+1)
E
SE
0)2/1,1( yxF
0)2/1,1( yxF
choose E
choose SE
)2/1,1()( yxFMFddecision variable d
82
Change of d when E is chosen
Mold
(x, y) (x+1, y)
(x+1, y+1)
E
SE
(x+2, y)
(x+2, y+1)
Mnew
32
)2/1()1(
)2/1()2(222
222
xddd
Ryxd
Ryxd
oldnew
old
new
83
Change of d when SE is chosen
Mold
(x, y) (x+1, y)E
SE
(x+1, y+2) (x+2, y+2)
Mnew
522
)2/1()1(
)2/3()2(222
222
yxddd
Ryxd
Ryxd
oldnew
old
new
84
Initial value of d
Rd
RRd
RFd
MFd
4/5
)2/1()1(
)2/1,1(
)(
0
2220
0
00
(0,-R)
M0
(1,-R)
(1,-R+1)
85
Midpoint Circle Algox = 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);
}
86
New Decision Variable
• Our circle algorithm requires arithmetic with real numbers.• Let’s create a new decision variable h
h=d-1/4
• Substitute h+1/4 for d in the code.• Note h > -1/4 can be replaced with h > 0 since h will always have an
integer value.
87
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);
}
88
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).
89
2nd Order Difference when E chosen
• When E chosen, we move from pixel (x,y) to (x+1,y).
2
3)1(2
32
oldnew
new
old
EE
xE
xE
2
5)1(2
5)(2
oldnew
new
old
SESE
yxSE
yxSE
90
2nd Order Difference when SE chosen
• When SE chosen, we move from pixel (x,y) to (x+1,y+1).
2
3)1(2
32
oldnew
new
old
EE
xE
xE
4
5)11(2
5)(2
oldnew
new
old
SESE
yxSE
yxSE
91
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);
}
92
93
94
95
96
97
98
99
100
101
102
103