WJ的Direct3D简明教程3:Create Texture with User-defined Image Data

转载请注明:来自 http://blog.csdn.net/skyman_2001
Usually we use D3DXCreateTextureFromFile() function to create a texture from external file, but how do we create it with user-defined image data? The way is as follows:
1. Create an empty texture:
LPDIRECT3DTEXTURE9 pTexture = NULL;
LPDIRECT3DSURFACE9 pRenderSurface = NULL;

D3DXCreateTexture(pDevice,
256, // width
256, // height
0, // number of mip levels(0 - a complete mipmap chain is created)
0, // usage
D3DFMT_A8R8G8B8, // format
D3DPOOL_MANAGED, // pool(cannot be D3DPOOL_DEFAULT here)
&pTexture);
2. Retrieve this texture surface level:
pTexture->GetSurfaceLevel(0, &pRenderSurface);
D3DSURFACE_DESC surfaceDesc;
pRenderSurface->GetDesc(&surfaceDesc);
3. Lock a rectangle on the surface:
D3DLOCKED_RECT lockedRect;
pRenderSurface->LockRect(&lockedRect, 0, 0); // entire surface

NOTE: This method cannot retrieve data from a surface that is is contained by a texture resource created with D3DUSAGE_RENDERTARGET because such a texture must be assigned to D3DPOOL_DEFAULT memory and is therefore not lockable. In this case, use instead GetRenderTargetData to copy texture data from device memory to system memory.
4. Retrieve and modify the surface's data :
DWORD* imageData = (DWORD*)lockedRect.pBits;
for(int i = 0; i < surfaceDesc.Height; i++)
{
for(int j = 0; j < surfaceDesc.Width; j++)
{
int index = i * (lockedRect.Pitch / 4) + j; // because pitch is in bytes, and 4 bytes per DWORD
imageData[index] = 0xffffff00;
}
}
5. Unlock the rectangle:
pRenderSurface->UnlockRect();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值