texture is data that is used during the shading process of surfaces to
give them more detail. Textures are usually images that are loaded from
image file formats such as .JPG, .TGA, .BMP, .DDS, and .PNG. The images
themselves are usually created using an application such as Adobe
Photoshop CS 3 or Microsoft Paint. Images that are not loaded from files
can be created using mathematical algorithms, which are common for
textures known as procedural textures. The topic of generating textures
using algorithms is beyond the scope of this book, as they can become
quite mathematically intense and require a lot of background knowledge.
The purpose of this article is to describe how to load textures into Direct3D 10 and how to
apply them to surfaces in real time within our virtual environments.
This is important because textures and their various uses are critical
to many of the graphical techniques commonly used in video games.
Throughout this book, textures will come up repeatedly in the contexts
of various topics, so a firm understanding of them is of the utmost
importance before moving on.
Types of Textures
Several types of
images can be loaded and used in Direct3D. Each of these texture types
has its own purpose, each of which will be discussed in this section.
The types of textures that can be used in Direct3D include the following
texture types, but what each texture type is used for depends on what
purpose the texture serves in the application, which can be different
than storing color information of a surface:
A 1D texture is akin to a
1D array of values. A 1D texture is often used as a look-up table in one
or more shaders. A look-up table in this sense refers to sending an
array of data to a shader so the shader can look up the values in the
array to compute whatever value it is meant to compute. For example,
let’s say you filled in an array with 10 color values. You can then use
some attribute of the vertex—for example, its height—as an array index
into the color array to select the prespecified color. This
can be done by taking the range of the height (where the height falls
within the minimum and maximum possible) as a percentage and then
multiplying that percentage by the size of the array. The integer result
could then be used as an array index to select a color from the look-up
2D textures are the
most common types of textures that you will likely work with in your
game projects. A 2D texture is essentially an ordinary image.
Technically, anything can be stored in a texture that includes
information not used for color.
The best way to understand
a 2D texture as an image is to open up any image file you have on your
computer that has a width and a height. In a 1D texture, the data can be
thought of as rows without columns or widths without heights, but a 2D
texture has both a width and a height. An example of a 2D texture
created in Adobe Photoshop is shown in Figure 1.
Figure 1. A 2D texture.
this chapter we will focus on the loading and rendering of these 2D
texture images. The textures loaded in this chapter will be used to
color a surface to increase the surface details. Textures used in this
manner are known as color maps or decal maps.
3D textures are also
called volume textures. A 3D texture is a texture that has a width, a
height, and a depth. The depth part of the 3D texture is what makes it a
volume rather than a flat slice like a 2D texture image. 3D textures
can be used for the following graphical effects:
3D textures have
traditionally required a lot of processing power and memory in
applications such as video games. Therefore, not many games use many 3D
textures. For example, a 2D texture of 128 × 128 is relatively small,
especially considering that many textures in today’s games exceed the
resolution of 1024 × 1024. A 128 × 128 2D texture has 16,384 pixels. If
each pixel is three bytes in size, then a 128 × 128 2D texture is 49,152
bytes, or almost 50 kilobytes. If you had a 3D texture with the same
size across all dimensions, such as 128 wide, 128 high, and 128 deep,
you would end up with a texture that has 2,097,152 pixels and would be
6,291,456 bytes in size assuming 3 bytes per pixel.
In other words, a 3D
texture with the resolution of 128 × 128 × 128 would be 6 megabytes in
size for a single, relatively low-resolution 3D texture. Imagine how big
a 512 × 512 × 512 3D texture would be. Even at a cubic size of 128, the
difference between 6 megabytes and 50 kilobytes is beyond tremendous.
If you had a 3D texture with a cubic size of 512, more memory (over to
380 megabytes, assuming 24 bits) would be required for that one texture
than some games have for entire game levels. Even if you assumed a
600-megabyte budget of texture data for each game level in your game, a
single 512 resolution 3D texture would consume more than half that
Cube and Sphere Maps
cube map is a collection of six 2D texture images that together often
represent the view of an environment from a point in space. A 3D texture
is a volume, but a cube map is just six 2D images that create not a
volume but rather what is known as an environment map. An environment
map can be created dynamically by placing the camera in the game world
and saving the view’s render as a texture six times for the forward,
backward, up, down, left, and right directions.
Technically the data in a cube
map can be used however you choose, but cube maps are commonly used for
storing the scene’s environment so that other graphical techniques such
as reflection mapping (simulating reflections using textures) can be
performed on objects. An example of a cube map is shown in Figure 2.
A cube map can be six separate images or one large image. Later in this
chapter you’ll see how to create and load cube maps in Direct3D 10.
Figure 2. A cube map.
A sphere map is a 2D texture
image in which the contents are spherical. When sampling from the
texture, specific equations are used to retrieve the information in a
way that allows the sphere map to be used to texture a sphere or some
other object with volume. Sphere maps and cube maps tend to serve the
same purpose, which is to store an environment’s information in an
image. An example of a sphere map is shown in Figure 3.
Figure 3. A sphere map.