3D空间中的DirectXTK spriteFont [英] DirectXTK spriteFont in 3D space

查看:85
本文介绍了3D空间中的DirectXTK spriteFont的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望使用DirectXTK在(50,0,0)处显示 X,并关注有关广告牌的文章。但是我似乎不正确。
有人可以帮帮我吗。

I was hoping to display an "X" at (50,0,0) using DirectXTK and following an article about billboarding. however i do not seem to have it correct. Could somebody help me out.

XMVECTOR textPosition = XMVectorSet(50, 0, 0, 0);
            unique_ptr<BasicEffect> basicEffect(new BasicEffect(m_pd3dDevice));
            unique_ptr<CommonStates> commonStates(new CommonStates(m_pd3dDevice));
            /*basicEffect->SetWorld(XMMatrixScaling(1,-1, 1) * XMMatrixTranslationFromVector(textPosition));*/
/*          basicEffect->SetWorld( XMMatrixTranslationFromVector(textPosition));
            basicEffect->SetView(m_View);*/
            basicEffect->SetProjection(m_Projection);
            basicEffect->SetTextureEnabled(true);
            basicEffect->SetVertexColorEnabled(true);
            basicEffect->Apply(m_pImmediateContext);
            wstring message = L"X";
            XMFLOAT2 textOrigin;
            XMStoreFloat2(&textOrigin, m_font->MeasureString(message.c_str()));
            const float textSize = 1.0f;
            m_spriteBatch->Begin(SpriteSortMode_Deferred, nullptr, nullptr, commonStates->DepthRead(), commonStates->CullNone());
            m_font->DrawString(m_spriteBatch.get(), message.c_str(), XMFLOAT2(0, 0), Colors::White, 0, textOrigin, XMFLOAT2(textSize, textSize), SpriteEffects_None, 0.0f);

            m_spriteBatch->End();

这似乎很有希望

XMVECTOR textPosition = XMVectorSet(50, 0, 0, 0);
wstring message = L"X";
textPosition = XMVector3Transform(textPosition, m_View);
textPosition = XMVector3Transform(textPosition, m_Projection);
XMFLOAT3 pos;
XMStoreFloat3(&pos, textPosition);
pos.x /= pos.z;
pos.y /= pos.z; 
pos.x = (pos.x + 1.0) * GetViewPort().Width  / 2.0;
pos.y = (-pos.y + 1.0) * GetViewPort().Height / 2.0;


m_spriteBatch->Begin();
m_font->DrawString(m_spriteBatch.get(), message.c_str(), XMFLOAT2(pos.x, pos.y), Colors::White, 0.0f, XMFLOAT2(0, 0), XMFLOAT2(0.5f, 0.5), SpriteEffects_None);     
m_spriteBatch->End();


推荐答案

我认为应遵循以下原则。

Going with the following I think.

XMVECTOR mp = XMVector4Transform(textPosition, m_World);
XMVECTOR pt = XMVector3Project(mp, 0, 0, GetViewPort().Width, GetViewPort().Height, 0, 1, m_Projection, m_View, m_World);
XMFLOAT3 pos;
XMStoreFloat3(&pos, pt);


m_spriteBatch->Begin();
m_font->DrawString(m_spriteBatch.get(), message.c_str(), XMFLOAT2(pos.x, pos.y), Colors::LightGreen, 0.0f, XMFLOAT2(0, 0), XMFLOAT2(0.6f, 0.6));
m_spriteBatch->End();

这篇关于3D空间中的DirectXTK spriteFont的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