Basic Syntax

OpenGL was designed to be very easy to learn and use. Thus, its syntax is relatively straightforward, and you can actually be coding within minutes. As an example, here is a very simple OpenGL code snippet:

 glClearColor(0.0, 0.0, 0.0, 0.0); glClear(GL_COLOR_BUFFER_BIT); glLookAt(0, 0, -10, 0, 0, 0, 0, 1, 0); glBegin(GL_TRIANGLES); glColor3f(1,0,0); glVertex3f(-1,0,0); glColor3f(1,1,0); glVertex3f(1,0,0); glColor3f(0,0,1); glVertex3f(0,1,0); glEnd(); 

This relatively simple program draws a triangle on the screen. You can see the output in Figure B.1. But right now we will focus on learning the basic OpenGL syntax, and not so much on the triangle.

Figure B.1. White triangle over black background; "Hello world" for OpenGL.


As you will probably have discovered, all OpenGL calls start with the gl prefix. All words after gl use initial capital letters, and word separations are not separated by the underscore sign. Symbolic constants, on the contrary, use all caps and underscore each space. This makes command recognition simpler, for example:

glRenderMode command

GL_TRIANGLES symbolic constant

OpenGL explicitly indicates the parameter types in the function name. So, a call like this:


receives three parameters, each one of float type. Different calls receive different parameter sets. Moreover, some calls have variants depending on the parameters you want to pass. For example, to specify colors, you could use:

  • glColor3d Specifies colors as 3 doubles, RGB

  • glColor3f Specifies colors as 3 floats, RGB

  • glColor3ub Specifies colors as 3 unsigned bytes, RGB

  • glColor4d Specifies colors as 4 doubles, RGBA

  • glColor4f Specifies colors as 4 floats, RGBA

  • glColor4ub Specifies colors as 4 unsigned bytes, RGBA

This makes coding a bit more intuitive. Because OpenGL is available for many programming languages (C/C++, Pascal, Basic), it has its own generic types, so you can hide the specific types each language uses. For the C/C++ version of OpenGL, Table B.1 lists the parameter types.

Table B.1. Data Types and Suffixes for OpenGL Calls

OpenGL Type

Data Type

C Type



8-bit integer

signed char



16-bit integer



GLint, Glsizei

32-bit integer



GLfloat, GLclampf

32-bit floating-point



GLdouble, GLclampd

64-bit floating-point



GLubyte, GLboolean

8-bit unsigned integer

unsigned char



16-bit unsigned integer

unsigned short


GLuint, GLenum,

32-bit unsigned integer

unsigned long

ui GLbitfield

Additionally, most calls to OpenGL accept vectors to be passed as parameters. For example, the following two calls are completely equivalent:

 glVertex3f(1.0, 0.0, 0.0); float vertex_array[] = {1.0, 0.0, 0.0}; glVertex3fv(vertex_array); 

Notice how the v suffix is used to indicate a vector is going to be passed. The numeric value then represents not the number of parameters, but the length of the vector.

So now we can review the preceding code and figure out what each line does. The first two lines specify the background color and clear the frame buffer to that value. Next, we define a camera with the gluLookAt call. Then, we declare a geometry object (a triangle), specifying its three vertices and a different color for each one. Let's now explore the rendering capabilities of OpenGL, to better understand how geometry is sent to the graphics hardware.

Core Techniques and Algorithms in Game Programming2003
Core Techniques and Algorithms in Game Programming2003
Year: 2004
Pages: 261 © 2008-2017.
If you may any questions please contact us: