GLUT Tutorials


GLUT Window

Tutorials > GLUT > GLUT Window

View Full Source


GLUT Window Welcome to the first GLUT tutorial. This set of tutorials is aimed at those people following the OpenGL tutorials. For more information on GLUT, please visit the What is OpenGL tutorial.

Before starting these tutorials, you will need to have GLUT properly installed. For more information on how to achieve this, please visit the Setting Up Your Environment tutorial for OpenGL.

Once you are able to compile GLUT applications, you will be able to continue with these tutorials. This tutorial will explain how an OpenGL window can be created using GLUT. This window will display all graphics in our 2D or 3D scenes.

Contents of main.cpp :

Our first line of code may seem slightly complicated. This is not necessary. It is simply sending a parameter to the linker, removing the console window. You need to pass on the message /subsystem:"windows" /entry:"mainCRTStartup". The line below includes the necessary escape characters.

The console window can be written to as per normal and is extremely useful for debugging. When distributing your application, it is likely that you would want to remove the console window.

#pragma comment(linker, "/subsystem:\"windows\" \

All GLUT applications that we will create need to include the glut.h header file which was previously installed.

#include <GL/glut.h>

As you will see in future tutorials, a number of steps need to be taken to initialize your application. We will place this initialization code within an init function. This function should tell us whether everything was initialized correctly and so we therefore return a boolean result.

bool init()
	return true;

Displaying graphics on the screen involved displaying a number of frames. Every frame, a function needs to be called to render the graphics. This function we will call the display function. This function may be called a number of times per second depending on your current frame rate.

void display()


Our main function is created as normal. We accept command line arguments as this information needs to be passed onto GLUT.

int main(int argc, char *argv[])

The first GLUT function we will discuss is the glutInit function. This is used to initialize the GLUT library. This function accepts two parameters, being the parameters for your command line arguments passed onto the main function. Note that pointers are required so the memory address(&) operator must be placed before argc.

Note that the contents of argc and argv will be modified as the parameters are extracted by GLUT.

	glutInit(&argc, argv);

Our next two functions are used to initialize the window's position and size.

glutInitWindowPosition accepts two parameters, an x and a y value. This specifies the top-left corner of the window where (0,0) is the top-left corner of your screen.

glutInitWindowSize also accepts 2 parameters, determining the width and height of the window in pixels.

	glutInitWindowPosition(50, 50);
	glutInitWindowSize(500, 500);

Once we have set the properties for our window, we can actually create the window. This is done with a call to glutCreateWindow. This function accepts one character string parameter which determines the text that will appear in the window's title bar.

	glutCreateWindow("01 - GLUT Window");

Remember we said that the display function is called for each frame. GLUT needs to know of this function. This is achieved through the use of the glutDisplayFunc function. It accepts one function pointer parameter which must point to your display function.


Next, we initialize our application and quit if there was any error. Obviously for this tutorial, this test will always pass.

	if (!init())
		return 1;

The final step is to enter GLUT's event processing loop. This causes the application to continue looping, processing window, keyboard, mouse and other events until the program is closed. Without this function call, the program would simply exit without you being able to view your scene.


	return 0;

You have now successfully created your first OpenGL window using GLUT. Upon running the program, you will be presented with a 500x500 window. Notice that whatever was behind your window is displayed within the window. This happens as we have not entered any code to clear the window or to display something.

Please let me know of any comments you may have : Contact Me

Source Files : Visual Studio Dev-C++ Unix / Linux

  Tutorial 02 - Keyboard Input >

Back to Top

All Rights Reserved, © Zeus Communication, Multimedia & Development 2004-2005

Read the Disclaimer