函数 CreateWICTextureFromFile() 实际上不会加载纹理(Direct3D11,C++)

The function CreateWICTextureFromFile() will not actually load a texture (Direct3D11, C++)

我正在尝试使用函数 DirectX::CreateWICTextureFromFile 将草地纹理加载到我的游戏中,但每次我这样做时,该函数似乎实际上不会加载任何东西,它只会加载黑色纹理。该函数成功 returns S_OK,并且在我实际调用该函数之前我还调用了 CoInitialize(NULL)。但是还是不行。

下面是我对函数的使用

// This is where i load the texture
void Load_Texture_for_Ground()
{
HRESULT status;
ID3D11ShaderResourceView * Texture;

CoInitialize(NULL);

status = DirectX::CreateWICTextureFromFile(device, L"AmazingGrass.jpg", NULL, &Texture);

if (Texture != NULL)    // This returns true
{
    MessageBox(MainWindow, L"The pointer points to the texture", L"MessageBox", MB_OK);
}

if (status == S_OK)   //This returns true
{
    MessageBox(MainWindow, L"The function succeeded", L"MessageBox", MB_OK);
}

CoUninitialize();
}

// This is where i actually load the texture onto an object, assuming i already declared all the variables in this function
void DrawTheGround ()
{

DevContext->VSSetShader(VS, 0, 0);      

DevContext->PSSetShader(PS, 0, 0);  

DevContext->IASetVertexBuffers(
                            0,                  
                            1,                  
                            &GroundVertexBuffer,
                            &strides,           
                            &offset             
                            );
DevContext->IASetIndexBuffer(
                        IndexBuffer,            
                        DXGI_FORMAT_R32_UINT, 
                        0
                        );

        /* Transforming the matrices*/
TransformedMatrix = GroundWorld * CameraView * CameraProjection ;

Data.WORLDSPACE = XMMatrixTranspose(GroundWorld);

Data.TRANSFORMEDMATRIX = XMMatrixTranspose(TransformedMatrix);

        /* Updating the matrix in application's Constant Buffer*/
DevContext->UpdateSubresource(
                        ConstantBuffer,                 
                        0,                              
                        NULL,                           
                        &Data,                          
                        0,                              
                        0                               
                        );

DevContext->VSSetConstantBuffers(0, 1, &ConstantBuffer);

DevContext->PSSetShaderResources(0, 1, &Texture);

DevContext->PSSetSamplers(0, 1, &TextureSamplerState);

DevContext->DrawIndexed(6, 0, 0);

}

这里可能有什么问题?为什么函数不加载纹理?

测试是否正确加载纹理数据的快速方法是在加载后立即在 ScreenGrab 模块中使用 SaveWICTextureToFile。当然,您只会在调试时这样做。

#include <wincodec.h>
#include <wrl/cient.h>
using Microsoft::WRL::ComPtr;

ComPtr<ID3D11Resource> Res;
ComPtr<ID3D11ShaderResourceView> Texture;
HRESULT status = DirectX::CreateWICTextureFromFile(device, L"AmazingGrass.jpg", &Res, &Texture);

if (FAILED(status))
    // Error handling

#ifdef _DEBUG
    status = SaveWICTextureToFile( DevContext, Res.Get(),
                GUID_ContainerFormatBmp, L"SCREENSHOT.BMP" );
#endif

然后你可以 运行 代码并检查 SCREENSHOT.BMP 是否不是全黑的。

I strongly suggest you adopt the ComPtr smart pointer and the FAILED / SUCCEEDED macros in your coding style. Raw pointers and directly comparing HRESULT to S_OK is setting yourself up for a lot of bugs.

你不应该每帧都调用 CoInitialize。作为应用程序初始化的一部分,您应该调用它一次。

你不应该每帧都创建一个新的 SpriteBatchSpriteFont 实例。只需在创建设备后创建它们并保留它们即可。