Shaw's principle: Build a system a fool can use and only a fool will use it. Concept9: The OpenGL Program Structure ====================================== Finally we reexamine all the components of an OpenGL progam. The main() function =================== Five routines perform tasks necessary to initialize a window. o glutInit(int *argc, char **argv) initializes and processes any commandline arguements. glutInit() should be called before any other glut routine. o glutInitDisplayMode(unsigned int mode) specifies whether to use an RGBA or color-index color model. We can also specify whether we want a single or double buffered window (GLUT_SINGLE or GLUT_DOUBLE) whether want the depth buffer. We shall utilize these in more detail in later chapters. o glutInitWindowPosition(int x, int y) specifies the screen location for the upper-left corner of your window. o glutInitWindowSize(int width, int size) specifies the size, in pixels, of the window. o int glutCreateWindow(char *string) creates a window with the string being used as the title for the window. o Calls to init(), glutDisplayFunc(display) follow next. o glutMainLoop() has the following effect. All the windows that have been created are now shown, rendering to those windows is now effective and event processing begins. Once this loop is entered, it is never exited. The init() function =================== Initializing operations go in this function. Typically operations that need to be performed only once such as setting the background color and coordinate system are put here. o glClearColor() establishes what color the window will be cleared to. o The next few lines of code set up a projection matrix for viewing the object. We shall look at these issues in greater detail later in this tutorial. The display() function ====================== Operations to render ( and possibly re-render ) the scene are in the display() procedure. o glClear(GL_COLOR_BUFFER_BIT) clears the color buffer and hence the window to the color values as per calls to glClearColor() in the init() function. o glBegin() and glEnd() specify the object to be drawn in this case a polygon. o glColor3f(float, float, float) specifies the object color o glVertex3f(float, float, float) specifies the coordinates of the polygon ( in this example a triangle ). o glFlush() ensures that the drawing commands are actually executed rather than storing, or waiting for additional OpenGL commands. Libraries ========= At the top of the program you should see calls to standard C libraries and OpenGL libraries. o You can use all the standard C libraries provided with your C package o For these sessions we will use the following three OpenGL related libraries #include #include #include The gl.h header file is required for any OpenGL application. Also almost all OpenGL applications will use GLU the openGL utility library (glu.h) OpenGL is designed to be independent of any window system or operating system. Hence the name OpenGL!!. Hence in this tutorial we shall make use of the GLUT (glut.h) library to simplify operations such as opening windows, detecting etc. Notes : o glut.h includes gl.h and glu.h automatically, so you need type in only #include at the top of your programs. o OpenGL constants begin with GL_ use all capital letters, and use underscores to separate words. Recollect GL_COLOR_BUFFER_BIT from previous concepts. o Also some commands have a number and a letter appended to the end of the command example : '3f' in glVertex3f(). Here the '3' in the suffix indicates that three arguements are given. The 'f' in the suffix indicates that the arguements are floating point numbers. o All the drawing commands go in between glBegin() and glEnd()