computer graphics- scc 342 chapter 2: overview of graphics systems chapter 3: graphics output...
TRANSCRIPT
Computer Graphics- SCC 342
Chapter 2: Overview of Graphics SystemsChapter 3: Graphics Output Primitives
Chapter 2: Overview of Graphics Systems
2.1 Video Display Devices
2.2 Raster-Scan Systems
Overview of a graphics system
3
Overview of a graphics system
Input devices include Pointing/locator devices: indicate location on screen
– Mouse/trackball/spaceball
– Data tablet
– Joystick
– Touch pad and touch screen Keyboard device: send character input Choice devices: mouse buttons, function keys
4
2.1 Video Display Devices
5
Cathode-ray-tube (CRT)
CRT basics: The screen output is stored in the frame buffer and
is converted into voltages across the reflection plates
via a digital-to-analog converter (DAG) Light is emitted when electrons hit phosphor But light output from the phosphor decays
exponentially with time, typically in 10 – 60
microseconds
– Thus the screen needs to be redrawn or refreshed
– Refresh rate is typically 60 Hz to avoid flicker (“twinkling”)
– Flicker: when the eye can no longer integrate individual light pulses from a point on screen, e.g., due to low refresh rate
6
2.1 Video Display Devices
Shadow-mask color CRTs Three different colored phosphors (R, G, B) dots are arranged in very small groups (triads) on
coating
We see a mixture of three
colors
Three electron guns (R, G,
B) emit electron beams in a
controlled fashion so that
only phosphors of the
proper colors are excited
7
2.1 Video Display Devices
2.1 Video Display Devices
Raster – Scan Display
8
2.1 Video Display Devices
The Frame Buffer Stores per-pixel information
– Depth of a frame buffer: number of bits per pixel
– E.g. for color representation, 1 bit => 2 colors,
8 bits => 256 colors,
24 bits => true color (16 million colors) Color buffer is only one of many buffers, other
information, e.g., depth, can also be used Implemented with special type of memory in
standard PCs or on a graphics card for fast
redisplay Part of standard memory in earlier systems
9
2.1 Video Display Devices
Raster-scan basics: The screen is a rectangular array
of picture elements, or pixels Resolution: determines the
details you can see
number of pixels in an image, e.g.,
1024×768, 1280x1024, 1366 x 768, etc. also in ppi or dpi – pixel or dot per inch
10
Raster-Scan Pattern: Horizontal scan rate: # scan lines per second Interlaced (TV) vs. non-interlaced displays
11
2.1 Video Display Devices
2.1 Video Display Devices
Random-Scan Display Also called Vector-scan display Pictures are generated as line drawings
12
2.2 Raster-Scan Systems
Simple raster-graphics system Video controller (display processor) controls
operations of display device.
13
Raster system with fixed portion of main memory reserved for the frame buffer.
14
2.2 Raster-Scan Systems
2.2 Raster-Scan Systems
Video controller
15
2.2 Raster-Scan Systems
Raster – scan display processor
- Digitizes picture definition into a set of pixel values for storage in the frame buffer (scan conversion).
16
2.2 Raster-Scan Systems
Additional functions for Raster – scan display processor
- generating line styles
- display color areas
- transformations
- interface with interactive devices.
17
Chapter 3: Graphics Output Primitives
3.5 Line Drawing Algorithms
3.9 Circle- Generating Algorithms
3.13 Pixel Addressing and Object Geometry
3.14 Fill Area Primitives
3.15 Polygon Fill Area
19
3.5 Line Drawing Algorithms
2D Graphics Primitives Graphics drawing is based on basic geometric
structures called graphics primitives Points Lines Circles Conic Sections
3.5 Line Drawing Algorithms
Drawing Points Input specification
Coordinates (x, y) of the point Color Specification
Procedure Write the required color value to the corresponding
position of the frame buffer Color can be specified depending upon the graphics hardware interface
e.g. RGB For a black and white screen whenever a value of 1 is encountered the
graphics system turns the corresponding pixel on
Function Call setPixel (x,y): To turn the pixel (x,y) on getPixel (x,y): To get the pixel value for (x,y)
20
3.5 Line Drawing Algorithms
Drawing Points Output
21
3.5 Line Drawing Algorithms
Drawing lines What is a line?
A set of connected points satisfying y = mx+c
22
Δx
Δy
c
(x1,y1)
(x2,y2)
2 1
2 1
1 1
y y ym
x x x
c y mx
3.5 Line Drawing Algorithms
In graphics, usually we are given the end points (xa,ya) and (xb,yb) of the line for drawing
A simple algorithm Calculate the slope
Start with (xa,ya) k=1 and set xk+1=xk+1
Then find Put a pixel at (xk+1 , round(yk+1))
Continue till the other xb end point is reached
|m|<1 ensures that the points will be continuous
23
1 1
b a
b a
k k k k
y y ym
x x x
y m x
y y m x x
(xa,ya)
(xb,yb)
1k ky y m
The digital differential analyzer (DDA) line drawing algorithm
3.5 Line Drawing Algorithms
For |m|>1 Gaps appear
Solution Use
Start with (xa,ya) and set yk+1=yk+1
Put a pixel at (round(xk+1) , yk+1)
Continue until the end point y2 is reached
24
(xa,ya)
(xb,yb)
11
k kk k
y yx x
m
1
1k kx x
m
3.5 Line Drawing Algorithms
25
(xa,ya)
3.5 Line Drawing Algorithms
DDA Characteristics Efficiency
Better than direct line drawing y=mx+c Eliminates Multiplication
But uses rounding off which is an expensive operation
Uses floating point operations Round off errors can cause the line to drift away
from the true line segment Especially for long lines Can be a problem for systems with low precision
numeric representation
26
3.5 Line Drawing Algorithms
Round off Error Accumulation for DDA
27
Drift caused by round-off error accumulation
Intended Line
3.5 Line Drawing Algorithms
The Bresenham Line Algorithm The Bresenham algorithm is
another incremental scan conversion algorithm
The big advantage of this algorithm is that it uses only integer calculations
Jack Bresenham worked for 27 years at IBM before entering academia. Bresenham developed his famous algorithms at IBM in the early 1960s
28
3.5 Line Drawing Algorithms
Bresenham Algorithm: Main Idea Move across the x axis in unit intervals and at each
step choose between two different y coordinates to select the point closest to original line
2 3 4 5
2
4
3
5
(xk, yk)
(xk+1, yk)
(xk+1, yk+1)For example, from position (2, 3) we have to choose between (3, 3) and (3,
4)
We would like the point that is closer to the original line
29
3.5 Line Drawing Algorithms
At sample position xk+1 the vertical separations from the mathematical line are labelled dupper and dlower
The y coordinate on the mathematical line at xk+1 is:
bxmy k )1(
y
yk
yk+1
xk+1
dlower
dupper
Our assumption here is that the line has a positive
slope less than one 30
Deriving The Bresenham Line Algorithm
3.5 Line Drawing Algorithms
So, dupper and dlower are given as follows:
and:
We can use these to make a simple decision about which pixel is closer to the mathematical line
klower yyd
kk ybxm )1(
yyd kupper )1(
bxmy kk )1(1
31
Deriving The Bresenham Line Algorithm (cont…)
3.5 Line Drawing Algorithms
This simple decision is based on the difference between the two pixel positions:
Let’s substitute m with ∆y/∆x where ∆x and ∆y are the differences between the end-points:
122)1(2 byxmdd kkupperlower
)122)1(2()(
byxx
yxddx kkupperlower
)12(222 bxyyxxy kk
cyxxy kk 22Constant terms collected in ‘c’
Since in our case Δx is positive therefore this term has the same sign
as dlower-dupper
32
Deriving The Bresenham Line Algorithm (cont…)
3.5 Line Drawing Algorithms
So, a decision parameter pk for the kth step along a line is given by:
The sign of the decision parameter pk is the same as that of dlower – dupper
If pk is negative, then we choose the lower pixel, otherwise we choose the upper pixel
cyxxy
ddxp
kk
upperlowerk
22
)(
33
Deriving The Bresenham Line Algorithm (cont…)
Remember coordinate changes occur along the x axis in unit steps so we can do everything with integer calculations
At step k+1 the decision parameter is given as:
Subtracting pk from this we get:
3.5 Line Drawing Algorithms
cyxxyp kkk 111 22
)(2)(2 111 kkkkkk yyxxxypp
34
Deriving The Bresenham Line Algorithm (cont…)
But, xk+1 is the same as xk+1 so:
where yk+1 - yk is either 0 or 1 depending on the sign of pk
The first decision parameter p0 is evaluated at (x0, y0) is given as:
3.5 Line Drawing Algorithms
1 12 2 ( )k k k kp p y x y y
0 2p y x
0 0 0
0 0
0 0
0 0
2 2
2 2 2 (2 1)
2 2 2 (2 1)
2 2 2 (2 1)
2 2 2 (2 1)
2 2 2 2 (2 1)
2
k k k
k k
p y x x y c
y x x y y x b
p y x x y y x b
y x x m x b y x b
yy x x x b y x b
x
y x y x x b y x b
y x
35
Deriving The Bresenham Line Algorithm (cont…)
BRESENHAM’S LINE DRAWING ALGORITHM (for |m| < 1.0)
1. Input the two line end-points, storing the left end-point in (x0, y0)
2. Plot the point (x0, y0)
3. Find:
4. At each xk along the line, starting at k = 0, perform the following test. If pk < 0, the next point to plot is (xk+1, yk) and:
Otherwise, the next point to plot is (xk+1, yk+1) and:
5. Repeat step 4 (Δx – 1) times
xyp 20
ypp kk 21
xypp kk 221
36
3.5 Line Drawing Algorithms
3.5 Line Drawing Algorithms
Bresenham Example Let’s have a go at this Let’s plot the line from (20, 10) to (30, 18) First off calculate all of the constants:
Δx: 10 Δy: 8 2Δy: 16 2Δy - 2Δx: -4
Calculate the initial decision parameter p0:
p0 = 2Δy – Δx = 6
37
17
16
15
14
13
12
11
10
18
292726252423222120 28 30
k pk (xk+1,yk+1)
0
1
2
3
4
5
6
7
38
Bresenham Example
3.5 Line Drawing Algorithms
Generalization of the Bresenham Line Drawing Algorithm
For lines with slopes greater than 1 Inter-change the roles of the x and y directions
Take a unit step in y direction and calculate x values nearest to the line path
For lines with negative slopes Procedure is generally similar except that now one
coordinate decreases as the other increases Vertical, Horizontal and Diagonal lines
Can be drawn as special cases without processing by the line plotting method
Interchange of the starting and ending points can be checked
39
3.5 Line Drawing Algorithms
Characteristics of Bresenham Line Drawing Algorithm
Uses only integer calculations More efficient Paves the way for making more complex curves
based upon a similar logic Take the screen pixel closest to the original curve
40
3.5 Line Drawing Algorithms
3.9 Circle- Generating Algorithms
Properties of circles: A Circle is the set of points that are all at a given
distance r (called radius) from a center point (xc,yc)
The equation for a circle is:
The circle is a frequently used component in pictures and graphics
41
2 2 2c cx x y y r
3.9 Circle- Generating Algorithms
A circle can be drawn by using the following equation by moving in unit steps from xc-r to xc+r and calculating the corresponding y value at each position
42
22c cy y r x x
3.9 Circle- Generating Algorithms
Problems with this approach Computations Spacing between points is not uniform
Reason Absolute value of the slope exceeds 1
Solution-1 Switch the roles of x and y when the absolute
value of slope exceeds 1 Increment y by one unit and calculate
corresponding x
Still computationally complex
43
3.9 Circle- Generating Algorithms
Problems… Solution-2
Use parametric polar form of the circle
Start with initial angle equal to zero and increment the angle to get and then plot the next point
The increment in the angle should be small (1/r) to ensure a continuous boundary or if a large step is used then the points can be connected by straight lines to approximate a circular boundary
44
cos
sin
c
c
x x r
y y r
3.9 Circle- Generating Algorithms
Reducing computational complexity Using 8-way symmetry
However, both the algorithms described earlier involve complex floating point calculations
45
3.9 Circle- Generating Algorithms
The Midpoint Circle Drawing Algorithm The midpoint circle algorithm has been
developed and patented by J. Bresenham Characteristics of MPCA
Incremental in nature Uses a decision parameter to find the pixel
closest to the circumference of the original circle
Uses (almost all) integer operations Uses 8-way symmetry to reduce computations
46
3.9 Circle- Generating Algorithms
Main Idea Assume we are drawing the
2nd octant of the circle Assume we have just plotted
point (xk,yk)
We have two choices for the next point (xk+1,yk) and (xk+1,yk-1)
This decision is made by calculating whether (xk+1,yk-1/2) is outside the circle or inside it
47
3.9 Circle- Generating Algorithms
Main Idea This can be done by using the circle function
based on the equation of the circle
Thus our decision parameter is
48
0,
( , ) 0,
0,circf x y
boundary circle theinside is ),( if yx
boundary circle on the is ),( if yx
boundary circle theoutside is ),( if yx
222 )21()1(
)21,1(
ryx
yxfp
kk
kkcirck
3.9 Circle- Generating Algorithms
Main Idea We decide yk+1 on the basis of pk
pk+1 is given by,
49
1
if 0
1k k
kk
y py
y else
1 1 1
2 2 21
22 21
1( 1, )211 1 ( )2
11 1 2 1 2
k circ k k
k k
k k k
p f x y
x y r
x x y r
- 1
3.9 Circle- Generating Algorithms
50
2 2 2
22 21 1
22 21 1
2 2 2
22
1
2 22 2
1 1
2 21 1
1( 1) ( )2
11 1 2 1 2
11 1 2 1 2
1( 1) ( )2
1 12 1 1 ( )2 2
1 12 1 1 4 4
2 1 1
k k k
k k k k
k k k k k
k k
k k k
k k k k k
k k k k k
p x y r
p x x y r
p p x x y r
x r y
x y y
x y y y y
x y y y y
2 2k+1 k k+1 k k+1 k kp = p + y - y - y - y + 2 x +1 +1
3.9 Circle- Generating Algorithms
The initial decision parameter can be obtained by evaluating the circle function at (x0,y0-1/2)=(0,r-1/2)
51r
rr
rfp circ
45
)21(1
)21,1(
22
0
2 21 1 1
1
1
1
1
- - -
if 0
1
2 1 2 1 i
2
f 0
1
1
2
1
k kk
k
k k k
k k k k k k
kk
k
k
k
y py
y else
p x y p
p x el
p p y y y y x
pse
If the redius is an integer then 5/4
can be rounded off to 1 to ensure
integer operations
- 1
The Mid-Point Circle Algorithm…
1. Input radius r and circle centre (xc, yc), then set the coordinates for the first point on the circumference of a circle centred on the origin as:
2. Calculate the initial value of the decision parameter as:
3. Starting with k = 0 at each position xk, perform the following test. If pk < 0, the next point along the circle centred on (0, 0) is (xk+1, yk) and:
),0(),( 00 ryx
rp 45
0
12 11 kkk xpp
52
The Mid-Point Circle Algorithm…
Otherwise the next point along the circle is (xk+1, yk-1) and:
4. Determine symmetry points in the other seven octants
5. Move each calculated pixel position (x, y) onto the circular path centred at (xc, yc) to plot the coordinate values:
6. Repeat steps 3 to 5 until x >= y
111 212 kkkk yxpp
cxxx cyyy
53
3.9 Circle- Generating Algorithms
Mid-Point Circle Algorithm Example To see the mid-point circle algorithm in action
lets use it to draw a circle centred at (0,0) with radius 10
54
Mid-Point Circle Algorithm Example (cont…)
9
76543210
8
976543210 8 10
10 k pk (xk+1,yk+1) 2xk+1 2yk+1
0
1
2
3
4
5
655
Mid-Point Circle Algorithm Exercise
Use the mid-point circle algorithm to draw the circle centred at (0,0) with radius 15
56
3.13 Pixel Addressing and Object Geometry
For displaying objects, their mathematical points are converted into pixel coordinates.
For preserving object geometry, display objects to correspond to dimensions given by mathematical points.
Screen grid coordinstes
57
3.13 Pixel Addressing and Object Geometry
Raster-scan algorthims refrenced the center of screen pixel position
Anothe refrencing method: pixel boundary
Area occubied by pixel at (x,y) = area of unit square with diagonally oppsite corners at (x,y) and (x+1,y+1)
Advatages:- Avoids half-integer pixel boundries- Facilitats precies object representation- Simplifies processing of many scan-
conversion steps.
58
3.14 Fill Area Primitives
Most graphics routins require the fill area to specified as polygon
- their boundries are described by linear equations.
- most surface can be approximated by polygons (called surface tessellation)
59
3.15 Polygon Fill Area
Polygons: plane figure specified by three or more vertices and connected by edges
Has all vertices within plane- no edge crossing Polygon classification:
- Convex: interior angle = or less than 180 degrees
- Concave: interior angle greater than 180 degrees
Implementations for filling algorithms are complicated for concave polygons
60
convexconcave
3.15 Polygon Fill Area
Identifying concave polygons
- extension of some edges will intersect other edges
- take a look at the polygon vertex positions relative to the extension line of any edge: some vertices are on one side and some are on the other
61
3.15 Polygon Fill Area
- Split concave polygons into a set of convex polygons
- Using edge vectors and edge cross products
- Use vertex position relative to edge extension line.
62
Splitting concave polygons
3.15 Polygon Fill Area
63
Splitting concave polygons Assume all polygons in xy plane Ek = Vk+1 – Vk
Calculate cross product of successive edge vectors
- if at least z component of one vector is negative, then concave polygon.
- no successive vertices are collinear. Split along the line of the first edge vector in the
cross product pair.
3.15 Polygon Fill Area
Splitting concave polygons
64
3.15 Polygon Fill Area
Splitting concave polygons
65
3.15 Polygon Fill Area
Splitting concave polygons Another method is a rotational method
66
3.15 Polygon Fill Area
67
Splitting convex polygons into triangle (tessellation)
- define any sequence of three consecutive vertices to a triangle.
- delete middle vertex from the original list
- continue forming triangle
- stop when only three vertices are remaining.
3.15 Polygon Fill Area
Inside-Outside test Also called odd-even test
68
P
Q
3.15 Polygon Fill Area
Winding-Number test Count counterclockwise encirclements of point
69
P
Q
3.15 Polygon Fill Area
Polygon tables objects in a scene are described as sets of
polygon surface facets Object description includes:
- Coordinate information (geometry for the polygon facets)
- Surface parameters(color, transparency, and light-reflection properties)
Object information are organized in tables.
70
3.15 Polygon Fill Area
Polygon tables
71
3.15 Polygon Fill Area
Polygon tables Important for check for consistency and
completeness:
1. each pixel is an end point for at least two edges.
2. every edge is part of at least one polygon
3. every polygon is closed
4. each polygon has at least one shared edge
72