Lect04 Slides
-
Upload
jatin-kumar -
Category
Documents
-
view
232 -
download
0
Transcript of Lect04 Slides
11
TCS2111COMPUTER GRAPHICS
LECTURE 4LECTURE 42D GRAPHICS ALGORITHMS2D GRAPHICS ALGORITHMS
22
Note for the students:Note for the students: These slides are meant for the lecturers to These slides are meant for the lecturers to
conduct lectures only. It is conduct lectures only. It is NOTNOT suitable to suitable to be used as a study material.be used as a study material.
Students are expected to study by reading Students are expected to study by reading the textbook for this course:the textbook for this course:
Interactive Computer Graphics – A Top Interactive Computer Graphics – A Top Down Approach Using OPENGL (5th Down Approach Using OPENGL (5th
Edition) by Edward AngelEdition) by Edward Angel
The textbook is available from MMU The textbook is available from MMU Bookstore.Bookstore.
Reading Assignments are given at the end Reading Assignments are given at the end of these slides.of these slides.
33
Content: 2D Graphics AlgorithmsContent: 2D Graphics Algorithms
Output Primitives Line Drawing Algorithms
Analytical Method DDA Algorithm Midpoint Algorithm Bresenham's Algorithm
Circle Drawing Algorithms Midpoint Circle Algorithm
Anti-aliasing Fill-Area Algorithms
(Extra note for self-study!)
44
Output Primitives
55
The basic objects out of which a graphics display is created are called.
Describes the geometry of objects and – typically referred to as geometric primitives.
Examples: point, line, text, filled region, images, quadric surfaces, spline curves
Each of the output primitives has its own set of attributes.
Output PrimitivesOutput Primitives
66
Output PrimitivesOutput Primitives
Points Attributes: Size, Color.
glPointSize(p);glBegin(GL_POINTS);
glVertex2d(x1, y1);
glVertex2d(x2, y2);
glVertex2d(x3, y3);
glEnd()
v1 v2
v3
77
Output PrimitivesOutput Primitives Lines
Attributes: Color, Thickness, Type
glLineWidth(p);glBegin(GL_LINES);
glVertex2d(x1, y1);
glVertex2d(x2, y2);
glVertex2d(x3, y3);
glVertex2d(x4, y4);
glEnd()
v1
v2 v3
v4
88
Polylines (open) A set of line segments joined end to end. Attributes: Color, Thickness, Type
glLineWidth(p);
glBegin(GL_LINE_STRIP);glVertex2d(x1,
y1);glVertex2d(x2,
y2);glVertex2d(x3,
y3);glVertex2d(x4,
y4);glEnd()
Output PrimitivesOutput Primitives
v1
v2
v3
v4
99
Polylines (closed) A polyline with the last point connected to the
first point . Attributes: Color, Thickness, Type
Note: A closed polyline cannot be filled.
glBegin(GL_LINE_LOOP);glVertex2d(x1, y1);glVertex2d(x2, y2);glVertex2d(x3, y3);glVertex2d(x4, y4);
glEnd()
Output PrimitivesOutput Primitives
v1
v2
v3
v4
1010
Polygons A set of line segments joined end to end. Attributes: Fill color, Thickness, Fill pattern
Note: Polygons can be filled
glBegin(GL_POLYGON);glVertex2d(x1, y1);glVertex2d(x2, y2);glVertex2d(x3, y3);glVertex2d(x4, y4);
glEnd()
Output PrimitivesOutput Primitives
v1
v2
v3
v4
1111
TextAttributes: Font, Color, Size,
Spacing, Orientation.Font:
Type (Helvetica, Times, Courier etc.) Size (10 pt, 14 pt etc.) Style (Bold, Italic, Underlined)
Output PrimitivesOutput Primitives
1212
ImagesAttributes: Image Size, Image
Type, Color Depth. Image Type:
Binary (only two levels) Monochrome Color.
Color Depth:Number of bits used to represent
color.
Output Primitives
13
TCS2111
Output Primitive AttributesPoint Size
ColorLine Thickness (1pt, 2pt …)
Type (Dashed, Dotted, Solid)Color
Text Font (Arial, Courier, Times Roman…)Size (12pt, 16pt ..)SpacingOrientation (Slant angle)Style (Bold, Underlined, Double lined)Color
Filled Region Fill PatternFill Type (Solid Fill, Gradient Fill)Fill Color
Images Color Depth (Number of bits/pixel)
Output Primitives (Summary)Output Primitives (Summary)
1414
Line Drawing Algorithms
1515
Line DrawingLine Drawing• Line drawing is fundamental to computer graphics.• We must have fast and efficient line drawing functions.
Rasterization Problem: Given only the two end points, how to compute the intermediate pixels, so that the set of pixels closely approximate the ideal line.
1616
Line Drawing - Analytical MethodLine Drawing - Analytical Method
y y
x x
y x
b am
b ac a ma
y
x
y=mx+c
ax bx
A(ax,ay)
B(bx,by)
1717
• Directly based on the analytical equation of a line. • Involves floating point multiplication and addition• Requires round-off function.
//Given (ax, ay) and (bx, by)double m = (double)(by-ay)/(bx-ax);double c = ay - m*ax;
double y; int iy;
for (int x=ax; x<=bx; x++) { y = m*x + c;
iy = round(y); setPixel(x, iy);}
Line Drawing - Analytical MethodLine Drawing - Analytical Method
1818
Compute one point based on the previous point:(x0, y0)…….…………..(xk, yk) (xk+1, yk+1) …….
I have got a pixel on the line (Current Pixel).How do I get the next pixel on the line?
Next pixel on next column(when slope is small)
Next pixel on next row(when slope is large)
Incremental AlgorithmsIncremental Algorithms
1919
To find (xk+1, yk+!):
xk+1 = xk+1
yk+1 = ?
CurrentPixel(xk, yk) (5,2)
(6,1)
(6,2)
(6,3)
• Assumes that the next pixel to be set is on the next column of pixels (Incrementing the value of x !)• Not valid if slope of the line is large
Incrementing along xIncrementing along x
2020
Digital Differential Analyzer Algorithm is an incremental algorithm.
Assumption: Slope is less than 1 (Increment along x).Current Pixel = (xk, yk).
(xk, yk) lies on the given line. yk = m.xk + c
Next pixel is on next column. xk+1 = xk+1Next point (xk+1, yk+1) on the line yk+1 = m.xk+1 + c
= m (xk+1) +c = yk + m
Given a point (xk, yk) on a line, the next point is given by xk+1 = xk+1 yk+1 = yk + m
Line Drawing - DDALine Drawing - DDA
2121
• Does not involve any floating point multiplication• Involves floating point addition.• Requires round-off function
Line Drawing - DDALine Drawing - DDA
double m = (double) (by-ay)/(bx-ax);double y = ay;int iy;for (int x=ax; x<=bx; x++) { iy = round(y); setPixel(x, iy); y += m;}
2222
xk+1 = xk+1
yk+1 = Either yk or yk+1
Midpoint algorithm is an incremental algorithm
Midpoint AlgorithmMidpoint Algorithm
Assumption:Slope < 1
CurrentPixel
2323
Candidate Pixels
Current Pixel(xk, yk)
Midpoint
Line
Coordinates of Midpoint = (xk+1, yk+½)
(xk+1, yk)
(xk+1, yk+1)
Midpoint Algorithm - NotationsMidpoint Algorithm - Notations
2424
Midpoint Below Line Midpoint Above Line
Midpoint Algorithm:Midpoint Algorithm:Choice of the next pixelChoice of the next pixel
• If the midpoint is below the line, then the next pixel is (xk+1, yk+1)
• If the midpoint is above the line, then the next pixel is (xk+1, yk)
2525
A(ax, ay)
B(bx, by)
Equation of a line revisitedEquation of a line revisited
Let w = bx ax, and h = by ay
Then, h (x ax) w (y ay) = 0
(h, w , ax , ay are all integers)
In other words, every point (x, y) on the line satisfies the equation F(x, y) =0, where
F(x, y) = h (x ax) w (y ay)
Equation of the line: y x
y y x x
y a x ab a b a
2626
Midpoint Algorithm:Midpoint Algorithm:Regions below and above the lineRegions below and above the line
F (x,y) > 0(for any point below line)
F(x,y) < 0(for any point above line)
F(x,y) = 0
2727
F(MP) > 0
0),( yxf
Midpoint below line
F(MP) < 0
Midpoint above line
Midpoint Algorithm:Midpoint Algorithm:Decision CriteriaDecision Criteria
2828
Midpoint AlgorithmMidpoint AlgorithmDecision CriteriaDecision Criteria
F(MP) = F(xk+1, yk+ ½) = Fk (Notation)
If Fk < 0 : The midpoint is above the line. So the next pixel is (xk+1, yk)
If Fk 0 : The midpoint is below or on the line. So the next pixel is (xk+1, yk+1)
Decision Parameter
2929
Midpoint Algorithm – Story so farMidpoint Algorithm – Story so far
Midpoint Below Line
Next pixel = (xk+1, yk+1)
Fk > 0yk+1 = yk+1
Midpoint Above Line
Next pixel = (xk+1, yk)
Fk < 0yk+1 = yk
3030
Midpoint Algorithm:Midpoint Algorithm:Update EquationUpdate Equation
Fk = F(xk+1, yk+ ½) = h (xk+1 ax) w (yk+½ ay)
Thus, given Fk< 0, yk+1 = yk then Fk+1 = Fk + h
given Fk 0, yk+1 = yk+1 then Fk+1 = Fk + h w
F0 = h (ax +1 ax) w (ay +½ ay) = h w/2
Fk+1 = F(xk+1+1, yk+1+ ½) = h (xk+1+1 ax) w (yk+1+½ ay)
Fk+1 - Fk = h (xk+1 xk) w (yk+1 yk) Fk+1 - Fk = h (xk +1 xk) w (yk+1 yk) Fk+1 = Fk + h w (yk+1 yk)
1
2
2 1
3131
Midpoint Algorithm:Midpoint Algorithm:Update EquationUpdate Equation
Summary: Fk+1 = Fk + h w (yk+1 yk)
given Fk< 0, yk+1 = yk then Fk+1 = Fk + h
given Fk 0, yk+1 = yk+1 then Fk+1 = Fk + h w
F0 = h w/2
Update Equation
3232
Midpoint AlgorithmMidpoint Algorithm
int h = by-ay;int w = bx-ax;float F = h-w/2;int y = ay;for (int x=ax; x<=bx; x++) { setPixel(x, y);
if(F < 0) F += h; else {
F += h-w; y++;
}}
3333
Bresenham’s AlgorithmBresenham’s Algorithm(Improved Midpoint Algorithm)(Improved Midpoint Algorithm)
int h = by-ay;int w = bx-ax;int F = 2*h-w;int y = ay;for (int x=ax; x<=bx; x++) { setPixel(x, y);
if(F < 0) F += 2*h;
else { F += 2*(h-w); y++;
}}
3434
Circle Drawing Algorithms
(Extra notes for self-study!)
3535
To determine the closest pixel position to the specified circle path at each step.
For given radius r and screen center position (xc, yc), calculate pixel positions around a circle path centered at the coodinate origin (0,0).
Then, move each calculated position (x, y) to its proper screen position by adding xc to x and yc to y.
Along the circle section from x=0 to x=y in the first quadrant, the gradient varies from 0 to -1.
Midpoint Circle Drawing AlgorithmMidpoint Circle Drawing Algorithm
36
TCS2111
Midpoint Circle Drawing AlgorithmMidpoint Circle Drawing Algorithm 8 segments of octants for a circle:
37
TCS2111
Midpoint Circle Drawing AlgorithmMidpoint Circle Drawing Algorithm Circle function: fcircle (x,y) = x2 + y2 –r2
> 0, (x,y) outside the circle
< 0, (x,y) inside the circle
= 0, (x,y) is on the circle boundary{fcircle (x,y) =
38
TCS2111
Midpoint Circle Drawing AlgorithmMidpoint Circle Drawing Algorithm
yk
yk-1
midpoint
Next pixel = (xk+1, yk)
Fk < 0yk+1 = yk
yk
yk-1
midpoint
Next pixel = (xk+1, yk-1)
Fk >= 0yk+1 = yk - 1
39
TCS2111
Midpoint Circle Drawing AlgorithmMidpoint Circle Drawing AlgorithmWe know xk+1 = xk+1,
Fk = F(xk+1, yk- ½) Fk = (xk +1)2 + (yk - ½)2 - r2 -------- (1) Fk+1 = F(xk+1+1, yk+1 - ½) Fk+1 = (xk +2)2 + (yk+1 - ½)2 - r2 -------- (2)
(2) – (1)Fk+1 = Fk + 2(xk+1) + (y2
k+1 – y2k) - (yk+1 – yk) + 1
If Fk < 0, Fk+1 = Fk + 2xk+1+1
If Fk >= 0, Fk+1 = Fk + 2xk+1+1 – 2yk+1
40
TCS2111
Midpoint Circle Drawing AlgorithmMidpoint Circle Drawing Algorithm
For the initial point, (x0 , y0) = (0, r)
f0 = fcircle (1, r-½ ) = 1 + (r-½ )2 – r2
= 5 – r 4≈ 1 – r
41
TCS2111
Midpoint Circle Drawing AlgorithmMidpoint Circle Drawing AlgorithmExample:Given a circle radius = 10, determine the circle octant in the first octant from x=0 to x=y.
Solution:
f0 = 5 – r 4= 5 – 10 4= -8.75≈ –9
42
TCS2111
Midpoint Circle Drawing AlgorithmMidpoint Circle Drawing Algorithm
Initial (x0, y0) = (1,10)Decision parameters are: 2x0 = 2, 2y0 = 20
k Fk x y 2xk+1 2yk+1
0 -9 1 10 2 20
1 -9+2+1=-6 2 10 4 20
2 -6+4+1=-1 3 10 6 20
3 -1+6+1=6 4 9 8 18
4 6+8+1-18=-3 5 9 10 18
5 -3+10+1=8 6 8 12 16
6 8+12+1-16=5 7 7 14 14
43
TCS2111
Midpoint Circle Drawing AlgorithmMidpoint Circle Drawing Algorithmvoid circleMidpoint(int xCenter, int yCenter, int radius){ int x = 0; Int y = radius; int f = 1 – radius;
circlePlotPoints(xCenter, yCenter, x, y); while (x < y) { x++; if (f < 0)
f += 2*x + 1; else { y--; f += 2*(x-y)+1; } circlePlotPoints(xCenter, yCenter, x, y); }}
44
TCS2111
Midpoint Circle Drawing AlgorithmMidpoint Circle Drawing Algorithm
void circlePlotPoints( int xCenter, int yCenter, int x, int y){
setPixel (xCenter + x, yCenter + y);setPixel (xCenter – x, yCenter + y);setPixel (xCenter + x, yCenter – y);setPixel (xCenter – x, yCenter – y);setPixel (xCenter + y, yCenter + x);setPixel (xCenter – y, yCenter + x);setPixel (xCenter + y, yCenter – x);setPixel (xCenter – y, yCenter – x);
}
4545
Anti-aliasing
4646
Anti-aliasingAnti-aliasingAnti-aliasing is a technique used to diminish the jagged edges of an image or a line, so that the line appears to be smoother; by changing the pixels around the edges to intermediate colors or gray scales.
E.g. Anti-aliasing disabled:
E.g. Anti-aliasing enabled:
4747
Anti-aliasing (OpenGL)Anti-aliasing (OpenGL)
Anti-aliasing disabled Anti-aliasing enabled
Setting anti-aliasing option for lines:glEnable
(GL_LINE_SMOOTH);
4848
Fill Area AlgorithmsFill Area Algorithms
4949
Fill Area AlgorithmsFill Area Algorithms Fill-Area algorithms are used to fill the
interior of a polygonal shape. Many algorithms perform fill operations
by first identifying the interior points, given the polygon boundary.
5050
The basic filling algorithm is commonly used in interactive graphics packages, where the user specifies an interior point of the region to be filled.
Basic Filling AlgorithmBasic Filling Algorithm
4-connected pixels
5151
[1] Set the user specified point.[2] Store the four neighboring pixels in
a stack.[3] Remove a pixel from the stack.[4] If the pixel is not set,
- Set the pixel- Push its four neighboring pixels
into the stack[5] Go to step 3[6] Repeat till the stack is empty.
Basic Filling AlgorithmBasic Filling Algorithm
5252
void fill(int x, int y) { if(getPixel(x,y)==0){
setPixel(x,y);fill(x+1,y);fill(x-1,y);fill(x,y+1);fill(x,y-1);
}}
Basic Filling Algorithm (Code)Basic Filling Algorithm (Code)
5353
Requires an interior point. Involves considerable amount of
stack operations. The boundary has to be closed. Not suitable for self-intersecting
polygons
Basic Filling Algorithm: ConditionsBasic Filling Algorithm: Conditions
5454
Boundary Fill AlgorithmFor filling a region with a single
boundary color.Condition for setting pixels:
Color is not the same as border color Color is not the same as fill color
Flood Fill AlgorithmFor filling a region with multiple
boundary colors.Condition for setting pixels:
Color is same as the old interior color
Types of Basic Filling AlgorithmsTypes of Basic Filling Algorithms
55
TCS2111
void boundaryFill(int x, int y, int fillColor, int borderColor) { getPixel(x, y, color); if ((color != borderColor)
&& (color != fillColor)) {setPixel(x,y);boundaryFill(x+1,y,fillColor,borderColor);boundaryFill(x-1,y,fillColor,borderColor);boundaryFill(x,y+1,fillColor,borderColor);boundaryFill(x,y-1,fillColor,borderColor);
}}
Boundary Fill Algorithm (Code)Boundary Fill Algorithm (Code)
56
TCS2111
void floodFill(int x, int y, int fillColor, int oldColor) { getPixel(x, y, color); if (color == oldColor) {
setPixel(x,y);floodFill(x+1, y, fillColor, oldColor);floodFill(x-1, y, fillColor, oldColor);floodFill(x, y+1, fillColor, oldColor);floodFill(x, y-1, fillColor, oldColor);
}}
Flood Fill Algorithm (Code)
5757
Filling Polygons (OpenGL)Filling Polygons (OpenGL)
Enabling polygon fill (Default):glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
Disabling polygon fill:glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
5858
Reading AssignmentReading Assignment Edward Angel, Interactive Computer
Graphics (5th Edition): Chapter 7: 7.8 – 7.10
(From Vertices to Fragments)
5959
Further ReadingsFurther Readings Hearn and Baker, Computer Graphics with
OpenGL (3rd Edition): Chapter 3: 3.1–3.5, 3.9, 3.14-3.17 (Graphics Output Primitives) Chapter 4: 4.1-4.9, 4.13-4.14, 4.17-4.18
(Attributes of Graphics Primitives)