To access the contents, click the chapter and section titles.
Cutting Edge Direct 3D Programming
(Publisher: The Coriolis Group)
Author(s): Stan Trujillo
ISBN: 1576100502
Publication Date: 11/01/96
Chapter 5 Texture Mapping
- Applying Textures
- Texture Wraps
- Decals
- Transparency
- Texture Animation
This chapter is the first of six that focus on specific Direct3D techniques. As much as possible, each technique is covered with discussion and a demo. For you impatient types (like myself), the demos well be looking at in this chapter are:
- Jade
- Wraps
- Decal
- OrbStar
- TextureDrift
- ShowRoom
The demos in this chapter, along with all of the demos on the CD-ROM, were started with the Direct3D AppWizard, so they all share the same general structure. This allows us to discuss only the portions of each demo that are unique. Chapter 4 documents the overall structure of programs created with the AppWizard.
Texture Mapping Testbed
Before we look at any code, lets talk about our goal. We are going to be mapping textures onto objects. One way to do this is with a texture wrap. A texture wrap is not a visible object, but a method that determines how a texture is applied to a mesh. Direct3D supports three types of texture wraps: flat, cylindrical, and spherical.
One way to experiment with texture wrapping methods is with Xpose, the X file viewer on the CD-ROM. You can use Xpose to rotate and spin a mesh, change its settings, and save the mesh when you are done. Xpose is shown in Figure 5.1.
Figure 5.1 Xpose.
Xpose allows you to apply textures to a mesh. Apply a new texture by selecting the Load option from the Texture menu. You will be presented with an Open File dialog box. You can specify either BMP or PPM files, but the dimensions of the texture must be a power of 2 (16, 32, 64, 128...).
By default, Xpose uses a spherical texture wrap to map textures, but this can be changed with the Texture Wrap Settings dialog. This dialog can be accessed with the Texture|Wrap Settings menu option. The Texture Wrap Settings dialog box appears in Figure 5.2.
Figure 5.2 The Texture Wrap Settings dialog box.
You can adjust any of the texture wrap settings by making changes to the dialog box and pressing the OK button.
Xpose is useful for experimenting with Direct3D features other than texture mapping.
Applying A Texture To A Mesh
The first order of business is applying a texture to an object. Our strategy is to take a demo that is very much like the default project that the Direct3D AppWizard creates and apply a texture to the object that the demo displays.
The Jade Demo
The Jade demo displays a mesh that forms the letters D3D and applies a jade texture to the mesh. For visual effect, and so that you can see the mesh from different angles, the mesh is animated. The Jade demo appears in Figure 5.3.
Figure 5.3 The Jade demo.
The Jade demo uses a flat texture wrap to apply the jade texture to the mesh. This means that the texture is applied straight on the mesh, with no curving or wrapping of the texture. For this reason, when the mesh is viewed from the same direction that the texture was applied, the texture appears much as it does when viewed in a paint program. The texture used in the Jade demo is shown in Figure 5.4.
Figure 5.4 The texture used by the Jade demo.
The Jade demo demonstrates the following techniques:
- Loading meshes and textures from a programs resources. Most of the demos on the CD-ROM require that meshes and/or textures be loaded at runtime. Rather than risk the possibility that these files wont be available, each demo stores the required files as resources. If you want to modify the demos to use files instead of resources, refer to the Sample demo in Chapter 4 or examine the code that the Direct3D AppWizard creates.
- Using the Direct3DRMWrap interface to map a texture onto a mesh.
- Using menu options to change the mesh render settings at runtime.
- Using a callback function to perform animation.
The JadeWin Class
Most the Jade demos functionality is provided by the JadeWin class. The JadeWin class inherits its basic Direct3D functionality from the RMWin class that is documented in Chapter 4. The JadeWin class definition looks like this:
class JadeWin : public RMWin
{
public:
JadeWin();
BOOL CreateScene();
static void MoveFrame(LPDIRECT3DRMFRAME frame, void*, D3DVALUE);
protected:
//{{AFX_MSG(JadeWin)
afx_msg void OnRenderWireframe();
afx_msg void OnRenderFlat();
afx_msg void OnRenderGouraud();
afx_msg void OnUpdateRenderFlat(CCmdUI* pCmdUI);
afx_msg void OnUpdateRenderGouraud(CCmdUI* pCmdUI);
afx_msg void OnUpdateRenderWireframe(CCmdUI* pCmdUI);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
private:
LPDIRECT3DRMMESHBUILDER meshbuilder;
};
Three public member functions are declared: a constructor, the CreateScene() function, and a callback function called MoveFrame(). The constructor is used to initialize the classs single data member:
JadeWin::JadeWin()
{
meshbuilder=0;
}
The CreateScene() function creates and configures the objects that will be used in the scene. The MoveFrame() function is declared static so that it can be installed as a callback by the CreateScene() function. MoveFrame() will be used to alter the orientation of the frame to which the demos mesh is attached. This will cause the mesh to appear at a different orientation for each screen update.
The six protected member functions are message handling functions that support the Jade demos Render menu functionality.
|