Using GLUT with MinGW


Introduction

OpenGL is a popular and widely implemented graphics specification, and is often a good choice when you need to write programs featuring 2D or 3D graphics. However, OpenGL defines how graphics should be drawn, and it is up to the programmer to handle things such as window definition and input from the mouse and keyboard. This can be performed using API specific to the windowing system, which of course requires that the programmer have a good knowledge of the API. Alternatively, libraries such as GLUT or SDL may be used. These provide an abstraction layer from the windowing system, with the added advantage that programs may be written which are independent of architecture and operating system (assuming of course that appropriate development tools and libraries are available).

This tutorial focuses on GLUT, and explains how to develop OpenGL applications for Windows using the C or C++ languages with MinGW (Minimalist GNU For Windows). MinGW was chosen because it is freely available for download, and because its usage will be familiar to anyone who has used the GCC.

Setting Up GLUT

If you haven’t already done so, make sure you have the latest stable versions of the MinGW packages installed. These may be obtained from the SourceForge project page. You’ll need the “gcc-core”, “binutils”, “MinGW Runtime”, and “w32api” packages to develop GLUT applications (these were probably installed by default if you used the MinGW setup program). To develop C++ applications, you’ll also need “gcc-c++”.

To use GLUT with MinGW, you’ll need a version of GLUT for Win32 which is suitable for use with MinGW. I was unable to find an up to date version on the internet, so I have created a GLUT MinGW package (and checksums) which you may download.

The MinGWw32api” package actually comes with some older and incompatible GLUT libraries. These may be found in the “lib\” directory of your MinGW installation, under the names of “libglut.a” and “libglut32.a”. Before you do anything else, you should remove them from the directory, and put them somewhere safe (in case you decide to reinstate them at a later date). Having done this, you can copy the new “libglut32.a” from my archive, and place it in the “lib\” folder. Also bear in mind, that when you update your “w32api” package, it could overwrite “libglut32.a” with the old version. If it does, simply repeat this stage.

The “glut.h” file can then be placed into the “include\GL\” directory of your MinGW installation, and “glut32.dll” (needed for running GLUT applications, but not for compiling them) can be put in a systems directory, such as “C:\WINDOWS\SYSTEM32\”.

Compiling GLUT Applications With MinGW

To use functions from the GLUT library, you should “#include <GL/glut.h>” in your source code. When linking a GLUT application, you should link against the GLUT and OpenGL libraries, with the flags “-lglut32 -lopengl32”. If you use GLU library functions, you will also need to link against the GLU library, with the flag “-lglu32”. If you don’t want a console window to appear, you can add the flag “-mwindows”. Thus the command line to compile a typical GLUT application would be:


gcc -o progname.exe sourcefile.c -lglut32 -lglu32 -lopengl32 -mwindows

[Windows XP cmd.exe: gcc -o example.exe example.c -lglut32 -lglu32 -lopengl32 -mwindows]

Here is an example C program which you can use to test your GLUT setup (for convenience, you can download this GLUT example along with a Makefile).


#include <stdlib.h>
#include <GL/glut.h>

void keyboard(unsigned char key, int x, int y);
void display(void);


int main(int argc, char** argv)
{
  glutInit(&argc, argv);
  glutCreateWindow("GLUT Test");
  glutKeyboardFunc(&keyboard);
  glutDisplayFunc(&display);
  glutMainLoop();

  return EXIT_SUCCESS;
}


void keyboard(unsigned char key, int x, int y)
{
  switch (key)
  {
    case '\x1B':
      exit(EXIT_SUCCESS);
      break;
  }
}


void display()
{
  glClear(GL_COLOR_BUFFER_BIT);

  glColor3f(1.0f, 0.0f, 0.0f);

  glBegin(GL_POLYGON);
    glVertex2f(-0.5f, -0.5f);
    glVertex2f( 0.5f, -0.5f);
    glVertex2f( 0.5f,  0.5f);
    glVertex2f(-0.5f,  0.5f);
  glEnd();

  glFlush();
}

[Image of a window contining a red square in the middle]

Distributing GLUT Applications

Before distributing your GLUT Win32 application, you should remember that “glut32.dll” is not a standard Windows DLL. For this reason, you should either include it with your application, or provide your users with a method of obtaining a current version of the DLL. If you include the DLL with your application, you can place the DLL in the same folder as your executable.

Cross‑Compiling GLUT Applications

Although this tutorial focused on compiling GLUT applications with the Windows version of MinGW, you can of course cross‑compile them on non‑Windows versions. You can use the same package as used in this tutorial, and follow the same instructions. You won’t need to use “glut32.dll”, but obviously you’ll still need to distribute it with your software.