Using 24-bit Bitmap Files as OpenGL Images


Desktop-as-a-Service Designed for Any Cloud ? Nutanix Frame

Environment: Visual C++ 5.0, NT4, Oxygen GMX2000 graphics accelerator

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.

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

To load an image from a file use:

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

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:

        HBITMAP hBmp = (HBITMAP) ::LoadImage (AfxGetResourceHandle(), 
                        MAKEINTRESOURCE(IDB_BITMAP1), IMAGE_BITMAP, 0, 0, 

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.

        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:

        glPixelStorei(GL_UNPACK_ALIGNMENT, 4);

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:

        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 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.

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


Download demo project - 39 Kb



  • Can we control lighting of the texture?

    Posted by Legacy on 01/29/2004 08:00am

    Originally posted by: Latha

    I am new to Graphics. I am looking for the code, which can control lighting for the image.

    I downloaded this project. I noticed that without the image, light control works fine on sphere. But after the bmp is loaded, changed light position does not have any effect!
    Can we control lighting of the loaded image?

    Actually I have to control the light for the image captured from the camera. I would appreciate your suggestions on this.


  • The code doesn't show any bitmap on the view

    Posted by Legacy on 01/22/2004 08:00am

    Originally posted by: Anonymous


    I tried running your code as you provided in the zip file and it was able to load the file but doesn't show any bitmap on the view. The image is not drawn or rendered at all on the view.

    Any hints will be good.

  • Borland 5.5 free compiler problem with openGL libraries

    Posted by Legacy on 08/08/2003 07:00am

    Originally posted by: Svet

    Compiling with the Borland C++ 5.5 free compiler a code using the openGL library "glaux.lib" I have the same problem like a lot of people: The linker cannot find a procedure included into it, even if the file exists in the right place, even with includes and pragmas and everything that is suggested to do. I was told about a DLL implementation but I cannot find such a library. Any suggestions (PLEEEEASE)?


  • how to use mfc application to open dicom data and image

    Posted by Legacy on 05/29/2003 07:00am

    Originally posted by: peifa

    i do not know how to use mfc to open the dicom data.
    pls help mi, if u know the ans. thank u.....

  • Do not forget to delete the bmp object

    Posted by Legacy on 09/04/2002 07:00am

    Originally posted by: David Pan

    Hi guys,

    The demo programme works quite well. Just a small problem, there will be some memory leakage.

    Please add the following line to the end of the function and the condition will get better.

    BOOL CGLDibTestView::LoadTexture(LPCTSTR fileName)


  • how to load DICOM image into my MFC application

    Posted by Legacy on 08/13/2002 07:00am

    Originally posted by: carrie

    any one help me how to load DICOM image into my 
    MFC application?
    how get information of pixel in that DICOM image?
    if any one find solution please share with me.

  • how can we work to make a word processer like a note pad

    Posted by Legacy on 06/03/2002 07:00am

    Originally posted by: farrukh

    i want to know abt the techniques to make a note pad in c++.
    it would b kind of u if u tell me abt tht.i shall be highly obliged.
    thanking u

  • Thanks

    Posted by Legacy on 03/12/2002 08:00am

    Originally posted by: lvhaojin

    hehe,It is very useful.I was worry about the .bmp can't be load in my project before I saw the article. Now I can do it with the code's help

  • OpenGL

    Posted by Legacy on 11/15/2001 08:00am

    Originally posted by: Jonny

    Thanks very much for this info. The ideas worked straight away and saved me from chasing down MSDN and fiddly code tests!


  • Thanks.

    Posted by Legacy on 11/11/2001 08:00am

    Originally posted by: Michael

    Thanks. I've been looking for a simple solution like this. Worked well for me. And very easy.

  • Loading, Please Wait ...

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date