GLUT Tutorials


Rendering And Animation

Tutorials > GLUT > Rendering And Animation

View Full Source


Rendering and Animation The purpose of GLUT is to allow you to render graphics to the screen using OpenGL. This tutorial will explain how to set up the window to allow for rendering of graphics.

Before jumping into the code, you need to understand how this occurs. As explained in a previous tutorial, the image you see on the screen is created by displaying a number of frames. A number of frames can be shown every second. You may remember that we said that the code for rendering each frame is placed within the display function.

Any information shown on the screen is written to a buffer. This buffer holds color values representing each pixel on the screen. If you write to this buffer at the same time that it is shown, you will notice a flashing effect. This is solved by using a technique known as double buffering. This technique works by you writing to a back buffer. While you are writing to the back buffer, the front buffer is being displayed. Once you have finished writing to the back buffer, the buffers are swapped. This eliminates the flashing effect due to writing and displaying the buffer at the same time. This will become more evident below.

Contents of main.cpp :

#include <GL/glut.h>

bool init()
	return true;

Any rendering code placed within the display function is written to the back buffer. As we said above, the buffers need to be swapped to display the contents of the back buffer. The buffers are swapped by making a call to glutSwapBuffers. This function does not take any parameters.

void display()
	// Rendering


When window system events are not being received, extra processing can be done within an idle function. This can contain any processing code that you require such as animation code.

After updating the data for a scene, you will most likely want to view the updated scene. A message therefore needs to be sent to GLUT to display another frame. This is achieved with a call to glutPostRedisplay as shown below.

void idle()
	// Animation Code


int main(int argc, char *argv[])
	glutInit(&argc, argv);

	glutInitWindowPosition(200, 200);
	glutInitWindowSize(200, 200);

An important GLUT function is the glutInitDisplayMode function. This function tells GLUT about what memory or resources need to be allocated for the program. One parameter is passed and can consist of the following values :

Flag Description
GLUT_SINGLE Enables single buffering.
GLUT_DOUBLE Enables double buffering.
GLUT_RGBA Sets the color mode to accept red, green, blue and alpha values.
GLUT_INDEX Sets a color index mode window.

Extra flags such GLUT_ACCUM, GLUT_ALPHA, GLUT_DEPTH and GLUT_STENCIL can also be passed to enable certain buffers. These will be discussed in future tutorials.

A number of these flags can be passed by separating each one with an OR(|) operator.

The code below specifies that the window must allow double buffering and the color buffer must be made up of RGB values.

	glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);

	glutCreateWindow("04 - Rendering and Animation");


	if (!init())
		return 1;


	return 0;

Upon running this program, you will notice that the window is finally filled. This is because the window is being constantly updated. The window may not display anything useful. You may see a window with a distorted picture. This is because the contents of the buffer are being written to the screen even though we have not written anything to it. To find out how something can be displayed in this window, please visit the OpenGL tutorials. You should now have an understanding of how a GLUT window can be set up for rendering and animation.

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

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

< Tutorial 03 - Mouse Input Tutorial 05 - Fullscreen >

Back to Top

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

Read the Disclaimer