HRESULT D3DXCreateTexture(
LPDIRECT3DDEVICE9 pDevice,
UINT Width,
UINT Height,
UINT MipLevels,
DWORD Usage,
D3DFORMAT Format,
D3DPOOL Pool,
LPDIRECT3DTEXTURE9 * ppTexture
);
void createTexture(int texWidth, int texHeight)
{
HRESULT hr = 0;
hr = D3DXCreateTexture(
_device,
texWidth, texHeight,
0,
0,
D3DFMT_A8L8,//纹理格式
D3DPOOL_MANAGED, &_tex);
if(FAILED(hr))
return false;
D3DSURFACE_DESC textureDesc;
_tex->GetLevelDesc(0, &textureDesc);
if(textureDesc.Format != D3DFMT_A8L8)
return false;
D3DLOCKED_RECT lockedRect;
_tex->LockRect(0, &lockedRect,0, 0);
//使用类型要对应(unsigned short --- D3DFMT_A8L8)
unsigned short* imageData = (unsigned short*)lockedRect.pBits;
for(int i = 0; i < texHeight; i++){
for(int j = 0; j < texWidth; j++){
//Pitch数据的总长度
int index = i * lockedRect.Pitch / 2/*D3DFMT_A8L8 二字节*/ + j;
if(i < 64)
imageData[index] = 0x00;
else
imageData[index] = 0xFF;
}
}
_tex->UnlockRect(0);
//保存纹理
hr = ::D3DXSaveTextureToFile("ABC.jpg", D3DXIFF_JPG, _tex, NULL);
if(D3DERR_INVALIDCALL == hr)
::MessageBox(NULL,NULL,NULL,NULL);
}