DirectX11如何从unsigned char *数据创建Text2

时间:2015-03-05 17:02:53

标签: c++ graphics directx directx-11 texture-mapping

我有一个函数Ge​​tCamImage,它将相机中的图像返回为:

unsigned char* p = GetCamImage(...);

我需要从这些数据创建ID3D11Texture2D。我怎么能在DirectX 11中做到这一点?

由于

1 个答案:

答案 0 :(得分:1)

这取决于p中实际内容的格式,以及宽度,高度和步幅。假设图像是RGBA 32位格式,具有简单的字节间距对齐,并且w的大小为h,那么它将是简单的:

D3D11_SUBRESOURCE_DATA initData = {0};
initData.pSysMem = (const void*)p;
initData.SysMemPitch = w * 4;
initData.SysMemSlicePitch = h * w * 4;

D3D11_TEXTURE2D_DESC desc;
desc.Width = w;
desc.Height = h;
desc.MipLevels = 1;
desc.ArraySize = 1;
desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
desc.SampleDesc.Count = 1;
desc.SampleDesc.Quality = 0;
desc.Usage = D3D11_USAGE_DEFAULT;
desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
desc.CPUAccessFlags = 0;

ID3D11Texture2D* tex = nullptr;
HRESULT hr = d3dDevice->CreateTexture2D( &desc, &initData, &tex );
if (FAILED(hr))
   ...