If you have worked with meshes and textures in OpenGL, than you probably have experienced aliasing on textures bound to meshes. Unfortunately OpenFrameworks 008 only supports mip mapping of textures that have compression on them. I usually work with straight jpgs or pngs for my textures, so that is not an option. Recently some friends over at Incredible Machines brought up the issue, so I thought it might be useful to post. Both for other people and myself.

Below is an example of a texture without mip mapping. Notice the jaggedness on the checkered image. It is more apparent when there is a sharp angle from the vertex to the camera.
Regular ofTexture

Now here is the mip mapped version of the texture. Notice how it appears more anti-aliased. (Click to enlarge.)
Mip Mapped ofTexture

Anisotropy limits how much “aliasing” is applied between the mip maps. A value of 1 will do nothing, as the value increases so does the appearance of anti-aliasing.
I wrote a convenience function that is based on ofTexture, but includes the mip map and anisotropy functions.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
void testApp::loadMipMapTexture( ofTexture& inTex, string inPath, float inAnisotropy ) {
    ofPixels pix;
	bool loaded = ofLoadImage(pix, inPath );
 
    inTex.allocate( pix.getWidth(), pix.getHeight(), ofGetGlInternalFormat(pix) );
 
    ofTextureData& texData = inTex.texData;
 
	if (texData.textureTarget == GL_TEXTURE_RECTANGLE_ARB){
		texData.tex_t = pix.getWidth();
		texData.tex_u = pix.getHeight();
	} else {
		texData.tex_t = (float)(pix.getWidth()) / (float)texData.tex_w;
		texData.tex_u = (float)(pix.getHeight()) / (float)texData.tex_h;
	}
 
    ofSetPixelStorei(pix.getWidth(),pix.getBytesPerChannel(),pix.getNumChannels());
 
    glGenTextures(1, &texData.textureID);
    glBindTexture( texData.textureTarget, texData.textureID);
    glTexImage2D( texData.textureTarget, 0, texData.glTypeInternal, pix.getWidth(), pix.getHeight(), 0, ofGetGlFormat(pix), ofGetGlType(pix), pix.getPixels());
    glHint(GL_GENERATE_MIPMAP_HINT, GL_NICEST);
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
    glEnable(  texData.textureTarget);
    glGenerateMipmap(texData.textureTarget);
    glTexParameteri( texData.textureTarget, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
    glTexParameteri( texData.textureTarget, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);
 
        // 1 means it does nothing //
    if( inAnisotropy >= 1 ) {
        glTexParameterf( texData.textureTarget, GL_TEXTURE_MAX_ANISOTROPY_EXT, (GLfloat)inAnisotropy);
    }
 
    glDisable( texData.textureTarget );
}

Download the MipMapExample Xcode Project.