• Advertisement
Sign in to follow this  

DirectX - Stencil buffer mirrors

This topic is 3137 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am trying to use the stencil buffer to create a mirror. Everything seems to be showing up on the mirror, but my program draws all pixels for all objects even ones that should be blocked by other objects. So the end result is that objects seems semi-transparent because objects that are behind them are showing up in the reflection.

Share this post


Link to post
Share on other sites
Advertisement
If you are reversing your vertex winding and mirroring the vertices around the mirror plane in the vertex shader, then you should get the proper visibility information from your z-buffer. Do you still have the z-buffer writes enabled when you are rendering to the mirror surface?

Share this post


Link to post
Share on other sites
I knew I forgot to add something to my question. Here is my code for creating the mirror.
Mirror

void RenderMirror(LPDIRECT3DDEVICE9 pDevice, D3D_OBJECT* pMirror, vector<D3D_OBJECT*> pObjectsToReflect)
{
pDevice->SetRenderState(D3DRS_STENCILENABLE, true);
pDevice->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
pDevice->SetRenderState(D3DRS_STENCILREF, 0x1);
pDevice->SetRenderState(D3DRS_STENCILMASK, 0xffffffff);
pDevice->SetRenderState(D3DRS_STENCILWRITEMASK, 0xffffffff);
pDevice->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_KEEP);
pDevice->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_KEEP);
pDevice->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);

pDevice->SetRenderState(D3DRS_ZWRITEENABLE, true);
pDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, true);
pDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO);
pDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);

pDevice->SetStreamSource(0, pMirror->tVertexIndex, 0, sizeof(CUSTOMVERTEX));
pDevice->SetIndices(pMirror->tIndicesIndex);

pDevice->SetFVF(CUSTOMFVF);
pDevice->SetMaterial(&pMirror->pMaterial);
pDevice->SetTexture(0, pMirror->tTextureIndex);
D3DXMATRIX matTransforms;
D3DXMatrixIdentity(&matTransforms);
D3DXMatrixTranslation(&matTransforms,
pMirror->tPosition.x,
pMirror->tPosition.y,
pMirror->tPosition.z);

D3DXMATRIX matScale;
D3DXMatrixScaling(&matScale, pMirror->tScale.x,pMirror->tScale.x, pMirror->tScale.x);
D3DXMATRIX matRotateX, matRotateY, matRotateZ;
D3DXMatrixRotationX(&matRotateX, pMirror->tRotations.x);
D3DXMatrixRotationY(&matRotateY, pMirror->tRotations.y);
D3DXMatrixRotationZ(&matRotateZ, pMirror->tRotations.z);
matTransforms = matTransforms * matRotateX * matRotateY * matRotateZ * matScale;
pDevice->SetTransform(D3DTS_WORLD, &matTransforms);
pDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, pMirror->nNumVertices, 0, pMirror->nNumIndices / 3);

pDevice->SetRenderState( D3DRS_ZWRITEENABLE, true );
pDevice->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_EQUAL);
pDevice->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_KEEP);

for(int i = 0; i < pObjectsToReflect.size(); i++)
{
D3DXMATRIX matTransforms, matReflect;
D3DXPLANE plane(0.0f, 0.0f, 1.0f, 0.0f); // xy plane
D3DXMatrixReflect(&matReflect, &plane);

D3DXMatrixTranslation(&matTransforms,
pObjectsToReflect->tPosition.x,
pObjectsToReflect->tPosition.y,
pObjectsToReflect->tPosition.z);

D3DXMATRIX matScale;
D3DXMatrixScaling(&matScale, pObjectsToReflect->tScale.x, pObjectsToReflect->tScale.x, pObjectsToReflect->tScale.x);

D3DXMATRIX matRotateX, matRotateY, matRotateZ;
D3DXMatrixRotationX(&matRotateX, pObjectsToReflect->tRotations.x);
D3DXMatrixRotationY(&matRotateY, pObjectsToReflect->tRotations.y);
D3DXMatrixRotationZ(&matRotateZ, pObjectsToReflect->tRotations.z);
matTransforms = (matTransforms * matRotateX * matRotateY * matRotateZ * matScale) * matReflect;

pDevice->Clear(0, 0, D3DCLEAR_ZBUFFER, 0, 1.0f, 0);
pDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_DESTCOLOR);
pDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ZERO);

pDevice->SetStreamSource(0, pObjectsToReflect->tVertexIndex, 0, sizeof(CUSTOMVERTEX));
pDevice->SetIndices(pObjectsToReflect->tIndicesIndex);

pDevice->SetTransform(D3DTS_WORLD, &matTransforms);
pDevice->SetMaterial(&pObjectsToReflect->pMaterial);
pDevice->SetTexture(0, pObjectsToReflect->tTextureIndex);

pDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW);

pDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, pObjectsToReflect->nNumVertices, 0, pObjectsToReflect->nNumIndices / 3);
pDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO);
pDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);

}
pDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, false);
pDevice->SetRenderState( D3DRS_STENCILENABLE, false);
pDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);
}





[Edited by - Omnimagus on July 21, 2009 10:48:17 AM]

Share this post


Link to post
Share on other sites
Why you clear z buffer every time in loop "for(int i = 0; i < pObjectsToReflect.size(); i++)"? It seems all the object will pass z test.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement