【发布时间】: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