游戏开发论坛

 找回密码
 立即注册
搜索
查看: 2019|回复: 0

请教关于龙书第八章阴影重绘的问题~~~

[复制链接]

1

主题

1

帖子

0

积分

新手上路

Rank: 1

积分
0
发表于 2011-11-20 13:14:00 | 显示全部楼层 |阅读模式
bool Display(float timeDelta)
{
        if( Device )
        {
                       .......(省略)
               
                //把模版缓存和目标(后缓存)以及深度缓存一起清除为0
                Device->Clear(0, 0,
                        D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER | D3DCLEAR_STENCIL,
                        0xff000000, 1.0f, 0L);

                Device->BeginScene();

                RenderScene();

                RenderShadow();

                RenderMirror();       

                Device->EndScene();
                Device-&gtresent(0, 0, 0, 0);
        }
        return true;
}

void RenderShadow()
{
        //将模版比较运算设为D3DCMP_EQUAL且将D3DRS_STENCILREF渲染状态设置为0x0,
        //因此假如在模版缓存中相应的值为0x0,那么就指定渲染阴影到后缓存中
        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);


        // 计算阴影变换并将它放置到场景中适当的位置
        /*D3DXVECTOR4 lightDirection(0.707f, -0.707f, 0.707f, 0.0f);
        D3DXPLANE groundPlane(0.0f, -1.0f, 0.0f, 0.0f);

        D3DXMATRIX S;
        D3DXMatrixShadow(
                &S,
                &lightDirection,
                &groundPlane);

        D3DXMATRIX T;
        D3DXMatrixTranslation(
                &T,
                TeapotPosition.x,
                TeapotPosition.y,
                TeapotPosition.z);

        D3DXMATRIX W = T * S;
        Device->SetTransform(D3DTS_WORLD, &W);*/
        D3DXVECTOR4  lightDirection(0.0f,-0.707f,0.707f,0.0f);
        D3DXPLANE groundPlane(0.0f,-1.0f,0.0f,0.0f);

        D3DXMATRIX S;
        D3DXMatrixShadow(
                &S,
                &lightDirection,
                &groundPlane);

        D3DXMATRIX T;
        D3DXMatrixTranslation(
                &T,
                TeapotPosition.x,
                TeapotPosition.y,
                TeapotPosition.z);

        D3DXMATRIX W = T*S;
        Device->SetTransform(D3DTS_WORLD,&W);

        //最后,我们设置一个50%透明度的黑色材质,关闭深度测试,
        //渲染阴影,然后开启深度缓存同时关闭alpha混合和模版测试

        // alpha blend the shadow
        /*Device->SetRenderState(D3DRS_ALPHABLENDENABLE, true);
        Device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
        Device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);

        D3DMATERIAL9 mtrl = d3d::InitMtrl(d3d::BLACK, d3d::BLACK, d3d::BLACK, d3d::BLACK, 0.0f);
        mtrl.Diffuse.a = 0.5f; // 50% transparency.*/

        Device->SetRenderState(D3DRS_ALPHABLENDENABLE, true);
        Device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
        Device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);

        D3DMATERIAL9 mtrl = d3d::InitMtrl(d3d::BLACK,d3d::BLACK,d3d::BLACK,d3d::BLACK,0.0f);
    mtrl.Diffuse.a = 0.5f;

        // Disable depth buffer so that z-fighting doesn't occur when we
        // render the shadow on top of the floor.
        /*Device->SetRenderState(D3DRS_ZENABLE, false);

        Device->SetMaterial(&mtrl);
        Device->SetTexture(0, 0);
        Teapot->DrawSubset(0);

        Device->SetRenderState(D3DRS_ZENABLE, true);
        Device->SetRenderState(D3DRS_ALPHABLENDENABLE, false);
        Device->SetRenderState(D3DRS_STENCILENABLE,    false);*/
        Device->SetRenderState(D3DRS_ZENABLE, false);
        Device->SetMaterial(&mtrl);
        Device->SetTexture(0,0);
        Teapot->DrawSubset(0);

        Device->SetRenderState(D3DRS_ZENABLE, true);
        Device->SetRenderState(D3DRS_ALPHABLENDENABLE, false);
        Device->SetRenderState(D3DRS_STENCILENABLE,    false);
}

为什么要用模板缓存防止阴影重绘(双倍混合)?阴影重绘怎么来的?是因为重复调用了Display()令阴影颜色加深?但是它每次不是清除模板缓存了吗?新人不懂啊,求大侠解 ~~[em4]
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2025-6-9 14:39

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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