Getting Started with OpenGL GLUT
Added 31 Jul 2008
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);
}
{
glClear(GL_COLOR_BUFFER_BIT);
glRectf(-5.0, 5.0, 5.0, -5.0);
glutSwapBuffers();
}
//-------------------