跳转至

切换 Z 缓冲

GameHackingExamples/Chapter8_Direct3DHook 中 main.cpp 的 Direct3D 钩子在 onDrawIndexedPrimitive() 函数中有这个示例透视:

void onDrawIndexedPrimitive(
    DirectXHook* hook,
    LPDIRECT3DDEVICE9 device,
    D3DPRIMITIVETYPE primType,
    INT baseVertexIndex, UINT minVertexIndex,
    UINT numVertices, UINT startIndex, UINT 
primCount)
{
    if (numVertices == 24 && primCount == 12) {
        // 是敌人,执行透视
    }
}

这个函数用作游戏 Direct3D 设备 VF 表索引 82 处 DrawIndexedPrimitive() 钩子的回调。游戏绘制的每个模型都会经过这个函数,并伴随一些模型特定的属性。通过检查属性子集(即 numVertices 和 primCount 值),钩子检测何时绘制敌方模型并开始透视。在这个示例中,代表敌方模型的值是 24 和 12。

神奇发生在 if() 语句内部。只用几行代码,透视就可以忽略 Z 缓冲绘制模型,像这样:

device->SetRenderState(D3DRS_ZENABLE, false); // 

用 Z 缓冲

DirectXHook::origDrawIndexedPrimitive( // 绘制模型
    minVertexIndex, numVertices, startIndex, 
primCount);
device->SetRenderState(D3DRS_ZENABLE, true); // 启用

Z 缓冲

简单来说,这段代码在绘制敌方模型时禁用 Z 缓冲,之后重新启用。Z 缓冲关闭时,敌人会绘制在所有东西前面。