模版
声明变量
ID3DXMesh* Teapot = 0; ID3DXMesh* Mirror = 0;
|
创建模版缓冲区
清除模版缓冲区
g_pd3dDevice->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER | D3DCLEAR_STENCIL, D3DCOLOR_XRGB(0, 0, 255), 1.0f, 0L);
|
模版参数
D3DRS_STENCILENABLE D3DRS_STENCILFAIL D3DRS_STENCILZFAIL D3DRS_STENCILPASS D3DRS_STENCILFUNC D3DRS_STENCILREF D3DRS_STENCILMASK D3DRS_STENCILWRITEMASK
|
绘制场景
D3DXMATRIX MirrorW, TeapotW; D3DXMatrixTranslation(&TeapotW, 1, 0, -2); g_pd3dDevice->SetTransform(D3DTS_WORLD, &TeapotW); g_pd3dDevice->SetMaterial(&red_mtrl); Teapot->DrawSubset(0);
D3DXMatrixTranslation(&MirrorW, 0.0f, 0.0f, 0.0f); g_pd3dDevice->SetTransform(D3DTS_WORLD, &MirrorW); g_pd3dDevice->SetMaterial(&white_mtrl); Mirror->DrawSubset(0);
g_pd3dDevice->SetRenderState(D3DRS_STENCILENABLE, true); g_pd3dDevice->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS); g_pd3dDevice->SetRenderState(D3DRS_STENCILREF, 0x1); g_pd3dDevice->SetRenderState(D3DRS_STENCILMASK, 0xffffffff); g_pd3dDevice->SetRenderState(D3DRS_STENCILWRITEMASK, 0xffffffff); g_pd3dDevice->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_KEEP); g_pd3dDevice->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_KEEP); g_pd3dDevice->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
g_pd3dDevice->SetRenderState(D3DRS_ZWRITEENABLE, false); g_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, true); g_pd3dDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO); g_pd3dDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
D3DXMatrixTranslation(&MirrorW, 0.0f, 0.0f, 0.0f); g_pd3dDevice->SetTransform(D3DTS_WORLD, &MirrorW); g_pd3dDevice->SetMaterial(&white_mtrl); Mirror->DrawSubset(0);
|
绘制镜像
g_pd3dDevice->Clear(0, 0, D3DCLEAR_ZBUFFER, 0, 1.0f, 0); g_pd3dDevice->SetRenderState(D3DRS_ZWRITEENABLE, true); g_pd3dDevice->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_EQUAL); g_pd3dDevice->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_KEEP); g_pd3dDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_DESTCOLOR); g_pd3dDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ZERO); g_pd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW);
D3DXPLANE planeXY(0.0f, 0.0f, 1.0f, 0.0f); D3DXMATRIX matReflect;
D3DXPlaneNormalize(&planeXY, &planeXY);
D3DXMatrixReflect(&matReflect, &planeXY); D3DXMatrixTranslation(&TeapotW, 1, 0, -2); D3DXMATRIX W = TeapotW * matReflect;
g_pd3dDevice->SetTransform(D3DTS_WORLD, &W); g_pd3dDevice->SetMaterial(&red_mtrl); Teapot->DrawSubset(0);
g_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, false); g_pd3dDevice->SetRenderState(D3DRS_STENCILENABLE, false); g_pd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);
|
效果
奇葩的Bug
D3DXMATRIX matReflect; D3DXPLANE planeXY(0.0f, 0.0f, 1.0f, 0.0f);
|
如果是先声明matReflect,会直接崩溃。。。