Using mipmaps results in garbage textures

1.3k views Asked by At

I'm creating my own mipmaps when creating textures, and as soon as I enable mipmaps via GL_LINEAR_MIPMAP_NEAREST (or anything MIPMAP), the textures are just a very dark, blurry mess. If I switch to GL_LINEAR, they're fine.

Here's how I'm creating the texture:

glGenTextures(1, &m_TextureId);
glBindTexture( GL_TEXTURE_2D, id );
int level = 0;

jobject mippedBitmap = srcBitmap;

while (width >= 2 && height >= 2) {
    jniEnv->CallStaticVoidMethod(s_GLUtilsClass, s_texImage2DMethodID,
                     GL_TEXTURE_2D, level, mippedBitmap, 0);

    width >>= 1;
    height >>= 1;
    level++;

    mippedBitmap = createScaledBitmap(jniEnv, srcBitmap, width, height, true);
}

I've omitted all Bitmap.recycle()/NewGlobalRef() calls for brevity. createScaledBitmap is obviously a JNI call to Bitmap.createScaledBitmap().

I also tried the other version of texImage2D that takes the format and type of the bitmap. I've verified that it's always RGBA.

EDIT: To elaborate on the textures - they're really almost black. I tried eraseColor() on the mips with bright colors, and they're still extremely dark.

3

There are 3 answers

0
kvark On BEST ANSWER

glGenerateMipmap will do this task faster and much more convenient for you.

1
ryanm On

It may be the case that Bitmap.createScaledBitmap does not correct the gamma. Have a look at this thread for code to create gamma-corrected mipmaps

1
Bahbar On

The code you're showing will not generate the lower mip levels (1x1 will be missing, e.g.), so your texture will be incomplete. That should make the rendering as if the texturing was not present at all. It's unclear from your description if this is what you're observing.

In all cases, you should provide the mipmaps all the way down to the 1x1 (and for non square textures, this requires some changes in the computation of the new texture size to keep passing 1, as in 4x1 -> 2x1 -> 1x1)