These are not really OpenGL problems as much as they're data manipulation problems.
Anyways....
Bitmap pitch
The pitch is the number of bytes between rows. This has to be calculated, as it is not stored directly in the bitmap header.
The pitch of the bitmap is equal to round_up_to_4byte_boundary( bytes_per_pixel * pixel_width )
So if there are 3 bytes per pixel, and you have a width of 150... this means there are actually 452 bytes per row... not 450.
Your code will need to account for this by either making the rows wider... or skip bytes in the file between reading each row.
Bitmaps are upside-down
Normal bitmaps are stored with the bottom row first and the top row last. OpenGL textures (and pretty much every other kind of imaging standard everywhere) does it the exact opposite, with the top row first. This means you will need to flip the image somehow. Either after it's loaded, or do it while you're loading.
Note that this isn't always the case. You can tell if an image is upside-down or not by looking at the biHeight value in the header. If it's positive, the bitmap is upside-down. If it's negative, the bitmap is rightside-up.
OpenGL needs power of 2 sizes
The buffer you give to openGL might have to be larger than the image data you have. For example if your image is 160 x 100 pixels... this means the smallest texture that image will fit on is 256 x 128 (unless you break the image up and rearrange it -- but that's another topic I won't get into).
What you need to do
Basically... this code is wrong.
1 2 3
|
data = (BYTE *)malloc(width * height * BytesofFile);
//...
fread(data, width * height, BytesofFile, picfile);
|
I would recommend you do several things differently:
1) Get the width, height, and bytes-per-pixel (bpp) value from the header
2) Use the width and bpp values to calculate the pitch
3) Use the width and height to calculate how big of an OpenGL texture you'll need. Remember the texture dims must be >= the image dims... and the texture dims must each be an exact power of 2 (2, 4, 8, 16, 32, 64, 128, 256, 512, 1024)
4) Allocate a buffer big enough to hold the entire
texture (ie: power of 2 texture dims). Probably a good idea to wipe it to transparent black as well because you probably will not set every pixel according to the bmp file contents, due to the necessary power-of-2 padding.
5) Start reading rows individually from the file, rather than trying to read the entire image in one read.
6) If the image needs to be vertically flipped (positive height), then you can flip it by reading rows into different areas in the buffer. For example if the image is 10 pixels tall, you can read the first row in the file into row 9 of your texture buffer. 2nd row in the file goes in row 8 of your texture, etc, etc.