0

在我的项目中,我画了一个房子,它由墙壁、地板、镜子、一个用于打开或关闭消防系统的开关组成。一切都在这里进行得很好。最后,我想在镜子上画一个人的倒影,但是我添加了这个drawReflection功能后,事情就出错了,场景变得一团糟。为什么?我按照书上写的函数。

 void House::draw(float timeDelta)
   {
_device->SetFVF(d3d::Vertex::FVF);
_device->SetStreamSource(0, _vb, 0, sizeof(d3d::Vertex));

// draw wall

_device->SetMaterial(&d3d::WHITE_MTRL);
_device->SetTexture(0, _wall);
_device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 10);

// draw floor

_device->SetMaterial(&d3d::WHITE_MTRL);
_device->SetTexture(0, _floor);
_device->DrawPrimitive(D3DPT_TRIANGLELIST, 30, 2);

// draw mirror

_device->Clear(0, NULL, D3DCLEAR_ZBUFFER, 0, 1.0f, 0);
_device->SetRenderState(D3DRS_ALPHABLENDENABLE, true);
_device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ONE);
_device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ZERO);
_device->SetMaterial(&d3d::WHITE_MTRL);
_device->SetTexture(0, _mirror);
_device->DrawPrimitive(D3DPT_TRIANGLELIST, 36, 2);
_device->SetRenderState(D3DRS_ALPHABLENDENABLE, false);


// draw switch button

D3DXMATRIX T, Ry, P;

D3DXMatrixTranslation(&T, -4.95f, 2.0f, 3.0f);
D3DXMatrixRotationY(&Ry, D3DX_PI * 1.5f);
P = Ry * T;
_device->SetTransform(D3DTS_WORLD, &P);
_device->SetMaterial(&d3d::BLUE_MTRL);
_device->SetTexture(0, NULL);
_switch->DrawSubset(0);

// draw fire system

D3DXMATRIX I;
D3DXMatrixIdentity(&I);
_device->SetTransform(D3DTS_WORLD, &I);
if(_fSwitchOn)
{
    if(_fs->isDead())
        _fs->reset();
    _fs->update(timeDelta);
    _fs->render();
}

// draw reflection of person
//drawReflection();
}

但是在我添加drawReflection()功能之后,它变得一团糟。为什么? drawReflection()功能如下:

 void House::drawReflection()
 {
// enable stencil
_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);

// stop write to target buffer and depth buffer
_device->SetRenderState(D3DRS_ALPHABLENDENABLE, true);
_device->SetRenderState(D3DRS_SRCBLEND,         D3DBLEND_ZERO);
_device->SetRenderState(D3DRS_DESTBLEND,        D3DBLEND_ONE);
_device->SetRenderState(D3DRS_ZWRITEENABLE,     false);

// draw the mirror to stencil buffer
_device->SetFVF(d3d::Vertex::FVF);
_device->SetStreamSource(0, _vb, 0, sizeof(d3d::Vertex));
_device->SetMaterial(&d3d::WHITE_MTRL);
_device->SetTexture(0, _mirror);
D3DXMATRIX I;
D3DXMatrixIdentity(&I);
_device->SetTransform(D3DTS_WORLD, &I);
_device->DrawPrimitive(D3DPT_TRIANGLELIST, 36, 2);
_device->SetRenderState(D3DRS_ZWRITEENABLE,     true);

// transform the mesh
_device->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_EQUAL);
_device->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_KEEP);

D3DXMATRIX Reflect, S, T, P;
D3DXPLANE plane(0.0f, 0.0f, 1.0f, -5.0f);
D3DXMatrixReflect(&Reflect, &plane);
 // D3DXMatrixScaling(&S, 0.2f, 0.2f, 0.2f);
D3DXVECTOR3 pos;
_person->getPosition(&pos);
D3DXMatrixTranslation(&T, pos.x, pos.y, pos.z);

P = T * Reflect;
_device->Clear(0, NULL, D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0);
_device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_DESTCOLOR);
_device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ZERO);
_device->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW);
_device->SetTransform(D3DTS_WORLD, &P);
_person->draw();

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

 }
4

0 回答 0