WIPIVERSE

Wrapping (graphics)

In computer graphics, wrapping is the process of limiting a position to an area. A common real-world analogy is wallpaper, where a single pattern is repeated indefinitely over a wall. Wrapping is used in 3D computer graphics to repeat a texture over a polygon, eliminating the need for large textures or multiple polygons.

Mathematical Definition

To wrap a position x to an area of width w, the wrapped value x′ is calculated as:

x′ = x mod w

For a general range from x_min to x_max, the wrapped value x′ of x can be expressed as:

x′ = x − ⌊(xx_min) / (x_maxx_min)⌋ · (x_maxx_min)

Implementation

In texture mapping, wrapping determines how texture coordinates (UV coordinates) are interpreted when they fall outside the standard [0, 1] range. Common wrapping modes include:

  • Repeat (Wrap): The texture tiles infinitely, repeating the pattern across the surface. This is equivalent to taking the fractional part of the texture coordinate.
  • Mirrored Repeat: The texture tiles but alternates between normal and mirrored orientation, creating seamless seams at tile boundaries.
  • Clamp (to edge): Coordinates outside the [0, 1] range are clamped to the nearest edge value, stretching the edge pixels outward.
  • Clamp (to border): Coordinates outside the [0, 1] range are rendered using a user-specified border color.

Pseudocode for General Wrapping

function wrap(X, Min, Max: Real): Real;
    X := X - Int((X - Min) / (Max - Min)) * (Max - Min);
    if X < 0 then
        X := X + Max - Min;
    return X;

Pseudocode for Wrapping to [0, 1]

function wrap(X: Real): Real;
    X := X - Int(X);
    if X < 0 then
        X := X + 1;
    return X;

A branchless version for the [0, 1] range:

function wrap(X: Real): Real;
    return ((X mod 1.0) + 1.0) mod 1.0;

Applications

Wrapping is fundamental to texture mapping in real-time graphics APIs such as OpenGL, Direct3D, and Vulkan. By setting the texture addressing mode to "wrap" (or "repeat"), a small texture can be tiled across a large surface, which conserves memory and reduces the need for high-resolution textures. Environment mapping (reflection mapping) also commonly uses texture wrapping to create the appearance of reflective surfaces.

Browse

More topics to explore

    Browse all articles