OpenGL VRML W3D            
  Home Tutorials Books Applications Tools Docs Models Textures  



GLUT Tutorial   

  GLUT Tutorial


Resizing the Window

Moving the Camera
Advanced Keyboard
Moving the Camera II

Pop-up Menus
Sub Menus
Modifying a Menu
Swapping Menus

Bitmap Fonts
Bitmaps and the Ortho View
Stroke Fonts

Frames per Second
GLUT Game Mode

Creating and Destroying Subwindows
Resizing SubWindows
Rendering to SubWindows


OpenGLTutorials @

Led Shader
View Frustum Culling
GLSL Tutorial
Maths Tutorial
Billboarding Tutorial
Picking Tutorial
Terrain Tutorial
Display Lists Tutorial
GLUT Tutorial

[Previous] [Next: Initialization]

GLUT Tutorial


GLUT stands for OpenGL Utility Toolkit. Mark J. Kilgard, to enable the construction of OpenGL applications that are truly window system independent, conceived the GLUT library. Thanks to GLUT, we can write applications without having to learn about X windows or Microsoft's own window system. Kilgard implemented the version for X windows, and later Nate Robins ported it to Microsoft Windows. Thanks to both, you did a great job.

In this tutorial I'll introduce you to the basics of building an application using GLUT. This tutorial won't introduce fancy visual effects in order to keep the code as simple as possible.

What you need

In order to write applications with GLUT you should have the latest version (at the time of writing this, I believe it is 3.7). The GLUT distribution comes with lots and lots of examples so after you read through the basics in here you'll have plenty of material to go on. Check out the GLUTs page.

In order to write a C application using GLUT you'll need three files:
  • glut.h - This is the file you'll have to include in your source code. The common place to put this file is in the gl folder which should be inside the include folder of your system.
  • glut.lib (SGI version for Windows) and glut32.lib (Microsoft's version) - This file must be linked to your application so make sure to put it your lib folder.
  • glut32.dll (Windows) and glut.dll (SGI version for Windows) - choose one according to the OpenGL you're using. If using Microsoft's version then you must choose glut32.dll. You should place the dll file in your system folder.
  • Setting up in Visual C/C++ 6.0

    There are two options available for a project in Visual C/C++: console, and Win32. the first is the most common option. The application will have two windows: a console window and the OpenGL window. With Win32 it is still possible to build an application using GLUT without messing up with windows programming. All you have to do is to change one setting.
  • Select project->settings from the main menu;
  • Select the "Link" tab from the dialog box;
  • Select "Output" from the "Category" combo box;
  • In the "Entry-point symbol" textbox type "mainCRTStartup"
  • For an existing console project there is a simple way to transform it into a Win32 application, i.e. to get rid of the console window.
  • Follow the steps above to add the entry-point symbol
  • In the "Project options" textbox replace "subsystem:console" with "subsystem:windows"
  • Alternatively just add the following line to the beginning of your c code:

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

    Note that the line is commented on purpose.

    And thats all there is to it, now the application has no console window, just the OpenGL window. In Visual C/C++ you'll have to do the following in order to link an application using GLUT:
  • Select Project/Settings from the main menu.
  • A dialog box appears, select the Link tab.
  • Add the following files to the Object/library modules: opengl32.lib glut32.lib glu32.lib
  • Note: I've added glu32.lib and opengl32.lib as well. These are the two libraries for the standard OpenGL. GLU is an API that comes with the standard OpenGL distribution.

    Notes on Using OpenGL with VS.NET

    Many people have experienced a small problem when using VS.NET to build OpenGL applications: a compiler error that, as far as I know, seems to exist only in these compilers (VS 2003 and 2005, VC 6.0 is OK). Here is the actual text generated by VS 2005:

    c:\programas\microsoft visual studio 8\vc\include\stdlib.h(406) : 
    	error C2381: 'exit' : redefinition; __declspec(noreturn) differs
            c:\opengl\toolkits\includes\gl\glut.h(146) : see declaration of 'exit'
    This problem seems to be caused by the inclusion of glut.h before stdlib.h. Reversing the order solves the problem. The following code causes the compiler error:
    #include <GL/glut.h>
    #include <stdlib.h>
    But when including first stdlib.h the problem disappears:
    #include <stdlib.h>
    #include <GL/glut.h>
    Do let me know if you have any tips so that I may include them in here and save time to all those who starting to learn OpenGL.

    OK, so all is set, lets learn how to write GLUT applications. If there is anything that is not quite clear please let me know and I'll try to improve it. Your feedback is important.

    I hope you enjoy this tutorial.


    [Previous] [Next: Initialization]


    Site designed and maintained by António Ramires Fernandes
    Your comments, suggestions and references to further material are welcome!

    Lighthouse 3D privacy statement