2013-04-30 82 views
1

我試圖用MSDN上提到的映射,寫入和取消映射方法填充任意數據(-1或1)的3D紋理,但我一直無法找到如何做到這一點,使用行和深度瀝青等DX11以編程方式填充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

回答

1

好吧,我終於摸索出我在做什麼錯。我不得不將rowStart和depStart除以4以將字節轉換爲浮點,並且固定了所有內容。

UINT depStart = mappedTex.DepthPitch/4*dep; 
for(UINT row = 0; row < m_3DTexDesc.Width; row++) 
{ 
    UINT rowStart = mappedTex.RowPitch/4*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; 
    } 
} 
相關問題