Reputation: 150
I've followed some tutorials on Sprite Rendering with DirectX 11, and understand them, but all the tutorials and samples seem to be centered around drawing 1 sprite. I'm trying to extend that, and be batch friendly.
The outcome so far, is that if I draw one sprite on top of another, the Alpha Transparency renders the background color instead of the sprite below. Does anyone know how to set this up properly?
Basically, this is my render loop:
void SpriteManager::render(){
ID3D11DeviceContext* deviceContext;
m_d3dDevice->GetImmediateContext(&deviceContext);
unsigned int stride = sizeof( Vertex );
unsigned int offset = 0;
float blendFactor[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
deviceContext->OMSetBlendState( m_alphaBlendState, blendFactor, 0xFFFFFFFF );
deviceContext->IASetInputLayout( m_inputLayout );
deviceContext->IASetVertexBuffers( 0, 1, &m_vertexBuffer, &stride, &offset );
deviceContext->IASetPrimitiveTopology( D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST );
deviceContext->VSSetShader( m_vShader, 0, 0 );
deviceContext->PSSetShader( m_pShader, 0, 0 );
deviceContext->PSSetSamplers( 1, 1, &m_sampler ); //Using S1 Register
for( unsigned int i = 0; i != m_sprites.size(); ++i )
{
deviceContext->PSSetShaderResources( 1, 1, &(m_sprites[i]->m_textureRV) );
XMMATRIX& world = m_sprites[i]->getWorldMatrix();
XMMATRIX& mvp = XMMatrixMultiply( world, vpMatrix_ );
mvp = XMMatrixTranspose( mvp );
deviceContext->UpdateSubresource( mvpCB_, 0, 0, &mvp, 0, 0 ); //Update ViewProjection Matrix in Shader
deviceContext->Draw( 6, 0 );
}
}
Upvotes: 1
Views: 1566
Reputation: 150
I solved this, it turns out I was missing 1 piece, which was setting the Depth Stencil State in my render loop and adding it to the SpriteManager class. I was working off an example in the book Beginning DirectX 11 Game Programming, which does not set the Depth Stencil State at all in the Sprite sample. Hopefully this is useful info for others as well.
D3D11_DEPTH_STENCIL_DESC dsDesc;
dsDesc.DepthEnable = FALSE;
dsDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
dsDesc.DepthFunc = D3D11_COMPARISON_LESS;
dsDesc.StencilEnable = false;
dsDesc.StencilReadMask = D3D11_DEFAULT_STENCIL_READ_MASK;
dsDesc.StencilWriteMask = D3D11_DEFAULT_STENCIL_WRITE_MASK;
dsDesc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_KEEP;
dsDesc.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
dsDesc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_REPLACE;
dsDesc.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
dsDesc.BackFace = dsDesc.FrontFace;
device->CreateDepthStencilState(&dsDesc, &m_depthStencilState);
In Render Loop, I now call this right after setting blend state:
deviceContext->OMSetDepthStencilState(m_depthStencilState, 0);
Upvotes: 2