游戏开发论坛

 找回密码
 立即注册
搜索
查看: 2313|回复: 6

模版 Stencil Mirror例程 的问题(附件见源码)

[复制链接]

36

主题

107

帖子

107

积分

注册会员

Rank: 2

积分
107
发表于 2007-9-12 08:42:00 | 显示全部楼层 |阅读模式
3D游戏程序设计入门这本书中的八章模版Stencil Mirror例程没搞懂,就是已经在RenderScene()中draw the mirror
void RenderScene()
{
     。。。。。。//省略了// draw the floor,// draw the walls
        // draw the mirror
        Device->SetMaterial(&MirrorMtrl);
        Device->SetTexture(0, MirrorTex);
        Device->DrawPrimitive(D3DPT_TRIANGLELIST, 18, 2);
}
       
怎么还要在 RenderMirror() draw the mirror 啊?我怎么去掉 RenderScene()中 的 draw the mirror 就看不到镜子啊?

void RenderMirror()
{
           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 );
//下面的代码省略
}
还有就是在RenderMirror()为什么要这个样子设置啊?我去掉下面的话之后也没错误发生啊
    Device->SetRenderState(D3DRS_STENCILZFAIL,     D3DSTENCILOP_KEEP);
Device->SetRenderState(D3DRS_STENCILFAIL,      D3DSTENCILOP_KEEP);

还要一个就是怎么要在 RenderMirror()中  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);

请各位师傅百忙之中指导一下。谢谢

sf_200791284223.rar

444.89 KB, 下载次数:

2

主题

429

帖子

435

积分

中级会员

Rank: 3Rank: 3

积分
435
发表于 2007-9-12 15:26:00 | 显示全部楼层

Re:模版 Stencil Mirror例程 的问题(附件见源码)

怎么还要在 RenderMirror() draw the mirror 啊
=====
那不在这里,在哪里draw?


D3DRS_STENCILZFAIL 是模板测试

Device->SetRenderState(D3DRS_ZWRITEENABLE, false);
这个就是关闭深度缓冲,剩下的3句是alpha混合。

不要光去看代码,先了解用到的相关技术,原理再来看。另外所有的函数,sdk里都有说明,有不明白的就去查查。

36

主题

107

帖子

107

积分

注册会员

Rank: 2

积分
107
 楼主| 发表于 2007-9-12 16:41:00 | 显示全部楼层

Re:模版 Stencil Mirror例程 的问题(附件见源码)

大哥,你可能没看清我的问题,是这个样子的?在就是已经在RenderScene()中draw the mirror 怎么还要在 RenderMirror() draw the mirror 啊?我怎么去掉 RenderScene()中 的 draw the mirror 就看不到镜子啊?

我知道Device->SetRenderState(D3DRS_ZWRITEENABLE, false);
这个就是关闭深度缓冲,剩下的3句是alpha混合。
但是我去掉他们程序运行起来也正确啊。

请各位师傅百忙之中指导一下。谢谢


还要一个就是怎么要在 RenderMirror()中  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);



2

主题

429

帖子

435

积分

中级会员

Rank: 3Rank: 3

积分
435
发表于 2007-9-12 20:25:00 | 显示全部楼层

Re:模版 Stencil Mirror例程 的问题(附件见源码)

所以说你没理解模版缓冲的原理。

RenderMirror() 里再次绘制,是绘制到模版缓冲里,不是为了显示。

Device->SetRenderState(D3DRS_STENCILWRITEMASK,0xffffffff);
Device->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_KEEP);
Device->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_KEEP);
你给的这些值都是默认值,有和没有当然没有什么实际影响。

Device->SetRenderState(D3DRS_ZWRITEENABLE, false);
Device->SetRenderState(D3DRS_ALPHABLENDENABLE, true);
Device->SetRenderState(D3DRS_SRCBLEND,  D3DBLEND_ZERO);
Device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
关闭深度缓冲是通常的做法,因为这里并不是用来显示。
注释掉没有影响是因为后面有Clear()。
而alpha的操作,由于src是0,dst是1,结果自然没有改变后缓冲。注释掉后应该有改变的,看不见是因为绘制的一模一样,你把其中一个改改,应该能看出不同。

这里的用法类似于gl的glColorMask。

36

主题

107

帖子

107

积分

注册会员

Rank: 2

积分
107
 楼主| 发表于 2007-9-13 10:27:00 | 显示全部楼层

Re: 模版 Stencil Mirror例程 的问题(附件见源码)

是这样吗?第一次的draw the mirror是绘制到Back buffer.
第二次的draw the mirror,由于Device->SetRenderState(D3DRS_STENCILENABLE,    true);
所以导致第二次的draw the mirror绘制到了stencil buffer。

36

主题

107

帖子

107

积分

注册会员

Rank: 2

积分
107
 楼主| 发表于 2007-9-13 16:41:00 | 显示全部楼层

Re:模版 Stencil Mirror例程 的问题(附件见源码)

是这个样子的吗?默认就是肯定吗?

2

主题

429

帖子

435

积分

中级会员

Rank: 3Rank: 3

积分
435
发表于 2007-9-14 01:39:00 | 显示全部楼层

Re:模版 Stencil Mirror例程 的问题(附件见源码)

是。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2025-6-21 18:15

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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