Before I start any OpenGL project, past experience has always taught me to make sure that I can get the basics to compile on my current system before trying to do anything clever. I have once spent countless hours trying to debug an OpenGL project only to find that I was using an old buggy version of FreeGLUT (Which has since improved greatly!). Below is a short code listing of a very basic OpenGL application.
* A very simple OpenGL application.
// These provide all we need for a basic OpenGL application.
double rotation = 0;
// Clear the GLUT window in preparation to draw a new frame.
glClearColor(1.0, 1.0, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Set the viewpoint back to the origin.
// Set the viewpoint to simulate the camera moving backwards.
glTranslatef(0, 0, -5);
// Rotate the viewpoint to simulate any subsequent shapes rotating.
glRotatef(rotation, 1, 1, 1);
// Enable states to manipulate how shapes are drawn.
// Set the color of any shapes drawn next.
glColor3f(0.04, 0.34, 0.60);
// A convenience function provided by GLUT to draw a 3D teapot.
// Disable the states again in order to keep track of what has been enabled.
// Update image to the GLUT window.
// Signal GLUT to draw a new frame.
void reshape(int width, int height)
// Make sure that the aspect ratio remains the same
// if the GLUT window resizes.
glViewport(0, 0, (GLsizei) width, (GLsizei) height);
gluPerspective(60, (GLfloat) width / (GLfloat) height, 1.0, 100.0);
void keyboard(unsigned char key, int x, int y)
if (key == 'q')
// The easiest way to stop the GLUT main loop.
int main(int argc, char* argv)
// Prepares GLUT and displays a window to draw to.
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
// Provides callbacks so to be able to interact with the GLUT main loop.
// Enters the GLUT processing loop.
In order to get this to compile we need to link it with 3 libraries: libGL, libGLU and libglut
This code can be compiled by issuing the following command:-
g++ simple.cpp -o simple -lglut -lGLU -lGL
Now when ./simple is run a window should appear with a rotating teapot.
If this has worked, then congratulations on creating your simple OpenGL application, now you can progress onto the fun stuff!