[WP8] How to correctly render a sprite in a png texture?

Mar 23, 2014 at 9:37 AM
Edited Mar 23, 2014 at 12:23 PM
Hello,

I'm quite new to DirectXTK and I'm trying to draw a sprite on WP8. My texture is png format and I know that only dds texture is support on WP8. So I convert the png to dds by opening in my project in VS2013 and save it as dds format

My codes to load the texture:

        HRESULT hr =
            CreateDDSTextureFromFile(m_d3dDevice.Get(),
            wszDDSFile,
            NULL,
            &m_shipTexture,
            0,
            NULL);
        if (FAILED(hr))
        {
            g_OutputDebugString(L"CreateDDSTextureFromFile (%s) fails 0x%x\n", wszDDSFile, hr);
        }
Mu codes to render:
        const float midnightBlue[] = { 0.098f, 0.098f, 0.439f, 1.000f };
        m_d3dContext->ClearRenderTargetView(
            m_renderTargetView.Get(),
            midnightBlue
            );

        m_d3dContext->ClearDepthStencilView(
            m_depthStencilView.Get(),
            D3D11_CLEAR_DEPTH,
            1.0f,
            0
            );

        m_d3dContext->OMSetRenderTargets(
            1,
            m_renderTargetView.GetAddressOf(),
            m_depthStencilView.Get()
            );

        m_spriteBatch->Begin();
        m_spriteBatch->Draw(m_shipTexture, XMFLOAT2(0, 0));
        m_spriteBatch->End();
However the sprite is not rendered correctly. The sprite itself is correctly rendered, but there are lots of noise in the rest part of the texture. The rest part in the texture should be transparent and should not be rendered as what we see if we open the original png file.

Does anyone know if there's an easy way to render the texture correctly converted from a png file? I shared my png file and the screenshot of actual image after rendering here:

Original PNG Texture

Actual Image Rendered
Mar 23, 2014 at 9:51 AM
Edited Mar 23, 2014 at 12:53 PM
Looks like this is because spriteBatch defaults expects the image with pre-multiplied alpha. I convert my image to pre-multiplied alpha image using this tool http://www.qualibyte.com/pixelformer/, and it works perfectly.

May I request DirectXTK provides an easy option to draw image with standard alpha?

Because as a XNA developer, most of sprite resources I'm using do not have pre-multiplied alpha. Developers on other platforms may hit the same problem as well. Images from internet are also do not have pre-multiplied alpha.
Marked as answer by D_BOY on 3/23/2014 at 5:10 AM
Coordinator
Mar 23, 2014 at 5:10 PM
This is already covered in the documentation for SpriteBatch under Alpha Blending

Note that DirectXTex's texconv tool also supports converting to DDS with or without premultiplied alpha.
Marked as answer by walbourn on 3/23/2014 at 9:10 AM