gpt4 book ai didi

c++ - 纹理显示为黑色

转载 作者:行者123 更新时间:2023-11-30 03:29:59 24 4
gpt4 key购买 nike

我正在尝试制作一个纹理来覆盖三角形而不是颜色,但看起来纹理初始化有问题...

纹理加载没问题,我用 Opengl 试过了,它工作得很好,但它在 Directx 11 中不工作。

    void DX11Texture2D::Create(Texture_Data Data, Texture_Desc Desc)
{
D3D11_TEXTURE2D_DESC texDesc;
ZeroMemory(&texDesc, sizeof(texDesc));

texDesc.Width = Data.width;
texDesc.Height = Data.height;
texDesc.MipLevels = 0;
texDesc.ArraySize = 1;
texDesc.Format = GetDXTextureFormat(Desc.Format);
texDesc.SampleDesc.Count = 1;
texDesc.SampleDesc.Quality = 0;
texDesc.Usage = D3D11_USAGE_DEFAULT;
texDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
texDesc.CPUAccessFlags = 0;
texDesc.MiscFlags = 0;

D3D11_SUBRESOURCE_DATA subData;


subData.pSysMem = Data.databuf;
subData.SysMemPitch = Data.width * 4;
subData.SysMemSlicePitch = Data.width * Data.height * 4;


Core::Internals::DX11Renderer::GetDevice()->CreateTexture2D(&texDesc, &subData, &textureID);

//TODO: Add a way to disable and enable mip map
Core::Internals::DX11Renderer::GetDevice()->CreateShaderResourceView(textureID, 0, &resourceView);

D3D11_SAMPLER_DESC samplerDesc;
ZeroMemory(&samplerDesc, sizeof(D3D11_SAMPLER_DESC));

samplerDesc.AddressU = GetDXTextureWrap(Desc.Wrap);
samplerDesc.AddressV = GetDXTextureWrap(Desc.Wrap);
samplerDesc.AddressW = GetDXTextureWrap(Desc.Wrap);
samplerDesc.Filter = GetDXTextureFilter(Desc.Filter);
samplerDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
samplerDesc.MinLOD = 0;
samplerDesc.MaxLOD = D3D11_FLOAT32_MAX;

Core::Internals::DX11Renderer::GetDevice()->CreateSamplerState(&samplerDesc, &samplerState);


}
void DX11Texture2D::Bind(unsigned int index)
{
Core::Internals::DX11Renderer::GetContext()->PSSetShaderResources(index, 1, &resourceView);
Core::Internals::DX11Renderer::GetContext()->PSSetSamplers(index, 1, &samplerState);

}
DXGI_FORMAT DX11Texture2D::GetDXTextureFormat(API::TextureFormat format)
{
switch (format)
{
case API::TextureFormat::R8: return DXGI_FORMAT_R8_UNORM;
case API::TextureFormat::R8G8: return DXGI_FORMAT_R8G8_UNORM;
case API::TextureFormat::R8G8B8: return DXGI_FORMAT_R8G8B8A8_UNORM;
case API::TextureFormat::R8G8B8A8: return DXGI_FORMAT_R8G8B8A8_UNORM;
default: return DXGI_FORMAT_R8G8B8A8_UNORM;
}
}
D3D11_TEXTURE_ADDRESS_MODE DX11Texture2D::GetDXTextureWrap(API::TextureWrap textureWrap)
{
switch (textureWrap)
{
case API::TextureWrap::Repeat: return D3D11_TEXTURE_ADDRESS_WRAP;
case API::TextureWrap::MirroredReapeat: return D3D11_TEXTURE_ADDRESS_MIRROR;
case API::TextureWrap::ClampToEdge: return D3D11_TEXTURE_ADDRESS_CLAMP;
case API::TextureWrap::ClampToBorder: return D3D11_TEXTURE_ADDRESS_BORDER;
default: return D3D11_TEXTURE_ADDRESS_WRAP;
}
}
D3D11_FILTER DX11Texture2D::GetDXTextureFilter(API::TextureFilter textureFilter)
{
//TODO: Add more texture filter types to control both min and mag
switch (textureFilter)
{
case API::TextureFilter::Nearest: return D3D11_FILTER_MIN_MAG_MIP_POINT;
case API::TextureFilter::Linear: return D3D11_FILTER_MIN_MAG_MIP_LINEAR;
default: return D3D11_FILTER_MIN_MAG_MIP_POINT;
}
}

还有我的着色器

三角形对比:

    struct VertexInputType
{
float3 position : POSITION;
float3 color : COLOR;
float2 tex : TEXCOORD;

};

struct PixelInputType
{
float4 position : SV_POSITION;
float2 tex : TEXCOORD;
};

PixelInputType main(VertexInputType input)
{
PixelInputType output;


// Calculate the position of the vertex against the world, view, and projection matrices.
output.position = float4(input.position, 1);


// Store the input texture for the pixel shader to use.
output.tex = input.tex;

return output;
}

三角形.ps

    struct PixelInputType
{
float4 position : SV_POSITION;
float2 tex : TEXCOORD;
};

Texture2D shaderTexture : register(t0);;
SamplerState SampleType : register(s0);;

float4 main(PixelInputType input) : SV_TARGET
{
return shaderTexture.Sample(SampleType, input.tex);
}

干杯,Zlixine.

最佳答案

看起来您正在创建一个带有完整 mip 映射链的纹理,但查看初始数据时您似乎只提供了一个切片(如果您的采样器试图访问另一个切片,因为没有提供数据,它将是黑色的)

在这种情况下,您应该使用

强制执行单个 mip:
texDesc.MipLevels = 1;

行:

subData.SysMemSlicePitch = Data.width * Data.height * 4;

不需要,因为在 2d 纹理的情况下会忽略此参数(您可以将其保留为 0 或任何值)。

此外,您应该在创建资源时检查结果代码,例如:

HRESULT hr = Core::Internals::DX11Renderer::GetDevice()->CreateTexture2D(&texDesc, &subData, &textureID);
if FAILED(hr)
{
//Handle issue if texture creation did fail
}

并确保使用 D3D11CreateDevice使用 D3D11_CREATE_DEVICE_DEBUG,所以在调试时,你会在你的 visual studio 输出窗口中有有意义的错误信息(如果创建失败,你会有一个解释,而不是只得到 INVALIDARG hresult,这不是很有用)

关于c++ - 纹理显示为黑色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45261935/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com