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

Posted skyman_2001

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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();

以上是关于WJ的Direct3D简明教程3:Create Texture with User-defined Image Data的主要内容,如果未能解决你的问题,请参考以下文章

glance image-create --name "wj_js_company_img" --file a0e1c7fa-d6d3-410f-9bb5-e699e342db91

Direct3D 11 Tutorial 3: Shaders and Effect System_Direct3D 11 教程3:着色器和效果系统

Direct3D 11 Tutorial 6:Lighting_Direct3D 11 教程6:灯光

Direct3D 11 Tutorial 4: 3D Spaces_Direct3D 11 教程4:3D空间

Direct3D 11 Tutorial 5: 3D Transformation_Direct3D 11 教程5:3D转型

Direct3D 11 Tutorial 2: Rendering a Triangle_Direct3D 11 教程2:渲染一个三角形