Programming with DirectX : Shading and Surfaces - Types of Textures

1/15/2011 9:44:11 AM


A 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:

  • 1D textures

  • 2D textures

  • 3D textures

  • Cube maps

  • Sphere maps

1D Textures

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 table.

2D Textures

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.

In 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

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:

  • Volumetric fog

  • Volumetric clouds

  • Various other environmental effects such as 3D textures used to light an environment

  • Visualization for scientific analysis instruments

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 budget.

Cube and Sphere Maps

A 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.

  •  iPhone 3D Programming : Adding Shaders to ModelViewer (part 2)
  •  iPhone 3D Programming : Adding Shaders to ModelViewer (part 1) - New Rendering Engine
  •  iPhone 3D Programming : Adding Depth and Realism - Shaders Demystified
  •  Programming with DirectX : Transformation Demo
  •  Programming with DirectX : View Transformations
  •  Programming with DirectX : World Transformations
  •  Programming with DirectX : Projection Transformations
  •  iPhone 3D Programming : Adding Depth and Realism - Lighting Up (part 2)
  •  iPhone 3D Programming : Adding Depth and Realism - Lighting Up (part 1)
  •  iPhone 3D Programming : Adding Depth and Realism - Surface Normals (part 2)
  •  iPhone 3D Programming : Adding Depth and Realism - Surface Normals (part 1)
  •  iPhone 3D Programming : Adding Depth and Realism - Filling the Wireframe with Triangles
  •  iPhone 3D Programming : Adding Depth and Realism - Creating and Using the Depth Buffer
  •  iPhone 3D Programming : Adding Depth and Realism - Examining the Depth Buffer
  •  iPhone 3D Programming : HelloCone with Fixed Function
  •  iPhone 3D Programming : Vector Beautification with C++
  •  jQuery 1.3 : An image carousel
  •  jQuery 1.3 : Headline rotator
  •  Silverlight : Print a Document
  •  Silverlight : Capture a Webcam
    Most View
    Rosewill Launches Armor Evolution Mid-Tower Case
    Keep Kids Online Safely (Part 1)
    Extending the Real-Time Communications Functionality of Exchange Server 2007 : Installing OCS 2007 (part 2)
    Buyer’s Guide: e-Readers That Fits Your Needs Best (Part 1)
    Thunderbolt External Drives (Part 1)
    Apple iPhone 5 - Fails To Return To The Top (Part 2)
    Syncing And Streaming (Part 2) - Apple TV, The remote app
    Preparing Your Windows 8 PC : Adding Devices in Windows 8 (part 1) - Viewing Installed Devices
    Group Test: Free Office Suites (Part 3) - LibreOffice
    Turn An Old Computer Into A Server Using Ubuntu (Part 2)
    Top 10
    The NZXT Kraken X40 Compact Liquid Cooler Review (Part 3)
    The NZXT Kraken X40 Compact Liquid Cooler Review (Part 2)
    T-Mobile’s Samsung Galaxy Note II Review (Part 6)
    T-Mobile’s Samsung Galaxy Note II Review (Part 5)
    T-Mobile’s Samsung Galaxy Note II Review (Part 4)
    T-Mobile’s Samsung Galaxy Note II Review (Part 3)
    T-Mobile’s Samsung Galaxy Note II Review (Part 2)
    T-Mobile’s Samsung Galaxy Note II Review (Part 1)
    Sony Cybershot DSC-TF1 - Affordable Water-Resistant Camera
    Buffalo MiniStation Slim 500GB External Hard Drive