You are on page 1of 35

UNIT I

Graphics Primitives and Scan Conversion Algorithms


Contents
• Introduction
• Graphics Primitives
• Applications
• OpenGL
Introduction
• The term computer graphics refers to "almost everything on
computers that is not text or sound”
• Refers to creation, Storage and manipulation of pictures and
drawing using a digital computer.
• Effective tool for presenting information.
• Computer aided design.
• Computer aided manufacturing
Graphics Primitives
• Pixel
• Line
• Polygon
• Frame Buffer
• Display Devices
Applications
 Computer Aided Design (CAD)
 Presentation Graphics
 Computer Art
 Entertainment (animation, games, …)
 Education & Training
 Visualization (scientific & business)
 Image Processing
 Graphical User Interfaces
What Is OpenGL?
• OpenGL: Graphics rendering API
• high-quality color images composed of geometric and
image primitives
• Operating system independent

 GLUT: OpenGL Utility Toolkit

7
Rendering ??
• Geometric primitives
• Points
• lines or polygons

• Image Primitives
• images and bitmaps
• separate pipeline for images and geometry
• linked through texture mapping

• Rendering depends on state


• colors, materials, light sources, etc.

8
OpenGL Architecture

Per Vertex
Polynomial Operations &
Evaluator Primitive
Assembly

Display Per Fragment Frame


CPU List
Rasterization
Operations Buffer

Texture
Memory
Pixel
Operations

9
OpenGL as a Renderer
• Geometric primitives
• points, lines and polygons
• Image Primitives
• images and bitmaps
• separate pipeline for images and geometry
• linked through texture mapping
• Rendering depends on state
• colors, materials, light sources, etc.

10
OpenGL and Related APIs
application program

OpenGL Motif GLUT


widget or similar
GLX, AGL
GLU
or WGL

X, Win32, Mac O/S GL

software and/or hardware

11
GLUT Basics
• Application Structure
• Configure and open window
• Initialize OpenGL state
• Register input callback functions
• render
• resize
• input: keyboard, mouse, etc.
• Enter event processing loop

12
OpenGL Geometric Primitives
• All geometric primitives are specified by vertices

GL_LINES
GL_POINTS GL_POLYGON
GL_LINE_STRIP GL_LINE_LOOP

GL_TRIANGLES
GL_TRIANGLE_STRIP GL_QUADS GL_QUAD_STRIP
GL_TRIANGLE_FAN

13
RGBA versus Color-Index Mode
Shapes Tutorial

15
Digital Differential Analyzer
Line Drawing Algorithm
DDA Algorithm
• The digital differential analyzer (DDA) is line drawing
algorithm based on calculating either δy or δx.
• A line is sampled at unit intervals in one coordinate and the
corresponding integer values nearest the line path are
determined for the other coordinate.
DDA Algorithm…
BRESENHAM’S
LINE
DRAWING
ALGORITHM
INTRODUCTION

• Raster line-generating algorithm


• Developed by Bresenham
•Scan conversion takes place
using only incremental integer
calculations
• Accurate and efficient than
DDA
DERIVATION
•Starting from the left endpoint (x0, y0) of a given line,
we step to each successive column (x position) and plot
the pixel whose scan-line y value is closest to the line
path.
• At sample positions 𝑥 𝑘 + 1 the vertical
from the line are labelled 𝑑𝑟and
separations 𝑒
𝑝
𝑢 𝑑
𝑟𝑤
𝑒
𝑜𝑙

• y coordinate on the line at 𝑥𝑘 + 1 is,


𝑦= 𝑚 𝑥𝑘 + 1 + 𝑏
• so,
𝑑
=𝑟𝑒𝑝𝑢 𝑦− 𝑦𝑘 = 𝑚 𝑥𝑘 + 1
𝑦 + 1 − 𝑦 = 𝑦𝑘 + 1 − 𝑚
𝑑𝑜𝑙𝑤𝑒𝑟= + 𝑏 − 𝑘𝑦𝑘
𝑥𝑘 + 1 + 𝑏
DERIVATION

• It can be used to make decision about which pixel is closer to the line
• This decision is based on the difference between the two pixel positions,
𝑑 − 𝑑 = 2𝑚 𝑥 + 1
− 2𝑦𝑘+ 2𝑏 − 1
𝑢𝑝𝑝𝑒𝑟 𝑜𝑙𝑤𝑒𝑟 𝑘
• By substituting 𝑚 = ∆𝑦/∆𝑥 and both are differences of end points,
∆𝑦
∆𝑥 𝑑𝑢𝑝𝑝𝑒𝑟− 𝑑𝑜𝑙𝑤𝑒𝑟
= ∆𝑥 2 𝑥𝑘 + 1 − 2𝑦𝑘+ 2𝑏 − 1
∆𝑥
= 2∆𝑦. 𝑥𝑘 − 2∆𝑥. 𝑦𝑘 + 2∆𝑦 + ∆𝑥(2𝑏 − 1)
= 2∆𝑦. 𝑥𝑘 − 2∆𝑥. 𝑦𝑘 + 𝐶
DERIVATION
• 𝑥𝑘+1 is same as 𝑥𝑘 + 1 so,
𝑃𝑘+1 = 𝑃𝑘+ 2∆𝑦 − 2∆𝑥(𝑦𝑘+1 − 𝑦𝑘)
• Here, 𝑦𝑘+1− 𝑦𝑘 is either 0 or 1 depending on the sign of 𝑃𝑘

