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 |