我试图用函数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);
}这里有什么问题吗?为什么函数不加载纹理?
发布于 2015-11-23 21:02:26
测试是否正确加载纹理数据的一个快速方法是在加载SaveWICTextureToFile模块后立即在ScreenGrab模块中使用它。当然,只有在调试时才会这样做。
#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宏。原始指针和直接将HRESULT与S_OK进行比较会给自己设置许多错误。
您不应该每帧都调用CoInitialize。您应该调用它一次,作为应用程序初始化的一部分。
您不应该创建每个框架的SpriteBatch和SpriteFont的新实例。只需在你创建你的设备后创建它们,并保持它们。
https://stackoverflow.com/questions/33835359
复制相似问题