我使用时遇到问题DirectX::SpriteFont
/DirectX::SpriteBatch
(来自 DirectXTK;与此处讨论的问题完全相同:使用 SpriteFont 类绘制文本时出现问题 https://stackoverflow.com/questions/33860576/problems-when-drawing-text-using-the-spritefont-class).
void DrawScene(void)
{
HRESULT hr;
float bgColor_a[4] = { 0.0f, 0.4f, 0.8f, 0.0f };
g_pDeviceContext->ClearRenderTargetView(g_pRenderTargetView, bgColor_a);
g_pDeviceContext->ClearDepthStencilView(g_pDepthStencilView, D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.0f, 0);
XMMATRIX cameraProj = XMLoadFloat4x4(&g_camera._cameraProjection);
XMVECTOR pos = XMLoadFloat3(&g_camera._pos);
XMVECTOR target = XMLoadFloat3(&g_camera._target);
XMVECTOR up = XMLoadFloat3(&g_camera._up);
XMMATRIX cameraView = XMMatrixLookAtLH(pos, target, up);
XMMATRIX worldBox2 = XMMatrixIdentity() * (XMMatrixTranslation(2.0f, 0.0f, 0.0f) * XMMatrixRotationY(XMConvertToRadians(g_rotBox2)));
XMMATRIX wvp = worldBox2 * cameraView * cameraProj;
XMMATRIX transposeWvp = XMMatrixTranspose(wvp);
XMStoreFloat4x4(&g_constantBufferPerObject._wvp, transposeWvp);
g_pDeviceContext->UpdateSubresource(g_pConstantBufferPerObject, 0, NULL, &g_constantBufferPerObject, 0, 0);
g_pDeviceContext->VSSetConstantBuffers(0, 1, &g_pConstantBufferPerObject);
g_pDeviceContext->PSSetShaderResources(0, 1, &g_pCageTexture);
g_pDeviceContext->PSSetSamplers(0, 1, &g_pCubeTextureSamplerState);
// box
g_pDeviceContext->DrawIndexed(36, 0, 0);
wchar_t buffer[32];
swprintf_s(buffer, 32, L"%.2f", g_fps._fps);
//g_pSpriteBatch->Begin();
//g_pSpriteFont->DrawString(g_pSpriteBatch, buffer, XMFLOAT2(30, 30));
//g_pSpriteBatch->End();
// Present the backbuffer to the screen
hr = g_pSwapChain->Present(0, 0);
if (FAILED(hr))
{
ErrorBoxW(L"Cannot present without error.");
}
}
无需致电SpriteBatch::Begin()
, SpriteFont::DrawString
and SpriteBatch::End()
您将看到一个带纹理的立方体在空间中旋转(笼子)。通过调用所描述的函数,您将只能看到左上角的每秒帧数,但看不到旋转的立方体。
我按照以下教程进行操作查克·沃尔伯恩在 github DirectXTK 上并结合了 2 个教程 Draw String usingSpriteFont
并渲染一个原始的 3D 对象(一个简单的三角形)。在此示例中,我将看到测试字符串绘制在三角形上。但我不明白为什么在使用绘制文本字符串时我的示例中的旋转立方体不可见SpriteFont
DirectXTK 类,我什至找不到根本原因。
像素着色器文件(PixelShader.hlsl):
struct VS_OUTPUT
{
float4 Pos : SV_POSITION;
float4 Color : COLOR;
float2 TexCoord : TEXCOORD;
};
Texture2D ObjTexture;
SamplerState ObjSamplerState;
float4 PS( VS_OUTPUT input ) : SV_TARGET
{
float4 diffuse = ObjTexture.Sample(ObjSamplerState, input.TexCoord);
clip(diffuse.a - 0.25);
return diffuse;
}
顶点着色器文件(VertexShader.hlsl):
cbuffer constantBufferPerObject
{
float4x4 WVP;
float ColorAdjust;
int Mode;
int Pad[2];
};
struct VS_OUTPUT
{
float4 Pos : SV_POSITION;
float4 Color : COLOR;
float2 TexCoord : TEXCOORD;
};
VS_OUTPUT VS( float4 pos : POSITION, float4 color : COLOR_ZERO, float4 color2 : COLOR_ONE, float2 texCoord : TEXCOORD )
{
VS_OUTPUT output;
float4 temp;
output.Pos = mul(pos, WVP);
temp = color;
output.Color.r = temp.r * color2.r * (1.0f - ColorAdjust);
output.Color.g = temp.g * color2.g * (1.0f - ColorAdjust);
output.Color.b = temp.b * color2.b * (1.0f - ColorAdjust);
output.Color.a = color.a * color2.a;
output.TexCoord = texCoord;
return output;
}
重现问题:我正在使用 Visual Studio 2015 社区版。我添加了 DirectXTK (.lib) 和 DirectXTex (WICTextureLoader
.cpp/.h,DDSTextureLoader
.cpp/.h、.lib) 添加到我的项目中。立方体的图像是带有 alpha 的 .png。
Note: 我创建了这个重复的问题, 至
avoid...寻求帮助、澄清或回应其他答案。
关于另一个问题。