使用模板防止双倍混合需要注意的问题

龙书中介绍了平面阴影,并且用模板防止双倍混合。说白了就是防止阴影部分重复渲染引发阴影更黑。

但使用了下段代码后,阴影完全不见了。

Device->SetRenderState(D3DRS_STENCILENABLE, true);
Device->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_EQUAL);
Device->SetRenderState(D3DRS_STENCILREF, 0x0);
Device->SetRenderState(D3DRS_STENCILMASK, 0xffffffff);
Device->SetRenderState(D3DRS_STENCILWRITEMASK, 0xffffffff);
Device->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_KEEP);
Device->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_KEEP);
Device->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_INCR); // increment to 1

问题是清空缓存的时候必须加上D3DCLEAR_STENCIL标志位,添加后的代码

Device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER |D3DCLEAR_STENCIL, 0x00ccccff, 1.0f, 0);

posted @ 2012-02-05 06:48  重甲土拨鼠  阅读(191)  评论(0编辑  收藏  举报