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…

BennUK


int Ctexture::LoadBMP(const char* szFileName)
{
HANDLE hFileHandle;
BITMAPINFO *pBitmapInfo = NULL;
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,
	NULL,OPEN_EXISTING,FILE_FLAG_SEQUENTIAL_SCAN,NULL);

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

// File is Open. Read in bitmap header information
BITMAPFILEHEADER	bitmapHeader;
DWORD dwBytes;
ReadFile(hFileHandle,&bitmapHeader,sizeof(BITMAPFILEHEADER),
	&dwBytes,NULL);

__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];

	ReadFile(hFileHandle,pBitmapInfo,lInfoSize,&dwBytes,NULL);

	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 *
		abs(nTextureHeight);

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

	// Read in the bitmap bits
	ReadFile(hFileHandle,pBitmapData,lBitSize,&dwBytes,NULL);

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

		return FALSE;
	}
}
__finally // Fail or success, close file and free working memory
{
	CloseHandle(hFileHandle);

	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,
	GL_BGR_EXT, GL_UNSIGNED_BYTE, pBitmapData);

if(pBitmapData)
	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,

BennUK

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:

BennUK