VC USES OPENGL to load BMP texture map method summary

  • 2020-04-02 02:27:15
  • OfStack

OpenGL is a professional graphics program interface, the function is very powerful, call the convenience of the underlying graphics library. This paper summarizes several methods of loading BMP texture map with OPENGL.

Method one:

First, get the bitmap handle:


HBITMAP hBmp = (HBITMAP) ::LoadImage (AfxGetResourceHandle(),MAKEINTRESOURCE(IDB_BITMAP1), IMAGE_BITMAP, 0, 0,LR_CREATEDIBSECTION);

Then get bitmap information according to the bitmap handle:


BITMAP BM;
::GetObject (hBmp, sizeof (BM), &BM);

Finally, texture is built according to the RGB value in bitmap information:


gluBuild2DMipmaps( GL_TEXTURE_2D, 3, BM.bmWidth, BM.bmHeight,GL_BGR_EXT, GL_UNSIGNED_BYTE,BM.bmBits);

Method 2:

First, use OpenGL auxiliary library to obtain bitmap information:


AUX_RGBImageRec* TextureImage[1];
TextureImage[0]=auxDIBImageLoad("1.bmp");

And then build the texture


gluBuild2DMipmaps(GL_TEXTURE_2D, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);

Method 3:

From the bottom, you need to understand the structure of the BMP file. First, you need to read the BMP file structure, including the file header, the information header, and the data. The data is used to define the texture later


long ImageWidth=256;
long ImageHeight=256; 
GLubyte Image[256][256][3];
void ReadHeader(FILE *fp , BITMAPFH * p_bitmapheader , BITMAPIH *p_bitmapinfo) 
{
 fseek(fp, 0, SEEK_SET) ;
 fread( &p_bitmapheader->bfType,sizeof(unsigned short), 1, fp );
 fseek(fp, 2, SEEK_SET) ;
 fread( &p_bitmapheader->bfSize,sizeof(unsigned long), 1, fp );
 fseek(fp, 6, SEEK_SET) ;
 fread( &p_bitmapheader->bfReserved1,sizeof(unsigned short), 1, fp );
 fseek(fp, 8, SEEK_SET) ;
 fread( &p_bitmapheader->bfReserved2,sizeof(unsigned short), 1, fp );
 fseek(fp, 10, SEEK_SET) ;
 fread( &p_bitmapheader->bfOffBits,sizeof(unsigned long), 1, fp );
 fseek(fp, 14, SEEK_SET) ;
 fread( &p_bitmapinfo->biSize, sizeof(unsigned long), 1, fp );
 fseek(fp, 18, SEEK_SET) ;
 fread( &p_bitmapinfo->biWidth, sizeof(unsigned long), 1, fp );
 fseek(fp, 22, SEEK_SET) ;
 fread( &p_bitmapinfo->biHeight, sizeof(unsigned long), 1, fp );
 fseek(fp, 26, SEEK_SET) ;
 fread( &p_bitmapinfo->biPlanes, sizeof(unsigned short), 1, fp );
 fseek(fp, 28, SEEK_SET) ;
 fread( &p_bitmapinfo->biBitCount, sizeof(unsigned short), 1, fp );
 fseek(fp, 30, SEEK_SET) ;
 fread( &p_bitmapinfo->biCompression, sizeof(unsigned long), 1, fp );
 fseek(fp, 34, SEEK_SET) ;
 fread( &p_bitmapinfo->biSizeImage, sizeof(unsigned long), 1, fp );
 fseek(fp, 38, SEEK_SET) ;
 fread( &p_bitmapinfo->biXPelsPerMeter, sizeof(unsigned long), 1, fp );
 fseek(fp, 42, SEEK_SET) ;
 fread( &p_bitmapinfo->biYPelsPerMeter, sizeof(unsigned long), 1, fp );
 fseek(fp, 46, SEEK_SET) ;
 fread( &p_bitmapinfo->biClrUsed, sizeof(unsigned long), 1, fp );
 fseek(fp, 50, SEEK_SET) ;
 fread( &p_bitmapinfo->biClrImportant, sizeof(unsigned long), 1, fp );
}
void ReadBitmapFile()
{
 BITMAPFH bitmapheader ;
 BITMAPIH bitmapinfo ;
 FILE *fp;
 fp = fopen("6.bmp" , "r") ;
 if(!fp)
 {
  puts("Read file failed.") ;
  return;
 }
 ReadHeader(fp, &bitmapheader , &bitmapinfo) ;
 if(bitmapinfo.biBitCount != 24)
 {
  puts("UNSUPPORT") ;
  return;
 }
 ImageWidth = bitmapinfo.biWidth;
 ImageHeight = bitmapinfo.biHeight;
 int i=bitmapheader.bfOffBits;
 while(i<bitmapheader.bfSize)
 {
  for(int j=0;j<ImageWidth;j++)
  for(int k=0;k<ImageHeight;k++)
  {
   fseek(fp, i, SEEK_SET) ;
   fread(Image[j][k]+2, 1, 1, fp) ;
   fseek(fp, i+1, SEEK_SET) ;
   fread(Image[j][k]+1, 1, 1, fp) ;
   fseek(fp, i+2, SEEK_SET) ;
   fread(Image[j][k], 1, 1, fp) ;
   i=i+3;
  } 
 } 
 fclose(fp) ;
}
glTexImage2D(GL_TEXTURE_2D,0,3,ImageWidth,ImageHeight,0,GL_RGB,GL_UNSIGNED_BYTE,&Image[0][0][0]);

# include < Gl \ glext. H >
This is because the following GL_BGR_EXT is defined in this header file, because the BMP format stores the image data in Blue,Green, and Red order, as opposed to OpenGL. GL_BGR_EXT does the conversion between the two.

The following is the function that does not use AUX library to load the BMP image as texture:


bool LoadTexture(LPTSTR szFileName, GLuint &texid)   // Creates Texture From A Bitmap File
{
HBITMAP hBMP;       // Handle Of The Bitmap
BITMAP BMP;       // Bitmap Structure
glGenTextures(1, &texid);      // Create The Texture
hBMP=(HBITMAP)LoadImage(GetModuleHandle(NULL), szFileName, IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION | LR_LOADFROMFILE );
if (!hBMP)        // Does The Bitmap Exist?
  return FALSE;       // If Not Return False
GetObject(hBMP, sizeof(BMP), &BMP);     // Get The Object
         // hBMP:    Handle To Graphics Object
         // sizeof(BMP): Size Of Buffer For Object Information
         // &BMP:    Buffer For Object Information
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);    // Pixel Storage Mode (Word Alignment / 4 Bytes)
// Typical Texture Generation Using Data From The Bitmap
glBindTexture(GL_TEXTURE_2D, texid);     // Bind To The Texture ID
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); // Linear Min Filter
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // Linear Mag Filter
glTexImage2D(GL_TEXTURE_2D, 0, 3, BMP.bmWidth, BMP.bmHeight, 0, GL_BGR_EXT, GL_UNSIGNED_BYTE, BMP.bmBits);
DeleteObject(hBMP);       // Delete The Object
return TRUE;       // Loading Was Successful
}

Related articles: