ARSI UNIVERSITY
COLLEGE OF BUSINESS AND
ECONOMICS
DEPARTEMENT OF COMPUTER
SCIENCE
INDIVIDUAL ASSIGNMENT OF COURSE
COMPUTER GRAPHICS
NAME IDNO
1. SOLOMON TADESE UGR/11311/13
2.HUNDAF WORKU UGR/10602/13
3.DEBORAH TEKALIGN UGR/10331/13
4.MUBARIK ALI UGR/10762/13
5.YAKOB IYASU UGR/11005/13
SUBMISSION DATE: 23/2/2016EC
INSTRUCTOR: MR. Shamble
1|Page
Overview of openGL
Introduction
OpenGL is a hardware-independent, operating system independent, vendor neutral graphics API
specification. Many vendors provide implementations of this specification for a variety of
hardware platforms. Bindings exist primarily for the C programming language, but bindings are
also available for Fortran and Ada.
OpenGL has been designed using a client/server paradigm, allowing the client application and the
graphics server controlling the display hardware to exist on the same or separate machines. The
network is transparent to the application.
OpenGL is window system independent, and therefore contains no windowing operations or
mechanisms for user input. Also, OpenGL does not provide direct support for complex geometrical
shapes, such as cubes or spheres. These must be built up from supported primitives.
OpenGL is an open-source graphics library that allows developers to create 2D and 3D graphics
applications. It provides a set of functions, procedures, and data structures that enable developers
to render complex images and animations on various platforms, including Windows, macOS,
Linux, and mobile devices. OpenGL uses a state-based API, meaning that developers can change
various parameters and settings to define how objects should be displayed. It supports a variety of
rendering options, including point, line, and polygon rendering. The library provides a low-level
interface to display graphics, making it highly efficient and flexible. It also supports hardware
acceleration, allowing developers to take advantage of the powerful capabilities of modern
graphics hardware. OpenGL operates on a rendering pipeline, which involves several stages,
including vertex processing, primitive assembly, rasterization, and pixel processing. Each stage
performs specific operations to transform vertices into pixels on the screen. OpenGL has evolved
over the years and the most recent version, OpenGL 4.x, introduced a number of advanced features,
such as shader programming and advanced shading techniques, which enable developers to create
2|Page
highly realistic and visually stunning graphics. In addition to its core functionality, OpenGL also
provides a range of extensions, which are additional features and capabilities that are not part of
the core specification. These extensions allow developers to take advantage of specific hardware
features or advanced rendering techniques.
Some features of OpenGL include the following:
1. Geometric and Raster Primitives: OpenGL supports a wide range of geometric primitives,
such as points, lines, and polygons. It also provides raster primitives for rendering pixels, such as
rectangles or textures.
2. RGBA or Color Index Mode: The color model in OpenGL can be represented using RGBA
(Red, Green, Blue, and Alpha) channels for each pixel or using a color index mode where colors
are stored as indices into a palette.
3. Display List or Immediate Mode: Display lists allow storing a sequence of OpenGL
commands for later efficient execution, while immediate mode allows for immediate rendering of
geometry without storing and reusing commands.
4. Viewing and Modeling Transformations: OpenGL provides functionality for
transformations such as translation, rotation, scaling, and perspective projection. These
transformations allow for manipulating the viewing and modeling of 3D objects. 5. Lighting and
Shading: OpenGL supports various lighting models, including diffuse, specular, and ambient
lighting, enabling realistic shading of objects.
6. Hidden Surface Removal (Depth Buffer): OpenGL uses a depth buffer (also known as a Zbuffer)
to handle hidden surface removal, ensuring that only the visible surfaces are rendered. 7. Alpha
Blending (Translucency): Alpha blending enables the rendering of translucent objects by blending
the object's color with the color of the underlying objects.
8. Anti-Aliasing: OpenGL provides techniques like multisampling to reduce the visual
artifacts caused by aliasing, resulting in smoother edges and higher quality rendering.
9. Texture Mapping: OpenGL supports texture mapping, allowing for applying an image
(texture) onto a 3D object's surface, enhancing visual realism.
3|Page
10. Atmospheric Effects (Fog, Smoke, Haze): OpenGL provides mechanisms for simulating
atmospheric effects like fog, smoke, or haze, adding depth and realism to the rendered scene.
11. Feedback and Selection: OpenGL allows for retrieving information about the rendered
objects using feedback or selection mechanisms, enabling interaction with the rendered scene.
12. Stencil Planes: Stencil planes provide a way to selectively render objects and apply
various effects by masking certain regions of the screen.
13. Accumulation Buffer: The accumulation buffer in OpenGL allows for storing and
blending multiple frames, enabling effects such as motion blur or frame averaging.
14. Depth Cueing: Depth cueing, also known as depth fog, simulates depth perception in a
scene by applying fog based on the distance from the viewer.
15. Wireframes: OpenGL can render objects as wireframes, displaying only the edges of a
3D object instead of filling the polygons.
16. Motion Blur: Motion blur is a visual effect achieved by blending multiple slightly offset
frames, creating the perception of motion or blur.
To initialize OpenGL, you typically need to do the following steps :
1. Include the necessary header files:
++
#include <GL/gl.h>
#include <GL/glut.h> // For creating a window
Copy code
2. Initialize the GLUT library (OpenGL Utility Toolkit) and create a window: ++ int
main(int argc, char** argv) { glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); // Specify the display
mode glutInitWindowSize(800, 600); // Set the initial window size
4|Page
glutInitWindowPosition(100, 100); // Set the initial window position
glutCreateWindow("OpenGL Window"); // Create the window with a title
// ...
Copy code
3. Set the initial background color using the `glClearColor()` function: ++
void init() { glClearColor(0.0, 0.0, 0.0, 1.0); // Set the background
color to black }
Copy code
4. Specify the OpenGL rendering callback functions, like the `display()` function for
drawing on the window: ++ void display() {
glClear(GL_COLOR_BUFFER_BIT); // Clear the color buffer
// Perform drawing operations here glFlush(); //
Flush the buffer and display the result
int main(int argc, char** argv) {
// ...
glutDisplayFunc(display); // Set the display callback function
// ...
Copy code
5. Enter the event processing loop using `glutMainLoop()`: ++ int main(int argc,
char** argv) {
5|Page
// ...
glutMainLoop(); // Start the event processing loop
return 0; }
Copy code
These are the basic routines and steps to initialize OpenGL, set the color attributes, and create a
window using GLUT. You can then add more functionality, such as drawing shapes, texture
mapping, and handling user input, to create a complete OpenGL application.
Graphics primitives in computer graphics refer to the basic geometric shapes or objects that can be
used to render images on a screen. These primitives are typically defined by their mathematical
equations or algorithms, and they can be used to create more complex and detailed graphics.
Some common graphics primitives include:
1. Points: A single point in space, typically represented by its coordinates (x, y, z).
2. Lines: A straight line segment defined by two endpoints.
3. Polygons: A closed shape with three or more straight sides. Common types of polygons
include triangles, quadrilaterals, and polygons with more than four sides.
4. Circles: A round shape defined by its center point and radius.
5. Ellipses: A stretched or squashed circle defined by its center point, major and minor axes
lengths, and rotation angle.
6. Curves: Smooth mathematical shapes that are not easily defined by simple equations.
Examples include Bézier curves and splines.
7. Text: Rendering of alphanumeric characters and symbols. These primitives can be used as
building blocks to create more complex images by combining, transforming, and modifying
them. Graphics libraries or software often provide functions or APIs (Application
Programming Interfaces) for drawing and manipulating these primitives, allowing developers
to create 2D or 3D graphics in their applications
6|Page
Points individual points
Lines pairs of vertices interpreted as individual line segments
Polygon boundary of a simple, convex polygon
Triangles triples of vertices interpreted as triangles
Quads quadruples of vertices interpreted as four-sided polygons
7|Page
Line Strip series of connected line segments
Line Loop same as above, with a segment added between last and first vertices
Triangle linked strip of triangles
Strip
Triangle linked fan of triangles
Fan
Quad Strip linked strip of quadrilaterals
8|Page
Points individual points
Lines pairs of vertices interpreted as individual line segments
Polygon boundary of a simple, convex polygon
Triangles triples of vertices interpreted as triangles
9|Page
Quads quadruples of vertices interpreted as four-sided polygons
Line Strip series of connected line segments
Line Loop same as above, with a segment added between last and first vertices
10 | P a g e
Triangle linked strip of triangles
Strip
Triangle linked fan of triangles
Fan
Quad Strip linked strip of quadrilaterals
Other Important Aspects:
a. Shaders: OpenGL supports programmable shaders, which allow developers to write custom
code for various stages of the rendering pipeline, such as vertex shaders, geometry shaders, and
fragment shaders. This provides advanced control over the rendering process.
b. Transformations: OpenGL allows for various transformations, such as translation, rotation,
and scaling, to manipulate objects in the 3D space. These transformations assist in creating
complex scenes and animations.
c. Texture Mapping: OpenGL supports texture mapping, enabling developers to apply images
or patterns to 3D objects, giving them a realistic appearance.
11 | P a g e
d. Lighting and Shading: OpenGL provides capabilities to simulate lighting effects, such as
ambient, diffuse, and specular lighting. Developers can control the shading models to achieve
desired rendering results.
#include <GL/glut.h>
// Function to draw points using glPoints
void drawPoints() {
glPointSize(10.0f); // Set point size
glBegin(GL_POINTS); // Begin point drawing
// Draw points
glVertex2f(0.0f, 0.0f);
glVertex2f(1.0f, 0.0f);
glVertex2f(0.0f, 1.0f);
glVertex2f(-1.0f, 0.0f);
glVertex2f(0.0f, -1.0f);
glEnd(); // End point drawing
// Function to draw lines using glLines
void drawLines() {
glLineWidth(2.0f); // Set line width
12 | P a g e
glBegin(GL_LINES); // Begin line drawing
// Draw lines glVertex2f(-1.0f,
0.0f); glVertex2f(1.0f, 0.0f);
glVertex2f(0.0f, 1.0f);
glVertex2f(0.0f, -1.0f);
glVertex2f(-1.0f, -1.0f);
glVertex2f(1.0f, 1.0f);
glEnd(); // End line drawing
// Function to draw line strip using glLineStrip
void drawLineStrip() {
glLineWidth(2.0f); // Set line width
glBegin(GL_LINE_STRIP); // Begin line strip drawing
// Draw line strip glVertex2f(-
1.0f, 0.0f); glVertex2f(1.0f, 0.0f);
glVertex2f(0.0f, 1.0f);
glVertex2f(0.0f, -1.0f);
13 | P a g e
glVertex2f(-1.0f, -1.0f);
glVertex2f(1.0f, 1.0f);
glEnd(); // End line strip drawing
// Function to draw line loop using glLineLoop void
drawLineLoop() {
glLineWidth(2.0f); // Set line width
glBegin(GL_LINE_LOOP); // Begin line loop drawing
// Draw line loop glVertex2f(-
1.0f, 0.0f); glVertex2f(1.0f, 0.0f);
glVertex2f(0.0f, 1.0f);
glVertex2f(0.0f, -1.0f);
glVertex2f(-1.0f, -1.0f);
glVertex2f(1.0f, 1.0f);
glEnd(); // End line loop drawing
14 | P a g e
// Function to draw triangles using glTriangles void
drawTriangles() { glBegin(GL_TRIANGLES); //
Begin triangle drawing
// Draw triangles glVertex2f(-
1.0f, 0.0f); glVertex2f(1.0f, 0.0f);
glVertex2f(0.0f, 1.0f);
glVertex2f(0.0f, -1.0f);
glVertex2f(-1.0f, -1.0f);
glVertex2f(1.0f, 1.0f);
glEnd(); // End triangle drawing
// Function to draw triangle strip using glTriangleStrip void
drawTriangleStrip() { glBegin(GL_TRIANGLE_STRIP); //
Begin triangle strip drawing
// Draw triangle strip glVertex2f(-
1.0f, 0.0f); glVertex2f(1.0f, 0.0f);
glVertex2f(0.0f, 1.0f);
15 | P a g e
glVertex2f(0.0f, -1.0f); glVertex2f(-
1.0f, -1.0f); glVertex2f(1.0f, 1.0f);
glEnd(); // End triangle strip drawing
// Function to draw triangle fan using glTriangleFan void
drawTriangleFan() { glBegin(GL_TRIANGLE_FAN); //
Begin triangle fan drawing
// Draw triangle fan
glVertex2f(0.0f, 0.0f);
glVertex2f(1.0f, 0.0f);
glVertex2f(0.0f, 1.0f); glVertex2f(-
1.0f, 0.0f); glVertex2f(0.0f, -1.0f);
glVertex2f(0.0f, 1.0f);
glEnd(); // End triangle fan drawing
// Function to draw quads using glQuads void
drawQuads() { glBegin(GL_QUADS); //
Begin quad drawing
// Draw quads glVertex2f(-
1.0f, 0.0f); glVertex2f(1.0f,
16 | P a g e
0.0f); glVertex2f(1.0f, 1.0f);
glVertex2f(-1.0f, 1.0f);
glVertex2f(-1.0f, 0.0f);
glVertex2f(1.0f, 0.0f);
glVertex2f(1.0f, -1.0f);
glVertex2f(-1.0f, -1.0f);
glEnd(); // End quad drawing
// Function to draw quad strip using glQuadStrip void
drawQuadStrip() { glBegin(GL_QUAD_STRIP); //
Begin quad strip drawing
// Draw quad strip glVertex2f(-
1.0f, 0.0f); glVertex2f(1.0f, 0.0f);
glVertex2f(-1.0f, 1.0f);
glVertex2f(1.0f, 1.0f); glVertex2f(-
1.0f, -1.0f); glVertex2f(1.0f, -1.0f);
glEnd(); // End quad strip drawing
17 | P a g e
// Function to draw polygon using glPolygon void
drawPolygon() { glBegin(GL_POLYGON); //
Begin polygon drawing
// Draw polygon glVertex2f(-
1.0f, 0.0f); glVertex2f(1.0f, 0.0f);
glVertex2f(1.0f, 1.0f);
glVertex2f(0.0f, 1.0f);
glVertex2f(-1.0f, 1.0f);
glEnd(); // End polygon drawing
// Display callback function void display() {
glClear(GL_COLOR_BUFFER_BIT); // Clear color buffer
glColor3f(1.0f, 0.0f, 0.0f); // Set drawing color to red
// Draw different types of primitives
drawPoints(); drawLines();
drawLineStrip(); drawLineLoop();
drawTriangles(); drawTriangleStrip();
18 | P a g e
drawTriangleFan();
drawQuads(); drawQuadStrip();
drawPolygon();
glFlush(); // Flush drawing commands
// Setup function void setup() { glClearColor(1.0f, 1.0f,
1.0f, 1.0f); // Set clear color to white
// Main function int main(int argc, char** argv) { glutInit(&argc, argv); //
Initialize GLUT glutCreateWindow("OpenGL Primitives"); // Create a window
with the given title glutInitWindowSize(500, 500); // Set window size
glutInitWindowPosition(100, 100); // Set window position
glutDisplayFunc(display); // Set display callback function setup(); // Setup
OpenGL parameters glutMainLoop(); // Enter the event processing loop
return 0;}
Problem-01: PROBLEMS BASED ON BRESENHAM LINE DRAWING ALGORITHM-
Calculate the points between the starting coordinates (10, 25) and ending coordinates (18, 30).
Solution-
- Given
• Starting coordinates = (X0, Y0) = (10, 25)
• Ending coordinates = (Xn, Yn) = (18, 30) Step-01:
Calculate ΔX and ΔY from the given input.
• ΔX = Xn – X0 = 18 – 10 = 8
19 | P a g e
• ΔY =Yn – Y0 = 30 – 25 =5 Step-02:
Calculate the decision parameter.
Pk
= 2ΔY – ΔX
=2x5–8
=2
So, decision parameter Pk = 2
Step-03:
As Pk >= 0, so case-02 is satisfied.
Thus,
• Pk+1 = Pk + 2ΔY – 2ΔX = 3 + (2 x 5) – (2 x 8) = -4
• Xk+1 = Xk + 1 = 10 + 1 = 11
• Yk+1 = Yk + 1 = 25 + 1 = 26
Similarly, Step-03 is executed until the end point is reached or number of iterations equals to 4
times.(Number of iterations = ΔX =8)
31
30
29
28
27
26
25
24
0 2 4 6 8 10 12 14 16 18 20
x-axis
20 | P a g e
Pk Pk+1 Xk+1 Yk+1
10 25
2 -4 11 26
-4 6 12 26
6 0 13 27
0 -6 14 28
-6 4 15 28
4 -2 16 29
-2 8 17 29
8 2 18 30
21 | P a g e
PROBLEMS BASED ON DDA ALGORITHM-
Problem-01:
Calculate the points between the starting point (10, 25) and ending point (18, 30).
Solution-
Given-
• Starting coordinates = (X0, Y0) = (10, 25)
• Ending coordinates = (Xn, Yn) = (18, 30) Step-01:
Calculate ΔX, ΔY and M from the given input.
• ΔX = Xn – X0 = 18 – 10 = 8
• ΔY =Yn – Y0 = 20 – 30 =5
• M = ΔY / ΔX = 5/ 8=0.625 Step-02:
Calculate the number of steps.
As |ΔX| >|ΔY| = 8>5, so number of steps = Δx = 8 Step-03:
As M <1, so case-01is satisfied.
Now, Step-03 is executed until Step-04 is satisfied.
Xp Yp Xp+1 Yp+1 Round off (Xp+1, Yp+1)
10 25 11 25.625 (11, 26)
12 27.25 (12, 27)
13 27.875 (13, 28
14 28.5 (14, 29)
15 29.125 (15, 29)
22 | P a g e
16 29.75 (16, 30)
17 30.375 (17,30)
18 30.9 (8,30)
Y-Values
35
30
25
20
15
10
0
0 2 4 6 8 10 12 14 16 18 20
Given the centre point coordinates (5, 5) and radius as 10, generate all the points to form a circle.
Solution-
Given-
• Centre Coordinates of Circle (X0, Y0) = (5, 5)
• Radius of Circle = 10
As stated in the algorithm,
• We first calculate the points assuming the centre coordinates is (0, 0).
• At the end, we translate the circle.
Step-01, Step-02 and Step-03 are already completed in Problem-01.
23 | P a g e
Now, we find the values of Xplot and Yplot using the formula given in Step-04 of the main algorithm.
The following table shows the generation of points for Quadrant-1-
• Xplot = Xc + X0 = 5 + X0
• Yplot = Yc + Y0 = 5 + Y0
(Xk+1, Yk+1) (Xplot, Yplot)
(0, 10) (5, 15)
(1, 10) (6, 15)
(2, 10) (7, 15)
(3, 10) (8, 15)
(4, 9) (9, 14)
(5, 9) (10, 14)
(6, 8) (11, 13)
24 | P a g e
(8, 6) (13, 11)
(9, 5) (14, 10)
(9, 4) (14, 9)
(10, 3) (15, 8)
(10, 2) (15, 7)
(10, 1) (15, 6)
(10, 0) (15, 5)
These are all points for Quadrant-1.
The following table shows the points for all the quadrants-
Quadrant-1 (X,Y) Quadrant-2 (-X,Y) Quadrant-3 (-X,- Quadrant-4 (X,-Y)
Y)
25 | P a g e
(5, 15) (5, 15) (5, -5) (5, -5)
(6, 15) (4, 15) (4, -5) (6, -5)
(7, 15) (3, 15) (3, -5) (7, -5)
(8, 15) (2, 15) (2, -5) (7, -5)
(9, 14) (1, 14) (1, -4) (9, -4)
(10, 14) (0, 14) (0, -4) (10, -4)
(11, 13) (-1, 13) (-1, -3) (11, -3)
(13, 11) (-3, 11) (-3, -1) (13, -1)
(14, 10) (-4, 10) (-4, -0) (14, -0)
(14, 9) (-4, 9) (-4, 1) (14, 1)
26 | P a g e
(15, 8) (-5, 8) (-5, 2) (15, 2)
(15, 7) (-5, 7) (-7, 3) (15,3)
(15, 6) (-5, 6) (-5, 4) (15, 4)
(15, 5) (-5, 5) (-5, 5) (15, 5)
These are all points of the Circle.
27 | P a g e