游戏开发论坛

 找回密码
 立即注册
搜索
查看: 4415|回复: 12

3D游戏程序设计入门中模板缓存的问题

[复制链接]

3

主题

16

帖子

16

积分

新手上路

Rank: 1

积分
16
发表于 2010-8-15 11:16:00 | 显示全部楼层 |阅读模式
                Device->BeginScene();

                RenderScene();

                RenderMirror();       

                Device->EndScene();
                Device-&gtresent(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;
模板缓存不是只是限制了渲染范围吗?就是下面这两种情况!






3

主题

16

帖子

16

积分

新手上路

Rank: 1

积分
16
 楼主| 发表于 2010-8-15 11:38:00 | 显示全部楼层

Re:3D游戏程序设计入门中模板缓存的问题

求助啊。

3

主题

16

帖子

16

积分

新手上路

Rank: 1

积分
16
 楼主| 发表于 2010-8-15 14:00:00 | 显示全部楼层

Re:3D游戏程序设计入门中模板缓存的问题

再顶。。。

180

主题

3511

帖子

3520

积分

论坛元老

Rank: 8Rank: 8

积分
3520
发表于 2010-8-15 16:04:00 | 显示全部楼层

Re:3D游戏程序设计入门中模板缓存的问题

Device->SetRenderState(D3DRS_STENCILFUNC,D3DCMP_EQUAL);

看似这一句很关键。
我没学过D3D,不懂。

3

主题

16

帖子

16

积分

新手上路

Rank: 1

积分
16
 楼主| 发表于 2010-8-15 19:27:00 | 显示全部楼层

Re:3D游戏程序设计入门中模板缓存的问题

额。

1

主题

266

帖子

280

积分

中级会员

Rank: 3Rank: 3

积分
280
发表于 2010-8-16 09:41:00 | 显示全部楼层

Re:3D游戏程序设计入门中模板缓存的问题

SetRenderState( D3DRS_STENCILREF, 0x1 ) 设置的是参考值

SetRenderState( D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE ) 如果测试通过就用参考值替换模板缓冲中的值
你还替换成0,说明模板缓存内容未被改变,还是0

SetRenderState( D3DRS_STENCILFUNC, D3DCMP_EQUAL ) 所有模板中等于参考值的地方都渲染
结合你上面的设置,这等于是让模板失去了标记功能,什么也不做,模型全被渲染

3

主题

16

帖子

16

积分

新手上路

Rank: 1

积分
16
 楼主| 发表于 2010-8-18 10:30:00 | 显示全部楼层

Re:3D游戏程序设计入门中模板缓存的问题

是啊。但是镜子中的就算设置为1还是会渲染镜子范围内的,设置为0也是会渲染,两个都一样。为什么会出现那种效果?

3

主题

16

帖子

16

积分

新手上路

Rank: 1

积分
16
 楼主| 发表于 2010-8-18 10:31:00 | 显示全部楼层

Re:3D游戏程序设计入门中模板缓存的问题

上面两图都是镜子范围内的。也就是说都会渲染啦。效果却不一样!

3

主题

16

帖子

16

积分

新手上路

Rank: 1

积分
16
 楼主| 发表于 2010-8-18 10:37:00 | 显示全部楼层

Re:3D游戏程序设计入门中模板缓存的问题

求助,谁能解释一下啊。是不是我什么地方理解错了?

3

主题

16

帖子

16

积分

新手上路

Rank: 1

积分
16
 楼主| 发表于 2010-8-18 14:00:00 | 显示全部楼层

Re:3D游戏程序设计入门中模板缓存的问题

深度缓存清楚为1.0后,墙、镜子、原茶壶不是可以认为是在同一深度1.0了吗?怎么会出现原茶壶挡住反射茶壶、而镜子却没有挡住反射茶壶这种情况呢?既然在同一深度,就应该一起挡住、或者一起被挡啊。

难道这个问题又要不了了之?就没有牛人肯指点一下吗?
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2025-6-3 05:29

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表