×

OpenGL in C

OpenGL stands for Open Graphics Library. It is a low-level, widely supported modeling and rendering software package that is widely available across all the platforms. It is an API used for rendering 2D and 3D vector graphics cross-platform and cross-language. It is mainly used for graphics applications, such as games, CAD design, or modeling. It uses polygons to represent images. It provides a graphics rendering option for many 3D games, such as Quake 3. The OpenGL API is designed mostly in hardware.

  • Design: This API is described as a collection of functions that the client program can call. Although the functions are comparable to those of the C language, it is not language specific.
  • Development: This API is always changing, and Chronos Group often releases updated versions with more features than before. Vendors of GPUs might also offer some extra functionality in the form of an extension.
  • Associated Libraries: The OpenGL utility library, a companion library, is included in the first edition. However, since OpenGL is a rather complicated procedure. Therefore, other libraries are introduced to simplify things, such as the OpenGL Utility Toolkit, which is eventually replaced by free glut. GLEE, GLEW, and gliding were later added to the library.
  • Implementation: OpenGL is implemented using Mesa 3D, an open-source program. By utilizing Direct Rendering Infrastructure, it can do pure software rendering and hardware acceleration on BSD, Linux, and other platforms.

How to install OpenGL on Ubuntu?

Enter the following command in the terminal to install OpenGL on Ubuntu (It is same as you do for any other installation):

Sudo apt-get install freeglut3-dev

To work on the Ubuntu operating system

gccfilename.c -lGL -lGLU -glut

Here, filename.c is the name of the file with which this program is saved.

Installing OpenGL in Code::Blocks on Windows

  • First of all download code block and install it on the system.
  • Navigate to the URL and download the zip file from the link that follows the freeglut MinGW package with the link named Download and extract freeglut 3.0.0 for MinGW.
  • Run notepad as administrator,
    1.  After that, select the File to open from this computer's C: (C-drive) > Program Files(x86) > CodeBlocks > share > CodeBlocks > templates (then click to show All Files).
    2. Next, search all the glut32 in glut.cbp and replace it with freeglut.
    3. After that, launch CodeBlocks from this computer's C: Program Filesx86CodeBlocksshareTemplatesWizardGlut (then click to show All Files).
    4. Open wizard.script and substitute freeglut for all glut32 there as well.
  • Afterward, navigate to the freeglut folder (where it was downloaded), and
    1. Copy all four files from Include > GL.
    2. To paste it, navigate to This PC > C:(C-drive) > Program Files(x86) > CodeBlocks > MinGW > include > GL.
    3. Then, copy two files from the Download's freeglut > lib folder, then paste them into This PC > C:(C-drive) > Program Files(x86) > CodeBlocks > MinGW > lib.
    4. Once more, open the downloaded folder freeglut > bin, copy the freeglut.dll File there, and then navigate to This PC > C: (C-drive) > Windows > SysWOW64 to paste it.
  • Open Code::Blocks now.
    1. Choose File> New > Project > GLUT project from the menu. Next,
    2. Enter anything for the project title, then select Next.
    3. For choosing the location of GLUT: This computer is located at C: Program Filesx86CodeBlocksMinGW.
    4. Click OK, Next, and Finish.

Code::Blocks is now prepared for testing with OpenGL files.

Working of OpenGL in C

Let’s see a simple program for circular drawing implemented in C, utilizing the OpenGL platform to demonstrate how OpenGL functions.

// C application to illustrate utilizing OpenGL to draw a circle
#include<stdio.h>
#include<GL/glut.h>
#include<math.h>
#define pi 3.142857
  
// work to initialize
voidmyInit (void)
{
    // making the background black as the first argument and the other three being zero
    glClearColor(0.0, 0.0, 0.0, 1.0);
      
    // making the color of the image green (in RGB mode), with 1.0 as the central argument.
    glColor3f(0.0, 1.0, 0.0);
      
    // The picture's perimeter is 1 pixel wide
    glPointSize(1.0);
    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity();
      
    // setting the X and Y dimensions of a window
    gluOrtho2D(-780, 780, -420, 420);
}
  
