Sie sind auf Seite 1von 103

Computer Graphics

1
Introduction

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.

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)

Major driving force now

7
Games are very important in Computer Graphics

8
Medical Imaging is another driving force

Much financial support


Promotes linking of graphics with video, scans, etc.
9
Computer Aided Design too

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]

→ →

→ high-X imaging (dynamic range, resolution, depth of field,…)15


• negative film = 130:1 (7 stops)
• paper prints = 46:1
• [Debevec97] = 250,000:1 (18 stops)

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

• 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 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
20
– Crow (1977) - anti-aliasing
• early 1980s - global illumination
– Whitted (1980) - ray tracing
– Goral, Torrance et al. (1984), Cohen (1985) -
radiosity
– Kajiya (1986) - the rendering equation

21
• 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 22
• 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

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

• 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

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?

• How do we choose which pixels to turn on?


34
• 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

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

• Now for each x value work out the y value:

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

We can see easily that 7


this line doesn’t look 6
very good!
5
We choose which way
to work out the line 4
pixels based on the 3
slope of the line 2
1
0
0 1 2 3 4 5 6 7 8
43
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=∞
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;

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);
}
}

}
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.

*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.

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.

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 and


graphics_mode values or you'll get unpredictable results. (The exception is
graphdriver = DETECT.)
After a call to initgraph, *graphdriver is set to the current graphics
driver, 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 47
*graphdriver to-2, -3, -4, or -5, and graphresul returns the same value.
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
48
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!!!

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

OR, increment along x-axis if dy<dx else increment along y-axis

52
DDA algorithm

Start point - ( x1 , y1 ) x(t ) = x1 + t ( x2 − x1 )


End point - ( x2 , y2 ) y (t ) = y1 + t ( y2 − y1 )

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

(xk+1, round(yk+m)) (round(xk+ 1/m), yk+1)

(xk, yk) (xk+ 1/m, yk+1)


(xk+1, yk+m) (xk, yk)

(xk, round(yk)) (round(xk), yk)

56
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.

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

Previous Choices for Choices for


Pixel Current pixel Next pixel
(xp,yp)

61
Decision variable.

Evaluate d for next pixel, Depends on whether E or NE Is chosen :

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

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.

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 ½

Slope between ½ and 1

Slope between 1 and 2

Slope greater than 2

68
Circle drawing.

M
SE

Previous Choices for Choices for


Pixel Current pixel Next pixel

69
Circle drawing.

f
(
x
,
y
)
=
(
x
x
c

2
(
y
y
c

2
r

If SE is chosen d new = d old + (2 x p − 2 y p + 5)


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

70
Problems with Bresenham algorithm

• Pixels are drawn as a single line ⇒ unequal


line intensity with change in angle.

Pixel density = √2.n pixels/mm


Can draw lines in darker colours
according to line direction.
-Better solution : antialiasing !
-(eg. Gupta-Sproull algorithm)

Pixel density = n pixels/mm


71
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.

72
73
74
75
76
77
78
79
Circle Midpoint Algorithm

x+ draw pixels in this octant


(0,-R) (draw others using symmetry)
y+
Implicit function for circle
F ( x, y ) = x 2 + y 2 − R 2

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

(x, y) (x+1, y) d old = ( x + 1) + ( y + 1 / 2) − R


2 2 2

E
∆d = d new − d old = 2 x + 2 y + 5
Mold

SE

Mnew

(x+1, y+2) (x+2, y+2)


83
Initial value of d
(0,-R) (1,-R)

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

• 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.

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

• When E chosen, we move from pixel (x,y) to (x+1,y).

∆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

Das könnte Ihnen auch gefallen