我已经学习了一些关于使用DirectX 11进行Sprite渲染的教程,并对它们进行了解,但所有教程和示例似乎都以绘制1精灵为中心。我正在尝试扩展它,并且是批处理友好的。
到目前为止的结果是,如果我在另一个上面绘制一个精灵,Alpha透明度渲染背景颜色而不是下面的精灵。有谁知道如何正确设置?
基本上,这是我的渲染循环:
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 );
}
}
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 );
}
}
答案 0 :(得分:2)
我解决了这个问题,结果发现我丢失了1件,这是在我的渲染循环中设置Depth Stencil State并将其添加到SpriteManager类中。我正在使用Beginning DirectX 11游戏编程一书中的一个例子,它没有在Sprite样本中设置深度模板状态。希望这对其他人来说也是有用的信息。
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);
在Render Loop中,我现在在设置混合状态后立即调用它:
deviceContext->OMSetDepthStencilState(m_depthStencilState, 0);