Here’s an OpenGL gotcha which caught me out for quite a while. I was writing code to load textures, but it only worked if I was using the GLU function to build mipmaps. After lots of hair-pulling, I realised that it was just my silly mistake.
My code used the FreeImage library to load the image files from disk. I was then doing the necessary re-scaling to powers-of-two dimensions, and checking that the input format was compatible, and finally loading the texture using “glTexImage2D”. I didn’t care about mipmaps at this early stage, so I loaded the texture at level 0, and had made sure the minification filter was set to “GL_LINEAR” (which doesn’t use mipmaps).
Everything seemed fine in the code, and yet the textures were not being rendered — all I could see were coloured polygons. The only way I could get the textures to render was if I switched my “glTexImage2D” function call for “gluBuild2DMipmaps”. I double-checked that my texture filters were correctly setup, and sure enough I had the following code in my OpenGL initialisation function:
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
It took a long time, but after some digging, I eventually realised my mistake. I had assumed that the texture parameters set by “glTexParameter*” were applied to the texture target (in this, GL_TEXTURE_2D). However, they are actually per-texture properties — i.e. the properties are applied only to the texture object which currently bound to that target.
What does this mean in practice?
It’s easy really. Instead of specifying the texture parameters in my OpenGL initialisation function, I specified them in the texture loading function, immediately after my call to “glTexImage2D”, and everything worked fine.