locked
SpriteBatch/SpriteFont drawing in 3D RRS feed

  • Question

  • I am trying to implement text/symbol rendering in 3D perspective projection setup using DirectXTK's SpriteBatch/SpriteFont. I've searched this forum and MSDN on related topics and found two helpful articles [1],[2] given at the end of this post.
    To experiment with walbourn's code given in [2], I start with the DirectXTK simple sample [3]. I want to change the code so that the Windows logo and the text "DirectXTK Simple Sample" take on a 3D perspective look. For example, the text is rendered on the surface of the tea pot. The only changes I've made so far is in function Renderer::Render() of file Renderer.cpp. In details, I pass an transform matrix as the last function argument to Renderer::Render. Here is the code:

    // Draw sprite
        XMMATRIX world = XMLoadFloat4x4( &m_world );
        XMMATRIX view = XMLoadFloat4x4( &m_view );
        XMMATRIX projection = XMLoadFloat4x4( &m_projection );
        XMMATRIX local = XMMatrixMultiply( world, XMMatrixTranslation( -2.f, -2.f, -4.f ) );
        XMMATRIX worldViewProj = local * view * projection;
    
        m_sprites->Begin(DirectX::SpriteSortMode_Deferred, nullptr, nullptr, nullptr, nullptr, nullptr, worldViewProj);
        //m_sprites->Begin();
        m_sprites->Draw( m_texture2.Get(), XMFLOAT2(10,75), nullptr, Colors::White );
        m_font->DrawString( m_sprites.get(), L"DirectXTK Simple Sample", XMFLOAT2( 100, 10 ), Colors::Yellow );
        m_sprites->End();
    The rendering result is apparently wrong as both logo and text are invisible at first and then become rays staring from the top-left corner of the screen, radiating rightward and downwards, and then disappear again at the end of animation period. Could someone tell me what's wrong with my code, or how to change the code so that the logo and the text are rendered on the surface of the tea pot or character face? Thank you in advance.

    [1] SpriteBatch billboards in a 3D world. http://blogs.msdn.com/b/shawnhar/archive/2011/01/12/spritebatch-billboards-in-a-3d-world.aspx?PageIndex=2.
    [2] SpriteBatch.Begin()
    https://directxtk.codeplex.com/discussions/459391.
    [3] DirectXTK Simple Sample (Windows 8.1)
    http://code.msdn.microsoft.com/DirectXTK-Simple-Sample-a0b6de36.

    Friday, November 29, 2013 12:01 AM

All replies

  • Well, if DirectXTK's SpriteFont is not suitable, what are the viable solutions for 3D text rendering for Direct3D11?
    Saturday, November 30, 2013 4:00 PM
  • For putting text on a 3D surface, the easiest thing is to use SpriteFont to render-to-texture, and then use that texture to render the 3D object...
    Saturday, November 30, 2013 9:14 PM
  • For putting text on a 3D surface, the easiest thing is to use SpriteFont to render-to-texture, and then use that texture to render the 3D object...

    Thank you for your reply.
    I think I did not present my question correctly when I said "the text is rendered on the surface of the tea pot" in the first thread. I want the text and the tea pot to be rendered independently, that is, the text is still visible even if the tea pot is removed from the scene, or vice visa.
    In my experiment with the code sample "DirectXTK Simple Sample", I get the feeling that a SpriteFont object is controlled not only by world-view-projection matrix I provided as the last argument of function m_sprites->Begin(), but also by other transform matrix. For example the third argument of function m_sprites->Draw() control the position of the SpriteFont object, a value of XMFLOAT2(0,0) (XMFLOAT(screenWidth/2, screenHeight/2) resp.) will put the text at the top-left (middle resp.) of the screen. The lack of detailed documentation of DirectX Toolkit API make it more difficult to debug the code.
    Nonetheless, I will keep experimenting the code and will look foreword to your further help.

    Sunday, December 1, 2013 12:11 AM
  • DirectTK's SpriteBatch (which is used to draw SpriteFont) takes locations in screen-coordinates, not world-coordinates. It implements a simple texture-based raster font. It sounds like you want a geometry-based vector font instead, which is not what SpriteBatch/SpriteFont implements. You might be able to get this functionality out of Direct2D/DirectWrite, but it's not generally intended for use with 3D scenes.
    Sunday, December 1, 2013 5:30 AM
  • DirectTK's SpriteBatch (which is used to draw SpriteFont) takes locations in screen-coordinates, not world-coordinates. It implements a simple texture-based raster font. It sounds like you want a geometry-based vector font instead, which is not what SpriteBatch/SpriteFont implements. You might be able to get this functionality out of Direct2D/DirectWrite, but it's not generally intended for use with 3D scenes.

    Thank you for your help, walbourn.
    My proposal to use SpriteFont/SpriteBatch as the 3D text solution is based on the idea that we can transform (rotate/translate) a 2D textured quad so that it is rendered with the desired position and orientation. In fact, this method works in OpenGL Android port of our app. According to your explanation, the primary difficulty is that SpriteFont/SpriteBatch uses screen coordinate, not world coordinate. So could you please develop DirectX Tool Kit to add another version of SpriteFont::Draw that use world space. Add this feature may provide a viable 3D text solution to DirectX11!

    Sunday, December 1, 2013 2:36 PM