C++ 在DirectX中创建和使用纹理
我尝试使用代码创建纹理,将其转换为着色器资源视图,然后将其应用于平面,但我得到的只是一个黑色正方形。我尝试在msdn上使用示例代码,但没有效果,还尝试使用了unsigned char和float(float如下所示,因为这是我最终目标需要使用的) 以下是尝试创建纹理的代码:C++ 在DirectX中创建和使用纹理,c++,directx,textures,directx-11,C++,Directx,Textures,Directx 11,我尝试使用代码创建纹理,将其转换为着色器资源视图,然后将其应用于平面,但我得到的只是一个黑色正方形。我尝试在msdn上使用示例代码,但没有效果,还尝试使用了unsigned char和float(float如下所示,因为这是我最终目标需要使用的) 以下是尝试创建纹理的代码: bool TerrainClass::CreateTexture(ID3D11Device* _device) { ID3D11Texture2D *texture; D3D11_TEXTURE2D_
bool TerrainClass::CreateTexture(ID3D11Device* _device)
{
ID3D11Texture2D *texture;
D3D11_TEXTURE2D_DESC tdesc;
D3D11_SUBRESOURCE_DATA data;
float *buf = (float *)malloc(m_terrainWidth * m_terrainHeight * 4 * sizeof(float));
for(int i = 0; i < m_terrainWidth * m_terrainHeight * 4; i++)
buf[i] = 1.0f;
data.pSysMem = (void *)buf;
data.SysMemPitch = m_terrainWidth * 4;
data.SysMemSlicePitch = m_terrainWidth * m_terrainHeight * 4;
tdesc.Width = m_terrainWidth;
tdesc.Height = m_terrainHeight;
tdesc.MipLevels = 1;
tdesc.ArraySize = 1;
tdesc.SampleDesc.Count = 1;
tdesc.SampleDesc.Quality = 0;
tdesc.Usage = D3D11_USAGE_DEFAULT;
tdesc.Format = DXGI_FORMAT_R32G32B32A32_FLOAT;
tdesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
tdesc.CPUAccessFlags = 0;
tdesc.MiscFlags = 0;
//attempt to create the 2d texture
if(FAILED(_device->CreateTexture2D(&tdesc,&data,&texture)))
return false;
//assign the texture made from fbm to one of the textures for the terrain
if(!(m_Textures->ChangeTexture(_device, texture, 2)))
return false;
delete[] buf;
return true;
}
它应该将着色器资源视图设置为我刚刚创建的纹理。
所以我完全不知道哪里出了错,有什么想法吗?
数据。SysMemPitch
应该以字节为单位,所以m_terrainWidth*4*sizeof(float)
。与SysMemSlicePitch
相同,但您可以将其设置为零,因为您只需要将其用于纹理立方体、体积或阵列。您还应该验证您正在运行的支持是否以您使用它的方式使用了R32G32B32A32
(我假设是Sample
)。通过查看或致电来验证支持。如果您的硬件不支持您正在使用的模式,请切换到更广泛支持的格式,如B8G8R8A8\u UNORM
(并确保更改初始数据以匹配格式布局)
bool TextureArrayClass::ChangeTexture(ID3D11Device* _device, ID3D11Texture2D* _texture, int _i)
{
if(FAILED(_device->CreateShaderResourceView(_texture,NULL, &m_textures[_i])))
{
return false;
}
return true;
}