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
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
//Set the window size
glutInitWindowSize(600,400);
//Set the window position
glutInitWindowPosition(100,100);
// Create window
glutCreateWindow("Our first glut window");

Where:

  • 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
glutDisplayFunc(display);

// Event procesing loop
glutMainLoop();

Where:

  • 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
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// Define the square color
glColor3f(1.0,1.0,1.0);

// creating the square
glBegin(GL_POLYGON);
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);
glEnd();

glutSwapBuffers();
}

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.

Links:

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