PDA

View Full Version : Manually generate mipmaps for textures loaded using glCompressedTexImage2D



Silverlan
09-19-2014, 02:48 PM
I need to generate the mipmaps for my textures on the fly, but I can't use glGenerateMipmap. The initialization of the texture data is supposed to be done on a separate thread in the background, independent of any OpenGL-calls.

Generating the mipmaps for uncompressed textures on the CPU is fairly straight forward (Following the information from this document (http://homepage.cs.uiowa.edu/~cwyman/classes/fall04-22C151/handouts/OGLmipmap.pdf)), however that somewhat fails when working with compressed textures. Apparantly OpenGL doesn't like it if you try to use both glCompressedTexImage2D and glTexImage2D on the same texture object, no errors are generated, but the texture ends up entirely black.
I'm guessing that the mipmap data needs to be in a compressed format as well. So, my question is, how do I generate the data I need to create the mipmaps of a compressed texture, and how do I then get that data into a format I can use with glCompressedTexImage2D?

Dark Photon
09-19-2014, 05:20 PM
Generating the mipmaps for uncompressed textures on the CPU is fairly straight forward ... however that somewhat fails when working with compressed textures. Apparantly OpenGL doesn't like it if you try to use both glCompressedTexImage2D and glTexImage2D on the same texture object, no errors are generated, but the texture ends up entirely black.

You wouldn't call both of these methods on the same texture -- they both allocate texels (and optionally provide texel data). The second call on the same MIP would free the previous texels and allocate a fresh new MIP level.

Use glTexImage2D to allocate each texture MIP (NULL ptr to allocate only and not provide pixel data), and then use glCompressedTexSubImage2D to upload the texels to each MIP level.

(I don't know if it's still true, but in ancient times it used to be that some drivers seg faulted if you provided a NULL pointer to glCompressedTexImage2D. Probably fixed by now. If so, then can use glCompressedTexImage2D to allocate, and optionally glCompressedTexSubImage2D to provide texels after allocation.)


I'm guessing that the mipmap data needs to be in a compressed format as well. So, my question is, how do I generate the data I need to create the mipmaps of a compressed texture, and how do I then get that data into a format I can use with glCompressedTexImage2D?
Yes. As to how to generate, depends on which compressed texture format you're talking about (which ones?). if you just want a solution for the common DXT compressed texture formats, websearch "squish simon brown". Read the links, download the library, and use it. There are other options out there as well.