• If 𝑃𝑘 < 0, the next point to plot is (𝑥𝑘+ 1, 𝑦𝑘) and new value of 𝑃is,
𝑃𝑘+1 = 𝑃𝑘+ 2∆𝑦
• If 𝑃𝑘 > 0, the next point to plot is (𝑥𝑘+ 1, 𝑦𝑘 + 1) and new value of 𝑃 is,
𝑃𝑘+1 = 𝑃𝑘 + 2∆𝑦 − 2∆𝑥
• The first decision parameter 𝑃0is evaluated at (𝑥0, 𝑦0) is,
𝑃0= 2∆𝑦 − ∆𝑥
Bresenham
Circle Drawing Algorithm,
(xk +1, yk)
FIRST
(xk+1, yk–1)
•For a pixel at (xk, kOCTANT
y ) the next pixel (x +1, y ) or at (x +1, y –1) is
k k k k
closer to the circle.

•The pixel point Pk for a corresponding point on the circle is given as:
Pk = f(xk + 1, yk – ½) = (xk + 1)2 + (yk - ½)2 – r2

•The next pixel point Pk+1 is given as:


Pk+1 = f(xk+1 + 1, yk+1 - ½) = [(xk + 1) + 1]2 + (yk+1 - ½ )2 – r2
Expanding Pk and Pk+1 and subtracting them, the eq. obtained is:
Pk+1 = Pk + 2(xk+1) + (y2 – y2 ) – (y –y )+1
k+1
k
k+1
k

(where the next Y – coordinate (yk+1) is either yk or yk-1 depending on


the sign of Pk.)
= xk + 1  2(xk+1) = 2xk + 2
xk+1
Since xk+1 denotes the next point along the X – axis (going rightwards)
Since yk+1 denotes the next point along the Y – axis (going downwards)
= yk - 1  2(yk+1) = 2yk - 2
yk+1
(0, r)
We’ve have to begin somewhere. So let the starting position be (x0,y0) = (0, r)

Substituting these values in the circle

equation, P0 = f(1, r – ½) = 𝟓 𝟒 - r
(0, 0)
Since r is a integer, neglecting the small
value of ¼, we have P0 = 1 - r
BRESSENHAM’S
ALGORITHM
1. Input radius r and centre of the circle (xc, yc), and obtain the first
point on the circumference of a circle centred on the origin as
(x0, y0) = (0, r)

2. Calculate the initial value of the decision parameter as


P0 = 1 – r
where r is the radius of the circle
3. At each xk position, starting at k= 0, perform the following test:

If (Pk < 0), then the NEXT POINT along the circle centred on (0, 0) is
(xk+1, yk) and Pk+1 = Pk + 2xk+1 +1
 Pk+1 = Pk + 2xk + 3
2xk+1 = 2xk + 2

Otherwise, the NEXT POINT along the circle is (xk+1, yk–1) and Pk+1 = Pk +
2xk+1 + 1 – 2yk+1
 Pk+1 = Pk + 2xk – 2yk + 5
2yk+1 = 2yk – 2
4. Determine the Symmetry Points on the other 7 Octants

5. Move each calculated pixel position (x,y) onto the circular

path centered at (xc,yc) and plot the coordinate values as

x = x + xc and y = y + yc

6. Repeat steps 3 to 5 until x >= y


Eight-Way Symmetry
• The first thing we can notice to make our circle drawing
algorithm more efficient is that circles centred at (0, 0)
have eight-way symmetry

(-x, y) (x, y)

(-y, x) (y, x)

R
2
(-y, -x) (y, -x)

(-x, -y) (x, -y)


Mid-Point Circle Algorithm
• Similarly to the case with lines, there
is an incremental algorithm for
drawing circles – the mid-point circle
algorithm
• In the mid-point circle algorithm we
use eight-way symmetry so only ever
calculate the points for the top right
eighth of a circle, and then use The mid-point circle
symmetry to get the rest of the points algorithm was
developed by Jack
Bresenham, who we
heard about earlier.
Mid-Point Circle Algorithm (cont…)
• Assume that we have
just plotted point (xk, yk) (xk, yk) (xk+1, yk)

• The next point is a


choice between (xk+1, yk )
(xk+1, yk-1)
and (xk+1, yk-1)
• We would like to choose
the point that is nearest to
the actual circle
• So how do we make this choice?
The Mid-Point Circle Algorithm
• MID-POINT CIRCLE ALGORITHM
• 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:
( x0 , y0 )  (0, r )
• Calculate the initial value of the decision parameter as:

p0  5  r
4
• Perform the test, Starting with k = 0 at each position xk,
perform the following test.
• (i) If pk < 0, the next point along the circle centred on (0, 0)
is (xk+1, yk) and: p  p  2x 1
k 1 k k 1
The Mid-Point Circle Algorithm (cont…)

• (ii) If Pk >0 then the next point along the circle is (xk+1,
yk-1) and:
pk 1  pk  2 xk 1  1  2 yk 1
• 2 xk 1 = 2Xk+2 and 2 yk 1= 2Yk – 2
where
• Identify the symmetry points in the other seven octants
• Move (x, y) according to:

x  x  xc y  y  yc
• Repeat steps 3 to 5 until x >= y

You might also like