From "Norm's DirectX Tips":
textures with transparent backgrounds and mipmapping:
use point sampling for the mipfilter when loading the texture. D3DX_FILTER_NONE is probaly undefined as a filter for loading textures and generating mipmaps, as its the flag used to turn mipmaps OFF! using D3DX_FILTER_NONE results in directx copying a portion of each mipmap (the UL 1/4 ?) to the next level down, with no scaling or filtering or anything. this somehow results in textures that appear and disappear as their triangles move in relation to the camera. you can see this effect on some plants in oblivion. the upside to this is that with no filtering or sampling, no background color gets blended in, and you dont get any dark edges around the sprite images.
so here's the deal with dark edges, sprites, and mipmapping:
sprite textures drawn without mipmaps tend to "sparkle" as the camera moves. the non-top level mipmaps in a sprite texture tend to have the background color blended into the edges of the image, either when directx makes the mipmaps, or when it samples them to texture a triangle.
when directx creates and / or uses mipmaps, it has a tendancy to blend the keycolor or background color into the pixels at the edge of the sprite image. linear, box, and triangle filters do this quite noticeably, point filtering only somewhat. playing with alpaha testing doesnt help much, as it only controls how diretcx draws these already messed up textures. weither the texture is colorkeyed, or has a black and white alpha channel or a grayscale alpha channel makes little difference. making your own mipmaps with black and white alpha helps very little, as directx still blends between miplevels.
about the best you can do, is set the alpha test high (i use 128), and use textures that aren't too dark. the high alpha test filters out the black edges pretty well.
but ideally the apha test should be "if greater than 0, draw". for this, you'd have to go to a LOD scheme, where you had textures at 256,128,64, etc sizes, and draw sprite textured meshes with no mipmaps, using a different size texture based on range (IE do the mipmap manually). but there, its possible you'd still get background color blended into the edges as directx scaled the texture. but maybe not, beacuse you get no black edges on the topmost sprite tex. you only get it on lower mipmap levels.
another technique that showed promise was a texture that filled a 256x256 (stone in my case), and an alpha channel that had a white silouhette of the object (a distant rock). all mipmaps of all levels of this texcture were solid stone, with no background color, guaranteeing that the background color couldn't get sampled and blended in.
setting min and mag filters to point filtering as opposed to anisotropic filtering may also help make sprite textures look better.
i was using sprite textures to draw billboards of distant rocks and plants. i kept moving the clip distance for meshes further out. i started at meshes up to 100 range, then billboards til 350 range. i got as far as meshes to 250 or 300 range and the rest billboards, and decided to just get rid of the billboards and draw all meshes out to 350 range.
=================================================================================
here's what i do in my current project:
// load sprite texture
void Zloadspritetex3(char *s) // as loadtex, w/ color key, and point mipfilter
{
HRESULT h;
char s2[100];
h=D3DXCreateTextureFromFileExA(Zd3d_device_ptr,s,
D3DX_DEFAULT,D3DX_DEFAULT, // width,height
0, // mip lvls. 0 or default = full chain, 1= no mip maps.
0, // usage: not dynamic, not render tgt.
D3DFMT_A8R8G8B8,D3DPOOL_DEFAULT,
D3DX_FILTER_NONE, // image filter
D3DX_FILTER_POINT, // mip filter. none=vanishing mipmaps problem. point=wrap problems.
// linear,tri,and box blend the keycolor into the edges of the image (dark edges problem).
// no mips causes sparkle problem.
0xFF000000, // 0XFF000000, color key
NULL,NULL,&(Ztex[numtextures].tex));
if (h != D3D_OK) { strcpy_s(s2,100,"Error loading "); strcat_s(s2,100,s); Zmsg2(s2); exit(1); }
strcpy_s(Ztex[numtextures].name,100,s);
numtextures++;
}
// turn alphatesting on/off
void Zalphatest(int onoff)
{
if (onoff==1)
{
Zd3d_device_ptr->SetRenderState(D3DRS_ALPHATESTENABLE, TRUE);
Zd3d_device_ptr->SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_GREATEREQUAL); // D3DCMP_NOTEQUAL
Zd3d_device_ptr->SetRenderState(D3DRS_ALPHAREF,(DWORD)Zalphatestlvl); // (DWORD)0x00000000
}
else
{
Zd3d_device_ptr->SetRenderState(D3DRS_ALPHATESTENABLE,FALSE);
}
}
unless you want to generate your own mipmaps that don't blend the background into the visible image edges, this is about the best you can do.
I also read about one game that first did alphatest pass, then an alphablend pass to draw the fine edges of the visible image that get truncated in a simple alphatest.
Hope this helps!
Norm Barrows
Rockland Software Productions - Building PC games since 1988