SAMPLE: GLTex Demos How to Use DIBs for Texture Mapping

Last reviewed: August 5, 1996
Article ID: Q148301
The information in this article applies to:
  • Microsoft Win32 Software Development Kit for:

        - Microsoft Windows NT version 3.51
        - Microsoft Windows 95
    

SUMMARY

The GLTex sample (GLTEXTUR.EXE) provides a demonstration of how to use a DIB (device-independent bitmap) as a texture-map for OpenGL by pasting a DIB (chosen by the user) onto all sides of a three-dimensional cube.

GLTex also allows you to modify various texture settings so that you can quickly and easily see the visual effect created.

You can find GLTEXTUR.EXE (size: 91394 bytes) 
                         , a self-extracting file, on these services:

  • Microsoft's World Wide Web site on the Internet

          On the www.microsoft.com home page, click the Support icon
          Click Knowledge Base, and select the product
    
          Enter kbfile GLTEXTUR.EXE (size: 91394 bytes) 
                                   , and click GO!
          Open the article, and click the button to download the file
    
    
  • Internet (anonymous FTP)

          ftp ftp.microsoft.com
          Change to the Softlib/Mslfiles folder
          Get GLTEXTUR.EXE (size: 91394 bytes) 
    
  • The Microsoft Network

          On the Edit menu, click Go To, and then click Other Location
          Type "mssupport" (without the quotation marks)
          Double-click the MS Software Library icon
          Find the appropriate product area
          Locate and Download GLTEXTUR.EXE
    
  • Microsoft Download Service (MSDL)

          Dial (206) 936-6735 to connect to MSDL
          Download GLTEXTUR.EXE (size: 91394 bytes) 
    

    For additional information about downloading, please see the following article in the Microsoft Knowledge Base:

       ARTICLE-ID: Q119591
       TITLE     : How to Obtain Microsoft Support Files from Online Services
    
    

    MORE INFORMATION

    OpenGL for Windows NT and Windows 95 support powerful texture-mapping capabilities. GLTex lets you easily experiment with many (but not all) of these capabilities by using a series of conversions to generate the texture map. It allows the user to modify various OpenGL settings.

    To use a DIB as a texture in OpenGL, you must first convert the DIB into a format that is compatible with OpenGL. Because a 32-bpp DIB is the most closely related to an OpenGL formatted image, you can take the DIB that the user provides and regardless of its format copy it by using StretchDIBits onto a 32-bpp DIB Section. Therefore, from this point on, GLTex can assume that it is working with a 32-bpp DIB Section.

    Note that because a 32-bpp DIB is used, you don't have to worry about the DWORD-alignment of DIBs. 32-bit DIBs are already DWORD-aligned. After ensuring that you have a 32-bpp DIB Section, you must convert that DIB Section into an OpenGL image. This involves converting the BGR format of the DIB into the RGBA format that OpenGL uses. This translation from an "any format DIB" into an OpenGL image is handled by GLTex's DIBtoGLImage function.

    Now that you have an OpenGL image, you must scale it to a format that is 2^n x 2^n. This is required for OpenGL textures. (There is one exception to this rule involving texture borders, but GLTex does not use borders.) You might want to chose to always scale the image to be square because you're mapping onto cube faces. Then GLTex scales the image sides to the closest 2^n by using the utility function gluScaleImage. Finally, this scaled image is set as the texture-image by calling glTexImage2D. This scaling and setting of the texture is handled by GLTex's GLImagetoTexture function.

    When the OpenGL scene is rendered, a simple three-dimensional cube is drawn with the converted and scaled texture mapped onto it. This mapping is taken care of by the glTexCoord function, which is called as the cube vertices are specified. This is handled by GLTex's BuildCube function.

    Settings

    Once you have loaded a DIB and see how it maps to the cube's sides, GLTex allows you to change several settings in order to see how these changes would effect the image. The following list gives modifications that GLTex allows you to make, a brief description of their effect, and which functions are used to change them:

    • "Scene Distance from Viewer" allows you to push the scene farther away or bring it closer in. This is achieved with glTranslate.
    • "Cube Rotation" allows you to modify the angle of rotation around the x, y, and z axes. This is achieved with glRotate for each axis.
    • "Light Position" allows you to move the single light source used. Note that by changing the w coordinate, you can modify whether you are using a directional (w = 0.0) or positional (w = 1.0) light source. This is achieved with glLight(...GL_POSITION...).
    • "Texture Mode" allows you to use two of the three texture modes available with OpenGL. Decal mode essentially means that the texture is applied directly to the object without any calculation of material properties or lighting. Modulate mode blends the underlying object (which is effected by material properties and lighting) with the texture. This gives the effect of a lighted texture. The third possibility, Blending, doesn't make sense for a three-component (RGB) image. This is achieved with glTexEnv.
    • "Perspective Hint" should be changed to Nicest if the texture appears to be projected incorrectly. You should try it anyway because it will allow you to see the difference. This is achieved with glHint.
    • "Minification Filter" controls how a screen pixel should be mapped to a collection of texels (texture elements). This is achieved with glTexParameter(...GL_TEXTURE_MIN_FILTER...).
    • "Magnification Filter" controls how a screen pixel should be mapped to a portion of a texel (texture element). This is achieved with glTexParameter(...GL_TEXTURE_MIN_FILTER...).
    • "Texture Wrap S" and "Texture Wrap T" allow you to specify whether the image should repeat in the S and T directions (these can be thought of as the X and Y directions in the texture's world). This is achieved with glTexParameter(...GL_TEXTURE_<S or T>_WRAP...).
    • "Texture Coordinates" specifies how the texture is mapped to the object. Because 0.0 is the default for the first parameter, one side of the texture is always mapped to the edge of the cube's face. This should be used in conjunction with Texture Wrap S and T for repeating the texture across the cube's faces. This is achieved with glTexCoord.

    See the effects of these settings by experimenting with them in GLTex. When loading large DIBs with high-resolution, be patient, there is a lot of translation going on as described above, so depending on your system, this may take some time.

    REFERENCES

    NOTE: Most of the following references can be found on the Microsoft Developer Network (MSDN) Developer Library CD-ROM. Some of these references are published by publishers independent of Microsoft; we make no warranty, implied or otherwise, regarding the reliability of these resources.

    Code for this GLTex sample was borrowed from the ShowDIB sample for DIB- handling (Dib.c) and from Dale Rogerson's articles/samples for OpenGL palette creation (Glpalette.c).

    For more information on the settings and OpenGL in general, please see the following resource:

    • Neider, Jackie, Tom Davis, and Mason Woo. OpenGL Programming Guide: The Official Guide to Learning OpenGL, Release 1. Reading, MA: Addison- Wesley, 1993. ISBN 0-201-63274-8. (This book is also known as the "Red Book.")

    For more information on DIBs and Texture Mapping in OpenGL, please see the following resources:
    • Rogerson, Dale. "OpenGL V: Translating Windows DIBs." January 1995. (Development Library, Technical Articles)
    • Rogerson, Dale. "OpenGL VII: Scratching the Surface of Texture Mapping." April 1995. (Development Library, Technical Articles)

    For more information on DIBs in general, please see the following resources:
    • Gery, Ron. "DIBs and Their Use.". March 1992. (Development Library, Technical Articles)
    • Gery, Ron. "Using DIBs with Palettes." March 1992. (Development Library, Technical Articles)


  • Additional reference words: 3.51 4.00 image texture mapping
    KBCategory: kbgraphic kbfile kbwebcontent
    KBSubcategory: GdiOpenGL


    THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

    Last reviewed: August 5, 1996
    © 1998 Microsoft Corporation. All rights reserved. Terms of Use.