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 |
                        LR_CREATEDIBSECTION);

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, 
                        LR_CREATEDIBSECTION);

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.

Downloads

Download demo project - 39 Kb

History



Comments

  • 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=http://rxhealth.pw ] gnc viagra alternative [/url] find much more How Erectile Dysfunction Art

    Reply
  • lkrzhphw

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

    vokajqse http://handtaschelouisvuittonsalz.de/ iyylgzwc http://hollisteroutletukoffzget.co.uk/ mihbxhhm http://thomassabocharmssaleofraus.com/ hxuawugl http://buypoloralphlaurenoffrz.fr/ qrlkqink http://hotzlouisvuittontaschenbillig.de/ 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.

    Reply
  • zztbfkiw

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

    vnvehzfl http://handtaschenlouisvuittonbietenoff.de/ ghrnehsv http://hollistercoshippingoffr.co.uk/ gtioxlyy http://thomassaboonlineshippingzaus.com/ cwhuyzky http://louisvuittonsonnenbrilleverkauff.de/ jymdminz http://poloralphlaurengetoffs.fr/ 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.

    Reply
  • hoabodin

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

    xhpiwggh http://louisvuittonoutletjetztoffz.de/ vgdbgbkn http://abercrombieandfitchoutlettz.co.uk/ ikxxmhwx http://louisvuittonhandtaschenoffz.de/ vrgmfzlb http://getithollisteruk.co.uk/ snlsorlc http://thomassabocharmsbuyitaus.com/ 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.

    Reply
  • wkcaixhf

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

    djikkbzl http://oakleysunglassesaustraliao.co.uk/ hlxdmmzj http://casquebeatspascherq.fr/ tuyseowv http://louisvuittonwalletet.com/ vzkqvlon http://hollisteroutletlet.co.uk/ opscbmyc http://todsbagndrivingshoese.com/ 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.

    Reply
  • qnvpvhnc

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

    qqyqpxuk http://oakleysunglassesaustraliao.co.uk/ ozcfliyc http://casquebeatspascherq.fr/ jrszsrus http://louisvuittonwalletet.com/ wrztynjc http://hollisteroutletlet.co.uk/ ivhuzirv http://todsbagndrivingshoese.com/ 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.

    Reply
  • viuqhnof

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

    czclkfou http://casquebeatsbydrec.fr/ ffjkefqe http://louisvuittonspeedy3035y.com/ wqrtcnhk http://hollistererruk.co.uk/ ywlidjko http://hollisteruker.co.uk/ hjcjewjo http://todsshoesess.com/ wprjslov casque beats by dre uqftught louis vuitton speedy 35 ughjmlgx hollister uk rwosuhym hollister uk gjseqyul tods shoes

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

    Thanks,
    Latha.

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

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

    Originally posted by: Anonymous

    Hi,

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

    Reply
  • 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)?

    Svet

    Reply
  • Loading, Please Wait ...

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

Top White Papers and Webcasts

  • Live Event Date: November 20, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Are you wanting to target two or more platforms such as iOS, Android, and/or Windows? You are not alone. 90% of enterprises today are targeting two or more platforms. Attend this eSeminar to discover how mobile app developers can rely on one IDE to create applications across platforms and approaches (web, native, and/or hybrid), saving time, money, and effort and introducing apps to market faster. You'll learn the trade-offs for gaining long …

  • IBM Worklight is a mobile application development platform that lets you extend your business to mobile devices. It is designed to provide an open, comprehensive platform to build, run and manage HTML5, hybrid and native mobile apps.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds