Open GL Super Bible

Previous Table of Contents Next


OpenGL Data Types

To make it easier to port OpenGL code from one platform to another, OpenGL defines its own data types. These data types map to normal C data types that you can use instead, if desired. The various compilers and environments, however, have their own rules for the size and memory layout of various C variables. By using the OpenGL defined variable types, you can insulate your code from these types of changes.

Table 3-2 lists the OpenGL data types, their corresponding C data types under the 32-bit Windows environments (Win32), and the appropriate suffix for literals. In this book we will use the suffixes for all literal values. You will see later that these suffixes are also used in many OpenGL function names.

Table 3-2 OpenGL variable types and corresponding C data types

OpenGL Data Type Internal Representation Defined as C Type C Literal Suffix

GLbyte 8-bit integer Signed char b
GLshort 16-bit integer Short s
GLint, GLsizei 32-bit integer Long I
GLfloat, GLclampf 32-bit floating point Float f
GLdouble, GLclampd 64-bit floating point Double d
GLubyte, GLboolean 8-bit unsigned integer Unsigned char ub
GLushort 16-bit unsigned integer Unsigned short us
GLuint, GLenum, GLbitfield 32-bit unsigned integer Unsigned long ui

All data types start with a GL to denote OpenGL. Most are followed by their corresponding C data types (byte, short, int, float, etc.). Some have a u first to denote an unsigned data type, such as ubyte to denote an unsigned byte. For some uses a more descriptive name is given, such as size to denote a value of length or depth. For example, GLsizei is an OpenGL variable denoting a size parameter that is represented by an integer. The clamp is used for color composition and stands for color amplitude. This data type is found with both f and d suffixes to denote float and double data types. The GLboolean variables are used to indicate True and False conditions, GLenum for enumerated variables, and GLbitfield for variables that contain binary bit fields.

Pointers and arrays are not give any special consideration. An array of ten GLshort variables would simply be declared as

GLshort shorts[10];

and an array of ten pointers to GLdouble variables would be declared with

GLdouble *doubles[10];

Some other pointer object types are used for NURBS and Quadrics. They take more explanation and will be covered in later chapters.

Function Naming Conventions

OpenGL functions all follow a naming convention that tells you which library the function is from, and often how many and what type of arguments the function takes. All functions have a root that represents the function’s corresponding OpenGL command. For example, the glColor3f() function has the root Color. The gl prefix represents the gl library (see Table 3-1), and the 3f suffix means the function takes three floating point arguments. All OpenGL functions take the following format:

<Library prefix><Root command><Optional argument count><Optional argument type>

Figure 3-1 illustrates the parts of an OpenGL function. This sample function with the suffix 3f takes three floating point arguments. Other variations take three integers (glColor3i()), three doubles (glColor3d()), and so forth. This convention of adding the number and type of arguments (see Table 3-1) to the end of OpenGL functions makes it very easy to remember the argument list without having to look it up. Some versions of glColor take four arguments to specify an alpha component, as well.


Figure 3-1  Dissected OpenGL Function

In the reference sections of this book, these “families” of functions are listed by their library prefix and root. Thus all the variations of glColor (glColor3f, glColor4f, glColor3i, etc.) will be listed under a single entry—glColor.


Clean Code
Many C/C++ compilers for Windows assume that any floating-point literal value is of type double unless explicitly told otherwise via the suffix mechanism. When using literals for floating point arguments, if you don’t specify that these arguments are of type float instead of double, the compiler will issue a warning while compiling because it detects that you are passing a double to a function defined to accept only floats, resulting in a possible loss of precision. As our OpenGL programs grow, these warnings will quickly number in the hundreds and will make it difficult to find any real syntax errors. You can turn these warnings off using the appropriate compiler options—but we advise against this. It’s better to write clean, portable code the first time. So clean up those warning messages by cleaning up the code (in this case, by explicitly using the float type)—not by disabling potentially useful warnings.

Additionally, you may be tempted to use the functions that accept double-precision floating point arguments, rather than go to all the bother of specifying your literals as floats. However, OpenGL uses floats internally, and using anything other than the single-precision floating point functions will add a performance bottleneck, as the values are converted to floats anyway before being processed by OpenGL.



Previous Table of Contents Next