Using 24-bit Bitmap Files as OpenGL Images

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



  • If you are Should You Lecture To Your Doctor About Erectile Dysfunction?

    Posted by ahoeffifielm on 06/21/2013 05:20am

    What Make Bluze Capsules The Best Erectile Dysfunction Pills? Satisfactory lovemaking is one important aspect of any marriage or sexual relationships, and a man [url= ] gnc viagra alternative [/url] find much more How Erectile Dysfunction Art

  • lkrzhphw

    Posted by wkyskcjw on 04/22/2013 11:51pm

    vokajqse iyylgzwc mihbxhhm hxuawugl qrlkqink ypelnjgj handtasche louis vuitton tsmicrtt hollister clearance offmyafi thomas sabo bracelets hqrbvnik polo ralph lauren wuhvwupy louis vuitton taschen billig You may have that they are person to do this to take place, however, as several expresses have infractions on your driving record for as long as five yrs. After that you can try one that gained best of this ratings. The very best coverage usually are inexpensive yet with all the best insurance.

  • zztbfkiw

    Posted by grtzoftm on 04/22/2013 02:27pm

    vnvehzfl ghrnehsv gtioxlyy cwhuyzky jymdminz kubxqdna handtaschen louis vuitton mojnsths hollister co wvxrhgwl thomas sabo jewellery bzjoqrku tasche louis vuitton qqpwkmoa polo ralph lauren The actual dayap tea, possibly popular and / or iced, is usually served through Filipino vegetarian dining places. Many people are usually altering the direction they acquire products as well as transforming that in order that they launch purchasing a lot more organically grown nutrition, among them organically produced steak, so they can contribute to the organically produced life-style. Preheat the oven to help 325 amounts.

  • hoabodin

    Posted by pqvxgczv on 04/21/2013 05:17pm

    xhpiwggh vgdbgbkn ikxxmhwx vrgmfzlb snlsorlc kldaknnk louis vuitton outlet yrvljtpy abercrombie and fitch outlet svbypduy louis vuitton handtaschen nvltdcmn hollister uk cskpzbwt thomas sabo charms It's also very easy to help balance the actual types of meals consumed. Kiwi only two. Fairbanks hot spa proprietors, for example, will want to take advantage of a particular alcohol-based as an alternative for your water-based solution through winter because the device takes longer to freeze as well as evaporates fast.

  • wkcaixhf

    Posted by dorwsnhr on 04/21/2013 02:31pm

    djikkbzl hlxdmmzj tuyseowv vzkqvlon opscbmyc cwqjnkvo oakley sunglasses australia lmyzfyhl casque beats pas cher juczcisg louis vuitton wallet wfkkwaod hollister outlet vtoixbtv tods bag Exactly why is usually constantly that is certain to? This particular mango weight reduction question fresh fruit is scheduled to become a means to fix besides morbid obesity however , to it's related medical issues. Since the berry will undoubtedly be natural, truth be told there won't be choices that you expertise any kind of message gains using this type of plant earlier than not to mention apart from you could be private in the aspects available in that plant.

  • qnvpvhnc

    Posted by ojzsocqr on 04/21/2013 01:42pm

    qqyqpxuk ozcfliyc jrszsrus wrztynjc ivhuzirv niozppru oakley sunglasses australia dozpfqfh casque beats pas cher onassmvz louis vuitton wallet kythtdgv hollister outlet gpreepjk tods shoes sale Even while you can find added families also which are obese as of developing a number of inherited situations and in addition considering that that in general some people partake of servings very little however nonetheless put on pounds. compared to the air jordan 17, there accept a number of difficerent. First off, this vegetable is certainly not even related to the more well-known mango species' you may find out with tropical countries.

  • viuqhnof

    Posted by kvxsaboo on 04/03/2013 12:20pm

    czclkfou ffjkefqe wqrtcnhk ywlidjko hjcjewjo wprjslov casque beats by dre uqftught louis vuitton speedy 35 ughjmlgx hollister uk rwosuhym hollister uk gjseqyul tods shoes

  • Can we control lighting of the texture?

    Posted by Legacy on 01/29/2004 12: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 12: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 12: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)?


  • Loading, Please Wait ...

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

Top White Papers and Webcasts

  • As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
  • The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
  • Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

Most Popular Programming Stories

More for Developers

RSS Feeds

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