Microsoft KB Archive/148301

{|
 * width="100%"|

GLTex Demos How to Use DIBs for Texture Mapping

 * }

Q148301

-

The information in this article applies to:


 * Microsoft Win32 Software Development Kit (SDK), used with:
 * Microsoft Windows NT Server version 3.51
 * Microsoft Windows NT Workstation version 3.51
 * Microsoft Windows 95
 * Microsoft Windows 2000 Advanced Server
 * Microsoft Windows 2000 Server
 * Microsoft Windows 2000 Professional

-

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

"Gltextur.exe" For additional information about how to download Microsoft Support files, click the article number below to view the article in the Microsoft Knowledge Base:

"Q119591 How to Obtain Microsoft Support Files from Online Services" Microsoft used the most current virus detection software available on the date of posting to scan this file for viruses. Once posted, the file is housed on secure servers that prevent any unauthorized changes to the file.

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