HI, I am trying to read all the pixel data (just the red channel) from a texture into a 2d array. The code I am using compiles and runs without error but seems to always get zero values. Not sure I am understanding how this is supposed to really work.. fairly new to dx11...here is my complete code for this function:
void load_heightmap(ID3D11Device *d3d11Device, ID3D11DeviceContext *d3d11DevCon)
{
UINT heightmapsize = 4096;
D3DX11_IMAGE_LOAD_INFO loadInfo;
loadInfo.Width = D3DX11_DEFAULT;
loadInfo.Height = D3DX11_DEFAULT;
loadInfo.Depth = D3DX11_DEFAULT;
loadInfo.FirstMipLevel = D3DX11_DEFAULT;
loadInfo.MipLevels = 0;
loadInfo.Usage = D3D11_USAGE_STAGING;
loadInfo.BindFlags = 0;
loadInfo.CpuAccessFlags = D3D11_CPU_ACCESS_READ;
loadInfo.MiscFlags = 0;
loadInfo.Format = DXGI_FORMAT_R8G8B8A8_UINT;
loadInfo.Filter = D3DX11_FILTER_NONE;
loadInfo.MipFilter = D3DX11_DEFAULT;
loadInfo.pSrcInfo = NULL;
ID3D11Resource* res = nullptr;
D3DX11CreateTextureFromFile(
d3d11Device,
L"world.dds",
&loadInfo,
NULL,
&res,
NULL
);
if (!res)
{
MessageBox(NULL, L"Error loading heightmap",
L"Error", MB_OK | MB_ICONERROR);
return;
}
D3D11_MAPPED_SUBRESOURCE mappedResource;
d3d11DevCon->Map(res, 0, D3D11_MAP_READ, NULL, &mappedResource);
//Set the pixels
UCHAR* pTexels = (UCHAR*)mappedResource.pData;
for (UINT row = 0; row < heightmapsize; row++)
{
//Row number * height
UINT rowStart = row * mappedResource.RowPitch;
for (UINT col = 0; col < heightmapsize; col++)
{
//width * number of channels (r,g,b,a)
UINT colStart = col * 4;
heightmap[row][col]=(UINT)pTexels[rowStart + colStart + 0];//read red channel
}
}
d3d11DevCon->Unmap(res, 0);
res->Release();
}