gpt4 book ai didi

VC运用OPENGL加载BMP纹理图的实现方法汇总

转载 作者:qq735679552 更新时间:2022-09-28 22:32:09 24 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章VC运用OPENGL加载BMP纹理图的实现方法汇总由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

OpenGL是专业的图形程序接口,功能非常强大,调用方便的底层图形库。本文汇总了运用OPENGL加载BMP纹理图的几个实现方法.

方法一:

首先获取位图句柄:

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

然后根据位图句柄得到位图信息:

?
1
2
BITMAP BM;
::GetObject (hBmp, sizeof (BM), &BM);

最后根据位图信息中的RGB值建立纹理:

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

方法二:

首先用OpenGL辅助库获得位图信息:

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

然后建立纹理 。

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

方法三: 从底层做,需要了解bmp文件的结构,首先读取bmp文件结构,包括文件头、信息头和数据,数据用于后面定义纹理 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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> 这是因为后面的GL_BGR_EXT是定义在这个头文件里的,因为BMP格式是按Blue,Green,Red顺序储存图像数据的,这与OpenGL中正好相反。GL_BGR_EXT就是完成两者之间的转换的.

下面就是不用AUX库来加载BMP图片作为纹理的函数:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
}

最后此篇关于VC运用OPENGL加载BMP纹理图的实现方法汇总的文章就讲到这里了,如果你想了解更多关于VC运用OPENGL加载BMP纹理图的实现方法汇总的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com