Mip-Mapping in Direct3D
IntroductionFor 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. The ugly 'bitty' look near the top is the moiré pattern in action. 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. Same scene rendered using mip-mapping. Mip-Map FactorySay 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.
The Rendering PipelineSince 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 ImplementationI 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).
The CreateSurface() call here will create the number of surfaces you specify in ddsd.dwMipMapCount, but it only returns a pointer to one surface. The mip-map surfaces are accessed by calling GetAttachedSurface(). There is no need to do anything special in clean-up because all auto-generated surfaces are released with the parent surface. Now that the mip-maps have been created, you will need to put in the texture images. Using the images generated from the code sample in the 'Mip-Map Factory' section of this article, you can copy the images to the mip-maps like so:
Now that the image data for the textures has been loaded, it's pretty much ready to go. The only thing missing is to tell Direct3D that you want it to render the mip-maps. If you were to render it now just like any other texture, it will only use the original full-sized texture. In order to get it to use the mip-maps, we must set the render state D3DRENDERSTATE_TEXTUREMIN to one of the following values:
Throw that into your Direct3D code, and just like that, your textures are mip-mapped. Remember though, that just like with the rest of Direct3D, you will have to check the capabilities of the driver to make sure it supports these render states before you can use them. If for some reason you want to render without the mip-maps, you can always set it back to either D3DFILTER_NEAREST or D3DFILTER_LINEAR.
In ClosingHopefully by now you should have been able to seemlessly integrate mip-mapping into your Direct3D graphics engine, so that some day you can display it to thousands of on-lookers who will say "ooohh, aaahh, smooth..." and stare in wonderment at the absence of moiré patterns :) Johnathan Skinner Discuss this article in the forums
See Also: © 1999-2011 Gamedev.net. All rights reserved. Terms of Use Privacy Policy
|