【问题标题】:Loading and Converting a HBITMAP to an OpenGL Texture加载 HBITMAP 并将其转换为 OpenGL 纹理
【发布时间】:2014-04-25 19:10:38
【问题描述】:

我想从资源文件中加载 HBITMAP 并将其用作 OpenGL 纹理。我使用的代码:

HBITMAP hBmp = (HBITMAP) LoadImage(hInstance, 
            MAKEINTRESOURCE(id), IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION);
BITMAP BM;
GetObject(hBmp, sizeof(BM), &BM);

glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
GLvoid* bits = BM.bmBits;
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, BM.bmWidth,
        BM.bmHeight, 0, GL_BGRA_EXT,
        GL_UNSIGNED_BYTE,
        bits);

但我总是从 Visual Studio 收到一个错误,即我无权访问指针 bits。错误在代码的最后一行:

bits);

我实际上可以使用 NULL 指针而不是 bits 而不会出错。我可以使用消息框输出bits。有人知道我的代码有什么问题吗?

【问题讨论】:

    标签: c++ winapi opengl bitmap hbitmap


    【解决方案1】:

    来自GetObject 文档:

    如果hgdiobj 是通过任何其他方式创建的位图的句柄,GetObject 仅返回位图的宽度、高度和颜色格式信息。可以通过调用GetDIBits或GetBitmapBits函数获取位图的位值。

    在上下文中,“其他方式”是指CreateDIBSection 以外的任何内容。你没有使用CreateDIBSection,你使用的是LoadImage。 LR_CREATEDIBSECTION 标志将您归入哪个类别尚不清楚,但解决方法很明确:使用GetDIBits。

    【讨论】:

      【解决方案2】:

      适用于 Windows XP 上的 GLUT 的有效解决方案 (tcc 或 lcc 编译器)

      GLuint LoadTexture(GLuint tex, const char * filename)
      {
      HBITMAP hBitmap;
      BITMAP bm;
      HINSTANCE hInstance = GetModuleHandle(NULL);
      
      //standard bmp 24 bit
      //supported resolutions 64x64, 128x128, 256x256, 512x512
      
      //type "char" has a 1 byte size, other types take more byte and will not work
      unsigned char * data;
      unsigned char R, G, B;
      
      //LoadImage() loads the bmp picture as an interlaced image
      hBitmap = LoadImage(NULL, filename, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE|LR_CREATEDIBSECTION);
          
      GetObject(hBitmap, sizeof(BITMAP), &bm);
      
      //get the address of the start of the image data in memory 
      data =  bm.bmBits;
      
      //swap R, G, B values for correct color display
      
      int index, i;
      
      for (i = 0; i < bm.bmWidth * bm.bmHeight ; i++)
          {
          index = i*3;
          B = data[index]; G = data[index+1]; R = data[index+2];
          data[index] = R; data[index+1] = G; data[index+2] = B;
          }
      
      //print image parameters
      printf ("bmType %u\n",bm.bmType); 
      printf ("bmWidth %u\n",bm.bmWidth); 
      printf ("bmHeight %u\n",bm.bmHeight); 
      printf ("bmWidthBytes %u\n",bm.bmWidthBytes); 
      printf ("bmPlanes %u\n",bm.bmPlanes); 
      printf ("bmBitsPixel %u\n",bm.bmBitsPixel); 
      printf ("bmBits %p\n",bm.bmBits);
      printf ("hInstance %p\n",hInstance);
      
      //create texture from loaded bmp image 
      glGenTextures( 1, &tex);
      glBindTexture( GL_TEXTURE_2D, tex);
      
      glTexImage2D(GL_TEXTURE_2D, 0, 4, bm.bmWidth, bm.bmHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, bm.bmBits);
      
      printf ("--- texture %u created ---\n", tex);
      
      //texture filtering
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
      glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
      glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
      glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
      }
      
      void init(void)
      {
      //enable texturing
      glEnable(GL_TEXTURE_2D);
      //load texture from bmp 24 bit image file
      //bmp exported from mspaint for windows xp
      LoadTexture(1, "image1.bmp");
      LoadTexture(2, "image2.bmp");
      LoadTexture(3, "image3.bmp");
      LoadTexture(4, "image4.bmp");
      LoadTexture(5, "image5.bmp");
      LoadTexture(6, "image6.bmp");
      . . . . . . . . . . . . . . . . . . . .
      

      正在编译。

      没有西里尔名称,"" 表示带有空格的名称

      tcc C:\tcc\src\box\box.c -o C:\tcc\src\box\box.exe -LC:\tcc\lib -luser32 -lgdi32 -lopengl32 -lglu32 -lglut32 -Wl, -subsystem=console

      (没有控制台 -Wl,-subsystem=windows)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-06-02
        • 1970-01-01
        • 2017-06-18
        • 1970-01-01
        • 1970-01-01
        • 2020-05-08
        • 1970-01-01
        • 2011-02-09
        相关资源
        最近更新 更多