GLTex Demos How to Use DIBs for Texture Mapping
ID: Q148301
|
The information in this article applies to:
-
Microsoft Win32 Software Development Kit (SDK), used with:
-
Microsoft Windows NT 3.51
-
Microsoft Windows 95
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:
- "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_<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 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