|
Device->BeginScene();
RenderScene();
RenderMirror();
Device->EndScene();
Device-> resent(0, 0, 0, 0);
}
return true;
}
void RenderScene()
{
// draw teapot
Device->SetMaterial(&TeapotMtrl);
Device->SetTexture(0, 0);
D3DXMATRIX W;
D3DXMatrixTranslation(&W,
TeapotPosition.x,
TeapotPosition.y,
TeapotPosition.z);
Device->SetTransform(D3DTS_WORLD, &W);
Teapot->DrawSubset(0);
D3DXMATRIX I;
D3DXMatrixIdentity(&I);
Device->SetTransform(D3DTS_WORLD, &I);
Device->SetStreamSource(0, VB, 0, sizeof(Vertex));
Device->SetFVF(Vertex::FVF);
// draw the floor
Device->SetMaterial(&FloorMtrl);
Device->SetTexture(0, FloorTex);
Device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 2);
// draw the walls
Device->SetMaterial(&WallMtrl);
Device->SetTexture(0, WallTex);
Device->DrawPrimitive(D3DPT_TRIANGLELIST, 6, 4);
// draw the mirror
Device->SetMaterial(&MirrorMtrl);
Device->SetTexture(0, MirrorTex);
Device->DrawPrimitive(D3DPT_TRIANGLELIST, 18, 2);
}
void RenderMirror()
{
//
// Draw Mirror quad to stencil buffer ONLY. In this way
// only the stencil bits that correspond to the mirror will
// be on. Therefore, the reflected teapot can only be rendered
// where the stencil bits are turned on, and thus on the mirror
// only.
//
Device->SetRenderState(D3DRS_STENCILENABLE, true);
Device->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
Device->SetRenderState(D3DRS_STENCILREF, 0x1);
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_REPLACE);
// disable writes to the depth and back buffers
Device->SetRenderState(D3DRS_ZWRITEENABLE, false);
Device->SetRenderState(D3DRS_ALPHABLENDENABLE, true);
Device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO);
Device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
// draw the mirror to the stencil buffer
Device->SetStreamSource(0, VB, 0, sizeof(Vertex));
Device->SetFVF(Vertex::FVF);
Device->SetMaterial(&MirrorMtrl);
Device->SetTexture(0, MirrorTex);
D3DXMATRIX I;
D3DXMatrixIdentity(&I);
Device->SetTransform(D3DTS_WORLD, &I);
Device->DrawPrimitive(D3DPT_TRIANGLELIST, 18, 2);
// re-enable depth writes
Device->SetRenderState( D3DRS_ZWRITEENABLE, true );
// only draw reflected teapot to the pixels where the mirror
// was drawn to.
Device->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_EQUAL);
Device->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_KEEP);
// position reflection
D3DXMATRIX W, T, R;
D3DXPLANE plane(0.0f, 0.0f, 1.0f, 0.0f); // xy plane
D3DXMatrixReflect(&R, &plane);
D3DXMatrixTranslation(&T,
TeapotPosition.x,
TeapotPosition.y,
TeapotPosition.z);
W = T * R;
// clear depth buffer and blend the reflected teapot with the mirror
//Device->Clear(0, 0, D3DCLEAR_ZBUFFER, 0, 1.0f, 0);
Device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_DESTCOLOR);
Device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ZERO);
// Finally, draw the reflected teapot
Device->SetTransform(D3DTS_WORLD, &W);
Device->SetMaterial(&TeapotMtrl);
Device->SetTexture(0, 0);
Device->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW);
Teapot->DrawSubset(0);
// Restore render states.
Device->SetRenderState(D3DRS_ALPHABLENDENABLE, false);
Device->SetRenderState( D3DRS_STENCILENABLE, false);
Device->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);
}
为什么我把镜子的值设置为0的话,镜子前的茶壶就不会挡住镜子里的茶壶?设置为1的话就可以呢?
Device->SetRenderState(D3DRS_STENCILREF, 0x1);就是这句。最后哪里改为0x0;
模板缓存不是只是限制了渲染范围吗?就是下面这两种情况!


|
|