首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >函数CreateWICTextureFromFile()不会实际加载纹理(Direct3D11,C++)

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

Stack Overflow用户
提问于 2015-11-20 20:14:35
回答 1查看 3K关注 0票数 0

我试图用函数DirectX::CreateWICTextureFromFile将草纹理加载到我的游戏中,但是每次我这样做时,这个函数似乎并没有实际加载任何内容,它只是加载一个黑色纹理。函数成功地返回S_OK,在实际调用该函数之前,我还调用了CoInitialize(NULL)。但还是没用。

下面是我对这个函数的用法

代码语言:javascript
复制
// 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);

}

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-11-23 21:02:26

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

代码语言:javascript
复制
#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的新实例。只需在你创建你的设备后创建它们,并保持它们。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33835359

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档