基于3D纹理的体绘制

时间:2014-05-21 15:21:24

标签: c++ directx direct3d directx-11 direct3d11

我一直在尝试获取一系列2D图像,这些图像大小相同,格式化为原始数据,加载到3D纹理中,然后在屏幕上渲染3D纹理。由于我对DirectX还很新,我不确定我做错了什么。这是相关的代码:

// Open the image and copy its contents into a buffer

std::ifstream image("head256x256x109", std::ifstream::binary);

int size;
char* buffer;
if (image.is_open()) {
  image.seekg(0, image.end);
  size = image.tellg();
  image.seekg(0, image.beg);
  buffer = new char[size];
  image.read(buffer, size);
  image.close();
} else {
  return false;
}

// Convert the data to RGBA data
// Here we are simply putting the same value to R, G, B and A channels.

char* RGBABuffer = new char[ size*4 ];
for( int nIndx = 0; nIndx < size; ++nIndx )
{
    RGBABuffer[nIndx*4] = buffer[nIndx];
    RGBABuffer[nIndx*4+1] = buffer[nIndx];
    RGBABuffer[nIndx*4+2] = buffer[nIndx];
    RGBABuffer[nIndx*4+3] = buffer[nIndx];
}

// Create the texture

D3D11_TEXTURE3D_DESC texture_desc;
ZeroMemory(&texture_desc, sizeof(texture_desc));
texture_desc.Width = 256;
texture_desc.Height = 256;
texture_desc.Depth = 109;
texture_desc.MipLevels = 1;
texture_desc.Format = DXGI_FORMAT_R32G32B32A32_FLOAT;
texture_desc.Usage = D3D11_USAGE_DEFAULT;
texture_desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;

D3D11_SUBRESOURCE_DATA image_data;
ZeroMemory(&image_data, sizeof(image_data));
image_data.pSysMem = RGBABuffer;
image_data.SysMemPitch = 256 * 4;
image_data.SysMemSlicePitch = 256 * 256 * 4;

d3dResult = d3dDevice_->CreateTexture3D(&texture_desc, &image_data, &texture_);

// Create a sampler

D3D11_SAMPLER_DESC colorMapDesc;
ZeroMemory( &colorMapDesc, sizeof( colorMapDesc ) );
colorMapDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
colorMapDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
colorMapDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
colorMapDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;
colorMapDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
colorMapDesc.MaxLOD = D3D11_FLOAT32_MAX;

d3dResult = d3dDevice_->CreateSamplerState( &colorMapDesc, &colorMapSampler_ );

着色器代码尽可能简单:

Texture3D colorMap_ : register( t0 );
SamplerState colorSampler_ : register( s0 );

struct VS_Input
{
  float4 pos  : POSITION;
  float4 tex0 : TEXCOORD0;
};

struct PS_Input
{
  float4 pos  : SV_POSITION;
  float4 tex0 : TEXCOORD0;
};

PS_Input VS_Main( VS_Input vertex )
{
  PS_Input vsOut = ( PS_Input )0;
  vsOut.pos = vertex.pos;
  vsOut.tex0 = vertex.tex0;

  return vsOut;
}

float4 PS_Main( PS_Input frag ) : SV_TARGET
{
  return colorMap_.Sample( colorSampler_, frag.tex0 );
}

现在我知道我需要实现某种alpha混合才能使其正常工作,但我希望我至少能得到第一张图片。但是,我得到的只是我在渲染函数中设置的背景颜色:

float clearColor[4] = { 0.0f, 0.0f, 0.0f, 1.0f };
d3dContext_->ClearRenderTargetView( backBufferTarget_, clearColor );

对于我可能做错的任何帮助以及为实现这一目标而需要做的任何进一步提示(从一系列图像中制作3D纹理并在屏幕上渲染)将会非常有用很有帮助,因为网上有关于此的文档很少。

0 个答案:

没有答案