BMP loading problem

Hi Guys n Gals, I have a little problem and would appreciate any advice anyone can give…

I am working on my first demo and have a bug in the loadBMP function which causes problems with the drawing of the rest of the scene. I know it is with this function as when I remove it, everything is Rosy. After the following function has executed, the color on the screen changes, with the blue elements turning black and the white parts turning brown :frowning: I have fiddled about with the glTexImage2D paramenter but dont really know much about them just yet so don’t seem to be getting any further.

Any help would be greatly appreciated, Thanks in advance…


int Ctexture::LoadBMP(const char* szFileName)
HANDLE hFileHandle;
unsigned long lInfoSize = 0;
unsigned long lBitSize = 0;
int nTextureWidth;
int nTextureHeight;
BYTE *pBitmapData;

// Open the Bitmap file
hFileHandle = CreateFile(szFileName,GENERIC_READ,FILE_SHARE_READ,

// Check for open failure (most likely file does not exist).
	return FALSE;

// File is Open. Read in bitmap header information
DWORD dwBytes;

__try {
	if(dwBytes != sizeof(BITMAPFILEHEADER))
		return FALSE;

	// Check format of bitmap file
	if(bitmapHeader.bfType != 'MB')
		return FALSE;

	// Read in bitmap information structure
	lInfoSize = bitmapHeader.bfOffBits - sizeof(BITMAPFILEHEADER);
	pBitmapInfo = (BITMAPINFO *) new BYTE[lInfoSize];


	if(dwBytes != lInfoSize)
		return FALSE;

	nTextureWidth = pBitmapInfo->bmiHeader.biWidth;
	nTextureHeight = pBitmapInfo->bmiHeader.biHeight;
	lBitSize = pBitmapInfo->bmiHeader.biSizeImage;

	if(lBitSize == 0)
		lBitSize = (nTextureWidth *
		pBitmapInfo->bmiHeader.biBitCount + 7) / 8 *

	// Allocate space for the actual bitmap
	pBitmapData = new BYTE[lBitSize];

	// Read in the bitmap bits

	if(lBitSize != dwBytes)
			delete [] (BYTE *) pBitmapData;
		pBitmapData = NULL;

		return FALSE;
__finally // Fail or success, close file and free working memory

	if(pBitmapInfo != NULL)
		delete [] (BYTE *)pBitmapInfo;

// This is specific to the binary format of the data read in.
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);

glTexImage2D(GL_TEXTURE_2D, 0, 4, nTextureWidth, nTextureHeight, 0,

	delete [] (BYTE *) pBitmapData;

return TRUE;


  • Default texture filtering is GL_LINEAR for magnification and GL_LINEAR_MIPMAP_NEAREST for minification. If you haven’t set them elsewhere your texture will be inconsistent and disable the current texture unit because you haven’t downloaded mipmaps.
  • Don’t use “4” as internalFormat in glTexImage2D, your data has no alpha, so GL_RGB8 will do.

Thanks for that Relic, how do I set the filtering though. Not sure what you mean by my texture will be disabled because I am not using mipmaps too, are they essential?

Also, am not using the GlBindTexture command (as I am only using one image), would this cause a problem or would it only be used for multiple texture handling?

Thanks again,


Aftre some more testing, it appears that once the file is loaded into memory, when I draw a quad and call the command:

glTexCoord2f(texture.tex[1][6], texture.tex[1][7]);

It changes the colour of the primitives in the scene - even if they were drawn before the texture was called, hmmmmn. :frowning: