1 eel 5771-001 introduction to computer graphics ppt2: graphics output primitives by arunbalaji...
TRANSCRIPT
1
EEL 5771-001Introduction to Computer Graphics
PPT2: Graphics Output Primitives
ByArunbalaji Prithiviraj
(U80066848)
2
Outline
• Coordinate Frames• Line Drawing Algorithms• Circle Algorithms• Ellipse Algorithms• Pixel Addressing• Area Fill Methods• Plane Equations• OpenGL Area Fill• Vertex Arrays• Pixel Array• Character Handling
3
Coordinate Frames
4
Coordinate Frames
• Coordinate frames can be any convenient Cartesian coordinate system, called the world-coordinate reference frame, which could be either two dimensional or three-dimensional.
– For instance, we define a straight-line segment with two endpoint positions, and a polygon is specified with a set of positions for its vertices.
• Screen coordinates represents the pixel positions in the frame buffer.– For example, we could specify an integer range for screen positions with the
coordinate origin at the lower-left of a screen area, or we could use non integer Cartesian values for a picture description.
5
Graphics programing: Points
• Plotting points give a coordinate positions in the world reference frame.
– for a random-scan CRT, deflect and activate the electron beam– for a raster scan CRT, set a bit or byte at the appropriate location in the
frame buffer.– Attributes: Size, Color
glPointSize(p);glBegin(GL_POINTS);
glVertex2d(x1, y1);glVertex2d(x2, y2);glVertex2d(x3, y3);
glEnd()
6
Graphics programing: Lines
• Plotting Lines specify the end points coordinates of each line segment– analog devices (random-scan CRTs, etc.)– digital devices (raster-scan CRTs, plasma panels, pen plotters) use pixels
near the true line, resulting in the “jaggies”– Attributes: Color, Thickness, Type
glLineWidth(p);glBegin(GL_LINES);
glVertex2d(x1, y1);glVertex2d(x2, y2);glVertex2d(x3, y3);glVertex2d(x4, y4);
glEnd()
7
Line drawing algorithm
8
Line Drawing Algorithms
• The math behind a simple line drawing algorithm
bxmy .
0
0
xx
yym
end
end
Y intercept
slope
9
Line Drawing Algorithms : Digital differential analyzer (DDA)
• line equation: y = m. x + b
m
m
yx
xy
.
X,Y interval
• DDA (digital differential analyzer)
10
Line Drawing Algorithms : Digital differential analyzer (DDA)
• Algorithm is an incremental scan conversion method.• Based on calculating either or• If |m|<1,
myy
xmy
kK
1
)1( x
11
Line Drawing Algorithms : Digital differential analyzer (DDA)
• If |m|>1, )1( y
If
mxx
m
yx
kK
11
)1( x
myy
xmy
kK
1
12
Line Drawing Algorithms : Bresenham’s algorithm
• Uses only integer calculations for efficiency • Given a pixel which is known, there are really only two possibilities for the next one.• faster than simple DDA• incremental integer calculations
– adaptable to circles, other curves
13
Line Drawing Algorithms : Bresenham’s algorithm
•By testing the sign of an integer parameter, whose value is proportional to the difference between the separation of two pixel positions from the actual line path.
14
Line Drawing Algorithms : Bresenham’s algorithm
15
Line Drawing Algorithms : Bresenham’s algorithm
16
Line Drawing Algorithms : Bresenham’s algorithm
y
yk
yk+1
xk+1
}
}d2
d1
17
Line Drawing Algorithms : Bresenham’s algorithm
d1 = y - yk = m (xk + 1) + b - yk
d2 = (yk + 1) - y = yk + 1 - m (xk + 1) –b
d1 - d2 = 2 m (xk + 1) - 2 yk + 2b -1
18
Line Drawing Algorithms : Bresenham’s algorithm
Pk = ΔX ( d1 - d2) = 2 ΔY . xk - 2 ΔX . yk + c
c = constant = 2 ΔY + ΔX (2b -1)
19
Line Drawing Algorithms : Bresenham’s algorithm
Let's get rid of multiplicationsPk+1 = 2 ΔY . xk+1 - 2 ΔX .y + c
Pk+1 - Pk = 2 ΔY (xk+1 - xk) -2 ΔX (yk+1 - yk) (get rid of the constant)
Pk+1 = Pk + 2 ΔY - 2 ΔX (yk+1 - yk)
Pk+1 = Pk + 2ΔY or = Pk + 2(ΔY – ΔX(
with (yk+1 - yk) = 0 or 1 depending on Pk sign
20
Line Drawing Algorithms : Bresenham’s algorithm
P0 =ΔX (d1-d2) =ΔX[2m(x0+1)-2y0+2b-1] =ΔX[2(mx0+b-y0)+2m-1]
P0=2ΔY -ΔX
21
Line Drawing Algorithms : Bresenham’s algorithm
Example: Digitize the line with endpoint (20,10) and (30,18) ΔX=10 ΔY= 8P0=2ΔY –Δx=6 2ΔY =16 2(ΔY – ΔX)=-4
P0=2ΔY –ΔXPk+1 = Pk + 2ΔY or = Pk + 2(ΔY – ΔX(
22
Line Drawing Algorithms : Bresenham’s algorithm example
23
Problem of Clipping
•Bad idea to rasterize outside of framebuffer bounds •Also, don’t waste time scan converting pixels outside window
24
Clipping: The Real World
•The Sutherland-Hodgeman algorithm (clipping polygons to a region one plane at a time) generalizes to 3-D•The problem: clipping a line segment to an arbitrary plane is relatively expensive
Projectionmatrix;homogeneousdivide
Clip in 2-D screen coordinates
Clip against
hither andyon planes
Transform intoscreen coordinates
25
Circle algorithm
26
Circle Algorithms
•Polar formx = rCosy = rSin (r = radius of circle)
P=(rCos, rSin)
rSin)
rCos)x
y
r
27
Circle Algorithms : Drawing a circle
•The equation of a circle
We could solve for y in terms of x
28
Circle Generating Algorithms
The spacing between plotted pixel positions is not uniform.
29
Circle Generating Algorithms
• Computation can be reduced by considering the symmetry of circles
30
Midpoint circle Algorithms
• As in the line algorithm, we sample at unit intervals and determine the closet pixel position to the circle path at each step.
31
Midpoint circle Algorithms
• Points are generated from 90º to 45º, moves will be made only in the +x and –y direction.
• positive x direction over this octant and use a decision parameter
32
Midpoint circle Algorithms
• We define a circle function:
o
ryxyxfcircle 0
0
),( 222
33
Midpoint circle Algorithms
• Midpoint
• Consider the coordinates of the point halfway between pixel T and pixel S
)21
,1( ii
yx
Midpoint
34
Midpoint circle Algorithms
35
Example
•r = 10
•p0 = 1 – r = -9 (if r is integer round p0 = 5/4 – r to integer)
•Initial point (x0, y0) = (0, 10)
i pi xi+1, yi+1 2xi+1 2yi+1
0 -9 (1, 10) 2 20
1 -6 (2, 10) 4 20
2 -1 (3, 10) 6 20
3 6 (4, 9) 8 18
4 -3 (5, 9) 10 18
5 8 (6, 8) 12 16
6 5 (7, 7)
10 9
8 7 6
5 4 3
2 1 0
0 1 2 3 4 5 6 7 8 9 10
36
Bresenham’s Circle Algorithm
General PrincipleGeneral Principle•The circle function:
and
2 2 2( , )circlef x y x y r
if (x,y) is inside the circle boundary
if (x,y) is on the circle boundary
if (x,y) is outside the circle boundary
0
( , ) 0
0circlef x y
Consider only 45° ≤ ≤ 90
37 37
Bresenham’s Circle Algorithm
p1 p3
p2
D(si)D(ti)
yi
yi - 1
xi xi + 1
r
38
Bresenham’s Circle Algorithm
Define: D(si) = distance of p3 from circle
D(ti) = distance of p2 from circle
i.e. D(si) = (xi + 1)2 + yi2 – r2 [always +ve]
D(ti) = (xi + 1)2 + (yi – 1)2 – r2 [always -ve]
•Decision Parameter pi = D(si) + D(ti)
so if pi < 0 then the circle is closer to p3 (point above) if pi ≥ 0 then the circle is closer to p2 (point below)
39
Bresenham’s Circle Algorithm
x0 = 0y0 = rp0 = [12 + r2 – r2] + [12 + (r-1)2 – r2] = 3 – 2r
if pi < 0 thenyi+1 = yi
pi+1 = pi + 4xi + 6
else if pi ≥ 0 thenyi+1 = yi – 1pi+1 = pi + 4(xi – yi) + 10
•Stop when xi ≥ yi and determine symmetry points in the other octants
xi+1 = xi + 1
40
Bresenham’s Circle Algorithm : Example
10 9 8 7 6
5 4 3
2 1 0
0 1 2 3 4 5 6 7 8 9 10
i pi xi, yi
0 -17 (0, 10)
1 -11 (1, 10)
2 -1 (2, 10)
3 13 (3, 10)
4 -5 (4, 9)
5 15 (5, 9)
6 9 (6, 8)
7 (7,7)
r = 10
p0 = 3 – 2r = -17
Initial point (x0, y0) = (0, 10)
41
Advantages of Bresenham circle
•Only involves integer addition, subtraction and multiplication•There is no need for squares, square roots and trigonometric functions
42
Ellipse algorithm
43
Ellipse Algorithm
)cos(xc rxx
)sin(yc ryy
44
Ellipse Algorithm
45
Example: Midpoint Ellipse Algorithm
46
Example: Midpoint Ellipse Algorithm
47
Pixel addressing
Setting frame-buffer values
• After generating a line or circle using scan conversion, we need to know where is the specified pixels are stored in the frame buffer.
• Frame buffer is stored in memory as an addressable array and stored row by row.
• Pixels are labeled from (0, 0) the lower left corner to (xmax,ymax) the top right corner.
48
Row by Row mapping
49
Setting frame-buffer values (cont.)
• For a bilevel system (1 bit per pixel),the Frame-Buffer bit address for pixel position (x, y) is calculated as:
1. addr(x, y) = addr (0,0) + y (xmax +1) + x
2. addr(x + 1, y) = addr(x, y) + 1
3. addr(x + 1, y + 1) = addr(x, y) + xmax + 2
50
Setting frame-buffer values (cont.)
• Example:Find the address of the pixel (6,5), where the address of (0,0) =500, xmax=12, and ymax = 10.
addr(x, y) = addr (0,0) + y(xmax +1) + x
addr(6, 5) = 500 + 5(12 + 1) + 6 = 571
addr(7, 5) = addr(6, 5) + 1 = 572addr(7, 6) = addr(6, 5) + xmax + 2 = 585
51
Pixel addressing and object geometry
• When an object is scan converted into the frame buffer, the input description is transformed to pixel coordinates.
• So, the displayed image may not correspond exactly with the relative dimensions of the input object.
• To preserve the specified geometry of world objects, we need to compensate for the mapping of mathematical input points to finite pixel area, we use one of the two ways:
52
Pixel addressing and object geometry (cont.)
1) Adjust the dimensions of displayed objects to account for the amount of overlap of pixel areas with the object boundaries.
(i.e. a rectangle with 40 cm width, will be displayed in 40 pixel)
2) Map world coordinates onto screen positions between pixels, so that we align objects boundaries with pixel boundaries instead of pixel centers.
53
Pixel addressing and object geometry (cont.)
Screen Grid Coordinates:
An alternative to addressing display positions in terms of pixel centers is to reference screen coordinates with respect to the grid of horizontal and vertical pixel boundary lines spaced one unit a part.
Pixel addressing and object geometry (cont.)
• Screen coordinate position is then the pair of integer values identifying a grid intersection position between two pixels.
• For example, the mathematical line path for a polyline with screen endpoints (0, 0), (5, 2), and (1,4) is shown beside.
Pixel addressing and object geometry (cont.)
• With the coordinate origin at the lower left of the screen, each pixel area can be referenced by the integer grid coordinates of its lower left corner.
• The following figure illustrates this convention for an 8 by 8 section of a raster, with a single illuminated pixel at screen coordinate position (4, 5).
Pixel addressing and object geometry (cont.)
• In general, we identify the area occupied by a pixel with screen coordinates ( x, y) as the unit square with diagonally opposite corners at (x, y) and ( x + 1, y + 1 ).
• This pixel addressing scheme has several advantages:1. It avoids half-integer pixel boundary2. It facilitates precise object representations.3. Simplifies the processing involved in many scan conversion
algorithms and in other raster procedures
57
Pixel addressing and object geometry (cont.)
Notes:
1) The previous algorithms for drawing line, circle, …etc are still valid when applied to input positions expressed as screen grid coordinates.
2) The decision parameter Pk is a measure of screen grid separation differences rather than separation differences from pixel centers.
58
Pixel addressing and object geometry (cont.)
A circle of radius 5 and center position (10, 10), for instance, would be displayed by the midpoint circle algorithm using screen grid coordinate positions.
But the plotted circle has a diameter of 11, To plot the circle with the defined diameter of 10, we can modify the circle algorithm to shorten each pixel scan line and each pixel column.
59
Pixel addressing and object geometry (cont.)
60
Pixel addressing and object geometry (cont.)
61
62
Area Fill Methods
Point and Line primitives
GL_POINTS: sequence of points
GL_LINES: sequence of line segments
GL_LINE_STRIP: polyline
GL_LINE_LOOP: closed polyline
12
34
1
2
3
4 5
6 7
8
1
2
3
4
5
1
23
4
5
Area fill primitives 1
• Point, line and curve, fill area• Usually polygons• 2D shapes and boundary 3D objects
Area fill primitives 2
• Approximation of curved surface:– Surface mesh or Surface tesselation– Face or facet (planar), patch (curved)
Polygon
• Polygon: – Planar shape, defined by a sequence of three or more vertices,
connected by line segments (edges or sides)– Standard or simple polygon: no crossing edges
bowtie polygon
Regular polygon
• Vertices uniformly distributed over a circle:
NiNirNiryx ii ...1)),/2sin(),/2cos((),( PP
r
Convex vs. concave 1
• Convex: – All interior angles < 180 graden, and– All line segments between 2 interior points in polygon, and– All points at the same side of line through edge, and– From each interior point complete boundary visible
• Concave: not convex
Convex vs. concave 2
• Puzzle:
Given a planar polygon in 3D, with vertices Pi, with i = 1, … , N.
Give a recipe to determine if the polygon is convex or concave.
Convex vs. concave 3
• Puzzle:
Given a planar polygon in 3D, with vertices Pi, with i = 1, … , N.
Give a recipe to determine if the polygon is convex or concave.
Solution: (multiple solutions possible)
. that here Assume concave. else
convex, then ,
with,,...,1 allfor 0 If
iNi
i1i1iii
i1i
PP
)P(P)P(PQ
Ni
Splitting concave polygons
.en through line thealongpolygon Split the
concave. is angle Suppose, concave.or convex
iscorner if determine and verticesalongWalk
i1i
1ii1i
PP
PPP
iP
1iP
1iP
Convex polygon triangles
Repeat Pick three succeeding points; Join the outside ones with a line; Remove the middle pointUntil three points are left over
OpenGL Area Fill
glBegin(GL_POLYGON); // Specify what to draw, // a polygon
// Geometric info via vertices: glVertex*(); // 1 glVertex*(); // 2 ... // ...glEnd;
glVertex[234][isfd]
[234]: 2D, 3D, 4D[isfd]: integer, short, float, doubleFor instance: glVertex2i(100, 25);
OpenGL Area Fill
GL_POLYGON: convex polygon
Concave polygons give unpredictable results.
1
2
34
5
1
3
45
OpenGL Area Fill
• GL_TRIANGLES: sequence of triangles
• GL_TRIANGLE_STRIP:
• GL_TRIANGLE_FAN:
1
2
3
4
56
7
8
9
1
2
3
4
5
6
7
8
1
23
4
5
6
OpenGL Area Fill
• GL_QUADS: sequence of quadrilaterals
• GL_QUAD_STRIP: strip of quadrilaterals
1
2
34
5 6
78
9
1011
12
1
2
3
4
5
6
7
8
OpenGL output functions
glBegin(GL_LINES); // Specify what to draw, // here lines// Geometric info via vertices: glVertex*(); // 1 glVertex*(); // 2 ... // ...glEnd;glVertex[234][isfd][234]: 2D, 3D, 4D[isfd]: integer, short, float, doubleFor instance: glVertex2i(100, 25);
Vertex Array
• OpenGL provides a facility called vertex arrays that allows us to store array data in the implementation
• Six types of arrays supported– Vertices– Colors– Color indices– Normals– Texture coordinates– Edge flags
• We will need only colors and vertices
79
Character Handling
•Letters, numbers, and other character can be displayed in a variety of size and styles.
•Typeface: The overall design style for a set of characters is call typeface: Zar, nazanin, Titr.
•Font: Referred to a set of cast metal character forms in a particular size and forma: 10 point Zar.
•Two different representation are used for storing computer fonts:– Bitmap font (or bitmapped font)
– Outline font
80
Bitmap Handling
• Bitmap font (or bitmapped font): A simple method for representing the character shapes in a particular typeface is to use rectangular grid pattern.
81
• The character grid only need to be mapped to a frame buffer position.
• Bitmap fonts required more space, because each variation (size and format) must be stored in a font cash.
Bold Italic
82
Outline Font
• Graphic primitives such as lines and arcs are used to define the outline of each character.
• Require less storage since variation does not require a distinct font cash.
• We can produce boldface, italic, or different size by manipulating the curve definition for the character outlines.
• It does take more time to process the outline fonts, because they must be scan converted into frame buffer.
83
References
• Hughes, John F., et al. Computer graphics: principles and practice. Pearson Education, 2013.
• Van Dam, Andries, et al. Introduction to computer graphics. Vol. 55. Reading: Addison-Wesley, 1994.
• Hearn, Donald, and M. Pauline Baker. "Computer Graphics with Open GL, 3/E."ISBN: 0-13-015390-7, Prentice Hall (2004).
• Van Aken, Jerry R. "An efficient ellipse-drawing algorithm." Computer Graphics and Applications, IEEE 4.9 (1984): 24-35.
• Wright, William E. "Parallelization of Bresenham's line and circle algorithms."Computer Graphics and Applications, IEEE 10.5 (1990): 60-67.