Creating a window with OpenGL (glut)

by on Nov.05, 2009, under OpenGL

Today, we will create our first OpenGL’s window (using glut). But what is it that “GLUT”?.It is the OpenGL Utility Toolkit, a window system independent toolkit for writing OpenGL programs. Basically it would help us to create windows, independent of the operating system we are programing for(Windows, Linux, Mac ,….). All of this functions belonging to GLUT have the initials glut at the begining of the function’s name.

How to configure your operating system, or programing environment, it is not the target of this post, so I assume, you have your own environment, ready to rock :D. So let’s start creating our window.

First of all we have to tell GLUT that we want to create a window, and it should start preparing all of the resources for it. To do that we call the function GlutInit to initialize the GLUT library as follows:

//GLUT initialization
glutInit(&argc, argv);

where argc, argv are our parameters in our function main. That’s means that our function main should looks as:

int main(int argc, char **argv)
//code here

After this step we will prepare, select the size, the position and the title of our glut window:

//RGBA with double buffer
//Set the window size
//Set the window position
// Create window
glutCreateWindow("Our first glut window");


  • glutInitDisplayMode will set the initial display mode.
  • glutInitWindowSize as you probably deduced, it sets the width and the height of the window
  • glutInitWindowPosition indicates the initial position of our window
  • glutCreateWindow creates the window.

Now we have to do 2 more things. Register our callback functions to (manage keyboard input, display, ….) and start the event processing loop.

// display glut callback

// Event procesing loop


  • glutDisplayFunc here we register our display callback function, where we will write all we want to show in the window.
  • glutMainLoop this initializes the event processing loop, (it will call our callback functions)

And we will have our window created and running. To finish lets write some code to show a square in our display window (remember our callback functions). This code will be purely OpenGL as follows:

static void display(void)
// Clear the screen to black

// Define the square color

// creating the square
glVertex3f(0.25, 0.25, 0.0);
glVertex3f(0.75, 0.25, 0.0);
glVertex3f(0.75, 0.75, 0.0);
glVertex3f(0.25, 0.75, 0.0);


And…. that’s all. Now we have our new window, with one pink square on it. If you want to download this code, just click in the Source code link below.


Source Code (here)
Project source for CodeBlocks and Ubuntu (here)
Glut API definition (here)
GLUT forĀ  Ubuntu (here)
GLUT for Windows (here)

Leave a Reply