Do this, this only works for 24 bit rgb BMP images, and maybe not them all (Microsoft has a real problem designing anything as trivial as an image format without turning it into a baroque disaster), it’s kinda old code. This includes the bmp loader, a texture creation code fragment and modifications to your code to make it use the texture. The glBindTexture call isn’t strictly required but it will come in handy if you decide to create more than one texture. Note that using the bind call you should take the texture loading out and call it once at the start only to create the textures, with the bind call at load time, then you can use the bind call any time later to bind that texture ready for use.
/* add this code to allow quick and simple BMP loading to an image in memory ready for OpenGL, limited to 24 bit RGB BMPs */
unsigned char *LoadBMP(LPCTSTR fname, long *x, long *y)
{
BYTE *image_data = NULL;
HANDLE hFileHandle;
BITMAPINFO *BitmapInfo = NULL;
unsigned long InfoSize = 0;
unsigned long BitSize = 0;
BITMAPFILEHEADER bitmapHeader;
DWORD dwBytes;
*x = 0;
*y = 0;
/* Open the Bitmap file */
hFileHandle = CreateFile(fname,GENERIC_READ,FILE_SHARE_READ,
NULL,OPEN_EXISTING,FILE_FLAG_SEQUENTIAL_SCAN,NULL);
/* make sure file exists */
if(hFileHandle == INVALID_HANDLE_VALUE)
return NULL;
/* read common header data */
ReadFile(hFileHandle,&bitmapHeader,sizeof(BITMAPFILEHEADER),
&dwBytes,NULL);
if(dwBytes == sizeof(BITMAPFILEHEADER) && bitmapHeader.bfType == 'MB')
{
/* Read in rest of bitmap information structure */
InfoSize = bitmapHeader.bfOffBits - sizeof(BITMAPFILEHEADER);
BitmapInfo = (BITMAPINFO *) malloc( sizeof(BYTE) * InfoSize );
ReadFile(hFileHandle,BitmapInfo,InfoSize,&dwBytes,NULL);
if(dwBytes == InfoSize) /* did we read what we asked for */
{
if( /* check for RGB uncompressed (non RLE) */
BitmapInfo->bmiHeader.biCompression == BI_RGB &&
/* check for 24 bit data */
BitmapInfo->bmiHeader.biBitCount == 24 )
{
*x = BitmapInfo->bmiHeader.biWidth;
*y = BitmapInfo->bmiHeader.biHeight;
/* docs say this may be zero for BI_RGB */
BitSize = BitmapInfo->bmiHeader.biSizeImage;
/* read size for BI_RGB if required,
we know it's a 3 byte per pixel image */
if(BitSize == 0)
BitSize = (*x * *y * 3);
image_data = (BYTE *) malloc(sizeof (BYTE)*BitSize);
/* Read in the bitmap bits */
ReadFile(hFileHandle,image_data,BitSize,&dwBytes,NULL);
if(BitSize != dwBytes)
{
if(image_data != NULL)
{
free (image_data);
image_data = NULL;
}
}
}
}
}
/* always close file (NULL already returned if file open failed) */
CloseHandle(hFileHandle);
if(BitmapInfo != NULL)
free (BitmapInfo);
return image_data;
}
.
.
.
.
.
/* Call this somewhere at the start once you have a context*/
unsigned char *image;
long x_image, y_image;
image = LoadBMP("MyCoolImage.bmp", &x_image, &y_image);
glBindTexture(GL_TEXTURE_2D, 1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, 3, x_image, y_image, 0, GL_BGR_EXT, GL_UNSIGNED_BYTE, image);
free(image);
.
.
.
.
.
.
.
.
/* change your polygon drawing code to this */
glBindTexture(GL_TEXTURE_2D, 1);
glEnable(GL_TEXTURE_2D);
glBegin(GL_POLYGON);
glTexCoord2f(1.0, 1.0);
glVertex2f(8.0, -3.0);
glTexCoord2f(0.0, 1.0);
glVertex2f(-8.0, -3.0);
glTexCoord2f(0.0, 0.0);
glVertex2f(-8.0, -6.0);
glTexCoord2f(1.0, 0.0);
glVertex2f(8.0, -6.0);
glEnd();
glDisable(GL_TEXTURE_2D);