Introduction to freeglut

freeglut is an open-sourceHaving code available for public to view, edit, and update . library released as an alternative to the OpenGL Utility Toolkit (GLUT) library. GLUT was designed to write OpenGL programs using OpenGL graphics APIApplication Programming Interface.

Why was freeglut written?

The main reason for writing freeglut was the lack of maintenance of the GLUT library. The library was written in the early 90s and was last updated in 1998 when its latest version, 3.7, was released. Due to the lack of updates, GLUT has limited support for new platforms and licensing issues. Therefore, freeglut was developed with the objective of providing open-source licenses to developers.

History

On December 1, 1999, Pawel W. Olszta, with the support of Andreas Umbach and Steve Baker, started writing freeGLUT as an open-source and actively updated alternative to GLUT. The main ambition was to overcome the limitations of the original GLUT by providing support for newer platforms, an open-source license, and introducing improvements in window management and event handling.

Comparison between GLUT and freeglut

GLUT simplifies tasks by providing functions for:

  • Creating and managing windows, making it easier for developers to display graphics

  • Callbacks to handle events and rendering

  • Supporting bitmaps and stroke fonts

  • Drawing basic geometric shapes like spheres, cubes, teapots, etc.

  • Querying system information, controlling the execution of the application, and managing timers

GLUT functionalities
GLUT functionalities

Although GLUT and freeglut share many core functionalities, freeglut offers several additional features compared to the original GLUT. The main advantages of freeglut over GLUT are:

freeglut functionalities
freeglut functionalities

Code

Here’s an example code that builds a blue square using the freeglut library.

#include <GL/freeglut.h>


void init() {
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);  
}


void display() {
    glClear(GL_COLOR_BUFFER_BIT);  

    glBegin(GL_QUADS);
    glColor3f(0.0f, 0.0f, 1.0f); 
    glVertex2f(-0.5f, -0.5f);     // Vertex 1
    glVertex2f(0.5f, -0.5f);      // Vertex 2
    glVertex2f(0.5f, 0.5f);       // Vertex 3
    glVertex2f(-0.5f, 0.5f);      // Vertex 4
    glEnd();
    glFlush(); 
}

int main(int argc, char** argv) {
    glutInit(&argc, argv);               
    glutInitDisplayMode(GLUT_SINGLE);   
    glutInitWindowSize(800, 600);       
    glutCreateWindow("Blue Square");    

    init();                   
    glutDisplayFunc(display); 
    
    glutMainLoop(); 
    return 0;
}
Code example to build to blue square using freeglut library

Explanation

  • Line 1: The freeglut library is included in the code (#include <GL/freeglut.h>).

  • Lines 4–6: The init() function is responsible for initializing OpenGL settings. In line 5, glClearColor() sets the clear color to black.

  • Lines 9–20: display() is a callback function of the freeglut library. It’s used to redisplay the window.

    • Line 10: glClear() is used to clear the buffer. It takes a single argument. In this example, glClear() takes the GL_COLOR_BUFFER_BIT argument, which clears the color buffer to the clear color specified in init() function, i.e., black.

    • Line 12: glBegin() defines type of the geometric shape. In this code, it takes GL_QUADS as an argument to begin drawing a quadrilateral, i.e., a square in this case.

    • Line 13: glColor3f() sets the color of the square to blue.

    • Lines 14–17: These lines define the vertices of the square by specifying its xx and yy coordinates.

    • Line 18: glEnd() ends the drawing of the quadrilateral.

    • Line 19: glFlush() forces the execution of the GL functions, which, in this case, produces the drawing of a square.

  • Lines 22–33: It’s the main function of the program.

    • Line 23: glutInit(&argc, argv) initializes freeglut.

    • Line 24: glutInitDisplayMode() sets the display mode. In this example, it takes GLUT_SINGLE as the argument, which specifies that there’s only one buffer for rendering.

    • Line 25: glutInitWindowSize() sets the window size by specifying width and height respectively.

    • Line 26: glutCreateWindow() creates the window with the Blue Square title.

    • Line 29: glutDisplayFunc() sets the display callback function to display.

    • Line 31: glutMainLoop() enters freeglut main loop to handle updates.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved