OpenGL Texture got corrupted

Started by
2 comments, last by Danicco 10 years, 7 months ago

I had a code that was loading a BMP file and displaying a quad with the BMP as texture on screen.

I was restructuring the code and when I tried to test it again, the textures weren't showing anymore.

After a bunch of tests, I got to something really weird.

The reading from the BMP wasn't changed at all, so I don't think that's the cause, and the draw function is also really simple (it's also copied from another project I'm 100% sure it's working).

I tried changing BMPs to see if that was the issue, and a bunch of other images didn't show either.

However a simple checker BMP image I did some time ago for tests was showing perfectly fine.

This is the checker image, and what displays when I call it:

Test1_zpsecc105f5.jpg

This is another image I tried to display (among a bunch of others) and this is what shows:

Test2_zps30f90d5e.jpg

After testing for hours and not finding anything, I decided to do a little test, I copied some pixels from the image that wasn't displaying to the one that was:

Test3a_zpsc68e3375.jpg

And then I called it "Weird.bmp" and tried to load it on my code, and this is what happened:

Test3_zps3afb1d13.jpg

(Note the messed up first pixel row)

Also, another BMP that I was using for tests (640x913 BMP) seems to cause my BMP loading code to bug out/infinite loop, as it's header's height/width are being set to a negative value:


if(fileReader.is_open())
{
    char *bitmapFileHeaderChar = new char[sizeof(BITMAPFILEHEADER)]; 
    fileReader.read(bitmapFileHeaderChar, sizeof(BITMAPFILEHEADER));
    BITMAPFILEHEADER *bitmapFileHeader = (BITMAPFILEHEADER*)(bitmapFileHeaderChar);
 
    char *bitmapInfoHeaderChar = new char[sizeof(BITMAPINFOHEADER)];
    fileReader.read(bitmapInfoHeaderChar, sizeof(BITMAPINFOHEADER));
    BITMAPINFOHEADER *bitmapInfoHeader = (BITMAPINFOHEADER*)(bitmapInfoHeaderChar);
 
    _texture->textureHeight = bitmapInfoHeader->biHeight; //On this specific BMP image, this has a -842150451
    _texture->textureWidth = bitmapInfoHeader->biWidth; //This too
 
    long imageTotalBytes = _texture->textureHeight * _texture->textureWidth * 4;
 
    _texture->_textureData = new GLubyte[imageTotalBytes];
 
    char pixelInfo[3];
    int c = 0;
    while(c < imageTotalBytes)
    {
        fileReader.read(pixelInfo, 3);
        _texture->_textureData[c] = (GLubyte)pixelInfo[2];
        _texture->_textureData[c - 1] = (GLubyte)pixelInfo[1];
        _texture->_textureData[c - 2] = (GLubyte)pixelInfo[0];
        _texture->_textureData[c - 3] = 255; //Manually filling alpha
    
        c += 4;
    }
 
    fileReader.close();
}

Even after I got the original and re-saved it again, I'm still getting the same wrong header file. And it seems to be just on this specific file.

What could be causing this, or do you have any suggestions on how can I debug this?

Edit: I have this same image that has a negative header value in a binary file I made for test reading/writing, and when I load it from there, it shows perfectly fine.

However, now even my project that I use to load/read files and pack them in a single binary file isn't working anymore because it's reading this BMPs wrongly as well (it's even a different file, same image, in a different folder).

Advertisement

I would look carefully at BITMAPFILEHEADER and BITMAPINFOHEADER, they have some non-standard packing going on, and if they're just simply defined without the necessary pragmas, then that could be breaking things. For instance, what value do you get for sizeof(BITMAPFILEHEADER)? If it's not 14, then you have some packing issues to attend to. Or perhaps your file reading code is assuming that it'll always be reading multiples of 4 bytes at a time?

I think you also need to look again at this loop:


    int c = 0;
    while(c < imageTotalBytes)
    {
        fileReader.read(pixelInfo, 3);
        _texture->_textureData[c] = (GLubyte)pixelInfo[2];
        _texture->_textureData[c - 1] = (GLubyte)pixelInfo[1];
        _texture->_textureData[c - 2] = (GLubyte)pixelInfo[0];
        _texture->_textureData[c - 3] = 255; //Manually filling alpha
    
        c += 4;
    }

On the first iteration through, you are using a negative subscript into _texture->_textureData, which is most likely splatting some random memory.

Oh - Also, you're making big assumptions about the format of the .bmp file.

For instance, if the file is palettized or greyscale, your code will not be loading correctly. The .bmp format has plenty of dusty corners that aren't worth supporting, but at the very least you should sanity check pretty much each and every field of BITMAPINFOHEADER and assert if you come across something that you don't support in your loader.

Oh that was a typo, it's actually c + 1, c + 2, etc...

I rechecked the sizes, it's 14 and 40 for the bitmap file header and info header sizes.

The sizes and values seems fine, on most images I do get the correct width x height, but it just doesn't show at all (like my test #2).

And sigh, just figured what it was... I forgot to add fileReader(resourceName.c_str(), ios::binary).

What's more annoying is that I took 3h and went to sleep frustrated because of it, and it was something this stupid...

This topic is closed to new replies.

Advertisement