void display (void) 
{
    glClear(GL_COLOR_BUFFER_BIT);
    glBegin(GL_POINTS);
    float x, y, i;
      
    // up to 2*pi, or 360 degrees, iterate the y axis
   //  with a modest angle increment since glVertex2i only draws a point at the supplied coordinate
    for( i = 0; i< (2 * pi); i += 0.001)
    {
        
        x = 200 * cos(i);
        y = 200 * sin(i);
          
        glVertex2i(x, y);
    }
    glEnd();
    glFlush();
}
  
int main (intargc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
      
    // window size in the X and Y directions
    glutInitWindowSize(1366, 768);
    glutInitWindowPosition(0, 0);
      
    // naming the window
    glutCreateWindow("Circle Drawing");
    my init();
      
    glutDisplayFunc(display);
    glutMainLoop();
}

Output:

OpenGL in C

Related Topics

C Programming Tutorial

What is C Programming C is most popular and widely used computer programming language. It was developed by Dennis Ritchie in 1972 at the Bell Lab. It is used to develop system application software. There...

8 minutes read.

Difference between C and Java

C programming and Java programming are two of the earliest programming languages. C programming follows a procedural approach whereas Java programming follows an object-oriented approach. Java programming is a part...

3 minutes read.

Expressions in C

Expressions in C: In the C programming language, an expression defines a formula in which the operands are linked to each other by using operators to compute the value. The operand...

4 minutes read.

Program to Find Mode of an Array in C

Mode is the highest occurring value or number in a given set of data elements. In a group of data values, a value that occurs most frequently is considered to...

3 minutes read.

Sum of N numbers in C using For loop

Before we move on the program of sum of N numbers, first we have to know about the For Loop statement. The syntax of ‘for’ loop in C programming language is...

3 minutes read.

Simple Programs in C Language

In this article, we will discuss the basic programs in C language. Addition of two values in C. Swapping two values. Finding the odd or even values. Finding vowels and consonants in C. Finding the...

11 minutes read.

How to use floor() function in C

Introduction: The floor() used as a function in the C programming language. This function uses to return the largest or most significant integer value. The integer value is smaller than...

3 minutes read.

Find the Largest Three Distinct Elements in an Array using C/C++

In this tutorial, we will demonstrate how to use a C/C++ programme to locate the highest three different elements in an array. C/C++ Program to Find the Largest Three Different Elements...

3 minutes read.

If else Programs in C Programming

If else Programs in C programming The if-else statement in C is based on some particular conditions to perform the operations. If and only if the given condition is valid, the operations listed in...

4 minutes read.

Caesar Cipher Program in C

What is Caesar Cipher? The Caesar Cipher is a type of substitution cipher that is named after Julius Caesar, who is said to have used it to encrypt messages sent to...

2 minutes read.

gets() function in C

gets() is a pre-define or built-in function present in the stdio.h header file. stdio stands for standard input and output. gets() function is used to read a stream and store...

4 minutes read.

C and C++ Binary Files

What is a binary file? A file in which the content is written in binary format is called a binary file. A binary file is not a text file. There are...

7 minutes read.

Macros in C

In the C programming language, the macro is defined as a segment of the code replaced by the macro defined value at the beginning. ‘#define’ is the directive that defines...

3 minutes read.

Results of Comparison Operations in C and C++

In this tutorial, we will explore comparison operators and how the system should compare an incoming value supplied as a context parameter to a given value or range of values. A...

2 minutes read.

#include in C

In the C programming language, #include is another way of inferring a standard, or a user defines file into the application or program. The preprocessor of the programming standard generally...

4 minutes read.

How to use sine() function in C

What is Function? The function is a set of statements. It takes input and performs some computation to produce output. The process is a set of codes only achieved when it is...

3 minutes read.

Multi-dimensional Arrays in C

Multi-dimensional Arrays in C The C programming allows the concept of multi-dimensional arrays. The data within the multidimensional arrays are stored in the tabular form, that is, in a row significant...

3 minutes read.

FIFO Example in the C Language

FIFO is an acronym that means "First In, First Out." It is a data structure handling method in which the first element is handled first, and the last element is...

3 minutes read.

Ceil and Floor in C

In arithmetic, a rational number is a number that can be expressed as the quotient p/q of two integers. Where q is zero. The set of rational numbers includes all...

6 minutes read.

Fibonacci series program in C using Recursion

In this tutorial, we’ll explore how to utilise recursion to build the Fibonacci sequence in C language. What does the term "Fibonacci Series" mean? The following number in a Fibonacci number series is...

2 minutes read.