This project has moved and is read-only. For the latest updates, please go here.

Spritebatch->Begin with AlphatestEffect

Mar 4, 2015 at 4:03 AM
Hello guys i'm new here :D, i was trying to handle metaballs programming following this XNA tutorial => http://nullcandy.com/2d-metaballs-in-xna/

everything works fine for the first parts, with the use of additive blend state, however for the last part i got stuck, i don't know how to handle the AlphatestEffect with DirectXTK ( wich use a lambda func instead of a simple effect Like XNA ) i tried the following :

in the draw methode :
auto dev = m_deviceResources->GetD3DDeviceContext();
m_SpriteBtch->Begin(SpriteSortMode::SpriteSortMode_Deferred, m_State->Opaque(), nullptr, nullptr, nullptr, [=]
{
m_effect->Apply(dev);

});

m_SpriteBtch->Draw(m_texture.Get(), m_CurentPos, nullptr, Colors::White, 0.f, XMFLOAT2(128.f, 128.f));
m_SpriteBtch->Draw(m_texture.Get(), XMFLOAT2(500.f, 200.f), nullptr, Colors::White, 0.f, XMFLOAT2(128.f, 128.f));
m_SpriteBtch->Draw(m_texture.Get(), XMFLOAT2(500.f, 200.f), nullptr, Colors::White, 0.f, XMFLOAT2(128.f, 128.f));
m_SpriteBtch->Draw(m_texture.Get(), XMFLOAT2(500.f, 300.f), nullptr, Colors::White, 0.f, XMFLOAT2(128.f, 128.f));
m_SpriteBtch->Draw(m_texture.Get(), XMFLOAT2(400.f, 200.f), nullptr, Colors::White, 0.f, XMFLOAT2(128.f, 128.f));

m_SpriteBtch->End();


Sprite effect init in the Ctor :

auto Device = deviceResources->GetD3DDevice();
auto DeviceContext = deviceResources->GetD3DDeviceContext();

m_deviceResources->RegisterDeviceNotify(this);
m_SpriteBtch.reset(new SpriteBatch(DeviceContext));
m_State.reset(new CommonStates(Device));
m_effect.reset(new AlphaTestEffect(Device));

static const XMVECTORF32 s_eye = { 0.0f, 0.7f, 1.5f, 0.0f };
static const XMVECTORF32 s_at = { 0.0f, -0.1f, 0.0f, 0.0f };
static const XMVECTORF32 s_up = { 0.0f, 1.0f, 0.0f, 0.0f };
XMMATRIX view = XMMatrixLookAtRH(s_eye, s_at, s_up);


m_effect->SetWorld(XMMatrixScaling(1, -1, 1) * XMMatrixTranslation(0.f, 45.f, 0.f));
m_effect->SetView(view);
m_effect->SetProjection(XMMatrixTranslation(-0.5f, -0.5f, 0) * XMMatrixOrthographicOffCenterRH(0, 1360.f, 768.f, 0, 0, 1));
m_effect->SetReferenceAlpha(128);


DX::ThrowIfFailed(CreateWICTextureFromFile(Device, L"XNAPart.png", nullptr, m_texture.GetAddressOf()));
m_effect->SetTexture(m_texture.Get());


the sprite didn't draw, i honestly don't know how can i handle effects such as Alphatesteffect with the lspritebatch begin methode using lambda :/

any help would be great :D thank you kindly in advance ^^
Mar 7, 2015 at 3:43 AM
You need a custom input layout object that matches up the SpriteBatch generated vertex format for the AlphaTestEffect.
Microsoft::WRL::ComPtr<ID3D11InputLayout> m_inputLayout;

...

void const* shaderByteCode;
size_t byteCodeLength;

m_effect->GetVertexShaderBytecode(&shaderByteCode, &byteCodeLength);

DX::ThrowIfFailed(
        device->CreateInputLayout(VertexPositionColorTexture::InputElements,
                                  VertexPositionColorTexture::InputElementCount,
                                  shaderByteCode, byteCodeLength, &m_inputLayout)
    );
In your lambda with m_effect->Apply(dev), also use dev->IASetInputLayout(m_inputLayout.Get());