Experiment - 1
Question. Short note on open GL
An open-source, cross-platform API (Application Programming Interface) called
OpenGL (Open Graphics Library) is used to render 2D and 3D graphics. It offers a
collection of tools for building intricate graphics programme, including support for
lighting, shading, texturing, and many other effects. Virtual reality, computer gaming,
and other applications requiring high-quality visuals frequently use OpenGL. The
Khronos Group, a non-profit organization of businesses and institutions that creates
open standards for multimedia and graphics, maintains it.
Question. Short note on GLU and GLUT
GLU
A companion library to OpenGL called GLU (OpenGL Utility Library) offers a
number of functions to carry out typical tasks including creating textures, drawing
polygons, and altering objects in 3D space. Additionally, GLU supports several of
OpenGL's more complex capabilities, such as tesselation and nurbs (non-uniform
rational B-splines). GLU was created as a component of the initial OpenGL definition
and is still present in current API iterations. However, many of its features are no
longer supported in favour of more contemporary methods for programming graphics.
GLUT
A library called GLUT (OpenGL Utility Toolkit) is used to build windowed OpenGL
applications. It offers a platform-independent method for generating windows and
managing events like keyboard and mouse input. GLUT is helpful for understanding
the fundamentals of 3D graphics programming and is frequently used to produce
straightforward demos and prototypes of OpenGL applications. Modern libraries like
FreeGLUT and GLFW, which offer further features including support for multiple
windows, support for contemporary input devices, and enhanced performance, have
supplanted GLUT.
Question. Short note on OpenGL architecture
To produce images on a computer screen, the OpenGL architecture combines a
number of software layers and a set of API calls.
The device driver is the lowest level component and interacts directly with the
graphics hardware, converting OpenGL commands into the proper hardware
instructions.
The implementation layer sits above the device driver and offers a platform-specific
version of OpenGL, along with any extensions or optimizations.
The application layer is the highest level, and it is here that a programmer works with
OpenGL to produce graphics. The device driver and hardware are reached through
several layers of software once the programmer's code has called the OpenGL API
methods.
There are various OpenGL extensions that offer more capabilities above and beyond
the core API in addition to these layers. These add-ons are optional, and not all
hardware or software platforms might support them.
Installing GLUT
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).
5. Go to Downloadfreeglut-MinGW-3.0.0-1.mp folder freeglut folder
bin folder
a. Copy the freeglut.dll file, if your PC is of 32 bit.
b. If your PC is of 64 bit, go to ×64 folder and copy the freeglut.dll
file.
6. Navigate to “C:\Windows” directory and paste your copied freeglut.dll file.
You have to provide administrator permission.
7. Now we have to set up the compiler. Go to Downloadfreeglut-MinGW-
3.0.0-1.mp folder freeglut folder include GL folder. Copy all the four
files (having .h extension)
8. Navigate to the directory where codeblock compiler is installed, i.e., C:\
Program Files\CodeBlocks\MinGW
a. Open the include folder and check whether the “GL” folder exists or
not. If yes, then paste the files copied from Downloadfreeglut-
MinGW-3.0.0-1.mp folder freeglut include GL to the path C:\
Program Files\CodeBlocks\MinGW.
b. If no, then check for the GL folder in x86_64-w64-mingw32\include
folder. Paste the copied files from Downloadfreeglut-MinGW-3.0.0-
1.mp folder freeglut include GL to the path C:\Program Files\
CodeBlocks\MinGW\x86_64-w64-mingw32\include\GL.
9. Allow the contents to be merged with the folder for all the items.
10. Now we have to set up the compiler. Go to Downloadfreeglut-MinGW-
3.0.0-1.mp folder freeglut lib ×64 folder. Copy all the two files
(having .a extension)
11. Navigate to the directory where codeblock compiler is installed, i.e., C:\
Program Files\CodeBlocks\MinGW
a. Open the lib folder and paste the copied files from
Downloadfreeglut-MinGW-3.0.0-1.mp folder freeglut lib
×64 to the path C:\Program Files\CodeBlocks\MinGW\lib. (If you
have followed the step 9(b), avoid doing this step, rather do step 11(b))
b. If no, 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 it with “freeglut”. Save the file.
15. Go to C:\Program Files\CodeBlocks\share\CodeBlocks\templates directory
and open glut.cbp file in notepad for editing.
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.
18. Run the template source code and enjoy!!!
First OpenGL program
Code:
#include <GL/glut.h>
void display() {
glClear(GL_COLOR_BUFFER_BIT); //clears existing frame buffer
glLoadIdentity(); // reset coordinate system
glFlush(); //display the drawing
}
void init()
{
glClearColor(1.0, 0.0, 0.0, 0.0);
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitWindowSize(640,480);
glutInitWindowPosition(10,10);
glutCreateWindow("Window");
glutDisplayFunc(display);
init();
glutMainLoop();
return 0;
}
Output:
Experiment - 2
Q1. Write a program to draw a simple line
Code:
#include<GL/glut.h>
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1,0,0);
glBegin(GL_LINES);
glVertex2f(-0.5,0);
glVertex2f(0.5,0);
glEnd();
glFlush();
}
int main(int argc,char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(300,300);
glutCreateWindow("Simple Line");
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
Output:
Q2. Write a program to draw line using DDA Line
generating algorithm
Code:
#include<iostream>
#include<GL/glut.h>
#include<cmath>
using namespace std;
void display()
{
double xa=0;
double ya=0;
double xb=4;
double yb=8;
double dx=xb-xa;
double dy=yb-ya;
double m=dy/dx;
double x=xa;
double y=ya;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0,1,0);
glBegin(GL_LINES);
glVertex2f(round(x),round(y));
while(x<xb && y <yb)
{
if(m>1)
{
x=x+(1/m);
y=y+1;
}
else if(m==1)
{
x=x+1;
y=y+1;
}
else if(m<1)
{
x=x+1;
y=y+m;
}
glVertex2f(round(xb),round(yb));
}
glEnd();
glFlush();
}
int main(int argc,char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(300,300);
glutCreateWindow("DDA Line Generation");
gluOrtho2D(-15, 15, -15, 15);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
Output:
Q3. Write a program to draw line using Bresenham
Line drawing algorithm
Code:
#include<iostream>
#include<GL/glut.h>
#include<cmath>
using namespace std;
void display()
{
double xa=9;
double ya=18;
double xb=14;
double yb=22;
double x=xa;
double y=ya;
double dx=xb-xa;
double dy=yb-ya;
double pk=2*dy-dx;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0,0,1);
glBegin(GL_LINES);
glVertex2f(round(x),round(y));
while(x<xb&&y<yb)
{
if(pk<0)
{
x=x+1;
pk=pk+2*dy;
}
else if(pk>=0)
{
x=x+1;
y=y+1;
pk=pk+2*(dy-dx);
}
glVertex2f(round(xb),round(yb));
}
glEnd();
glFlush();
}
int main(int argc,char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(300,300);
glutCreateWindow("Bresenham Line Generation");
gluOrtho2D(-30, 30, -30, 30);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
Output:
Experiment - 3
Q1. Write a program to draw a simple circle
Algorithm:
Step 1: Declare float points x and y
Step 2: Set the value of i as 0
Step 3: Set the value of x and y, x=r*cos(i) and y=r*sin(i) where r is radius of the
circle
Step 4: Plot the point (x,y)
Step 5: Increase the value of I by 0.5
Step 6: If i<=360 go to step 3 else go to step 7
Step 7: End
Code:
#include<iostream>
#include<GL/glut.h>
#include <cmath>
using namespace std;
void display()
{
float x,y;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1, 0, 0);
glBegin(GL_LINES);
for(double i =0; i <= 360;){
x=5*cos(i);
y=5*sin(i);
glVertex2f(x, y);
i=i+.5;
}
glEnd();
glFlush();
}
int main(int argc,char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(300,300);
glutCreateWindow("Circle");
gluOrtho2D(-6, 6, -6, 6);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
Output:
Q2. Write a program to draw a circle using
Bresenham circle drawing algorithm
Algorithm:
Step 1: Declare cemter points (xc,yc) = (0,0) and r=8
Step 2: Initial point (x0,y0) will be (0,r) that is (0,8)
Step 3: Calculate initial decision parameter p=3-2*r
Step 4: Check if x0<=y0 go to step 5 else go to step 10
Step 5: plot(x0,y0)
plot(y0,x0)
plot(-x0,y0)
plot(-y0,x0)
plot(x0,-y0)
plot(y0,-x0)
plot(-x0,-y0)
plot(-y0,-x0)
Step 6: Check if p<0 go to step 7 else go to step 8
Step 7: x0=x0+1 and p=p+4*xo+6;
Step 8: Check if p>=0 go to step 9 else go to step 10
Step 9: xo=xo+1;
yo=yo-1;
p=p+4*(xo-yo)+10;
Go to step 2
Step 10: End
Code:
#include<iostream>
#include<GL/glut.h>
#include <cmath>
using namespace std;
void display()
{
int xc=0;
int yc=0;
int r=8;
int xo=0;
int yo=r;
int xplot;
int yplot;
int p=3-2*r;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0,1,0);
glBegin(GL_POINTS);
while(xplot<=yplot)
{
if(xc!=0&&yc!=0)
{
xplot=xo+xc;
yplot=yo+yc;
}
else
{
xplot=xo;
yplot=yo;
}
glColor3f(0,1,0);
glVertex2f(xplot,yplot);
glVertex2f(yplot,xplot);
glVertex2f(-xplot,yplot);
glVertex2f(-yplot,xplot);
glVertex2f(xplot,-yplot);
glVertex2f(yplot,-xplot);
glVertex2f(-xplot,-yplot);
glVertex2f(-yplot,-xplot);
cout<<xplot<<" "<<yplot<<endl;
if(p<0)
{
xo=xo+1;
p=p+4*xo+6;
}
else if(p>=0)
{
xo=xo+1;
yo=yo-1;
p=p+4*(xo-yo)+10;
}
}
glEnd();
glFlush();
}
int main(int argc,char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(300,300);
glutCreateWindow("Bresenham circle drawing algorithm");
gluOrtho2D(-25,25,-25,25);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
Output:
Q3. Write a program to draw a circle using Mid point
circle drawing algorithm
Algorithm:
Step 1: Declare cemter points (xc,yc) = (0,0) and r=10
Step 2: Initial point (x0,y0) will be (0,r) that is (0,10)
Step 3: Calculate initial decision parameter p=1-r
Step 4: Check if x0<=y0 go to step 5 else go to step 10
Step 5: plot(x0,y0)
plot(y0,x0)
plot(-x0,y0)
plot(-y0,x0)
plot(x0,-y0)
plot(y0,-x0)
plot(-x0,-y0)
plot(-y0,-x0)
Step 6: Check if p<0 go to step 7 else go to step 8
Step 7: x0=x0+1 and p=p+2*xo+1;
Step 8: Check if p>=0 go to step 9 else go to step 10
Step 9: xo=xo+1;
yo=yo-1;
p=p-2y0+2x0+1;
Go to step 2
Step 10: End
Code:
#include<iostream>
#include<GL/glut.h>
#include <cmath>
using namespace std;
void display()
{
int xc=0;
int yc=0;
int r=10;
int xo=0;
int yo=r;
int xplot;
int yplot;
int p=1-r;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0,0,1);
glBegin(GL_POINTS);
while(xplot<=yplot)
{
if(xc!=0&&yc!=0)
{
xplot=xo+xc;
yplot=yo+yc;
}
else
{
xplot=xo;
yplot=yo;
}
glColor3f(0,1,0);
glVertex2f(xplot,yplot);
glVertex2f(yplot,xplot);
glVertex2f(-xplot,yplot);
glVertex2f(-yplot,xplot);
glVertex2f(xplot,-yplot);
glVertex2f(yplot,-xplot);
glVertex2f(-xplot,-yplot);
glVertex2f(-yplot,-xplot);
cout<<xplot<<" "<<yplot<<" "<<p<<endl;
if(p<0)
{
xo=xo+1;
p=p+2*xo+1;
}
else if(p>=0)
{
xo=xo+1;
yo=yo-1;
p=p-2*yo+2*xo+1;
}
}
glEnd();
glFlush();
}
int main(int argc,char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(300,300);
glutCreateWindow("Mid point circle generation algorithm");
gluOrtho2D(-30,30,-30,30);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
Output:
Experiment - 4
Q1. Write a program to draw a simple ellipse
Algorithm:
Step 1: Declare center (xc,yc), current x and y coordinates (x,y) and radii along x and
y axis as rx and ry respectively
Step 2: Initial point will be (0,ry)
Step 3: Calculate initial decision parameter for region 1 as p1=(ry*ry)+(0.25*rx*rx)-
(rx*rx*ry);
Step 4: Check if 2*(ry*ry)*x<2*(rx*rx)*y go to step 5 else go to step 10
Step 5: Check if p1<0 go to step 6 else go to step 7
Step 6: x=x+1;
p1=p1+2*(ry*ry)*x+(ry*ry);
Step 7: Check if p1>=0 go to step 8 else go to step 9
Step 8: x=x+1;
y=y-1;
p1=p1+2*(ry*ry)*x-2*(rx*rx)*y+(ry*ry);
Step 9: Plot(x,y)
Plot(-x,y)
Plot(x,-y)
Plot(-x,-y)
Go to step 4
Step 10: Calculate initial decision parameter for region 2 as
p2=(ry*ry)*(x+0.5)*(x+0.5)+(rx*rx)*(y-1)-(rx*rx)*(ry*ry);
Step 11: Check if y>0 go to step 12 else go to step 17
Step 12: Check if p2>0 go to step 13 else go to step 14
Step 13: y=y-1;
p2=p2-2*(rx*rx*y)+(rx*rx);
Step 14: Check if p2<=0 go to step 15 else go to step 16
Step 15: x=x+1;
y=y-1;
p2=p2+2*(ry*ry*x)-2*(rx*rx*y)+(rx*rx);
Step 16: Plot(x,y)
Plot(-x,y)
Plot(x,-y)
Plot(-x,-y)
Go to step 11
Step 17: End
Code:
#include<GL/glut.h>
#include<iostream>
using namespace std;
void display()
{
double xc=0;
double yc=0;
double x;
double y;
double rx=8;
double ry=6;
x=0;
y=ry;
double p1=(ry*ry)+(0.25*rx*rx)-(rx*rx*ry);
glColor3f(0,1,0);
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POINTS);
while((2*(ry*ry)*x)<(2*(rx*rx)*y))
{
if(p1<0)
{
x=x+1;
p1=p1+2*(ry*ry)*x+(ry*ry);
}
else
{
x=x+1;
y=y-1;
p1=p1+2*(ry*ry)*x-2*(rx*rx)*y+(ry*ry);
}
glVertex2f((xc+x),(y+yc));
glVertex2f(-(xc+x),(y+yc));
glVertex2f((xc+x),-(y+yc));
glVertex2f(-(xc+x),-(y+yc));
cout<<(xc+x)<<" "<<(yc+y)<<" "<<p1<<endl;
}
double p2=(ry*ry)*(x+0.5)*(x+0.5)+(rx*rx)*(y-1)-(rx*rx)*(ry*ry);
cout<<"Region 2"<<endl;
while(y>0)
{
if(p2>0)
{
y=y-1;
p2=p2-2*(rx*rx*y)+(rx*rx);
}
else
{
x=x+1;
y=y-1;
p2=p2+2*(ry*ry*x)-2*(rx*rx*y)+(rx*rx);
}
glVertex2f((xc+x),(y+yc));
glVertex2f(-(xc+x),(y+yc));
glVertex2f((xc+x),-(y+yc));
glVertex2f(-(xc+x),-(y+yc));
cout<<(x+xc)<<" "<<(y+yc)<<" "<<p2<<endl;
}
glEnd();
glFlush();
}
int main(int argc,char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(300,300);
glutCreateWindow("Midpoint Ellipse");
gluOrtho2D(-15,15,-15,15);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
Output:
Experiment - 5
Some basic in built functions
1. In built function to draw a line
Code:
#include <graphics.h>
#include <conio.h>
//To draw a line
int main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, (char*)"");
line(50,60,150,160);
getch();
closegraph();
return 0;
}
Output: