CG Unit 1 Notes
CG Unit 1 Notes
Unit – I
Graphics Primitives & Scan Conversion
Algorithms
1. Introduction
Computer graphics is a field of computer science that focuses on creating, manipulating, and
displaying visual content using computers. This includes everything from simple 2D images to
complex 3D models and animations.
It displays the information in the form of graphics objects such as pictures, charts, graphs and
diagrams instead of simple text. Hence, we can say that computer graphics makes it possible
to express data in pictorial form.
Video Game Development: Used to create visually rich and immersive game environments,
character models, and dynamic effects in both 2D and 3D video games.
Movie and Animation Production: Essential for generating special effects, animated
characters, and realistic environments in films, TV shows, and animated features, including
CGI (Computer–Generated Imagery).
Medical Imaging: Important for visualizing complex medical data from MRI, CT scans, and
other imaging technologies, aiding in diagnosis, surgical planning, and medical research.
Presentation Graphics: For the preparation of reports or summarizing the financial, statistical,
mathematical, scientific, and economic data for research reports, and managerial reports,
moreover creation of bar graphs, pie charts, and time charts, can be done using the tools present
in computer graphics.
2|Page
Machine Drawing: Computer graphics are very frequently used for designing, modifying, and
creating various parts of a machine and the whole machine itself.
Graphical User Interface: The use of pictures, images, icons, pop–up menus, and graphical
objects helps in creating a user–friendly environment where working is easy and pleasant, using
computer graphics we can create such an atmosphere where everything can be automated and
anyone can get the desired action performed in an easy fashion.
2. Graphics Primitives
2.1 Pixel
Pixels are tiny squares or dots that represent a single point of color. They are usually arranged
in a regular 2–dimensional grid. Each pixel is made up of a red, green, and blue (RGB) subpixel
that emits color at different intensities.
2.2 Resolution
Resolution refers to the amount of detail or clarity of the display, measured in pixels. It indicates
the number of pixels along the width and height of a screen or image. Higher resolutions have
more pixels and, therefore, can display more detail and sharper images.
Aspect ratio is the ratio of the width to the height of an image, screen, or display. It is usually
expressed as two numbers separated by a colon (e.g., 16:9). This ratio defines how the width
compares to the height, and it helps determine the shape of the image or screen.
A frame buffer is a portion of memory used to store image data that will be displayed on a
screen. It acts as a temporary storage area where the graphical data is kept before it is sent to
the display. The frame buffer holds the color and pixel information for each point on the screen,
allowing for smooth and consistent image rendering.
A refresh buffer is a type of memory used in display systems to store data that is continuously
refreshed or updated for display purposes. It ensures that the image on the screen remains stable
and clear by repeatedly sending the stored image data to the display.
Frame rate is the number of frames (individual images) displayed per second in a video or
animation. It’s measured in frames per second (FPS).
It determines how smooth motion appears in videos, games, or animations. A higher frame rate
results in smoother and more fluid motion.
For example, a video playing at 30 FPS means 30 individual frames are shown each second.
Refresh rate is the number of times per second a display updates the image it shows. It’s
measured in hertz (Hz).
It indicates how often the screen refreshes the displayed image. A higher refresh rate can reduce
flicker and improve the overall visual experience.
A monitor with a 60 Hz refresh rate updates the image 60 times per second.
Color depth, also known as bit depth, refers to the number of bits used to represent the color
of a single pixel in an image or display. It determines the range of colors that can be displayed
or captured and affects the overall color accuracy and richness of images.
4|Page
Common Values:
24–bit: 16.7 million colors (true color, 8 bits per color channel).
30–bit or 36–bit: More than 1 billion colors (deep color, with additional bits for greater
precision).
2.9 Persistence
Persistence refers to the duration for which a pixel or image remains visible before it fades or
is replaced by new information.
3. Introduction to OpenGL
2D Graphics Support: While it’s most known for 3D graphics, OpenGL also supports 2D
rendering. This is useful for creating user interfaces, 2D games, and other applications that
require 2D graphics.
Shaders and Programmable Pipeline: OpenGL includes support for programmable shaders,
which allow developers to write custom programs (shaders) for the GPU to control rendering
effects and perform complex visual computations.
5|Page
High Performance: OpenGL is designed to provide high performance for graphics rendering
by using the capabilities of modern GPUs (Graphics Processing Units). It allows for efficient
use of hardware resources to achieve smooth and detailed graphics.
Extensibility: OpenGL supports extensions that provide additional features and capabilities
beyond the core specification. This allows for customization and innovation in graphics
rendering.
Community and Ecosystem: Being an open standard, OpenGL has a large community of
developers and extensive documentation. There are many libraries, tools, and resources
available to assist in development.
Video Games: For rendering graphics, creating game environments, and implementing visual
effects.
Simulations: For creating realistic simulations in fields like aviation, engineering, and science.
Graphics Applications: For designing and rendering complex 3D models and visualizations
in applications like CAD (Computer–Aided Design) and 3D modeling software.
OpenGL ES: A variant of OpenGL designed for embedded systems, such as mobile devices
and tablets. It’s used in applications where resources are limited and is often found in mobile
games and apps.
OpenGL 4.x: Represents the latest versions of the OpenGL specification, offering advanced
features and improved performance.
Display List: Commands may either be accumulated in display lists, or processed immediately
through the pipeline. Display lists allow for greater optimization and command reuse, but not
all commands can be put in display lists.
Evaluator: The first stage in the pipeline is the evaluator. This stage effectively takes any
polynomial evaluator commands and evaluates them into their corresponding vertex and
attribute commands.
Per–Vertex Operations & Primitive Assembly: The second stage is the per–vertex
operations, including transformations, lighting, primitive assembly, clipping, projection, and
viewport mapping.
Rasterization: The third stage is rasterization. This stage produces fragments, which are series
of framebuffer addresses and values, from the viewport–mapped primitives as well as bitmaps
and pixel rectangles.
Per–fragment Operations: The fourth stage is the per–fragment operations. Before fragments
go to the framebuffer, they may be subjected to a series of conditional tests and modifications,
such as blending or z–buffering.
Parts of the framebuffer may be fed back into the pipeline as pixel rectangles. Texture memory
may be used in the rasterization process when texture mapping is enabled.
OpenGL supports various basic primitive types such as point, line and polygons. Sequence of
vertices is used to specify these primitives. There are ten different primitive types. OpenGL
interprets each primitive using following guidelines.
In OpenGL, attributes refer to various properties associated with vertices and other elements
that affect how they are rendered. Attributes are crucial for defining the appearance and
behavior of graphical objects. Here’s an overview of some OpenGL attributes:
2. Color: It defines the color of a vertex. The function used is ‘glColor3f(r, g, b)’ or
‘glColor4f(r, g, b, a)’.
3. Texture Coordinates: It specifies coordinates used to map textures onto geometry. The
function used is glTexCoord2f(x, y).
4. Line Width: It specifies the thickness of the lines. The function used is
‘glLineWidth(width)’.
5. Line Style: It defines the pattern of lines (e.g., dashed or solid). The function used is
‘glLineStipple(factor, pattern)’.
Modeling and rendering are fundamental concepts in computer graphics. They involve
creating and displaying geometric objects on a screen. Here’s a brief overview of simple
approaches for 2D and 3D objects:
To model a rectangle, we specify four vertices corresponding to the corners of the rectangle.
Rendering: Rendering 2D objects involves mapping the defined shapes onto the screen.
Steps:
Example Code:
Rendering: Rendering 3D objects involves projecting them onto the 2D screen while
considering depth.
Steps:
2. Define the camera view and projection using matrices. For example, use ‘ glOrtho’ for
orthographic projection or ‘glFrustum’ for perspective projection.
3. Specify vertices for 3D primitives. For example, define a cube using vertices and faces.
Example Code:
glEnd();
OpenGL provides a simple collection of utilities, called the GL Utility Library or GLU in short.
Functions from the OpenGL library begin with ‘gl’, functions from GLU begin with glu, and
functions from GLUT begin with ‘glut’.
The gl.h header file needs to be included in each program for all OpenGL applications. Almost
all OpenGL applications use GLU i.e. OpenGL Utility Library, which needs glu.h header file
to be included in a program.
#include <GL/gl.h>
#include <GL/glu.h>
If we are using GLUT to access window manager tasks, then we should use
#include <GL/glut.h>
Where glut.h itself includes gl.h, and glu.h, so whenever we use glut.h, there’s no need
to include gl.h and glu.h separately.
3.9 GLUT
GLUT (OpenGL Utility Toolkit) is a library that provides a set of functions for creating and
managing windows with OpenGL contexts, handling user input, and performing other tasks
that are common in OpenGL applications.
Window Management: Functions to create and manage windows where OpenGL renders
graphics. For example, glutCreateWindow("Window Title")
Event Handling: Functions to handle keyboard, mouse, and other user inputs. For example,
glutKeyboardFunc(keyboardCallback), where keyboardCallback is a function that handles
key presses.
12 | P a g e
Rendering: Functions to set up the rendering loop where OpenGL drawing commands are
executed. For example, glutDisplayFunc(displayCallback), where displayCallback is a
function that contains the OpenGL rendering code.
Timer and Idle Functions: Functions to set up timers and manage animations or periodic
tasks. For example, glutTimerFunc(millis, timerCallback, value)
Utility Functions: Functions to manage the OpenGL context and handle window resizing. For
example, glutReshapeFunc(reshapeCallback), where reshapeCallback is a function that
adjusts the viewport when the window is resized.
In OpenGL, keyboard and mouse interactions are typically handled through additional libraries
or toolkits that manage windowing and input events. OpenGL itself does not provide direct
mechanisms for handling input; instead, it relies on these external libraries to interface with the
system's input devices.
Two commonly used libraries for this purpose are GLUT (OpenGL Utility Toolkit) and
GLFW (Graphics Library Framework).
Here’s a general overview of how keyboard and mouse interactions can be managed using the
GLUT library:
Keyboard Interaction:
Use glutKeyboardFunc to register a function that will be called whenever a key is pressed.
if (key == 'q') {
glutInit(&argc, argv);
glutMainLoop();
return 0;
2. Callback Parameters
int x, int y: The x and y coordinates of the mouse when the key was pressed (if
applicable).
Mouse Interaction:
Use glutMouseFunc to register a function that will be called when mouse buttons are
pressed or released.
glutInit(&argc, argv);
glutMainLoop();
return 0;
}
14 | P a g e
2. Callback Parameters
int button: The mouse button that was pressed (e.g., GLUT_LEFT_BUTTON,
GLUT_RIGHT_BUTTON).
int x, int y: The x and y coordinates of the mouse pointer when the button was pressed
or released.
4. Scan Conversion
2 Uses a beam of electrons to draw The electron beam sweeps across the
lines directly on the screen in any screen from left to right and top to bottom
order. to illuminate the pixels.
5 Ideal for applications involving line Suitable for rendering complex images,
drawings, CAD, and simple graphics. including photos, videos, and detailed
graphics.
6 Requires less memory since only the Requires more memory to store the entire
coordinates of the endpoints of lines frame buffer, which holds the pixel data
are stored. for the entire screen.
15 | P a g e
The Digital Differential Analyzer (DDA) Line Drawing Algorithm is a popular algorithm used
in computer graphics to draw lines between two points on a grid or screen. The DDA algorithm
is simple and efficient for generating lines with arbitrary slopes.
The idea behind the DDA algorithm is to calculate values between the start and end points by
incrementing either the x–coordinate or the y–coordinate based on the line's slope. The
algorithm steps through one coordinate at a time (either x or y) and calculates the corresponding
y or x value using the line's slope.
1. Write the start and end points of the line, (x0, y0) and (x1, y1)
o Compute the differences Δx and Δy between the start and end points:
Δx = x1 − x0
Δy = y1 − y0
o The number of steps required to generate the line is the maximum of ∣Δx∣ and
∣Δy∣.
o xi =
16 | P a g e
o yi =
o For each step, plot the pixel at the current (x, y) coordinates, then update the
coordinates:
o x = x + xi
o y = y + yi
o Since pixel coordinates must be integers, round the values of x and y to the
nearest integer before plotting the pixel.
Advantages of DDA:
Can handle all types of slopes (positive, negative, zero, and infinite).
Disadvantages of DDA:
The algorithm involves floating–point arithmetic, which can be slower and less efficient
on systems without floating–point hardware.
Example: Consider a line segment from A(−2, −1) to B(6, 3). Use DDA Line drawing algorithm
to rasterize this line.
xi = = =1
yi = = = 0.5
For each step, plot the pixel at the current (x, y) coordinates, then update the coordinates:
x = x + xi
y = y + yi
1 −1 −1 (−1, −1)
2 0 0 (0, 0)
3 1 0.5 (1, 1)
4 2 1 (2, 1)
5 3 1.5 (3, 2)
6 4 2 (4, 2)
7 5 2.5 (5, 3)
8 6 3 (6, 3)
Bresenham's Line Drawing Algorithm is an efficient method used in computer graphics to draw
lines on a pixel–based display. Unlike the DDA (Digital Differential Analyzer) algorithm,
Bresenham's algorithm uses only integer arithmetic, making it faster and more suitable for
systems that lack floating–point hardware.
18 | P a g e
The algorithm determines the closest pixel to the theoretical line at each step, using incremental
error terms to decide whether to move horizontally, vertically, or diagonally.
Δx = x1 − x0
Δy = y1 − y0
pk = 2Δy – Δx
If pk < 0, the next point is (xk + 1, yk). Update the decision parameter:
o pk+1 = pk + 2Δy
Example: Consider a line from (1, 1) to (6, 4). Use Bresenham’s algorithm to rasterize this
line.
Δx = x1 − x0 = 6 – 1 = 5
Δy = y1 − y0 = 4 – 1 = 3
pk = 2Δy – Δx = 2(3) – 5 = 1
1 1 2 2 (2, 2)
2 –3 3 2 (3, 2)
3 3 4 3 (4, 3)
4 –1 5 3 (5, 3)
5 5 6 4 (6, 4)
To derive an expression that helps determine the next pixel to be drawn when rasterizing a line
with a slope 0 ≤ m ≤ 1, ensuring the algorithm only uses integer arithmetic.
20 | P a g e
We know, y = mx + c
But x = xk+1
∴ y = m(xk+1) + c
pk = Δx(d1–d2)
Now, d1 = y – yk = m(xk+1) + c – yk
d1 – d2 = m(xk+1) + c – yk – yk – 1 + m(xk+1) + c
= 2m(xk+1) – 2yk + 2c – 1
pk = Δx [2m(xk+1) – 2yk + 2c – 1]
We know, 𝑚 =
∴ pk = Δx [2 (xk+1) – 2yk + 2c – 1]
To find pk+1
Now,
As y = mx + c, c = y – mx
21 | P a g e
∴ pk = 2Δy – Δx
This is the derivation for the decision parameter in Bresenham’s Line drawing Algorithm.
4.4 Comparison between DDA Line Drawing Algorithm and Bresenham’s Line
Drawing Algorithm
Slower due to the use of floating– Faster, as it uses only integer arithmetic,
4 point calculations, which require making it more efficient on systems
more processing power. without floating–point hardware.
May produce lines with small Produces smooth lines with evenly
6 deviations due to rounding, resulting distributed pixels, closely following the
in uneven pixel distribution. actual line.
22 | P a g e
Requires more memory due to Requires less memory due to the use of
7
floating–point operations. integer arithmetic.
The Digital Differential Analyzer (DDA) algorithm for drawing a circle is a method used to
plot the points of a circle in a rasterized form. The DDA algorithm is more commonly
associated with line drawing, but it can be adapted for circles. However, the Bresenham's
algorithm is typically preferred for circle drawing due to its efficiency.
Circle Equation:
x2 + y2 = r2
1. Initialize Parameters:
o Start from a point on the circle, typically at the topmost point (0, r).
o Calculate the angle increment, θ, which is small enough to ensure that the points
are close to each other:
Δ𝜃 =
Δx = cos (θ) × Δθ
Δy=sin (θ) × Δθ
x = x + Δx
23 | P a g e
y = y −Δy
4. Symmetry Consideration:
o Because of the circle's symmetry, you can mirror the points in all eight octants
of the circle. This means that for each calculated point (x, y), you can also plot
the points (y, x), (−x, y), (−y, x), and so on.
The algorithm is efficient because it uses only integer addition, subtraction, and bit shifting,
making it much faster than other methods that involve floating–point arithmetic.
Key Concepts:
Symmetry: A circle is symmetric about its center, which means that when you calculate
one point on the circumference, you can easily find the corresponding points in the
other seven octants of the circle.
1. Initial Setup:
o This decision parameter helps in determining whether the next point will be
directly to the right or diagonally down to the right.
2. Iterative Calculation:
24 | P a g e
o For each point (x, y) on the circle's perimeter, the next point is determined based
on the value of the decision parameter pk.
o If pk < 0, the next point is (x+1, y) and the decision parameter is updated as:
pk + 1 = pk + 4xk + 6
o If pk ≥ 0 the next point is (x+1, y−1), and the decision parameter is updated as:
pk + 1 = pk + 4(xk − yk) + 10
3. Symmetry Exploitation:
o For every calculated point (x, y), the corresponding points in all eight octants of
the circle are plotted. The symmetry points are (x, y), (y, x), (−x, y), (−y, x),
(x,−y), (y,−x), (−x,−y), (−y,−x)
4. Stopping Condition:
Assume that the circle is at some random pixel P whose coordinates are (xk, yk).
To find the next pixel, we need to decide whether the next pixel would be in North or South
direction.
25 | P a g e
Bresenham's circle drawing algorithm helps us decide this by calculating the difference
between radius and the coordinates of the next pixels.
xk+1 = xk + 1
Similarly,
yk–1 = yk – 1
F(C) = x2 + y2 – r2
Function of Circle at N
Here the value of F(N) is positive because N is outside the circle and that makes
Function of Circle at S
Here the value of F(S) will be Negative because S is inside the circle and that makes
Now we need a decision parameter which helps us decide the next pixel, i.e., pk
pk = F(N)+F(S)
If pk < 0, F(S) is bigger than F(N), which means that Point N is closer to the circle than point S,
so we select pixel N as our next pixel.
If pk > 0, F(N) is bigger than F(S), which means that point S is closer to the circle than point N,
and we select pixel S as our next pixel.
To find pk
To find pk+1
Replacing xk+1 with xk + 1, but we cannot replace yk+1 as we do not know the exact value of yk
pk+1 – pk = 2(xk+2)2 + (yk+1)2 + (yk+1 –1)2 – 2r2 – [ 2(xk + 1)2 + (yk)2 + (yk –1)2 – 2r2]
pk+1 – pk = 2(xk)2 + 8xk + 8 + (yk+1)2 + (yk+1)2 – 2yk+1 + 1 – 2r2 – 2xk – 4xk – 2 – (yk)2 –
(yk)2 + 2yk – 1 + 2r2
If (pk < 0), we choose point N as discussed above, i.e., (xk+1, yk)
That means our next x coordinate is xk+1 and next y coordinate is yk,
27 | P a g e
pk+1 = pk + 4xk + 6
We know
yk–1 = yk – 1
Therefore,
To find initial decision parameter (p0), we use the starting points (0, r)
p0 = 2 + r2 +r2 + 1 – 2r – 2r2
p0 = 3–2r
This is the derivation for the decision parameter in Bresenham’s Circle drawing Algorithm.
28 | P a g e
The Midpoint Circle Drawing Algorithm is another efficient way to draw a circle on a raster
display. It is conceptually similar to Bresenham's Circle Drawing Algorithm but is derived
directly from the midpoint method, which uses the midpoint between two candidate pixels to
determine which one is closer to the actual circle.
Concept:
The Midpoint Circle Drawing Algorithm uses the symmetry of a circle to minimize the number
of calculations. By calculating the points for one octant (1/8th of the circle), the algorithm can
reflect those points into the other seven octants, significantly reducing the amount of work
required.
1. Circle Equation:
o The equation of a circle centered at the origin (0, 0) with radius r is:
x2 + y2 = r2
2. Initial Setup:
o Start at the topmost point on the circle: (x0, y0) = (0, r).
This decision parameter helps determine whether the next pixel should be
directly East or South–East of the current pixel.
3. Decision Making: For each pixel, the algorithm needs to decide between two possible
next points:
o The decision is made based on the value of the decision parameter pk:
o If pk < 0: The midpoint is inside the circle, so the next point is East.
5. Plotting Points:
o For each calculated point (x,y) in the first octant, plot it and use the circle’s
symmetry to plot the corresponding points in the other seven octants; (x, y), (y,
x), (−x, y), (−y, x), (x,−y), (y,−x), (−x,−y), (−y,−x)
6. Continue Until x ≥ y: The process continues until x ≥ y, which means the entire octant
has been drawn.
Assume that there is a random pixel P whose coordinates are (xk, yk).
The circle is moving forward and downwards so y can never be increased, either it can be the
same or decremented.
For that decision, midpoint circle drawing technique helps us decide whether our next pixel be
towards N or S.
xk+1 = xk + 1
Similarly,
yk−1 = yk − 1
M =
= ,
1
= ( 𝑥 + 1, 𝑦 − )
2
x2 + y2 = r2
F(C) = x2 + y2 – r2
1
Function of Midpoint M ( 𝑥 + 1, 𝑦 − ) in circle equation
2
1
F(M) = xk+12 + (𝑦 − )2 – r2
2
1
F(M) = (xk+1)2 + (𝑦 − )2 – r2
2
1
pk = (xk+1)2 + (𝑦 − )2 – r2 …(1)
2
31 | P a g e
1
pk+1 = (xk+1+1)2 + (𝑦 − )2 – r2
2
1 1
pk+1 – pk = (xk+1+1)2 + (𝑦 − )2 – r2 –[(xk+1)2 + (𝑦 − )2 – r2]
2 2
1 1
pk+1 – pk = [(xk+1) 2 + (1) 2] + (𝑦 − )2 – (xk+1)2 – (𝑦 − )2
2 2
1 1
pk+1 – pk = (xk+1)2 + 12 + 2(xk+1) + yk+12 + ( ) – yk+1 – (xk+1)2 – yk2 – ( ) + yk+1
4 4
p0 = (0+1)2 + (r – )2 – r2
p0 = 1 + r 2 + – r – r2
p0 = 1 + – r
If pk ≥ 0 that means midpoint is outside the circle and S is closest pixel so we will
choose S (xk+1, yk–1)
= pk + 2(xk+1) – 2yk +2 + 1
Therefore,
And if pk < 0 that means midpoint is inside the circle and N is closest pixel so we will
choose N (xk+1, yk)
i.e. yk+1 = yk
= pk + 2(xk+1) +1
pk+1 = pk + 2xk+1 +1