Free since 2005 · No login required
AT

Academic Tutorials

Learn at your own pace

site-mobile-top-banner · 320x50

Getting Started with OpenGL GLUT

Added 31 Jul 2008

GLUT is the OpenGL Utility Toolkit. GLUT is good for learning OpenGL since it is platform independent, and you don't have to worry about Windows programming. The same program should compile under Windows, Linux, UNIX, etc. GLUT applications compile as console applications so you can jump right in and start trying things out. The only drawback of course is no system specific windows components such as menus, buttons, etc. you can still however use the keyboard, a mouse, a joystick, etc. for user input. GLUT also has it's own menu system which will be covered.

Since GLUT is platform independent the natural language to choose is ANSI C, and for Windows of course, Pelles C. In this tutorial I will show you how to set up Pelles C for programming in GLUT and also show some sample code.

Before you begin you will need the GLUT header and library files, glut32.lib, glut.lib and glut.h. I have a download available here that my code samples are tested with:
http://www.trajectorylabs.com/OpenGL/GLUT.ZIP These files are from the OpenGL SuperBible book 3rd Edition CD. The very latest Win32 GLUT files can be found here: http://www.xmission.com/~nate/glut.html My code however is not guaranteed to work with anything other than the files I have supplied.

Once you have downloaded the header and library files add them to the proper Pelles C directories as shown:
PellesC\Lib\Win\glut32.lib
PellesC\Lib\Win\glut.lib
PellesC\Include\Win\gl\glut.h

Now fire up Pelles C and start a new Win32 program (EXE) as a new workspace. Select File - New - Source code and paste the code below.

//---------------------------------------------------------------------
// Getting Started with OpenGL GLUT
// A Very Simple OpenGL Example
//
// Draws a simple window with a rectangle in it
//---------------------------------------------------------------------

#include
#include
#include

void init(void);
void display(void);

int main (int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(250, 250);
glutInitWindowPosition(100, 100);
glutCreateWindow("My First OpenGL Application");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glColor3f(0.0, 0.0, 1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-10.0, 10.0, -10.0, 10.0, -10.0, 10.0);
}

void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glRectf(-5.0, 5.0, 5.0, -5.0);
glutSwapBuffers();
}
//-------------------