×

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

Multilevel Feedback Queue Scheduling (MLFQ) CPU Scheduling

In this article, you will learn how to initialise an array to 0 in C In C, an array is declared as: char ZEROARRAY[2022]; The global scope changes at runtime to all zeros....

3 minutes read.

Use of fflush(stdin) in C

Use of fflush(stdin) in C Usually, fflush() is only used for the output stream. The purpose is to clean (or flush) the output buffer and transfer the buffered data into...

2 minutes read.

Random function in C

Random function in C In the C programming language, the rand() is a function used for Pseudo Random Number Generator (PRNG). The random number generated by the rand() function is not...

4 minutes read.

C Program for Mean and Median of an Unsorted Array

In this tutorial, we will look at how to determine the mean and median of a given unsorted array. To determine the Mean: To get the average, mean is determined. The formula...

2 minutes read.

DSA Program in C

How is a Data Structure Implemented? The foundational terms of a data structure are the following terms. Data may be arranged using data structures to make it easier to use. Each data...

20 minutes read.

For Loop in C

The Syntax of For Loop for (initialization statement; test expression; update statement) {     /* main body of the FOR loop */ } How does For loop work? In for loop, the initialization command is...

3 minutes read.

C Program to Find Largest Number Using Dynamic Memory Allocation

This tutorial will teach us how to locate the highest number a user has ever input into a dynamically allocated memory. C Program: #include <stdio.h> #include <stdlib.h> int main ()  {   int no;   double *data_n;   printf...

1 minute read.

Ferror() in c

Ferror():  In C, the ferror() function checks assuming there is a blunder in the given stream. The ferror() function is utilized to check for the document blunder on given stream. A return...

4 minutes read.

Example of Iteration in C

The iterations in the C language are the statements which are executed number of times until a certain condition is reached. These iterations are regarded as “Loops” in C language....

3 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.

GPA Calculator in C

GPA stands for Grade Point Average. This GPA is used to measure the student's academic performance in educational institutes. By using this, segregation takes place and lets us know how...

4 minutes read.

Character Class Tests in C

Introduction The <ctype.h> is a header file in C which is used to declare functions for testing characters. This header file of the C standard library is used to declare multiple...

4 minutes read.

Formatted Input and output function in C

Formatted I/O functions display multiple outputs to the user by taking various inputs. All data types like int, float, char and double are supported by the formatted I/O function. In...

4 minutes read.

Floyd’s triangle in C

Floyd’s triangle in C Floyd’s triangle is named after a person Robert Floyd. It is a right-angled triangle that is of natural numbers starting from 1 and consecutively selects the upcoming...

4 minutes read.

memcpy() in C

Introduction: Here we discuss about the memcpy() function in C. The memcpy() function is also called the Copy Memory Block function. Used to create a copy of a specific drawing...

4 minutes read.

2f in C language

Float data type in c: Double-precision floating-point numbers with up to 17 significant digits are stored in the FLOAT data type. FLOAT is equivalent to C's double data type and IEEE...

3 minutes read.

Call Back Function in Embedded C

Callback function are one the most remarkable systems in C. A callback function is any code that is passed as a contention to another code, so this is the last...

3 minutes read.

How to Merge Array in C?

Introduction Merging arrays is a common task for many developers but might be difficult for beginners of C programming. Fortunately, our guide will show you how to quickly merge arrays in...

6 minutes read.

For loop in C Programming

In the C programming language, the for loop is used to repeatedly iterate over a set of instructions or a section of code. It is frequently used to traverse array-based...

3 minutes read.

Multiplication Table in C

Displaying table up to 10 #include <stdio.h> int main() {     int num, i = 1;     printf("     Enter any Number:");     scanf("%d", &num);     printf("Multiplication table of %d: ", num);  ...

1 minute read.