VC using OpenGL loaded BMP texture Map of the implementation method summary _c language

Source: Internet
Author: User
Tags fread

OpenGL is a professional graphics program interface, the function is very powerful, easy to invoke the underlying graphics library. This paper summarizes several implementation methods of using OpenGL to load BMP texture graphs.

Method One:

Get the bitmap handle first:

Hbitmap hbmp = (HBITMAP):: LoadImage (AfxGetResourceHandle (), Makeintresource (IDB_BITMAP1), Image_bitmap, 0, 0,LR_ CreateDIBSection);

Bitmap information is then obtained based on the bitmap handle:

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

Finally, based on the RGB value of the bitmap information to establish the texture:

Glubuild2dmipmaps (gl_texture_2d, 3, Bm.bmwidth, Bm.bmheight,gl_bgr_ext, gl_unsigned_byte,bm.bmbits);

Method Two:

First use the OpenGL Helper library to obtain the bitmap information:

aux_rgbimagerec* textureimage[1];
Textureimage[0]=auxdibimageload ("1.bmp");

Then build the texture

Glubuild2dmipmaps (gl_texture_2d, 3, Textureimage[0]->sizex, Textureimage[0]->sizey, GL_RGB, GL_UNSIGNED_BYTE, Textureimage[0]->data);

Method Three:

from the bottom to do, you need to understand the structure of BMP files, first read the BMP file structure, including file headers, information headers and data, data for the following definition of texture

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, seek_set);
 Fread (&p_bitmapheader->bfoffbits,sizeof (unsigned long), 1, FP);
 Fseek (FP, seek_set);
 Fread (&p_bitmapinfo->bisize, sizeof (unsigned long), 1, FP);
 Fseek (FP, seek_set);
 Fread (&p_bitmapinfo->biwidth, sizeof (unsigned long), 1, FP);
 Fseek (FP, seek_set);
 Fread (&p_bitmapinfo->biheight, sizeof (unsigned long), 1, FP);
 fseek (FP, num, seek_set); Fread (&p_bitmapiNfo->biplanes, sizeof (unsigned short), 1, FP);
 Fseek (FP, seek_set);
 Fread (&p_bitmapinfo->bibitcount, sizeof (unsigned short), 1, FP);
 Fseek (FP, seek_set);
 Fread (&p_bitmapinfo->bicompression, sizeof (unsigned long), 1, FP);
 Fseek (FP, seek_set);
 Fread (&p_bitmapinfo->bisizeimage, sizeof (unsigned long), 1, FP);
 Fseek (FP, seek_set);
 Fread (&p_bitmapinfo->bixpelspermeter, sizeof (unsigned long), 1, FP);
 Fseek (FP, N, Seek_set);
 Fread (&p_bitmapinfo->biypelspermeter, sizeof (unsigned long), 1, FP);
 Fseek (FP, seek_set);
 Fread (&p_bitmapinfo->biclrused, sizeof (unsigned long), 1, FP);
 Fseek (FP, M, 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!=) {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 are defined in this header file because the BMP format stores the image data in blue,green,red order, which is the opposite of OpenGL. Gl_bgr_ext is the transition between the two.

Here is a function that does not use the AUX library to load a BMP picture as a 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, &AMP;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 inform    ation//&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.BMB
its);       DeleteObject (hbmp);       Delete the Object return TRUE; Loading was successful}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.