Weird mip map related issue using SOIL - OpenGL

668 views Asked by At

I am using SOIL to generate OpenGL textures. Everything is fine except for distant artifacts caused by the mip map generation in SOIL. When I don't use the flag:

SOIL_FLAG_MIPMAPS

It doesn't artifact BUT it looks bad. Has anyone seen artifacts like this? Over the water, there is a maroon color and over the sand, it looks like a light green/blue distortion.

Here is what it looks like:

enter image description here

2

There are 2 answers

1
genpfault On

Anisotropic filtering might help:

// right after you set your GL_TEXTURE_MIN/MAG_FILTER
if( glewIsSupported("GL_EXT_texture_filter_anisotropic") )
{
    GLfloat max;
    glGetFloatv( GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &max );
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, max);
}

Alternatively some sort of loading/texture coordinate error, though there's not enough information to be sure (i.e., a complete, minimal program that demonstrates the problem).

1
SigTerm On

My guess is that that whatever you use to generate mipmap has a bug and doesn't filter mip levels properly - either "leaks" random color from outside of "valid" area in which pixels of previous levels are stored, or forgets to calculate vaules for some pixels. I.e. there is a bug that affects only last levels of mipmap chain.

To verify that this is the cause, read data back from mip levels of a texture you loaded. Using glReadPixels or whatever. If last mip levels contains garbage - it is fault of your texture loading routine. You could also use GL_LINEAR_MIPMAP_NEAREST filter and see if there's a specific mip level with purple pixels.

To bypass the problem, generate image for the first level and use glGenerateMipmaps (OpenGL 3+), glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);(OpenGL 2+ or OpenGL 1.4+ - I forgot in which version it was added. Deprecated in OpenGL 3+), or gluBuild2DMipMap(ancient OpenGL versions).

Or you could calculate mipmaps yourself. Box filter isn't hard to implement. Gaussian isn't hard either.

If you generate textures using formula (since you said "generate") another possible scenario is that the formula causes computation error on last mip level which causes garbage colors to appear.

Yet another possible scenario is faulty hardware (you won't believe what kind of picture you can get on overheated GPU with broken cooler) or buggy driver. But that should cause artifacts in many applications, not just in your program.