在DirectX 9中,如何让zbuffer正常工作?

发布于 2024-08-18 11:39:11 字数 2739 浏览 1 评论 0原文

我未能成功获得打开阴影的简单立方体几何图形以正确显示。

这是 C# 代码,但值通过 SlimDX 直接传递到 C++ 代码。

            pParams.BackBufferWidth = 0;
            pParams.BackBufferHeight = 0;
            pParams.BackBufferCount = 1;
            pParams.BackBufferFormat = Format::X8R8G8B8;
            pParams.Multisample = MultisampleType::None;
            pParams.MultisampleQuality = 0;
            pParams.DeviceWindowHandle = this.Handle;
            pParams.Windowed = true;
            pParams.AutoDepthStencilFormat = Format.D24X8;
            pParams.EnableAutoDepthStencil = true;
            pParams.PresentFlags = PresentFlags.None;
            pParams.FullScreenRefreshRateInHertz = 0;
            pParams.PresentationInterval = PresentInterval.Immediate;
            pParams.SwapEffect = SwapEffect.Discard;

...是 PresentParameter 结构中用于设置 Direct3D9Device 对象的值。

在渲染过程中,SetRenderState 的调用方式如下:

        this.D3DDevice.Clear(ClearFlags.Target | ClearFlags.ZBuffer, this.BackColor, 10000.0f, 0);
        this.D3DDevice.SetRenderState(RenderState.Ambient, false);
        this.D3DDevice.SetRenderState(RenderState.ZEnable, ZBufferType.UseZBuffer);
        this.D3DDevice.SetRenderState(RenderState.ZWriteEnable, true);
        this.D3DDevice.SetRenderState(RenderState.ZFunc, Compare.LessEqual);
        this.D3DDevice.BeginScene();

同样,这会传递到 C++ 代码,该代码将值编组到 C++ 程序员不会担心的调用中。

图元是漫反射彩色顶点 (D3DFVF_XYZ | D3DFVF_DIFFUSE)。线框视图如下所示: 线框视图 http://gallery.me.com/robert。 perkins/100045/z-fightingwireframe/web.jpg

较近的一对较大三角形是立方体的近面。

填充后的视图如下所示: 完整视图 1 http://gallery.me.com/robert .perkins/100045/Z-fighting/web.jpg

或者,在后续渲染调用中: 完整视图 2 http://gallery.me.com/robert.perkins /100045/zfight2/web.jpg

我不知道如何解决这个问题。我应该从哪里开始寻找?

编辑:对于其中一帧,相机投影矩阵看起来像这样:

{[[M11:0.6281456 M12:0.7659309 M13:0.1370506 M14:0] 
  [M21:0.7705086 M22:-0.5877584 M23:-0.2466911 M24:0] 
  [M31:-0.1083957 M32:0.2605566 M33:-0.9593542 M34:0] 
  [M41:-3.225646 M42:-1.096823 M43:20.91392 M44:1]]}

并且,视图矩阵看起来像这样:

camera.ViewMatrix = {[[M11:0.6281456 M12:0.7659309 M13:0.1370506 M14:0] 
                      [M21:0.7705086 M22:-0.5877584 M23:-0.2466911 M24:0] 
                      [M31:-0.1083957 M32:0.2605566 M33:-0.9593542 M34:0] 
                      [M41:-3.225646 M42:-1.096823 M43:20.91392 M44:1]]}

I've been unsuccessful at getting a simple cube geometry with shading turned on to display correctly.

This is c# code, but the values are being passed through SlimDX directly to C++ code.

            pParams.BackBufferWidth = 0;
            pParams.BackBufferHeight = 0;
            pParams.BackBufferCount = 1;
            pParams.BackBufferFormat = Format::X8R8G8B8;
            pParams.Multisample = MultisampleType::None;
            pParams.MultisampleQuality = 0;
            pParams.DeviceWindowHandle = this.Handle;
            pParams.Windowed = true;
            pParams.AutoDepthStencilFormat = Format.D24X8;
            pParams.EnableAutoDepthStencil = true;
            pParams.PresentFlags = PresentFlags.None;
            pParams.FullScreenRefreshRateInHertz = 0;
            pParams.PresentationInterval = PresentInterval.Immediate;
            pParams.SwapEffect = SwapEffect.Discard;

... are the values in the PresentParameter struct used to set up my Direct3D9Device object.

During a rendering, SetRenderState is called as follows:

        this.D3DDevice.Clear(ClearFlags.Target | ClearFlags.ZBuffer, this.BackColor, 10000.0f, 0);
        this.D3DDevice.SetRenderState(RenderState.Ambient, false);
        this.D3DDevice.SetRenderState(RenderState.ZEnable, ZBufferType.UseZBuffer);
        this.D3DDevice.SetRenderState(RenderState.ZWriteEnable, true);
        this.D3DDevice.SetRenderState(RenderState.ZFunc, Compare.LessEqual);
        this.D3DDevice.BeginScene();

Again, this is passed through to C++ code, which marshals the values in to calls a C++ programmer would not fear.

The primitives are diffuse colored vertices (D3DFVF_XYZ | D3DFVF_DIFFUSE). The wireframe view looks like this:
wireframe view http://gallery.me.com/robert.perkins/100045/z-fightingwireframe/web.jpg

The nearer pair of larger triangles is the near face of a cube.

The filled view looks like this:
full view 1 http://gallery.me.com/robert.perkins/100045/Z-fighting/web.jpg

Or this, on a subsequent rendering call:
full view 2 http://gallery.me.com/robert.perkins/100045/zfight2/web.jpg

I'm not sure how to fix this. Where should I begin looking?

Edit: The camera projection matrix looks about like this for one of the frames:

{[[M11:0.6281456 M12:0.7659309 M13:0.1370506 M14:0] 
  [M21:0.7705086 M22:-0.5877584 M23:-0.2466911 M24:0] 
  [M31:-0.1083957 M32:0.2605566 M33:-0.9593542 M34:0] 
  [M41:-3.225646 M42:-1.096823 M43:20.91392 M44:1]]}

And, the view matrix looks like this:

camera.ViewMatrix = {[[M11:0.6281456 M12:0.7659309 M13:0.1370506 M14:0] 
                      [M21:0.7705086 M22:-0.5877584 M23:-0.2466911 M24:0] 
                      [M31:-0.1083957 M32:0.2605566 M33:-0.9593542 M34:0] 
                      [M41:-3.225646 M42:-1.096823 M43:20.91392 M44:1]]}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

黒涩兲箜 2024-08-25 11:39:11

将 Z 缓冲区清除为 1.0f,而不是 10000.0f。

从 SDK 中的 Clear 文档:

[in] Clear the depth buffer to this new z value which ranges from 0 to 1.. 

查看投影矩阵和视口设置也可能很有用...

编辑:如何构建投影矩阵?您已将 zNear 设置为 0,将 zFar 设置为 1。尝试将 zNear 设置为 0.001f,将 zFar 设置为 1000.0f,看看这是否对您有帮助...

Clear the Z-Buffer to 1.0f not 10000.0f.

From the Clear docs in the SDK:

[in] Clear the depth buffer to this new z value which ranges from 0 to 1.. 

It may also be useful to see your projection matrix and viewport settings ...

Edit: How do you build that projection matrix? You have set zNear to 0 and zFar to 1. Try setting your zNear to 0.001f and zFar to 1000.0f and see whether that helps you at all...

一袭水袖舞倾城 2024-08-25 11:39:11

预感:在清除之前尝试启用 Z 缓冲区。

A hunch: Try enabling the Z-Buffer before you clear.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文