Getting started with OpenGL

29 Jun, 2022
Open Graphics Library (OpenGL) is a cross-language (language independent), cross-platform (platform-independent) API for rendering 2D and 3D Vector Graphics(use of polygons to represent image). OpenGL API is designed mostly in hardware. 
Design : This API is defined as a set of functions which may be called by the client program. Although functions are similar to those of C language but it is language independent.
Development : It is an evolving API and Khronos Group regularly releases its new version having some extended feature compare to previous one. GPU vendors may also provide some additional functionality in the form of extension.
Associated Libraries : The earliest version is released with a companion library called OpenGL utility library. But since OpenGL is quite a complex process. So in order to make it easier other library such as OpenGL Utility Toolkit is added which is later superseded by free glut. Later included library were GLEE, GLEW, and gliding.
Implementation : Mesa 3D is an open source implementation of OpenGL. It can do pure software rendering and it may also use hardware acceleration on BSD, Linux, and other platforms by taking advantage of Direct Rendering Infrastructure.
Install OpenGL on UbuntuFor installing OpenGL on Ubuntu, just execute the following command (like installing any other thing) in terminal : 
sudo apt-get install freeglut3-dev

For working on Ubuntu operating system: 
gcc filename.c -lGL -lGLU -lglut 
where filename.c is the name of the file
with which this program is saved.

Install OpenGL on windows in Code::Blocks 
Download code block and install itGo to the link and download zip file from the download link that appears after freeglut MinGW package with having link name as Download freeglut 3.0.0 for MinGW and extract it.Open notepad with run as administrator and open file from This PC > C:(C-drive) > Program Files(x86) > CodeBlocks > share > CodeBlocks > templates, (then click to show All Files)Next, open glut.cbp and search all glut32 and replace with freeglut.Then, open from This PC > C:(C-drive) > Program Files(x86) > CodeBlocks > share > CodeBlocks > templates > wizard > glut (then click to show All Files)Open wizard.script and here, also replace all glut32 with freeglut Then go to freeglut folder (where it was downloaded) and Include > GL and copy all four file from thereGo to This PC > C:(C-drive) > Program Files(x86) > CodeBlocks > MinGW > include > GL and paste it.Then, from download folder freeglut > lib, copy two files and go to This PC > C:(C-drive) > Program Files(x86) > CodeBlocks > MinGW > lib and paste it.Again go to downloaded folder freeglut > bin and copy one file (freeglut.dll) from here and go to This PC > C:(C-drive) > Windows > SysWOW64 and paste this file.Now open Code::Blocks. Select File > New > Project > GLUT project > Next.Give project title anything and then choose Next.For selecting GLUT’s location : This PC > C:(C-drive) > Program Files(x86) > CodeBlocks > MinGW.Press OK > Next > Finish.
Download code block and install it
Go to the link and download zip file from the download link that appears after freeglut MinGW package with having link name as Download freeglut 3.0.0 for MinGW and extract it.
Open notepad with run as administrator and open file from This PC > C:(C-drive) > Program Files(x86) > CodeBlocks > share > CodeBlocks > templates, (then click to show All Files)Next, open glut.cbp and search all glut32 and replace with freeglut.Then, open from This PC > C:(C-drive) > Program Files(x86) > CodeBlocks > share > CodeBlocks > templates > wizard > glut (then click to show All Files)Open wizard.script and here, also replace all glut32 with freeglut 
This PC > C:(C-drive) > Program Files(x86) > CodeBlocks > share > CodeBlocks > templates, (then click to show All Files)Next, open glut.cbp and search all glut32 and replace with freeglut.Then, open from This PC > C:(C-drive) > Program Files(x86) > CodeBlocks > share > CodeBlocks > templates > wizard > glut (then click to show All Files)Open wizard.script and here, also replace all glut32 with freeglut 
This PC > C:(C-drive) > Program Files(x86) > CodeBlocks > share > CodeBlocks > templates, (then click to show All Files)
Next, open glut.cbp and search all glut32 and replace with freeglut.
Then, open from This PC > C:(C-drive) > Program Files(x86) > CodeBlocks > share > CodeBlocks > templates > wizard > glut (then click to show All Files)
Open wizard.script and here, also replace all glut32 with freeglut 
Then go to freeglut folder (where it was downloaded) and Include > GL and copy all four file from thereGo to This PC > C:(C-drive) > Program Files(x86) > CodeBlocks > MinGW > include > GL and paste it.Then, from download folder freeglut > lib, copy two files and go to This PC > C:(C-drive) > Program Files(x86) > CodeBlocks > MinGW > lib and paste it.Again go to downloaded folder freeglut > bin and copy one file (freeglut.dll) from here and go to This PC > C:(C-drive) > Windows > SysWOW64 and paste this file.
Include > GL and copy all four file from thereGo to This PC > C:(C-drive) > Program Files(x86) > CodeBlocks > MinGW > include > GL and paste it.Then, from download folder freeglut > lib, copy two files and go to This PC > C:(C-drive) > Program Files(x86) > CodeBlocks > MinGW > lib and paste it.Again go to downloaded folder freeglut > bin and copy one file (freeglut.dll) from here and go to This PC > C:(C-drive) > Windows > SysWOW64 and paste this file.
Include > GL and copy all four file from there
Go to This PC > C:(C-drive) > Program Files(x86) > CodeBlocks > MinGW > include > GL and paste it.
Then, from download folder freeglut > lib, copy two files and go to This PC > C:(C-drive) > Program Files(x86) > CodeBlocks > MinGW > lib and paste it.
Again go to downloaded folder freeglut > bin and copy one file (freeglut.dll) from here and go to This PC > C:(C-drive) > Windows > SysWOW64 and paste this file.
Now open Code::Blocks. Select File > New > Project > GLUT project > Next.Give project title anything and then choose Next.For selecting GLUT’s location : This PC > C:(C-drive) > Program Files(x86) > CodeBlocks > MinGW.Press OK > Next > Finish.
Select File > New > Project > GLUT project > Next.Give project title anything and then choose Next.For selecting GLUT’s location : This PC > C:(C-drive) > Program Files(x86) > CodeBlocks > MinGW.Press OK > Next > Finish.
Select File > New > Project > GLUT project > Next.
Give project title anything and then choose Next.
For selecting GLUT’s location : This PC > C:(C-drive) > Program Files(x86) > CodeBlocks > MinGW.
Press OK > Next > Finish.
Now, Code::Blocks is ready to test for OpenGL File. 
Demonstrate working with OpenGL
To show how OpenGL works, a simple program of circle – drawing is added in C using OpenGL platform.
C
// C program to demonstrate// drawing a circle using// OpenGL#include<stdio.h>#include<GL/glut.h>#include<math.h>#define pi 3.142857  // function to initializevoid myInit (void){    // making background color black as first     // 3 arguments all are 0.0    glClearColor(0.0, 0.0, 0.0, 1.0);          // making picture color green (in RGB mode), as middle argument is 1.0    glColor3f(0.0, 1.0, 0.0);          // breadth of picture boundary is 1 pixel    glPointSize(1.0);    glMatrixMode(GL_PROJECTION);     glLoadIdentity();          // setting window dimension in X- and Y- direction    gluOrtho2D(-780, 780, -420, 420);}  void display (void) {    glClear(GL_COLOR_BUFFER_BIT);    glBegin(GL_POINTS);    float x, y, i;          // iterate y up to 2*pi, i.e., 360 degree    // with small increment in angle as    // glVertex2i just draws a point on specified co-ordinate    for ( i = 0; i < (2 * pi); i += 0.001)    {        // let 200 is radius of circle and as,        // circle is defined as x=r*cos(i) and y=r*sin(i)        x = 200 * cos(i);        y = 200 * sin(i);                  glVertex2i(x, y);    }    glEnd();    glFlush();}  int main (int argc, char** argv){    glutInit(&argc, argv);    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);          // giving window size in X- and Y- direction    glutInitWindowSize(1366, 768);    glutInitWindowPosition(0, 0);          // Giving name to window    glutCreateWindow("Circle Drawing");    myInit();          glutDisplayFunc(display);    glutMainLoop();}
To compile the above program in Ubuntu, 
gcc filename.c -lGL -lGLU -lglut -lm 
where filename.c is the name of the file
with which this program is saved.

Output of above program is shown in below screenshot
Reference for installation in Code::Blocks : https://www.youtube.com/watch?v=NPcnymtP2SE
This article is contributed by Aditya Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
jainshubham766
computer-graphics
OpenGL
Misc
Misc
Misc
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.