• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# OpenGL Texture got corrupted

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

3 replies to this topic

### #1Danicco  Members

Posted 28 August 2013 - 01:31 AM

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:

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

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:

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

(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())
{

_texture->textureHeight = bitmapInfoHeader->biHeight; //On this specific BMP image, this has a -842150451

long imageTotalBytes = _texture->textureHeight * _texture->textureWidth * 4;

_texture->_textureData = new GLubyte[imageTotalBytes];

char pixelInfo[3];
int c = 0;
while(c < imageTotalBytes)
{
_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;
}

}


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).

Edited by Danicco, 28 August 2013 - 01:35 AM.

### #2C0lumbo  Members

Posted 28 August 2013 - 01:56 AM

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)
{
_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.

### #3C0lumbo  Members

Posted 28 August 2013 - 04:46 AM

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.

### #4Danicco  Members

Posted 28 August 2013 - 11:17 AM

Oh that was a typo, it's actually c + 1c + 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...

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.