Topic : Mip-Mapping in Direct3D
Author : Johnathan Skinner
Page : 1 Next >>
Go to page :

Mip-Mapping in Direct3D
by Johnathan Skinner


For those of you who don't know, mip-mapping is a form of anti-aliasing that is used in many 3D rendering engines. It prevents the well-known interference pattern that occurs on detailed textures, known as the 'moiré pattern'. This happens because when the texture is far enough away, there are more texels (texture pixels) then there are pixels to be rendered on the screen. This means that some texels get skipped over, and visual information is lost.

In a properly anti-aliased rendering, what would happen is all of the texels that land within a single pixel on the screen would be weighted, summed and a final average value is placed on the screen. This could be very processor intensive... just imagine being far away from a small box that has a 256 x 256 texture on it. If this box only covers an 8 x 8 pixel area on the screen, that's 1024 texels per pixel! You would have to sum up and average all those texels just to render one pixel on the screen!!! Obviously this isn't going to happen in real-time.

The idea of mip-mapping is simple: if what you are drawing is really big then use a big texture, and if it's small, use a small texture. The great thing about using a smaller texture when needed is that the texel colours can be averaged from the higher resolution texture. And if you use a texture that has a less or equal number texels for the pixels to be rendered, then there is no moiré pattern.

Mip-Map Factory

Say you have a detailed texture of size 128 x 128. If you down-sample it by a factor of 2 simply by taking the average of every 2 x 2 texel area, you end up with the same texture at 64 x 64, just with less detail. But you won't need the detail because you will only view it from further away. This becomes our level one mip-map (the original texture is refered to as level zero). If you repeat the process on your newly generated texture, then you get level two, and so on. Generally you stop at a smallest of a 2 x 2 texture (after that, you just have a single solid colour).

Mip-maps generated by averaging 2 x 2 texel areas.
Below is some sample code for generating the image data for the mip-maps.

  /* example C code for generating mip-maps (recursive function) */
  /* NOTE: this is untested (and un-optimal ;) */

  void MakeMipMaps( Texture *parentTexture)
    int width = parentTexture->width / 2;
    int height = parentTexture->height / 2;
    Texture *newTexture;
    int u, v;
    int texel;

    /* we want to stop recursing after 2 x 2 map */
    if (width < 2) return;
    if (height < 2) return;

    newTexture = CreateTexture( width, height);

    /* find the new texture values */
    for (u = 0; u < width; u++)
      for (v = 0; v < height; v++)
        /* sum up 2 x 2 texel area */
        /* for simplicity of example, this doesn't seperate
           the RGB channels like it should */
        texel = GetTexel( parentTexture, u * 2  , v * 2  )
              + GetTexel( parentTexture, u * 2 + 1, v * 2  )
              + GetTexel( parentTexture, u * 2  , v * 2 + 1)
              + GetTexel( parentTexture, u * 2 + 1, v * 2 + 1);

        /* take the average */
        texel /= 4;

        PutTexel( newTexture, u, v, texel);

    /* make a link to the mip map */
    parentTexture->mipMap = newTexture;

    /* recurse until we are done */
    MakeMipMaps( newTexture);

The Rendering Pipeline

Since this article is directed to people using Direct3D, I won't be getting into specific rendering algorithms, but it's always good to have a general idea of what's going on in the lower levels.

In order to render a textured polygon, the rendering engine needs to know how many texels it has to advance to get to the pixel beside it, and how many to get to the line below it. For it to choose a mip-map, it checks if either of these values is greater than 1.0, which means there are texels that will potentially be skipped over, so instead, use the next level (lower resolution) mip-map. Repeat this check until the values are both less than or equal to 1.0, or the lowest resolution mip-map is reached, and you have the one that will be used. And of course, for each time the next level is selected, the texel coordinates are divided by two. This process may sound like an expensive operation, but in the rendering pipeline it can be optimized to simple non-iterative instructions. Granted though, it generally is slower than regular texture mapping (but the results are worth it!).

There are also other algorithms used to figure out where the mip-maps go, such as spliting the polygons at boundries based on the surface angle and distance from the view point, but they all produce basically the same results (though some are faster in certain situations).

This demonstrates where the different mip-map levels are used by inverting the texture colour at each level.
Mip-maps usually can be bilinear filtered just like any other texture, but most 3D hardware also allows for a third level of linear filtering on mip-maps. This level will fade smoothly between the mip-map levels instead of having a sharp break (the break usually isn't very noticable though because you are going between two textures of the same colour composition).

Direct3D Implementation

I am going to assume that you already know how to initialize Direct3D and get textured polygons on the screen. If you don't, there are plenty of good tutorials already written about that, so please refer to those. This will just give you the additional steps required to get your polygons mip-mapped.

The first step is to allocate your mip-maps. This is done in the CreateSurface() call for your texture. By specifying the DDSCAPS_MIPMAP and DDSCAPS_COMPLEX capabilities for your surface, the driver will automatically allocate the appropriate mip-maps and attach them to your surface. You will also need to specify the DDSD_MIPMAPCOUNT flag and set the number of mip-map levels that you want (remember that the original texture also counts as a level).

  /* allocates a texture surface with optional mip-maps */
  /* NOTES: - texture width and height need to be powers of 2 */
  /*        - as a general rule, it's good to have width = height for textures */
  /*        - this is untested (my code is in C++ classes so I re-wrote it here
              in C for simplicity, so there may be some subtle differences or
              typos that prevent it from compiling) */

                                        int width, int height,
                                        int mipmap,
                                        DDPIXELFORMAT *pixelFormat)
    LPDIRECTDRAWSURFACE surface;  /* pointer to surface to be created */
    DDSURFACEDESC ddsd;           /* description of surface to create */
    DWORD mipLevels

Page : 1 Next >>