Draw PrimitiveBatch with texture?

Jan 4 at 10:08 PM
Hi. Is there a easy way to draw with PrimitiveBatch with texture?

I have tried to done this:
std::unique_ptr<DirectX::PrimitiveBatch<DirectX::VertexPositionColorTexture>>  m_batch;
And trying to modify End() method and copied some code from SpriteBatch:
ID3D11ShaderResourceView* currentTexture(m_nullTexture.Get());
m_batch->End(currentTexture);
But I dont know how to assign the texture, it just flickering when i trying to render PrimitiveBatch with different textures and so on. Anyone how know how I can do? :)
Coordinator
Jan 9 at 5:47 PM
Edited Jan 9 at 5:48 PM
PrimitiveBatch only manages a subset of state as noted on the wiki.

You can easily extend the basic drawing tutorial since it uses BasicEffect by using SetTexture on the BasicEffect before you call Apply. You will also need to set a sampler in slot 0. See BasicEffect for more details.

Something like:

During setup of BasicEffect, you need to make sure it selects the textured shader...
m_effect = std::make_unique<BasicEffect>(m_d3dDevice.Get());
m_effect->SetVertexColorEnabled(true);
m_effect->SetTextureEnabled(true);

void const* shaderByteCode;
size_t byteCodeLength;

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

DX::ThrowIfFailed(
        m_d3dDevice->CreateInputLayout(VertexPositionColorTexture::InputElements,
            VertexPositionColorTexture::InputElementCount,
            shaderByteCode, byteCodeLength,
            m_inputLayout.ReleaseAndGetAddressOf()));
During drawing with textures, you need a sampler
m_d3dContext->OMSetBlendState( m_states->Opaque(), nullptr, 0xFFFFFFFF );
m_d3dContext->OMSetDepthStencilState( m_states->DepthNone(), 0 );
m_d3dContext->RSSetState( m_states->CullNone() );

ID3D11SamplerState* samplers[] = { states->LinearWrap() };
m_d3dContextcontext->PSSetSamplers(0, 1, samplers);

// This sets the texture object to use
m_effect->SetTexture(m_myTexture.Get());
m_effect->Apply(m_d3dContext.Get());

m_d3dContext->IASetInputLayout(m_inputLayout.Get());

m_batch->Begin();

VertexPositionColorTexture v1(Vector3(0.f, 0.5f, 0.5f), Colors::White, Vector2(0,0));
VertexPositionColorTexture v2(Vector3(0.5f, -0.5f, 0.5f), Colors::White, Vector2(0,1));
VertexPositionColorTexture v3(Vector3(-0.5f, -0.5f, 0.5f), Colors::White, Vector2(1,0));

m_batch->DrawTriangle(v1, v2, v3);

m_batch->End();