CPEN 461/661, Spring 1997 |
OpenGL Tutorial |
As a geometric primitive is drawn, each of its vertices is affected by the current OpenGL "state" variables. These state variables specify information such as line width, line stipple pattern, color, shading method, fog, polygon culling, etc . . .
Some state variables refer to OpenGL capabilities that are either "off" (set to the value GL_FALSE) or "on" (set to the value GL_TRUE). Other state variables refer to a certain mode (set to a value of type GLenum) chosen from a fixed set of modes. Lastly, there are state variables that are set to certain values (GLfloat, GLint, etc . . .).
Each state variable has a default value. The values of the state variables, whether set by default or by the programmer, remain in effect until changed.
The remainder of this section will cover some of the most commonly used state variables. Others will be covered in later sections. It is not within the scope of this tutorial to cover all of the state variables, so refer to other sources for additional information.
State variables that refer to OpenGL capabilities can be either enabled or disabled with the commands glEnable(GLenum capability) and glDisable(GLenum capability) respectively, where capability specifies a symbolic constant indicating an OpenGL capability.
Their current value can be queried using the command glIsEnabled(GLenum capability) which returns either GL_TRUE or GL_FALSE.
Some OpenGL capabilities include:
GL_POINT_SMOOTH If enabled, draw points with proper filtering. Otherwise, draw aliased points. GL_LINE_SMOOTH If enabled, draw lines with correct filtering. Otherwise, draw aliased lines. GL_LINE_STIPPLE If enabled, use the current line stipple pattern when drawing lines.
Mode state variables require commands specific to the state variable being accessed in order to change its value. One example of this is the command to set the shading mode state variable, GL_SHADE_MODEL.
A line or filled polygon primitive can be drawn with a single color (flat shading) or with many different colors (smooth shading, also called Gouraud Shading). The desired shading technique can be specified with the command glShadeModel(GLenum mode) where mode is either GL_SMOOTH for smooth shading or GL_FLAT for flat shading, the default.
Value state variables require commands specific to the state variable being accessed in order to change it value.
At any point, the programmer can query the system for any variable's current value. Typically, one of the four following commands is used to do this depending upon the desired data type of the answer: glGetBooleanv(), glGetDoublev(), glGetFloatv(), or glGetIntegerv().
The most commonly used variant of the command to set RGBA colors is glColor3f(GLfloat red, GLfloat green, GLfloat blue) where red, green, and blue are values between 0.0 and 1.0, inclusive. The value 0.0 corresponds to the minimum amount of that color while 1.0 corresponds to the maximum amount of that color.
Commonly Used RGB ColorsglColor3f(0.0, 0.0, 0.0); Black glColor3f(1.0, 0.0, 0.0); Red glColor3f(0.0, 1.0, 0.0); Green glColor3f(0.0, 0.0, 1.0); Blue glColor3f(1.0, 1.0, 0.0); Yellow glColor3f(0.0, 1.0, 1.0); Cyan glColor3f(1.0, 0.0, 1.0); Magenta glColor3f(1.0, 1.0, 1.0); White
The RGB values for each pixel are stored in the color buffer. The tk command tkInitDisplayMode(TK_RGB | any other modes . . .) must be called to specify use of the color buffer. The color buffer should be cleared each time before drawing a scene using the command glClear(GL_COLOR_BUFFER_BIT).
To control the size of a rendered point use the command glPointSize(GLfloat size) where size is the desired width in pixels for rendered points. size must be greater than 0.0 and by default is 1.0.
To control the width of lines use the commandglLineWidth(GLfloat width) where width is the desired width in pixels for rendered lines. width must be greater than 0.0 and by default is 1.0.
To make stippled (dotted or dashed) lines, the stipple pattern must be defined using the command glLineStipple(GLint factor, GLushort pattern) where the pattern argument is a 16-bit series of 0s and 1s, and it's repeated as necessary to stipple a given line. A 1 indicates that drawing occurs, and 0 that it does not, on a pixel by pixel basis. The pattern can be stretched out by using factor, which multiplies each sub-series of consecutive 1s and 0s. factor is any value between 0 and 255, inclusive. The capability state variable GL_LINE_STIPPLE must be enabled by the call glEnable(GL_LINE_STIPPLE) for stippling to occur.