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 The Up in the images Color Table Which is a bit more completed.
Using the Windows bitmap in OpenGL as a material map, the limit is only 24-bit bitmap, you must use a color table with other types of bitmaps
Use WindowAPI to load image
You can load a bitmap using the windows api function loadimage.
To load an image from a file us:
From
Hbitmap HBMP = (Hbitmap) :: loadImage (null,
(Lpctstr) filename, image_bitmap, 0, 0, lr_loadfromfile |
LR_createdibsection;
..................... ..
To load an image from the resources use:
Hbitmap HBMP = (Hbitmap) :: loadImage (AfxgetResourceHandle (),
MakeintResource (idb_bitmap1), image_bitmap, 0, 0,
LR_createdibsection;
WHERE IDB_BITMAP1 is The id of the bitmap resource.
Note That Bitmaps Created in The De 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.
Bitmap BM;
:: GetObject (HBMP, SIZEOF (BM), & BM);
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:
GLPIELSTOREI (GL_Unpack_alignment, 4);
This is the default setting on most systems.
In Order for OpenGL to Undr Use GL_BGR_EXT INSTEAD OF GL_RGB in Your Call To Glteximage2D or Glubuild2DMIPMAPS:
Glubuild2DMIPMAPS (GL_Texture_2D, 3, Bm.Bmwidth, BM.Bmheight,
GL_BGR_EXT, GL_UNSIGNED_BYTE,
BM.BMBITS;
Note That if you use glteximage2d instead of glacket2dmipmaps you'll, to scale the Image so...................
....................... ..
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.