【问题标题】:The function CreateWICTextureFromFile() will not actually load a texture (Direct3D11, C++)CreateWICTextureFromFile() 函数实际上不会加载纹理(Direct3D11,C++)
【发布时间】:2015-11-20 20:14:35
【问题描述】:

我正在尝试使用函数DirectX::CreateWICTextureFromFile 将草纹理加载到我的游戏中,但每次我这样做时,该函数似乎都不会真正加载任何内容,它只是加载黑色纹理。该函数成功返回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);

}

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

【问题讨论】:

  • 你应该调用 CoInitialize(NULL);在您的应用程序开始时,并在最后进行 CoUninitialize
  • 它仍然不会产生影响,该函数仍然没有加载任何内容
  • 您可以发送一个指向您的 jpg 文件的链接吗?我想尝试重现您的问题。您能否显示构建GroundVertexBuffer 的源代码。此外,您应该按照 marcinj 的说明在应用程序的开头调用 CoInitialize,而不是在函数的开头。
  • 没关系。我发现了问题。我的像素着色器似乎有问题。光照计算不正确。
  • 请注意,status == S_OK 不是有效的 COM。您需要使用SUCCEEDED(status)FAILED(status)

标签: c++ direct3d direct3d11


【解决方案1】:

测试您是否已正确加载纹理数据的快速方法是在加载后立即在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 不是全黑的。

我强烈建议您在编码风格中采用ComPtr 智能指针和FAILED / SUCCEEDED 宏。原始指针和直接比较 HRESULTS_OK 会导致很多错误。

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-16
    • 1970-01-01
    • 2017-03-25
    • 1970-01-01
    • 1970-01-01
    • 2014-08-31
    • 1970-01-01
    • 2021-12-15
    相关资源
    最近更新 更多