|
|
用stencil buffer实现镜像,场景绘制过程如下:
// 绘制地板、墙等
...
// 绘制镜子, 位于xy平面上
m_pDevice->SetMaterial( &m_MirrMtrl );
m_pDevice->SetTexture( 0, m_pTexMirr );
m_pDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 18, 2 );
// 绘制茶壶, <0.0f, 3.0f, -7.5f>
D3DXMatrixTranslation( &matTrans, m_TeapotPos.x, m_TeapotPos.y, m_TeapotPos.z );
matWorld = matTrans;
m_pDevice->SetTransform( D3DTS_WORLD, &matWorld );
m_pDevice->SetMaterial( &m_TeapotMtrl );
m_pDevice->SetTexture( 0, 0 );
m_pTeapot->DrawSubset( 0 );
// 设置stencil buffer,写入公式中的各个参数
m_pDevice->SetRenderState( D3DRS_STENCILENABLE, true );
m_pDevice->SetRenderState( D3DRS_STENCILREF, 0x1 );
m_pDevice->SetRenderState( D3DRS_STENCILMASK, 0xffffffff );
m_pDevice->SetRenderState( D3DRS_STENCILWRITEMASK, 0xffffffff );
m_pDevice->SetRenderState( D3DRS_STENCILFUNC, D3DCMP_ALWAYS );
m_pDevice->SetRenderState( D3DRS_STENCILZFAIL, D3DSTENCILOP_KEEP );
m_pDevice->SetRenderState( D3DRS_STENCILFAIL, D3DSTENCILOP_KEEP );
m_pDevice->SetRenderState( D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE );
m_pDevice->SetRenderState( D3DRS_ZWRITEENABLE, false );
m_pDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, true );
m_pDevice->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_ZERO );
m_pDevice->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_ONE );
// 绘制场景,镜子对应的stencil buffer清为1
D3DXMatrixIdentity( &matWorld );
m_pDevice->SetTransform( D3DTS_WORLD, &matWorld );
m_pDevice->SetStreamSource( 0, m_pVBScene, 0, sizeof( Vertex ) );
m_pDevice->SetFVF( Vertex::FVF );
m_pDevice->SetMaterial( &m_MirrMtrl );
m_pDevice->SetTexture( 0, m_pTexMirr );
m_pDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 18, 2 );
m_pDevice->SetRenderState( D3DRS_ZWRITEENABLE, true );
m_pDevice->SetRenderState( D3DRS_STENCILFUNC, D3DCMP_EQUAL );
m_pDevice->SetRenderState( D3DRS_STENCILPASS, D3DSTENCILOP_KEEP );
// 现在绘制镜子中的茶壶, 求反射矩阵
D3DXMATRIX matMirr;
D3DXPLANE plane( 0.0f, 0.0f, 1.0f, 0.0f );
D3DXMatrixReflect( &matMirr, &plane );
D3DXMatrixTranslation( &matTrans, m_TeapotPos.x, m_TeapotPos.y, m_TeapotPos.z );
matWorld = matTrans * matMirr;
m_pDevice->SetTransform( D3DTS_WORLD, &matWorld );
// 在这里清zbuffer为1,
m_pDevice->Clear( 0, 0, D3DCLEAR_ZBUFFER, 0, 1.0f, 0 );
m_pDevice->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_DESTCOLOR );
m_pDevice->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_ZERO );
// 绘制
m_pDevice->SetTexture( 0, 0 );
m_pDevice->SetMaterial( &m_TeapotMtrl );
m_pDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_CW );
m_pTeapot->DrawSubset( 0 );
m_pDevice->EndScene();
m_pDevice-> resent( 0, 0, 0, 0 );
我的问题是,清zbuffer后在原茶壶镜面对称的位置绘制镜像茶壶,为什么镜像茶壶没有覆盖离镜头最近的原茶壶的像素?? |
-
|