Question

I implemented the loading of DXT compressed textures in OpenGL following this tutorial:

OpenGL DXT tutorial

The problem is, it works fine when the width and height are the same (always powers of two), but it just shows up black when they are not (1024 * 512, 256 * 512, etc do not work...). What could be the problem?

I'm using OpenGL 3.3 and my video card is an AMD Radeon HD 7610M (notebook, but pretty good). Also, glGetError() shows no errors (triple checked).

I'm posting my version of the code just to be sure (the difference is that I read the textures from a packed file format, which cannot be the problem since I load all my models from these and everything's fine):

void Texture::loadDDS(unsigned char* data)
{
    unsigned int height = *(unsigned int*)&(data[12]);
    unsigned int width = *(unsigned int*)&(data[16]);
    unsigned int mipMapCount = *(unsigned int*)&(data[28]);
    unsigned int fourCC = *(unsigned int*)&(data[84]);
    unsigned int format = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;

    switch(fourCC)
    {
        case FOURCC_DXT1: format = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT; break;
        case FOURCC_DXT3: format = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT; break;
        case FOURCC_DXT5: format = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT; break;
    }

    glGenTextures(1, &_id);
    glBindTexture(GL_TEXTURE_2D, _id);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    unsigned int blockSize = (format == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT) ? 8 : 16;
    unsigned int offset = 124 + 4;

    for(unsigned int level = 0; level < mipMapCount && (width || height); ++level)
    {
        unsigned int size = ((width + 3) / 4) * ((height + 3) / 4) * blockSize;
        //if(size == 0) break; // neccessary?
        glCompressedTexImage2D(GL_TEXTURE_2D, level, format, width, height, 0, size, &data[offset]);

        offset += size;
        width /= 2;
        height /= 2;
    }

    glBindTexture(GL_TEXTURE_2D, 0);
}
Was it helpful?

Solution

I suppose your problem is due to the following lines :

width /= 2;
height /= 2;

Which will lead to incorrect 0 (zero) values.

You should try to replace them with something like :

width = MAX(1, width/2);
height = ...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top