CG Lab File
CG Lab File
CG Lab File
Lab File
Introduction to OpenGL
OpenGL:
OpenGL provides many functions for creating and manipulating graphics. For drawing
primitive shapes (points, lines, triangles), textures, and lighting. It's used in a variety of
applications, including video games, scientific simulations, virtual and augmented reality, and
computer-aided design. One of OpenGL's strengths is its portability. It is available for various
platforms such as Windows, macOS, Linux, Android and iOS. It is also compatible with
various programming languages such as C, C++, Java and Python.
OpenGL has been updated several times over the years, with the latest version being OpenGL
4.6 released in 2017.
Although GLU has been largely superseded by other libraries and APIs in modern graphics
programming, it is still sometimes used in applications that require compatibility with legacy
code and older systems.
GLUT (OpenGL Utility Toolkit) is an open source, cross-platform library that provides a set
of tools and functions for creating and managing windows and user interfaces in OpenGL
applications. It was originally developed by Mark Kilgard in 1993 to make writing OpenGL
programs easier.
GLUT simplifies the task of creating graphical user interfaces in OpenGL applications by
providing numerous functions for managing windows, keyboard and mouse input, and
timing. It also provides a simple API for drawing geometric primitives, text, and images on
the screen.
Commonly used to create simple graphic demos, educational software, and small games,
GLUT remains popular for quick prototyping and experimenting with new ideas. However, it
has limited functionality, and developers of more complex applications often use other
libraries and frameworks such as GLFW, SDL, and Qt.
OpenGL Architecture:
Application layer:
At this level, graphics application code is written. Connect to the OpenGL API to specify the
desired rendering operations, such as: For example, camera settings, object creation, lighting
and shading definitions, etc.
1. Install Codeblock.
2. Go to https://www.transmissionzero.co.uk/software/freeglut-devel/
3. Click on Download freeglut 3.0.0 for MinGW (with PGP signature and PGP key)
4. Extract the contents of the extracted file (freeglut-MinGW-3.0.0-1.mp).
6. Navigate to “C:\Windows” directory and paste your copied freeglut.dll file. You have
to provide administrator permission.
9. Allow the contents to be merged with the folder for all the items.
b. If not, then check for the lib folder in x86_64-w64-mingw32 folder. Paste the
copied files from Downloadfreeglut-MinGW-3.0.0-1.mp folder freeglut
lib to the path C:\Program Files\CodeBlocks\MinGW\x86_64-w64-
mingw32\lib.
12. Allow the contents to be merged with the folder for all the items.
13. Go to C:\Program Files\CodeBlocks\share\CodeBlocks\templates\wizard\glut
directory and open wizard.script file in notepad.
14. Search for the term “glut32” in the wizard.script file and replace all the occurrence in
with “freeglut”. Save the file.
16. Search for the term “glut32” in the glut.cbp file and replace all the occurrence in it
with “freeglut”. Save the file.
17. Now create a new GLUT project in codeblocks. Type a title of the project and enter
the GLUT’s location as installation directory of the GLUT compiler (the directory
used in step 8, i.e., C:\Program Files\CodeBlocks\MinGW\x86_64-w64-mingw32).
Click Next and then Finish.
#include<GL/freeglut.h>
void init(){
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); //Line C
glutInitWindowSize(640,480);
glutInitWindowPosition(1000,200);
glutCreateWindow("Simple Window");
}
void display()
{
glClearColor(0,0,1.0,1.0);
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
}
return 0;
}
Output:
Difference between C & OpenGL
Differences between C and OpenGL:
Purpose:
C is a general-purpose programming language used for developing a wide range of software
applications, while OpenGL is a graphics rendering API used for creating 2D and 3D
graphics.
Syntax:
C has its own syntax, control structures, and libraries that enable developers to create
software applications, while OpenGL provides a set of functions and interfaces for graphics
rendering.
Abstraction level:
C is a low-level programming language that provides direct access to hardware and memory,
while OpenGL provides a higher level of abstraction by hiding the details of the graphics
hardware and providing a set of functions and interfaces that can be used to create graphics.
Implementation:
C can be implemented on a variety of platforms and hardware architectures, while OpenGL
requires an implementation that supports the specific hardware and operating system being
used.
Scope:
C can be used to develop a wide range of software applications, including systems
programming, operating systems, compilers, and more, while OpenGL is specifically
designed for graphics rendering.
Data type:
C provides a variety of data types for representing variables and values, while OpenGL
provides data types specific to graphics rendering, such as vertex buffers, textures, and
shaders.
Library:
C provides a wide range of libraries for performing various tasks, while OpenGL itself is a
library that provides a set of functions and interfaces for rendering graphics.
Compatibility:
C code can be compiled to run on a wide variety of platforms and hardware architectures,
while OpenGL code depends on the hardware and operating system used and will need to be
modified to be compatible with a variety of platforms. There are cases.
CG LAB EXP-2
2. Determine the number of steps required to draw the line. This can be done by taking
the maximum difference between the x-coordinates and the y-coordinates, and then
rounding up to the nearest integer:
steps = max(abs(x2 - x1), abs(y2 - y1))
3. Calculate the increment in x and y coordinates for each step using the formula:
dx = (x2 - x1) / steps
dy = (y2 - y1) / steps
4. Initialise the starting point of the line to (x1, y1) and increment the x and y
coordinates by dx and dy respectively for each step.
5. Round off the x and y coordinates to the nearest integer to get the pixel coordinates
for each step.
6. Plot the pixels along the line using the calculated pixel coordinates.
DDA Code
#include <stdio.h>
#include <GL/glut.h>
#include <math.h>
void display() {
float x1 = -0.7f, y1 = -0.7f;
float x2 = 0.7f, y2 = 0.7f;
float dx = x2 - x1;
float dy = y2 - y1;
float steps = abs(dx) > abs(dy) ? abs(dx) : abs(dy);
float x_inc = dx / steps;
float y_inc = dy / steps;
float x = x1, y = y1;
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_LINES);
Output:
Bresenham Line Drawing Algorithm
1. Determine the endpoints of the line segment to be drawn: (x1, y1) and (x2, y2).
2. Calculate the differences between the endpoints in the x and y directions: dx = abs(x2
- x1) and dy = abs(y2 - y1).
3. Determine the sign of the differences: sx = sign(x2 - x1) and sy = sign(y2 - y1). The
sign function returns -1 if the value is negative, 0 if the value is zero, and 1 if the
value is positive.
5. Loop from x1 to x2, and for each x value, calculate the corresponding y value using
the error term:
If the error term is greater than or equal to dx, increment y by sy and subtract dx
from the error term. Otherwise, leave y unchanged and
add dy to the error term.
Increment x by sx.
Bresenham Code
#include <gl/glut.h>
#include <stdio.h>
void myInit() {
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(0.0, 0.0, 0.0, 1.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0, 500, 0, 500);
}
void draw_pixel(int x, int y) {
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
}
dx = x2-x1;
dy = y2-y1;
} else {
draw_pixel(x, y);
e = 2*dx-dy;
inc1 = 2*(dx-dy);
inc2 = 2*dx;
for (i=0; i<dy; i++) {
if (e >= 0) {
x += incx;
e += inc1;
}
else
e += inc2;
y += incy;
draw_pixel(x, y);
}
}
}
void myDisplay() {
draw_line(x1, x2, y1, y2);
glFlush();
}
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(0, 0);
glutCreateWindow("Bresenham's Line Drawing");
myInit();
glutDisplayFunc(myDisplay);
glutMainLoop();
}
Output
CG LAB EXP-3
Code:
#include <GL/glut.h>
#include <cmath>
glBegin(GL_LINE_LOOP);
for(i = 0; i <= lineAmount;i++) {
glVertex2f(
x + (radius * cos(i * twicePi / lineAmount)),
y + (radius* sin(i * twicePi / lineAmount))
);
}
glEnd();
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
}
Output:
b) W.A.P to draw a circle using Bresenham circle
drawing algorithm.
Algorithm:
1. Start
3. Set x = 0, y = r and d = 3 - 2r
4. Plot point (x + x c, y + y c)
Code:
#include <GL/glut.h>
#include <cmath>
void init() {
glClearColor(0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0, WINDOW_WIDTH, 0.0, WINDOW_HEIGHT);
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
}
Output:
c) W.A.P to draw a circle using the Midpoint circle
drawing algorithm.
Algorithm:
1. Start
2. Declare x, y, r as variables, where (x, y) are coordinates of the center and r is the
radius of the circle.
3. Set x = 0, y = r and p = 1 - r
4. Plot point (x + x c, y + y c)
Code:
#include <GL/glut.h>
#include <cmath>
void init() {
glClearColor(0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0, WINDOW_WIDTH, 0.0, WINDOW_HEIGHT);
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
}
Code:
#include <GL/glut.h>
#include <cmath>
void init() {
glClearColor(0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0, WINDOW_WIDTH, 0.0, WINDOW_HEIGHT);
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
}
Output:
CG LAB EXP-5
1. Line
Code:
#include <GL/glut.h>
void display() {
glClear(GL_COLOR_BUFFER_BIT); // Clear the screen
2. Rectangle
Code:
#include <GL/glut.h>
void rect_display() {
glClear(GL_COLOR_BUFFER_BIT); // clear the screen
glColor3f(0.0, 0.0, 1.0); // set rectangle color to blue
glBegin(GL_QUADS); // begin drawing rectangle
glVertex2f(-0.5, -0.5); // set first point of rectangle
glVertex2f(0.5, -0.5); // set second point of rectangle
glVertex2f(0.5, 0.5); // set third point of rectangle
glVertex2f(-0.5, 0.5); // set fourth point of rectangle
glEnd(); // end drawing rectangle
glFlush(); // flush the buffer
}
Output:
3. Arc
Code:
#include <GL/glut.h> // include the GLUT library
#include <math.h> // include the math library
void display()
{
glClear(GL_COLOR_BUFFER_BIT); // clear the color buffer
glColor3f(1.0, 1.0, 1.0); // set the color to white
4. Circle
Code:
#include <GL/glut.h>
#include <math.h>
void display() {
glClear(GL_COLOR_BUFFER_BIT); // clear the screen
glColor3f(0.0, 1.0, 1.0); // set circle color to green
glBegin(GL_TRIANGLE_FAN); // begin drawing circle
glVertex2f(0.0, 0.0); // set center point of circle
float radius = 0.5; // set radius of circle
int num_segments = 50; // set number of segments for circle
for (int i = 0; i <= num_segments; i++) {
float angle = i * 2.0f * M_PI / num_segments;
float x = radius * cosf(angle);
float y = radius * sinf(angle);
glVertex2f(x, y);
}
glEnd(); // end drawing circle
glFlush(); // flush the buffer
}
Output:
CG LAB EXP-6
Boundary Fill
Boundary fill is a computer graphics algorithm used to fill a closed area (a region) with a
specific colour or pattern. The algorithm starts from a given point inside the region and
proceeds to fill the region with the specified colour or pattern until it reaches the boundary of
the region. The boundary is defined by a different colour or pattern, which is typically used to
distinguish the region from its surroundings. The boundary fill algorithm can be implemented
in different ways, such as the 4-connected approach, where pixels are considered neighbours
if they share a common edge, or the 8-connected approach, where pixels are considered
neighbours if they share a common edge or corner. The boundary fill algorithm is commonly
used in computer graphics applications such as drawing and painting programs, where it
allows users to quickly fill in areas of colour without having to manually colour each
individual pixel.
Algorithm:
1. Start by selecting a point inside the region to be filled.
4. If a neighbour is not already filled and its color is not the boundary color, set its color
to the fill color and recursively repeat step 3 for this neighbour.
5. Repeat steps 3-4 for all 4/8-connected neighbours of the selected pixel.
6. Continue this process until all pixels within the region are filled.
Boundary Fill 4-connected
Code:
#include <GL/glut.h>
void display() {
glClear(GL_COLOR_BUFFER_BIT);
// draw a square
glColor3f(1.0f, 1.0f, 1.0f);
glBegin(GL_LINE_LOOP);
glVertex2i(100, 100);
glVertex2i(200, 100);
glVertex2i(200, 200);
glVertex2i(100, 200);
glEnd();
glFlush();
}
Output:
Boundary Fill 8-connected
Code:
#include <GL/glut.h>
#include <stdlib.h>
glFlush();
}
// Main function
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);
glutCreateWindow("Boundary Fill 8 Connected Algorithm");
glutDisplayFunc(display);
init();
glutMainLoop();
return 0;
}
Output:
Flood Fill
Flood fill is a computer graphics algorithm used to fill a closed region with a specific color or
pattern. The algorithm works by selecting a starting point inside the region to be filled and
then colouring all the neighbouring pixels that are not part of the boundary until the entire
region is filled.The algorithm is named after the idea of "flooding" the region with color
starting from the initial point. It is commonly used in painting software, graphic design
applications, and video game development.The flood fill algorithm can be implemented in
either a recursive or iterative manner. Recursive implementation uses function calls to fill
neighbouring pixels while iterative implementation uses a stack or queue to keep track of
pixels to be filled.The flood fill algorithm can be modified to work with patterns, gradients,
or textures. It can also be used in combination with other algorithms such as edge detection to
create more complex effects.
Flood Fill algorithm
1. Choose a starting point inside the region to be filled.
5. If a neighbouring pixel has the same color as the starting point and is not already
filled, set its color to the fill color and add it to the list of pixels to check.
Code:
#include <GL/glut.h>
#include <stdlib.h>
glFlush();
}
// Main function
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);
glutCreateWindow("Flood Fill 4 Connected Algorithm");
glutDisplayFunc(display);
init();
glutMainLoop();
return 0;
}
Output:
Flood Fill 8-connected
Code:
#include <GL/glut.h>
#include <stdlib.h>
glFlush();
}
// Main function
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);
glutCreateWindow("Flood Fill 8 Connected Algorithm");
glutDisplayFunc(display);
init();
glutMainLoop();
return 0;
}
Output: