The Wayback Machine - https://web.archive.org/web/20240620134733/https://www.geeksforgeeks.org/getting-started-with-opengl/
Open In App

Getting started with OpenGL

Last Updated : 19 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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 Ubuntu
For 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 

  1. Download code block and install it
  2. 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.
  3. Open notepad with run as administrator and open file from 
    1. This PC > C:(C-drive) > Program Files(x86) > CodeBlocks > share > CodeBlocks > templates, (then click to show All Files)
    2. Next, open glut.cbp and search all glut32 and replace with freeglut.
    3. Then, open from This PC > C:(C-drive) > Program Files(x86) > CodeBlocks > share > CodeBlocks > templates > wizard > glut (then click to show All Files)
    4. Open wizard.script and here, also replace all glut32 with freeglut 
  4. Then go to freeglut folder (where it was downloaded) and 
    1. Include > GL and copy all four file from there
    2. Go to This PC > C:(C-drive) > Program Files(x86) > CodeBlocks > MinGW > include > GL and paste it.
    3. 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.
    4. 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.
  5. Now open Code::Blocks. 
    1. Select File > New > Project > GLUT project > Next.
    2. Give project title anything and then choose Next.
    3. For selecting GLUT’s location : This PC > C:(C-drive) > Program Files(x86) > CodeBlocks > MinGW.
    4. 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 initialize
void 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

 



Previous Article
Next Article

Similar Reads

Apache POI | Getting Started
POI stands For "Poor Obfuscation Implementation". Apache POI is an API provided by Apache foundation which is a collection of different java libraries. These libraries gives the facility to read, write and manipulate different Microsoft files such as excel sheet, power-point, and word files. It's first version release on 30 December 2001. Apache PO
4 min read
Rendering a Triangle using OpenGL(using Shaders)
In this article we'll see how to render a triangle using OpenGL. A triangle is probably the simplest shapes you can draw in OpenGL after points and lines and any complicated geometry that you make will me made up of number of triangles joined together.We'll be using the programmable pipeline, so we'll be writing simple shader programs as well and c
9 min read
OpenGL Rendering Pipeline | An Overview
Rendering Pipeline is the sequence of steps that OpenGL takes when rendering objects. Vertex attribute and other data go through a sequence of steps to generate the final image on the screen. There are usually 9-steps in this pipeline most of which are optional and many are programmable. Sequence of steps taken by openGL to generate an image : Vert
3 min read
Program to draw circles using mouse moves in OpenGL
In this article, the task is to draw circles using a single mouse click in OpenGL. OpenGL: OpenGL is a cross-language, cross-platform API for rendering 2D and 3D Vector Graphics. It will make a lot of design as well as animations using this. Create a circle anywhere on the console using a single left mouse click and the coordinates of the center of
3 min read
Draw circles using the polar coordinates and mid-point circle drawing algorithm on the same console using openGL in C++
In this article, the task is to draw circles in two different subwindows using two different algorithms, and the circles are created using a single mouse click. Approach: There are two subwindows and both of them use different algorithms to create circles.The left subwindow creates a circle using Midpoint Circle drawing algorithm and the right subw
5 min read
C program to color the different figures drawn on the console using OpenGL
In this article, the task is to create different figures on a computer screen using OpenGL. Approach: In this article, in order to fill the figures with colors, different algorithms are used, so the filling can be done in the optimized pattern.Here, the flood fill and boundary fill algorithm with the implementation in the C++ program with the conce
5 min read
Program to draw a Hut in using OpenGL in C++
In this article, we will discuss how to create a front-facing view of a hut in OpenGL using two basic shapes i.e. triangle and rectangle. Approach: Follow the steps below to solve the problem: Initialize the toolkit using the function glutInit (&amp;argc, argv).Set the display mode and specify the color scheme using the function glutInitDisplayMode
3 min read
Bezier Curves in OpenGL
OpenGL is a cross-language, cross-platform API for rendering 2D and 3D Vector Graphics. It is used to perform a lot of design as well as animation using OpenGL. In this article, we will discuss the concept and implementation of the Bezier Curves OpenGL. Beizer Curves: Béziers curves are one of the parametric curves most frequently used in computer
5 min read
Error Handling in OpenGL
Many of the features of the OpenGL API are very useful and powerful. But, it’s quite possible that OpenGL programs may contain errors. So, it becomes important to learn error handling in OpenGL programs. The OpenGL and GLU libraries have a simple method of recording errors. When an OpenGL program encounters an error in a call to a base library rout
3 min read
Scan-line Polygon filling using OPENGL in C
Figures on a computer screen can be drawn using polygons. To fill those figures with color, we need to develop some algorithm.There are two famous algorithms for this purpose: Boundary fill and Scanline fill algorithms.Boundary filling requires a lot of processing and thus encounters few problems in real time. Thus the viable alternative is scanlin
8 min read
Article Tags :
Practice Tags :