Display Window Management Using GLUT
We can consider a simplified example, minimal number of operations for displaying a picture.
Step 1: initialization of GLUT
- We are using the OpenGL Utility Toolkit, our first step is to initialize GLUT.
- This initialization function could also process any command line arguments, but we will not need to use these parameters for our first example programs.
- We perform the GLUT initialization with the statement
glutInit (&argc, argv);
Step 2: title
- We can state that a display window is to be created on the screen with a given caption for the title bar. This is accomplished with the function
glutCreateWindow ("An Example OpenGL Program");
- where the single argument for this function can be any character string that we want to use for the display-window title.
Step 3: Specification of the display window
- Then we need to specify what the display window is to contain.
- For this, we create a picture using OpenGL functions and pass the picture definition to the GLUT routine glutDisplayFunc, which assigns our picture to the display window.
- Example: suppose we have the OpenGL code for describing a line segment in a procedure called lineSegment.
- Then the following function call passes the line-segment description to the display window:
glutDisplayFunc (lineSegment);
Step 4: one more GLUT function
- But the display window is not yet on the screen.
- We need one more GLUT function to complete the window-processing operations.
- After execution of the following statement, all display windows that we have created, including their graphic content, are now activated:
glutMainLoop ( );
- This function must be the last one in our program. It displays the initial graphics and puts the program into an infinite loop that checks for input from devices such as a mouse or keyboard.
Step 5: these parameters using additional GLUT functions
Although the display window that we created will be in some default location and size,we can set these parameters using additional GLUT functions.
GLUT Function 1:
- We use the glutInitWindowPosition function to give an initial location for the upper left corner of the display window.
- This position is specified in integer screen coordinates, whose origin is at the upper-left corner of the screen.
GLUT Function 2:
After the display window is on the screen, we can reposition and resize it.
GLUT Function 3:
- We can also set a number of other options for the display window, such as buffering and a choice of color modes, with the glutInitDisplayMode function.
- Arguments for this routine are assigned symbolic GLUT constants.
- Example: the following command specifies that a single refresh buffer is to be used for the display window and that we want to use the color mode which uses red, green, and blue (RGB) components to select color values:
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
- The values of the constants passed to this function are combined using a logical or operation.
- Actually, single buffering and RGB color mode are the default options.
- But we will use the function now as a reminder that these are the options that are set for our display.
- Later, we discuss color modes in more detail, as well as other display options, such as double buffering for animation applications and selecting parameters for viewing three dimensional scenes.