使用24位位图作为OpenGL的材质贴图

原创 2005年04月25日 15:26:00

Here is a fairly fast and easy way to use a Windows bitmap file (.bmp file) as a texture map in OpenGL. The limitation is that it only works with 24-bit bitmaps. To work with other bitmaps you need to pick apart the pixel values and look them up in the images color table which is a bit more complicated.

在OpenGL里使用windows位图作为材质贴图,限制是只能是24位位图,要使用其它类型的位图必须使用颜色表

使用windowAPI来装载Image

You can load a bitmap using the Windows API function LoadImage.

To load an image from a file use:

where filename is the name of the bitmap file. According to the documentation this doesn't work on Windows NT but I'm running NT 4.0 and it works fine for me.

To load an image from the resources use:

where IDB_BITMAP1 is the ID of the bitmap resource.

Note that bitmaps created in the DevStudio resource editor are not 24-bit color. You'll need to create your bitmap with some other program (like mspaint).

Once you have a handle to the bitmap you can get information about it with the windows function GetObject. GetObject fills in a BITMAP structure that contains the dimensions, bits per pixel and a pointer to the pixels themselves.

If the bitmap is a 24-bit bitmap then the format that the RGB values stored is not far off from the way OpenGL RGB bitmaps are formed. BM.bmBits points to an array of unsigned chars where each set of 3 bytes represents an BGR value - an RGB value where blue and red are switched. In addition each row of the bitmap must start on a longword boundary so rows are padded with extra bytes at the end to ensure this.

In order for OpenGL to ignore the padding at the end of the rows use:

This is the default setting on most systems.

In order for OpenGL to understand BGR use GL_BGR_EXT instead of GL_RGB in your call to glTexImage2D or gluBuild2DMipmaps:

Note that if you use glTexImage2D instead of gluBuild2DMipmaps you'll have to scale the image so that both dimensions are a power of 2.

The sample program uses these routines to load a bitmap and texture map it onto a sphere. The interesting code is in the LoadTexture method of the view class.

        HBITMAP hBmp = (HBITMAP) ::LoadImage (NULL,
                        (LPCTSTR) fileName, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE |
                        LR_CREATEDIBSECTION);

        if (hBmp == NULL)
        {
          AfxMessageBox("Error: unable to load bitmap file");
          return FALSE;
        }

        // Get bitmap info.
        BITMAP BM;
        ::GetObject (hBmp, sizeof (BM), &BM);

        // Must be 24 bit
        if (BM.bmBitsPixel != 24)
        {
          AfxMessageBox("Error: bitmap must be 24 bit");
          return FALSE;
        }

 // Make the rendering context current
        // (must do b/f any OpenGL calls)
        ::wglMakeCurrent(m_hDC,m_hRC);

        // Generate name for and bind texture.
        if (!m_texName)
        {
              glGenTextures(1, &m_texName);
        }
        glBindTexture(GL_TEXTURE_2D, m_texName);

        // Set up all the the texture mapping params.
        glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
        glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
        glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
        glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST );
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
        glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);

        // Build the texture map
        // Could use glTexImage2D instead but then we would have to scale
        // the image to make it a power of 2.
        gluBuild2DMipmaps( GL_TEXTURE_2D, 3, BM.bmWidth, BM.bmHeight, GL_BGR_EXT, GL_UNSIGNED_BYTE,
              BM.bmBits );

        glBindTexture(GL_TEXTURE_2D, 0);

This code has been tested with VisualC++ 5.0, Windows NT with an OxygenGMX2000 graphics accelerator.

3ds max材质贴图批量重命名插件

  • 2016年06月03日 12:08
  • 14KB
  • 下载

Houdini 如何加材质贴图

建立场景 在Material Palette面板下找到“Basic Surface”材质,拖出来 然后切到Shop面板下,"Basic Color Map"下选择图片 在SO...

3d材质贴图 海上观日出

  • 2011年04月20日 09:23
  • 3.14MB
  • 下载

BRDF 材质贴图

Segment 0 关键词 BRDF 材质,一种材质纹理,用来贴反射光的。 PBS physical based shading, 基于物理成像算法。 microfacet model,一种表面模型,...

3DMAX卧室材质贴图

  • 2011年06月14日 13:23
  • 9.73MB
  • 下载

3ds max 材质贴图

  • 2013年03月16日 19:51
  • 4.88MB
  • 下载

unity开发经验之用材质贴图模拟流体特效

前两天做项目时(unity版本5.3),被一个水泥的流体特效难着了,找了好几个插件,调出的效果都不理想,还花费了好多时间。最后解决的方法是调材质贴图,利用材质贴图的帧动画实现。 我使用了240张贴图...

maya 材质贴图整理插件

  • 2016年07月18日 11:04
  • 10KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:使用24位位图作为OpenGL的材质贴图
举报原因:
原因补充:

(最多只允许输入30个字)