OpenGL Wrapper Classes

All of the basic OpenGL classes have apply() functions to apply their specific feature, and (except for Transforms) disable() functions to turn them off.

Each class includes some set of member variables for the data needed in its apply(). There are a series of functions for reading and writing the values of these variables; for example, for a variable named foo, there would be a function setFoo(value) to change its value, and a function foo() that would return its current value.

Light

Implements a single OpenGL light source. Wraps the code:

glEnable(lightNumber);
glLightfv(lightNumber, GL_POSITION, position);
glLightfv(lightNumber, GL_AMBIENT, ambient);
glLightfv(lightNumber, GL_DIFFUSE, diffuse);
glLightfv(lightNumber, GL_SPECULAR, specular);
glLightf(lightNumber, GL_CONSTANT_ATTENUATION,
                      attenuationConstant);
glLightf(lightNumber, GL_LINEAR_ATTENUATION,
                      attenuationLinear);
glLightf(lightNumber, GL_QUADRATIC_ATTENUATION, 
                      attenuationQuadratic);
glLightf(lightNumber, GL_SPOT_CUTOFF, spotCutoff);
glLightfv(lightNumber, GL_SPOT_DIRECTION, spotDirection);
glLightf(lightNumber, GL_SPOT_EXPONENT, spotExponent);

One special feature is that dms::Light automatically generates the lightNumber value (e.g. GL_LIGHT0), guaranteeing that no two lights will accidentally use the same number (unless the maximum number of lights - about 3000 - is exceeded).

Material

Implements surface material properties for lighting. Wraps the code:

glEnable(GL_LIGHTING);
glMaterialfv(GL_FRONT, GL_AMBIENT, ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, specular);
glMaterialfv(GL_FRONT, GL_EMISSION, emission);
glMaterialf(GL_FRONT, GL_SHININESS, shininess);

Transparency

Encapsulates blending and alpha test features, which can be used for transparency:

if (blend)
    {
    glEnable(GL_BLEND);
    glBlendFunc(blendSource, blendDest);
    }
if (alphaTest)
    {
    glEnable(GL_ALPHA_TEST);
    glAlphaFunc(alphaFunc, alphaRef);
    }

The constant objects Transparency::StandardBlend and Transparency::AlphaTestZero are pre-defined to encapsulate the most common transparency types. StandardBlend applies a (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) blending function; AlphaTestZero applies a (GL_GREATER, 0.0) alpha test.

Texture

Texture is an abstract class that is intended as the parent of classes for 1D, 2D, and 3D textures (so far only the 2D texture class is implemented). This makes it possible for other code to make use of a texture generically, without having to worry whether its 1D, 2D, or 3D.

Texture itself keeps track of the wrapping modes (GL_REPEAT or GL_CLAMP), and minification and magnification filters, because these features are common to all textures.

Texture2D

Implements a 2D texture. Texture2D's includes a define() function, besides apply(). define() executes the code to create a new texture, and binds it to a generated texture ID; apply() simply applies that texture. If define() has not been directly called, it will be called automatically on the first apply().

The basic code that it wraps is:

void define(void)
    {
    glGenTextures(1, &id);
    glBindTexture(GL_TEXTURE_2D, id);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, xdim, ydim, 0,
                 GL_RGBA, GL_UNSIGNED_BYTE, imgp);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
                    magFilter());
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
                    minFilter());
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS());
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT());
    }

void apply(void)
    {
    glBindTexture(GL_TEXTURE_2D,id_);
    glEnable(GL_TEXTURE_2D);
    }

glTexImage2D() is automatically replaced by gluBuild2DMipmaps() if the minification filter uses mipmapping (GL_LINEAR_MIPMAP_LINEAR, etc.). If the dimensions of the image data are not powers of two, gluScaleImage() is used to resize it down to the nearest powers of two.

Texture2D uses the class Image to store its image data. An Image object can be created separately and passed to a Texture2D for use; or, the name of a TIFF image file can be passed to the Texture2D constructor, and a TIFFImage object will be automatically created.

Image

Image is intended as a parent class to represent any generic image that can be used by Texture2D. It keeps track of the image's size (width & height) and a pointer to its data.

The basic class Image can be used for data that is generated by a program itself. Call the function setSize(width,height) to set the image's size, and either setData(data) to pass it a pointer to data that has been created, or allocateData() to have Image allocate space for the image itself (the pointer to which can then be obtained from data()).

TIFFImage

TIFFImage is derived from Image, and includes code for reading a TIFF image file into an RGBA array of data. It can be used either by calling loadFile(filename), or by passing the file name to the class's constructor (e.g. TIFFImage myImage("picture.tif");).

Transform

Transform is an abstract class that is intended to represent any sort of transformation that might be applied to an object. Different derived classes can be created to implement this transformation in different ways.

Transform itself includes the functions apply() to apply the transformation, pushApply() to perform a glPushMatrix followed by the transformation, and pop() to perform a glPopMatrix.

SimpleTransform

Represents a transformation that consists of a translation, rotation about a single axis, and a scaling. Wraps the code:

glTranslatef(translate[0], translate[1], translate[2]);
glRotatef(rotateAngle, rotateAxis[0], rotateAxis[1],
          rotateAxis[2]);
glScalef(scale[0], scale[1], scale[2]);



next