GLTex Demos How to Use DIBs for Texture Mapping

ID: Q148301


The information in this article applies to:


SUMMARY

The GLTex sample 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.


MORE INFORMATION

The following file is available for download from the Microsoft Software Library:

~ GLTEXTUR.EXE
For more information about downloading files from the Microsoft Software Library, please see the following article in the Microsoft Knowledge Base:
Q119591 How to Obtain Microsoft Support Files from Online Services


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

For more information on DIBs and Texture Mapping in OpenGL, please see the following resources: For more information on DIBs in general, please see the following resources:

Additional query words: 4.00 image texture mapping kbgraphic kbfile


Keywords          : kbfile kbgraphic kbNTOS351 kbSDKWin32 kbWinOS95 GdiOpenGL 
Version           : WINDOWS:95
Platform          : WINDOWS 
Issue type        : kbinfo 

Last Reviewed: June 15, 1999