XNA-以正确的顺序绘制四边形(和图元) [英] XNA - Drawing Quads (and primitives) in right order

查看:79
本文介绍了XNA-以正确的顺序绘制四边形(和图元)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对XNA中的3D知识还很陌生,很不幸,我遇到了找不到解决方案的问题。 (甚至不知道问题出在哪里。)

I'm fairly new to 3D-stuff in XNA and unfortunately I have encountered a problem I can't find the solution for. (Don't even know what the problem is).

简而言之:我正在使用以下游戏在游戏中绘制四边形:

To cut it short: I am drawing quads in my game using:

        effect.World = Matrix.Identity *
            Matrix.CreateRotationX(Rotation.X) *
            Matrix.CreateRotationY(Rotation.Y) *
            Matrix.CreateRotationZ(Rotation.Z) *
            Matrix.CreateTranslation(Position);

        // Apply camera-matrixes
        effect.View = viewMatrix;
        effect.Projection = projectionMatrix;

        graphics.VertexDeclaration = vertDec;

        // Begin effect drawing
        effect.Begin();
        foreach (EffectPass pass in
            effect.CurrentTechnique.Passes)
        {
            pass.Begin();
            graphics.DrawUserIndexedPrimitives
                <VertexPositionNormalTexture>(
                PrimitiveType.TriangleList,
                quad.Vertices, 0, 4,
                quad.Indexes, 0, 2);
            pass.End();
        }

        effect.End();

我的效果还具有以下属性:

My effect also has these properties:

        this.effect.TextureEnabled = true;
        this.effect.Texture = texture;

它有效。我的四边形很漂亮,我很高兴。但是,有一个小问题。如果一个四边形位于另一个四边形或常规模型的后面,则确定绘制在顶部的顺序就是我绘制它们的顺序。

And it works. My quad is drawn nicely and me is happy. However there is one slight problem. If one quad is behind another quad or regular model the way that determines which is drawn on top is the order that I draw them in.

让我们说我在绘制四边形A之前Quad B,然后B将显示在Quad A的顶部,即使Z轴上的距离少40个单位。我必须说,看起来非常令人困惑! ;)

Lets say I draw Quad A before Quad B, then B will be shown as on top of Quad A even if it is 40 units lesser in the Z-axis. Looks very confusing I must say! ;)

现在我的常规网格物体没有出现这样的问题,这是在涉及到基元的情况下,事情变得如此混乱。绘制常规线条也有同样的问题:

Now I haven't had any problem like this with my regular meshes, it is when primitives are involved things get this messy. The very same problem goes for drawing regular lines:

            graphics.DrawUserIndexedPrimitives<VertexPositionColor>(
                PrimitiveType.LineStrip,
                pointList,
                0,  // vertex buffer offset to add to each element of the index buffer
                6,  // number of vertices in pointList
                new short[] { 0, 1 },  // the index buffer
                0,  // first index element to read
                1   // number of primitives to draw

所以我真正要问的是:

A)这正常吗?我是否应该构建一个特殊的引擎来决定何时绘制内容? 我可以在适当的时间通过绘制内容轻松地在项目中解决此问题,但是我可以想象一个我做不到的游戏。例如,一个fps不能在各处绘制广告牌! ;)

B)如果这不正常,可能是什么原因,什么是解决错误的解决方案/好的方法。

我希望能帮助您解决所有问题! :)

I would appreciate any help to sort this out! :)

推荐答案

听起来好像您已关闭深度缓冲区。也许您是在事先画精灵? (使用SpriteBatch将更改正常3D绘图所需的各种渲染状态。)

It sounds like you've got the depth buffer turned off. Perhaps you are drawing sprites before hand? (Using SpriteBatch will change various render states needed for normal 3D drawing.)

在绘制任何3D对象之前尝试添加以下代码行:

Try adding this line of code before you draw any 3D objects:

GraphicsDevice.RenderState.DepthBufferEnable = true;

相关链接:

  • SpriteBatch and renderstates (for XNA 3.1)
  • SpriteBatch and renderstates in XNA Game Studio 4.0
  • RenderState.DepthBufferEnable Property

有关深度缓冲区(或 Z缓冲区)的说明,您可以检查维基百科文章

For an explaination of the depth buffer (or "Z-buffer"), you could check the wikipedia article.

简要描述深度缓冲区:它基本上是您的位图图像该场景存储每个像素深入屏幕的深度(深度)。当您绘制第二个对象时,它将首先计算将要绘制的每个像素的深度-并且在区域中已经存在一个前面(深度值更近的像素)对象的区域中深度缓冲区,它只会跳过那些像素的绘制。

To describe the depth buffer briefly: It is basically a bitmap image of your scene that stores how far "into" the screen each pixel is (the depth). When you go to draw your second object, it will first calculate the "depth" of each pixel it is about to draw - and in the areas where there is already an object "in front" (pixels with a closer depth value) in the depth buffer, it will simply skip drawing for those pixels.

(您对常规网格没有问题的原因可能是背面剔除,并且您的网格没有从前到后的顺序绘制任何重叠位。)

(The reason you don't have problems with your regular meshes is probably thanks to backface-culling and your meshes not having any "overlapping bits" being drawn in a back-to-front order.)

作为调试此类错误的好方法(奇怪的深度缓冲区问题,错误的渲染状态以及大多数其他情况,您的渲染输出不是您期望的) ,我强烈建议安装 DirectX SDK 学习使用PIX

As for a good way to debug this kind of error (weird depth buffer problems, wrong render states, and most other cases where your rendered output is not what you expect), I strongly recommend installing the DirectX SDK and learning to use PIX.

这篇关于XNA-以正确的顺序绘制四边形(和图元)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