Background
The OpenGL spec does not specify any API in order to createand manipulate windows. Modern windowing systems that support OpenGLinclude a sub-system that provides the binding between an OpenGLcontext and the windowing system. In the X Window system that interfaceis called GLX. Microsoft provides WGL (pronounced: Wiggle) for Windowsand MacOS has CGL. Working directly with these interfaces in order tocreate a window in which to display graphics is usually grunt workwhich is why we use a high level library that abstracts away the finedetails. The library we use here is called the 'OpenGL utilitylibrary', or GLUT. It provides a simplified API for window managementas well as event handling, IO control and a few other services. Inaddition, GLUT is cross platform which makes portability easier.Alternatives to GLUT include SDL and GLFW.
Source walkthru
glutInit(&argc, argv);
This call initializes GLUT. The parameters can be provideddirectly from the command line and include useful options such as'-sync' and '-gldebug' which disable the asynchronous nature of X andautomatically checks for GL errors and displays them (respectively).
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
Here we configure some GLUT options. GLUT_DOUBLE enables doublebuffering (drawing to a background buffer while another buffer isdisplayed) and the color buffer where most rendering ends up (i.e. thescreen). We will usually want these two as well as other options whichwe will see later.
glutInitWindowSize(1024, 768);
glutInitWindowPosition(100, 100);
glutCreateWindow("Tutorial 01");
These calls specify the window parameters and create it. You also havethe option to specify the window title.
glutDisplayFunc(RenderSceneCB);
Since we are working in a windowing system most of the interaction withthe running program occurs via event callback functions. GLUT takescare of interacting with the underlying windowing system and providesus with a few callback options. Here we use just one - a "main"callback to do all the rendering of one frame. This function iscontinuously called by GLUT internal loop.
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
This is our first encounter with the concept of state in OpenGL. Theidea behind state is that rendering is such a complex task that itcannot be treated as a function call that receives a few parameters(and correctly designed functions never receive a lot of parameters).You need to specify shaders, buffers and various flags that effect howrendering will take place. In addition, you would often want to keepthe same piece of configuration across several rendering operations(e.g. if you never disable the depth test then there is no point inspecifying it for every render call). That is why most of theconfiguration of rendering operations is done by setting flags andvalues in the OpenGL state machine and the rendering calls themselvesare usually limited to the few parameters that revolve around thenumber of vertices to draw and their starting offset. After calling astate changing function that particular configuration remains intactuntil the next call to the same function with a different value. Thecall above sets the color that will be used when clearing theframebuffer (described later). The color has four channels (RGBA) andit is specified as a normalized value between 0.0 and 1.0.
glutMainLoop();
This call passes control to GLUT which now begins its own internalloop. In this loop it listens to events from the windowing system andpasses them via the callbacks that we configured. In our case GLUT willonly call the function we registered as a display callback(RenderSceneCB) to give us a chace to render the frame.
glClear(GL_COLOR_BUFFER_BIT);
glutSwapBuffers();
The only thing we do in our render function is to clear the framebuffer(using the color specified above - try changing it). The second calltells GLUT to swap the roles of the backbuffer and the frontbuffer. Inthe next round through the render callback we will render into thecurrent frames front buffer and the current backbuffer will bedisplayed.