Hello world:  drawing a square in OpenGL

The following code fragment demonstrates a very simple OpenGL program which opens a graphics window and draws a square. It also prints 'helllo world' in the console window. The code is illustrative of the use of the glut library in opening the graphics window and managing the display loop. The code is in the directory  class/tut/square on the CD-ROM, which contains the following files:
 
main.cpp source code
Makefile.win Windows makefile usable from the command line. This will possibly require some customisation for your machine. Usage is: 
nmake -f Makefile.win
sq.dsp project file for Visual C++
sq.dsw workspace file for Visual C++.  Double click this to load and compile the code within the Visual C++ environment.
Debug subdirectory containing object and executable files
Makefile.linux Makefile for Linux. This will possibly require some customisation for your machine. 
make -f Makefile.linux
 
#include <stdio.h>
#include <GL/glut.h>

void display(void)
{
 glClear( GL_COLOR_BUFFER_BIT);
 glColor3f(0.0, 1.0, 0.0);
 glBegin(GL_POLYGON);
  glVertex3f(2.0, 4.0, 0.0);
  glVertex3f(8.0, 4.0, 0.0);
  glVertex3f(8.0, 6.0, 0.0);
  glVertex3f(2.0, 6.0, 0.0);
 glEnd();
 glFlush();
}

int main(int argc, char **argv)
{
 printf("hello world\n");
 glutInit(&argc, argv);
 glutInitDisplayMode ( GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);

 glutInitWindowPosition(100,100);
 glutInitWindowSize(300,300);
 glutCreateWindow ("square");

 glClearColor(0.0, 0.0, 0.0, 0.0);         // black background
 glMatrixMode(GL_PROJECTION);              // setup viewing projection
 glLoadIdentity();                           // start with identity matrix
 glOrtho(0.0, 10.0, 0.0, 10.0, -1.0, 1.0);   // setup a 10x10x2 viewing world

 glutDisplayFunc(display);
 glutMainLoop();

 return 0;
}

Discussion

We begin by looking at the code within main().

glutInit()

Following the initial print statement, the glutInit() call initializes the GLUT library and also processes any command line options related to glut. These command line options are window-system dependent and are thus not discussed further here.
 

glutInitDisplaymode()

Before opening a graphics window, we need to decide on the `depth' of the buffers associated with the window. The following table shows the types of parameters that can be stored on a per-pixel basis: The various GLUT_* options are invoked together by ORing them together, as illustrated in the example code, which creates a graphics window which has only a single copy of all buffers (GLUT_SINGLE), does not have an alpha buffer (GLUT_RGB), and has a depth buffer (GLUT_DEPTH).
 

glutInitWindowPosition(),   glutInitWindowSize(),    glutCreateWindow()

These calls assign an initial position, size, and name to the window and create the window itself.
 

glClearColor(),   glMatrixMode(),   glLoadIdentity(),   glOrtho()

glClearColor() sets the colour to be used when clearing the window, to be discussed shortly. The remaining calls are used
to define the type of camera projection. In this case, an orthographic projection is specified using a call to  glOrtho(x1,x2,y1,y2,z1,z2).
This defines the field of view of the camera, in this case 0<=x<=10, 0<=y<=10, -1<=z<=1.
 

glutDisplayFunc(display),   glutMainLoop()

This provides the name of the function you would like to have called whenever glut thinks the window needs to be redrawn. Thus, when the window is first created and whenever the window is uncovered or moved, the user-defined display() function will be called.  glutDisplayFunc() registers the call-back function, while glutMainLoop() hands execution control over to the glut library.
 

display()

The display()  call-back function clears the screen, sets the current colour to red and draws a square polygon. The last call, glFlush(), forces previously issued OpenGL commands to begin execution.