MSDN Graphics Documentation

Your guide to advanced graphics programming.

Texture Mapping

Texture mapping is a fundamental technique in computer graphics used to add detail, realism, and visual complexity to 3D models without requiring them to have a very high polygon count. It involves applying a 2D image (the texture) onto the surface of a 3D object.

Core Concepts

The process of texture mapping involves several key components:

Texture Coordinates (UV Mapping)

UV coordinates are crucial for defining how a texture wraps around a 3D model. The U coordinate typically represents the horizontal axis of the texture, and the V coordinate represents the vertical axis. A point with UV coordinates (0,0) usually corresponds to the bottom-left corner of the texture, while (1,1) corresponds to the top-right corner. These coordinates are typically assigned to each vertex of the 3D model.

Example of UV mapping on a 3D cube
Illustration of UV coordinates mapping texture to a cube face.

Texture Filtering

When rendering a 3D scene, the relationship between the size of a texel and the size of the pixel it covers on screen can vary greatly. Texture filtering techniques smooth out these transitions.

Mipmapping

Mipmaps (from the Latin 'multum in parvo', meaning 'much in little') are essential for efficient and high-quality texture rendering. They are a set of pre-filtered, reduced-resolution versions of an original texture. When a rendered object is far away, the GPU can use a smaller mipmap level, which requires less memory bandwidth and processing power, while also preventing aliasing artifacts that would occur if a high-resolution texture were heavily downsampled on the fly.

Illustration of different mipmap levels for a texture
Different levels of mipmaps for a single texture.

Shader Implementation

In modern graphics APIs like DirectX or Vulkan, texture mapping is primarily handled within shaders. A pixel shader typically samples the texture using the provided UV coordinates and returns the color to be applied to the pixel.

Example Pixel Shader Snippet (HLSL-like)


struct PixelShaderInput {
    float4 Position : SV_POSITION;
    float2 TexCoord : TEXCOORD0;
};

Texture2D g_Texture;
SamplerState g_SamplerState;

float4 main(PixelShaderInput input) : SV_TARGET {
    // Sample the texture using the interpolated UV coordinates
    float4 textureColor = g_Texture.Sample(g_SamplerState, input.TexCoord);

    // Return the sampled color
    return textureColor;
}
            
Tip: Properly unwrapping your 3D models for UV mapping is crucial. Poor UV layouts can lead to distorted textures and wasted texture space.

Common Texture Mapping Techniques

Note: The choice of filtering method and the use of mipmaps significantly impact both visual quality and rendering performance. Always consider these factors during optimization.

Further Reading