• No results found

Compressed Textures and the DXTex Tool

这里只有一组顶点数据。多组顶点数据表示一个顶点的多个数据项。

Chapter 11: Texturing Overview

11.11 Compressed Textures and the DXTex Tool

The (video) memory requirements for textures add up quickly as your virtual worlds grow with hundreds of textures (remember we need to keep all these textures in video memory to apply them). To help alleviate memory overuse, Direct3D supports five compressed texture formats: D3DFMT_DXT1, D3DFMT_DXT2,

D3DFMT_DXT3, D3DFMT_DXT4, and D3DFMT_DXT5 (you can look up the specifics of these formats in the documentation). If the graphics device supports the compressed format as a texture (check if the format is a supported D3DRTYPE_TEXTURE resource with CheckDeviceFormat ), then the application can create textures of the compressed format, apply them, and they will be decompressed by the hardware automatically during rasterization. (So note that we are not talking about using compressed texture formats to save disk space, as that is not our problem. We want to store compressed texture data on the video card and then decompress it on the fly when it is needed during rasterization.) Note that for the Direct3D compressed texture coordinates, the dimensions must be powers of 2.

D3DXCreateTextureFromFile does not give the option of specifying the texture format (it is taken from the file) of the texture being created. However, D3DXCreateTextureFromFileEx does provide the option of specifying the texture format of the texture being created. So using

D3DXCreateTextureFromFileEx provides one way to create compressed textures. Alternatively, you can save your texture files to the .dds format (a DirectX format), which supports the Direct3D compressed texture formats (in addition to the other Direct3D surface formats). Then D3DXCreateTextureFromFile may be used (it will see from the file the compressed format type).

You may be wondering how you create .dds files, as they are not very common outside of DirectX development. The DirectX documentation documents the .dds file format, so you could always create your own program that imports image data from popular formats and exports them to .dds (you probably do not want to do this).

NVIDIA's developer website (http://developer.nvidia.com/object/nv_texture_tools.html) provides an Adobe Photoshop plug-in that enables Photoshop to export to .dds. The DirectX SDK also ships with a program called DXTex (located at DX9SDK\Utilities\Bin\x86), which can load popular image formats and export to .dds. The DXTex tool can also generate mipmap levels, which can be saved and stored in the .dds format; if you do this, then the mipmap levels do not need to be generated at application initialization time, as they can be directly read from the .dds file.

11.12 Summary

Texture coordinates are used to define a triangle on the texture that gets mapped to the 3D triangle.

We can create textures from image files stored on disk using the D3DXCreateTextureFromFile function.

We can filter textures by using the minification, magnification, and mipmap filter sampler states.

Address modes define what Direct3D is supposed to do with texture coordinates outside the [0, 1] range. For example, should the texture be tiled, mirrored, clamped, etc.?

Multi-texturing refers to the technique of combining several texture maps together to form a net texture.

We can generate texture coordinates that are approximately accurate for objects that are roughly spherical and cylindrical in shape by using a spherical and cylindrical mapping, respectively.

By modifying texture coordinates in some way (e.g., translating, rotating, scaling), and thereby changing which part of the texture gets mapped to the polygon with respect to time, we can animate the textures.

By using a compressed Direct3D texture format such as D3DFMT_DXTl, D3DFMT_DXT2, D3DFMT_DXT3, D3DFMT_DXT4 , or D3DFMT_DXT5 , we can save a considerable amount of video memory.

11.13 Exercises

1. Draw a pyramid-shaped object and apply a segment of the crate texture to each face.

2. Look up D3DXCreateTextureFromFileEx in the SDK documentation and explain each parameter in your own words.

3. Modify the Tiled Ground demo by experimenting with different MaxAnisotropy values. For MaxAnisotropy = 1, MaxAnisotropy = 2, MaxAnisotropy = 4, MaxAnisotropy = 8, and MaxAnisotropy = 16 , record your frame rate. (You should check

D3DCAPS9::MaxAnisotropy to see that you support these levels.) Does it change? Make sure that you record it in a consistent way for each trial (e.g., same camera position and orientation).

4. Modify the Tiled Ground demo by experimenting with different scaling factors to change the texture coordinates (more precisely, experiment with the value set to texScale ). What happens if you tile less and less?

5. Modify the Tiled Ground demo by changing the address modes. Use mirror and clamp instead of the wrap mode. Describe your results. You might want to try using a different texture to see the results more clearly.

6. In this chapter's demo directory there are two textures, as shown in the left and middle images of Figure 11.20. Use multi-texturing to combine them together to produce the image at the right in Figure 11.20. In addition, animate the flare by rotating it as a function of time (rotate the color and grayscale textures at different rates). Display the resultant texture on each face of a cube. (Hint: The center in texture coordinates is not the origin; it is (1/2, 1/2), and thus the rotation will be off because the rotation equations rotate about (0, 0). Therefore, you will need to first translate the texture coordinates so that the center is at the origin, apply the rotation transformation, and then translate back so that the center is back at (1/2, 1/2) for texturing.)

7. Generate texture coordinates for a teapot by using a spherical mapping, and render the teapot with the texture of your choice.

8. Generate texture coordinates for a cone by using a cylindrical mapping, and render the cone with the texture of your choice.

Figure 11.20: Combining textures to make a fireball.