OpenGL Command Syntax

As you might have observed from the simple program in the preceding section, OpenGL commands use the prefix gl and initial capital letters for each word making up the command name (recall glClearColor(), for example). Similarly, OpenGL defined constants begin with GL_, use all capital letters, and use underscores to separate words (for example, GL_COLOR_BUFFER_BIT).

You might also have noticed some seemingly extraneous letters appended to some command names (for example, the 3f in glColor3fO and glVertex3f()). It’s true that the Color part of the command name glColor3f() is enough to define the command as one that sets the current color. However, more than one such command has been defined so that you can use different types of arguments. In particular, the 3 part of the suffix indicates that three arguments are given; another version of the Color command takes four arguments. The f part of the suffix indicates that the arguments are floatingpoint numbers. Having different formats allows OpenGL to accept the user’s data in his or her own data format.

Some OpenGL commands accept as many as eight different data types for their arguments. The letters used as suffixes to specify these data types for ISO C implementations of OpenGL are shown in Table 1-1, along with the corresponding OpenGL type definitions. The particular implementation of OpenGL that you’re using might not follow this scheme exactly; an implementation in C++ or Ada, for example, wouldn’t need to.


Suffix

Data Type

Typical Corresponding C-Language Type

OpenGL Type Definition

b

8-bit integer

signed char

GLbyte

s

16-bit integer

short

GLshort

i

32-bit integer

int or long

GLint, GLsizei

f

32-bit floating-point

float

GLfloat, GLclampf

d

64-bit floating-point

double

GLdouble, GLclampd

ub

8-bit unsigned integer

unsigned char

GLubyte, GLboolean

us

16-bit unsigned integer

unsigned short

GLushort

ui

32-bit unsigned integer

unsigned int or unsigned long

GLuint, GLenum, GLbitfield

Table 1 -1    Command Suffixes and Argument Data Types

Thus, the two commands

tmp5324-3_thumb

are equivalent, except that the first specifies the vertex’s coordinates as 32-bit integers, and the second specifies them as single-precision floatingpoint numbers.

Note: Implementations of OpenGL have leeway in selecting which C data type to use to represent OpenGL data types. If you resolutely use the OpenGL defined data types throughout your application, you will avoid mismatched types when porting your code between different implementations.

Some OpenGL commands can take a final letter v, which indicates that the command takes a pointer to a vector (or array) of values, rather than a series of individual arguments. Many commands have both vector and nonvector versions, but some commands accept only individual arguments and others require that at least some of the arguments be specified as a vector. The following lines show how you might use a vector and a nonvector version of the command that sets the current color:

tmp5324-4_thumb

Finally, OpenGL defines the type of GLvoid. This is most often used for OpenGL commands that accept pointers to arrays of values.

In the rest of this guide (except in actual code examples), OpenGL commands are referred to by their base names only, and an asterisk is included to indicate that there may be more to the command name. For example, glColor*() stands for all variations of the command you use to set the current color. If we want to make a specific point about one version of a particular command, we include the suffix necessary to define that version. For example, glVertex*v() refers to all the vector versions of the command you use to specify vertices.

Next post:

Previous post: