Quick Reference

This page gives a quick summary of OpenGL syntax. Just click on the appropriate topic for a short example of how to do it. (No wading through detailed documentation.)

Topics:

Alpha Blending

[From "Lesson 12: Alpha Blending"]

void initRendering() {
    //...
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_BLEND); //Enable alpha blending
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); //Set the blend function
    //...
}

void drawScene() {
    //...
    
    //Draw non-transparent objects
    //...
    
    glColor4f(1.0f, 0.0f, 0.0f, 0.6f); //Red, 60% opaque
    
    //Draw faces from back to front
    
    //or
    
    //For convex surfaces,
    glEnable(GL_CULL_FACE); //Enable backface culling
    glCullFace(GL_FRONT);   //Cull front faces
    drawConvexObject();     //Draw back faces of convex surface
    glCullFace(GL_BACK);    //Cull back faces
    drawConvexObject();     //Draw front faces of convex surface
    
    //...
}

Backface Culling

[From "Lesson 16: Backface Culling"]

void initRendering() {
    //...
    glEnable(GL_CULL_FACE);
    //Subsequently specify polygons' vertices in counterclockwise order
    //...
}

Color

[From "Lesson 3: Color"]

void initRendering() {
    //...
    glEnable(GL_COLOR_MATERIAL); //Enable color
    glClearColor(0.7f, 0.9f, 1.0f, 1.0f); //Set the background color
    //...
}

void drawScene() {
    //...
    
    //Solid color
    glColor3f(1.0f, 0.0f, 0.0f);
    glBegin(GL_TRIANGLES);
    glVertex3f(-1.0f, -1.0f, 0.0f);
    glVertex3f(0.0f, 1.0f, 0.0f);
    glVertex3f(1.0f, -1.0f, 0.0f);
    glEnd();
    
    //Color blending
    glBegin(GL_TRIANGLES);
    glColor3f(1.0f, 0.0f, 0.0f);
    glVertex3f(-1.0f, -1.0f, -2.0f);
    glColor3f(0.0f, 1.0f, 0.0f);
    glVertex3f(0.0f, 1.0f, -2.0f);
    glColor3f(0.0f, 0.0f, 1.0f);
    glVertex3f(1.0f, -1.0f, -2.0f);
    glEnd();
    
    //...
}

Display Lists

[From "Lesson 17: Display Lists"]

GLuint displayListId;

//Sets up the display lists, so that they can be called later
void setupDisplayLists() {
    //Make room for two display lists: displayListId and displayListId + 1
    displayListId = glGenLists(2);
    
    //Display list for first object
    glNewList(displayListId, GL_COMPILE); //Begin display list
    drawObject1(); //Add object 1 to the display list
    glEndList(); //End display list
    
    //Display list for second object
    glNewList(displayListId + 1, GL_COMPILE_AND_EXECUTE); //Begin display list
    drawObject2(); //Add object 2 to the display list, and draw object 2
    glEndList(); //End display list
}

void drawScene() {
    //...
    glCallList(displayListId);     //Draw the first object
    glCallList(displayListId + 1); //Draw the second object
    //...
}

Fog

[From "Lesson 15: Fog"]

void initRendering() {
    //...
    glEnable(GL_FOG);
    //...
}

void drawScene() {
    //...
    GLfloat fogColor[] = {0.5f, 0.5f, 0.5f, 1}; //Gray fog
    glFogfv(GL_FOG_COLOR, fogColor);
    
    //Linear fog
    glFogi(GL_FOG_MODE, GL_LINEAR);
    glFogf(GL_FOG_START, 10.0f);
    glFogf(GL_FOG_END, 20.0f);
    //...
    
    //Exponential fog (1 - e^(-0.04d))
    glFogi(GL_FOG_MODE, GL_EXP);
    glFogf(GL_FOG_DENSITY, 0.04f);
    //...
    
    //Exponential-squared fog (1 - e^(-(0.05d)^2))
    glFogi(GL_FOG_MODE, GL_EXP2);
    glFogf(GL_FOG_DENSITY, 0.05f);
    //...
}

Lighting

[From "Lesson 4: Lighting"]

void initRendering() {
    //...
    glEnable(GL_LIGHTING); //Enable lighting
    glEnable(GL_LIGHT0); //Enable light #0
    glEnable(GL_LIGHT1); //Enable light #1
    glEnable(GL_NORMALIZE); //Have OpenGL automatically normalize our normals
    glShadeModel(GL_SMOOTH); //Enable smooth shading
    //...
}

void drawScene() {
    //...
    
    //Add ambient light
    GLfloat ambientColor[] = {0.2f, 0.2f, 0.2f, 1.0f}; //Color (0.2, 0.2, 0.2)
    glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientColor);
    
    //Add positioned light
    GLfloat lightColor0[] = {0.5f, 0.5f, 0.5f, 1.0f}; //Color (0.5, 0.5, 0.5)
    GLfloat lightPos0[] = {4.0f, 0.0f, 8.0f, 1.0f}; //Positioned at (4, 0, 8)
    glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor0);
    glLightfv(GL_LIGHT0, GL_POSITION, lightPos0);
    
    //Add directed light
    GLfloat lightColor1[] = {0.5f, 0.2f, 0.2f, 1.0f}; //Color (0.5, 0.2, 0.2)
    //Coming from the direction (-1, 0.5, 0.5)
    GLfloat lightPos1[] = {-1.0f, 0.5f, 0.5f, 0.0f};
    glLightfv(GL_LIGHT1, GL_DIFFUSE, lightColor1);
    glLightfv(GL_LIGHT1, GL_POSITION, lightPos1);
    
    //Flatly shaded triangle
    glBegin(GL_TRIANGLES);
    glNormal3f(0.0f, 0.0f, 1.0f); //Specify the normal vector (magnitude 1)
    glVertex3f(-1.0f, -1.0f, 0.0f);
    glVertex3f(0.0f, 1.0f, 0.0f);
    glVertex3f(1.0f, -1.0f, 0.0f);
    glEnd();
    
    //Smoothly shaded triangle
    glBegin(GL_TRIANGLES);
    glNormal3f(-1.0f, -1.0f, 1.0f); //Specify the first normal
    glVertex3f(-1.0f, -1.0f, 0.0f);
    glNormal3f(0.0f, 1.0f, 1.0f); //Specify the second normal
    glVertex3f(0.0f, 1.0f, 0.0f);
    glNormal3f(1.0f, -1.0f, 1.0f); //Specify the third normal
    glVertex3f(1.0f, -1.0f, 0.0f);
    glEnd();
    
    //...
}

Materials

[From "Lesson 20: Materials"]

void drawScene() {
    //...
    
    //Set up the lighting
    
    //Ambient lighting
    GLfloat ambientLight[] = {0.2f, 0.2f, 0.2f, 1.0f};
    glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientLight);
    
    GLfloat lightColor[] = {0.6f, 0.6f, 0.6f, 1.0f};
    GLfloat lightPos[] = {1.5f * RADIUS, 2 * RADIUS, 1.5 * RADIUS, 1.0f};
    //Diffuse (non-shiny) light component
    glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor);
    //Specular (shiny) light component
    glLightfv(GL_LIGHT0, GL_SPECULAR, lightColor);
    glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
    
    
    
    //Set up the material
    
    //The color of the object
    GLfloat materialColor[] = {0.2f, 0.2f, 1.0f, 1.0f};
    //The specular (shiny) component of the material
    GLfloat materialSpecular[] = {0.8f, 0.8f, 0.8f, 1.0f};
    //The color emitted by the material
    GLfloat materialEmission[] = {0, 0, 0, 1.0f};
    
    glDisable(GL_COLOR_MATERIAL); //Required for the glMaterial calls to work
    glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, materialColor);
    glMaterialfv(GL_FRONT, GL_SPECULAR, materialSpecular);
    glMaterialfv(GL_FRONT, GL_EMISSION, materialEmission);
    //The shininess parameter.  A higher number indicates a more concentrated
    //shiny area, while a lower number indicates a larger shiny area.  The
    //shininess must be between 0 and 128.
    glMaterialf(GL_FRONT, GL_SHININESS, 15.0f);
    
    
    
    //Draw the object with a material
    
    drawObject();
}

Mipmapping

[From "Lesson 19: Mipmapping"]

#include "imageloader.h"

//Makes the image into a mipmapped texture, and returns the id of the texture
GLuint loadMipmappedTexture(Image *image) {
    GLuint textureId;
    glGenTextures(1, &textureId);
    glBindTexture(GL_TEXTURE_2D, textureId);
    gluBuild2DMipmaps(GL_TEXTURE_2D,
                      GL_RGB,
                      image->width, image->height,
                      GL_RGB,
                      GL_UNSIGNED_BYTE,
                      image->pixels);
    return textureId;
}

void initRendering() {
    //...
    Image *image = loadBMP("vtr.bmp");
    _textureId = loadMipmappedTexture(image);
    delete image;
    //...
}

void drawScene() {
    //...
    
    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, _textureId);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D,
                    GL_TEXTURE_MIN_FILTER,
                    GL_LINEAR_MIPMAP_LINEAR);
    /* Also available: GL_NEAREST_MIPMAP_LINEAR, GL_LINEAR_MIPMAP_NEAREST, and
     * GL_LINEAR_MIPMAP_LINEAR.  The first linear / nearest indicates blocky /
     * blurry; the second linear / nearest indicates one / two texture image
     * sizes.
     */
    
    glColor3f(1.0f, 1.0f, 1.0f); //Don't use special coloring
    
    glBegin(GL_TRIANGLES);
    glTexCoord2f(0.0f, 0.0f);
    glVertex3f(-1.0f, -1.0f, 0.0f);
    glTexCoord2f(1.0f, 2.0f);
    glVertex3f(0.0f, 1.0f, 0.0f);
    glTexCoord2f(2.0f, 0.0f);
    glVertex3f(1.0f, -1.0f, 0.0f);
    glEnd();
    
    glDisable(GL_TEXTURE_2D); //Switch back to using colors instead of textures
    
    //...
}

Reflections

[From "Lesson 14: Drawing Reflections"]

void initRendering() {
    //...
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); //Set the blend function
    //...
}

void drawScene() {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
    //...
    
    drawObjects(); //Draw all of the objects that will be reflected
    
    glEnable(GL_STENCIL_TEST); //Enable using the stencil buffer
    glColorMask(0, 0, 0, 0); //Disable drawing colors to the screen
    glDisable(GL_DEPTH_TEST); //Disable depth testing
    glStencilFunc(GL_ALWAYS, 1, 1); //Make the stencil test always pass
    //Make pixels in the stencil buffer be set to 1 when the stencil test passes
    glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
    //Set all of the pixels covered by the surface to be 1 in the stencil buffer
    drawReflectiveSurface();
    
    glColorMask(1, 1, 1, 1); //Enable drawing colors to the screen
    glEnable(GL_DEPTH_TEST); //Enable depth testing
    //Make the stencil test pass only when the pixel is 1 in the stencil buffer
    glStencilFunc(GL_EQUAL, 1, 1);
    glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP); //Make the stencil buffer not change
    
    glPushMatrix();
    reflect(); //Use OpenGL transformations to reflect across the surface
    drawObjects(); //Draw reflected copies of the objects
    glPopMatrix();
    
    glDisable(GL_STENCIL_TEST); //Disable using the stencil buffer
    
    //Blend the reflective surface onto the screen
    glEnable(GL_BLEND);
    glColor4f(1, 1, 1, 0.7f); //Give the reflective surface an opacity of 70%
    drawReflectiveSurface();
    glDisable(GL_BLEND);
}

int main(int argc, char** argv) {
    //...
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH |
                        GLUT_STENCIL); //Enable the stencil buffer in GLUT
    //...
}

Spheres

void drawScene() {
    //...
    glutSolidSphere(0.5f,   //Radius
                    15, 8); //Indicate the # of polygons used to draw the sphere
    //...
}

Text

[From "Lesson 8: Drawing Text"]

Note that this code is specific to the text-drawing functionality on my site, provided by text3d.h and text3d.cpp. There are, of course, other ways to draw text.

//Make sure that the "charset" file is in the same directory as the code

#include "text3d.h"

void initRendering() {
    //...
    t3dInit();
    //...
}

void cleanup() {
    //...
    t3dCleanup();
    //...
}

void drawScene() {
    //...
    t3dDraw2D("Rock\n'n' Roll", //The text to draw
              -1,               //Left-aligned
              -1,               //Top-aligned
              1.6f);            //The height of each line (may be omitted)
    //...
    t3dDraw3D("Rock the\nHouse", //The text to draw
              0,                 //Centered horizontally
              1,                 //Bottom-aligned
              0.3f,              //The 3D depth of each character
              1.6f);             //The height of each line (may be omitted)
    
    //...
    
    //Compute the draw width of "A rolling stone gathers no moss"
    float width = t3dDrawWidth("A rolling stone gathers no moss");
    //Compute the draw height of "Leave no\nstone unturned"
    float height =
        t3dDrawHeight("Leave no\nstone unturned",
                      1.6f); //The height of each line (may be omitted)
}

Textures

[From "Lesson 5: Textures"]

#include "imageloader.h"

//Makes the image into a texture, and returns the id of the texture
GLuint loadTexture(Image* image) {
    GLuint textureId;
    glGenTextures(1, &textureId);
    glBindTexture(GL_TEXTURE_2D, textureId);
    glTexImage2D(GL_TEXTURE_2D,
                 0,
                 GL_RGB,
                 image->width, image->height,
                 0,
                 GL_RGB,
                 GL_UNSIGNED_BYTE,
                 image->pixels);
    return textureId;
}

GLuint textureId; //The id of the texture

void initRendering() {
    //...
    Image *image = loadBMP("vtr.bmp");
    textureId = loadTexture(image);
    delete image;
    //...
}

void drawScene() {
    //...
    
    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, textureId);
    
    //Use blurry texture mapping (replace GL_LINEAR with GL_NEAREST for blocky)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    
    glColor3f(1.0f, 1.0f, 1.0f); //Don't use special coloring
    
    glBegin(GL_TRIANGLES);
    glTexCoord2f(0.0f, 0.0f);
    glVertex3f(-1.0f, -1.0f, 0.0f);
    glTexCoord2f(1.0f, 2.0f);
    glVertex3f(0.0f, 1.0f, 0.0f);
    glTexCoord2f(2.0f, 0.0f);
    glVertex3f(1.0f, -1.0f, 0.0f);
    glEnd();
    
    glDisable(GL_TEXTURE_2D); //Switch back to using colors instead of textures
    
    //...
}

Timers

[From "Lesson 2: Transformations and Timers"]

//The number of milliseconds between calls to update
const int NUM_MILLISECONDS = 25;

void update(int value) {
    //Body of timer
    //...
    
    glutPostRedisplay(); //Let GLUT know that the scene has changed
    //Tell GLUT to call update in NUM_MILLISECONDS milliseconds
    glutTimerFunc(NUM_MILLISECONDS, update, 0);
}

int main(int argc, char** argv) {
    //...
    //Tell GLUT to call update in NUM_MILLISECONDS milliseconds
    glutTimerFunc(NUM_MILLISECONDS, update, 0);
    //...
}

Transformations

[From "Lesson 2: Transformations and Timers"]

void drawScene() {
    //...
    
    glMatrixMode(GL_MODELVIEW); //Switch to "normal" transformations
    glLoadIdentity(); //Reset to origin, facing in negative z direction
    glTranslatef(0.0f, 0.0f, -10.0f); //Move 10 units forward
    
    //First triangle
    glPushMatrix(); //Save the current transformation state
    glTranslatef(-2.0f, 0.0f, 0.0f); //Move 2 units left
    glRotatef(20.0f, 1.0f, -1.0f, 0.0f); //Rotate 20 degrees about (1, -1, 0)
    
    glBegin(GL_TRIANGLES);
    glVertex3f(-1.0f, -1.0f, 0.0f);
    glVertex3f(0.0f, 1.0f, 0.0f);
    glVertex3f(1.0f, -1.0f, 0.0f);
    glEnd();
    
    glPopMatrix(); //Restore the transformation state
    
    //Second triangle
    glPushMatrix(); //Save the current transformation state
    glTranslatef(2.0f, 0.0f, 0.0f); //Move 2 units right
    glScalef(0.5f, 0.5f, 0.5f); //Shrink to 50% size
    
    glBegin(GL_TRIANGLES);
    glVertex3f(-1.0f, -1.0f, 0.0f);
    glVertex3f(0.0f, 1.0f, 0.0f);
    glVertex3f(1.0f, -1.0f, 0.0f);
    glEnd();
    
    glPopMatrix(); //Restore the transformation state
    
    //...
}