Before we delve into the recipes, let's have a quick overview of some OpenGL functions that we will be using in this chapter. The following are some of the most frequently used OpenGL functions:
Function | Description |
glutInit | Used to initialize GLUT. |
glutCreateWindow | Used for creating a top-level window. You can supply the window name as a label while creating the window. |
glutInitWindowSize | Used to define the window size. The width and height of the window are specified in pixels while defining the window size. |
void glutInitWindowPosition | Used to set the initial window position. The window's x and y locations are specified in terms of pixels. |
glutDisplayFunc | Used to specify the callback function to be executed to display graphics in the current window. For redisplaying the content in the window too, the specified callback function is executed. |
glutMainLoop | This is an entry point for the GLUT event processing loop. |
glClearColor | Used to specify clear values for the color buffers. You need to specify the red, green, blue, and alpha values used when the color buffers are cleared. The initial values are all 0. |
glClear | Used to clear the buffers to preset values. Certain masks can be used to specify the buffers to be cleared. The following are three masks that can be used. |
GL_COLOR_BUFFER_BIT | This mask represents the buffers that are currently being used for applying colors. |
GL_DEPTH_BUFFER_BIT | This mask represents the depth buffer. |
GL_STENCIL_BUFFER_BIT | This mask represents the stencil buffer. |
glBegin | Used for grouping statements that lead to a specific shape. You can create different shapes such as points, lines, triangles, rectangles, and more, by grouping the required vertices within this grouping statement. The shape that you want to create can be specified by specifying any of the modes: GL_POINTS, GL_LINES, GL_LINE_STRIP, GL_LINE_LOOP, GL_TRIANGLES, GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN, GL_QUADS, GL_QUAD_STRIP, and GL_POLYGON. |
glEnd | Used to end the group of statements. |
glColor3f | Used to set the current color for drawing graphics. The values for the red, green, and blue colors (in this strict sequence) can be specified to set the color. The values of these colors can be between 0 and 1, where 0 is the lowest intensity and 1 is the highest intensity of the color. |
glVertex | Used to specify coordinates for the point, line, and polygon vertices. This function has to be enclosed between a glBegin/glEnd pair. There might be a suffix of 2, 3, or 4 after glVertex, depending on the number of coordinates required for defining the vertex. For example, if two coordinates, x and y, are required to specify the vertex, then a value of 2 will be added as a suffix to glVertex making it glVertex2. Similarly, 3 and 4 can be suffixed if the coordinates required for specifying the vertex are 3 and 4, respectively. Also, one more suffix can be added, such as s, i, f, or d if the vertex coordinates are of the short, int, float or double data types respectively. For example, glVertex2f() can be used to specify the vertex with x and y coordinates, and the coordinate values will be of the float data type. |
glLineWidth | Used to specify the width of the line to be drawn. The width of the line can be specified in terms of pixels. The default width is 1. |
glPointSize | Used to specify the diameter of rasterized points. The default diameter is 1. |
glFlush | The commands sometimes get buffered depending on the resource utilization and networking situation. The glFlush function empties all the buffers and ensures that the commands are executed as early as possible. |
glutSwapBuffers | This function is used for swapping the front buffer with the back buffer. The front buffer displays the image or frame on the screen and the back buffer is where the image (or frame) hasn't yet been rendered. Once the image or frame is rendered in the back buffer, then this function swaps the front and back buffers, displaying the image that is now ready in the back buffer. |
glutReshapeFunc | Used for specifying the reshape callback function for the current window. The function is invoked automatically during these situations: when a window is reshaped, before a window's first display callback, and after a window is created. |
glViewport | Used to set the viewport, that is, the vicinity of the window in which we want the rendered image to appear. Four arguments are passed to the function. The first two arguments represent the lower-left corner of the viewport rectangle in terms of pixels. The third and fourth arguments represent the width and height of the viewport. The width and height of the viewport are usually set to be equal to the dimensions of the window or less in size. |
glMatrixMode | Used to specify which matrix is the current matrix. The vertices are rendered on the basis of the current state of the matrix, so a matrix must be chosen so that it serves our needs. The following are the two main options. |
GL_MODELVIEW | This is the default matrix option. This option is used when the user wants to perform translation, rotation, or similar operations. |
GL_PROJECTION | This option is used when the user wants to perform parallel projection, perspective projection, and so on. |
glLoadIdentity | Used to replace the current matrix with the identity matrix. |
gluOrtho2D | Used to set up a two-dimensional orthographic viewing region. Four arguments are passed to this function. The first two coordinates represent the left and right vertical clipping planes. The last two specify the coordinates for the bottom and top horizontal clipping planes. |
glutMouseFunc | Used to set the mouse callback function for the current window. That is, whenever the mouse button is pressed or released, each action invokes the mouse callback function. In the callback function, the following three arguments are automatically passed. |
button | It represents any of the three buttons, GLUT_LEFT_BUTTON, GLUT_MIDDLE_BUTTON, or GLUT_RIGHT_BUTTON, depending on which of the mouse buttons is pressed. |
state | The state can be either GLUT_UP or GLUT_DOWN depending on whether the callback was invoked because of the mouse release or mouse press, respectively. |
x and y | Represents the window's relative coordinates when the mouse button state changes. |
glutIdleFunc | Used to set the global idle callback, mainly used for performing background processing tasks. The idle callback is continuously called even if no event is occurring. The NULL parameter is sent to this function to disable the generation of the idle callback. |
You need to initialize the X Window System (X11) for working with graphics. X11 provides a GUI environment, that is, it enables the displaying of windows and graphics, and provides an environment for interacting with a mouse and keyboard. The command used for starting the X11 is the xinit command.