我正在尝试使用 MSDN 上提到的 map、write 和 unmap 方法用任意数据(-1 或 1)填充 3D 纹理,但我找不到任何实际的代码示例来说明如何做到这一点,使用行间距和深度间距等。
纹理描述为:
D3D11_TEXTURE3D_DESC m_3DTexDesc;
m_3DTexDesc.Height = 33;
m_3DTexDesc.Width = 33;
m_3DTexDesc.Depth = 33;
m_3DTexDesc.MipLevels = 1;
m_3DTexDesc.Format = DXGI_FORMAT_R32_FLOAT;
m_3DTexDesc.Usage = D3D11_USAGE_DYNAMIC;
m_3DTexDesc.BindFlags = D3D10_BIND_SHADER_RESOURCE;
m_3DTexDesc.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE;
m_3DTexDesc.MiscFlags = 0;
result = p_device->CreateTexture3D(&m_3DTexDesc, NULL, &m_voxels);
我用来填充纹理的代码是:
D3D11_MAPPED_SUBRESOURCE mappedTex;
result = p_context->Map(m_voxels, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedTex);
float* pVoxels = (float*)mappedTex.pData;
// DepthPitch * 33 + RowPitch * 33 + 33;
float* pTexels = (float*)malloc(287265*4);
for( UINT dep = 0; dep < m_3DTexDesc.Depth; dep++ )
{
UINT depStart = mappedTex.DepthPitch*dep;
for( UINT row = 0; row < m_3DTexDesc.Width; row++ )
{
UINT rowStart = mappedTex.RowPitch*row;
for( UINT col = 0; col < m_3DTexDesc.Height; col++ )
{
if(pos.y + col < 0.0f) pTexels[depStart + rowStart + col] = -1.0f;
else pTexels[depStart + rowStart + col] = 1.0f;
}
}
}
memcpy(pVoxels, (void*)pTexels, 287265);
p_context->Unmap(m_voxels, 0 );
free((void*)pTexels);
使用这种方法,纹理中的一些数据很好,但其余的则大错特错。有人可以解释使用这些指针和音高来遍历纹理并初始化它的正确方法吗?
参考 DepthPitch = 8448 和 RowPitch = 256。