Sign in to follow this  
Omnimagus

DirectX - Stencil buffer mirrors

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
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[i]->tPosition.x,
pObjectsToReflect[i]->tPosition.y,
pObjectsToReflect[i]->tPosition.z);

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

D3DXMATRIX matRotateX, matRotateY, matRotateZ;
D3DXMatrixRotationX(&matRotateX, pObjectsToReflect[i]->tRotations.x);
D3DXMatrixRotationY(&matRotateY, pObjectsToReflect[i]->tRotations.y);
D3DXMatrixRotationZ(&matRotateZ, pObjectsToReflect[i]->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[i]->tVertexIndex, 0, sizeof(CUSTOMVERTEX));
pDevice->SetIndices(pObjectsToReflect[i]->tIndicesIndex);

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

pDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW);

pDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, pObjectsToReflect[i]->nNumVertices, 0, pObjectsToReflect[i]->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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this