Search Contact information
University of Cambridge Home Department of Engineering
University of Cambridge >  Engineering Department >  computing help

Using GLUT

GLUT is a facility to make OpenGL graphics easier to use. Here's a short example followed by an explanation of selected commands. The See Also section leads to full documentation.

Example 1

/* Copyright (c) Mark J. Kilgard, 1996. */

/* This program is freely distributable without licensing fees 
   and is provided without guarantee or warrantee expressed or 
   implied. This program is -not- in the public domain. */

#include <glut.h>

void reshape(int w, int h)
{
  /* This short bit of code sets up the coordinate
     system to correspond to actual window coordinates.  This code
     wouldn't be required if you chose a (more typical in 3D) abstract
     coordinate system. */

  glViewport(0, 0, w, h);       /* Establish viewing area to cover entire window. */
  glMatrixMode(GL_PROJECTION);  /* Start modifying the projection matrix. */
  glLoadIdentity();             /* Reset project matrix. */
  glOrtho(0, w, 0, h, -1, 1);   /* Map abstract coords directly to window coords. */
  glScalef(1, -1, 1);           /* Invert Y axis so increasing Y goes down. */
  glTranslatef(0, -h, 0);       /* Shift origin up to upper-left corner. */
}

void display(void)
{
  glClear(GL_COLOR_BUFFER_BIT);
  glBegin(GL_TRIANGLES);
    glColor3f(0.0, 0.0, 1.0);  /* blue */
    glVertex2i(0, 0);
    glColor3f(0.0, 1.0, 0.0);  /* green */
    glVertex2i(200, 200);
    glColor3f(1.0, 0.0, 0.0);  /* red */
    glVertex2i(20, 200);
  glEnd();
  glFlush();  /* Single buffered, so needs a flush. */
}

int main(int argc, char **argv)
{
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_RGB);
  if (! glutGet(GLUT_DISPLAY_MODE_POSSIBLE))
     glutInitDisplayMode(GLUT_INDEX);
  glutCreateWindow("single triangle");
  glutDisplayFunc(display);
  glutReshapeFunc(reshape);
  glutMainLoop();
  return 0;           
}
If you call this file simple.cc then it can be compiled on CUED's machines using g++ -I/usr/include/GL -o simple simple.cc -lglut -lGLU -lGL

Comments about example 1

The compile line is long, but on Unix you can use Makefiles or Shell scripts to save on typing.

The source code displays a triangle (a colourful one on sufficiently good hardware). It's about as simple as graphics code gets, but it's still far from trivial.

The main function

In main the system's initialised, a window is created, then functions are defined to specify what to display and how to respond to window resizing. After that the program goes into a loop, responding to events until the program's killed. Such a structure is typical for modern graphics programs.

The display function

This is called whenever the window needs to be redrawn

The reshape function

This is called whenever the window is moved or reshaped.

Other Commands

This is just a small selection of available commands. More can be seen in the examples and documentation of the See Also section

Windows

Text

Menus

Coordinates

3D shapes

Callbacks

Example 2

This example add a key-event handler and does some error checking.
#include <iostream>
#include <glut.h>

void reshape(int w, int h)
{
  std::cout  << "calling reshape, dimensions " << w << " and " << h << std::endl;
  glViewport(0, 0, w, h);       /* Establish viewing area to cover entire window. */

  glMatrixMode(GL_PROJECTION);  /* Start modifying the projection matrix. */
  glLoadIdentity();      /* Reset project matrix. */       
  glOrtho(0, w, 0, h, -1, 1);   /* Map abstract coords directly to window coords. */
  glScalef(1, -1, 1);           /* Invert Y axis so increasing Y goes down. */
  glTranslatef(0, -h, 0);       /* Shift origin up to upper-left corner. */
  GLenum e;
  while ((e=glGetError()) != GL_NO_ERROR)
    std::cout  << "Error " << e << std::endl;
}

void display(void)
{
  std::cout << "calling display" << std::endl;
  glClear(GL_COLOR_BUFFER_BIT);
  glBegin(GL_TRIANGLES);
    glColor3f(0.0, 0.0, 1.0);  /* blue */
    glVertex2i(0, 0);
    glColor3f(0.0, 1.0, 0.0);  /* green */
    glVertex2i(200, 200);
    glColor3f(1.0, 0.0, 0.0);  /* red */
    glVertex2i(20, 200);
  glEnd();
  glFlush();  /* Single buffered, so needs a flush. */
  GLenum e;
  while ((e=glGetError()) != GL_NO_ERROR)
    std::cout << "Error " << e << std::endl;
}

void keyboard (unsigned char key, int x, int y )
{
  switch ( key ) 
  {
  case 'q':
      exit ( 0 );
      break;
  case 'f':
      glutFullScreen ();
      break;
  case 'w':
      glutReshapeWindow (300,300 );
      break;
  default:
      break;
  }
}

using namespace std;

int main(int argc, char **argv)
{
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_RGB);
  cout << glutGet(GLUT_DISPLAY_MODE_POSSIBLE) << endl;
  if (! glutGet(GLUT_DISPLAY_MODE_POSSIBLE))
     glutInitDisplayMode(GLUT_INDEX);

  glutInitWindowSize(600,600);
  glutCreateWindow((char*)"single triangle");
  glutDisplayFunc(display);
  glutReshapeFunc(reshape);
  glutKeyboardFunc (keyboard);
  glutMainLoop();
  return 0;
}

See Also

© Cambridge University Engineering Dept
Information provided by Tim Love (tpl)
Last updated: May 2008