简体   繁体   English

如何对3D引擎进行Z排序?

[英]How do I Z-sort triangles for a 3D engine?

I'm building a small 3D engine for a game I'm working on. 我正在为我正在进行的游戏构建一个小型3D引擎。 I've got my basics sorted: textured triangles with backface culling. 我已经将我的基本知识排序了:带有背面剔除的纹理三角形。 However depth sorting is proving to be a difficult problem. 然而,深度分类被证明是一个难题。

I'm calculating the face Z by averaging out the 3 points that make up the triangular face. 我通过平均构成三角形面的3个点来计算面部Z. The longer faces sometimes overlap the smaller faces since they have a larger Z value and therefore rise up in the depth sorted display list. 较长的面有时会与较小的面重叠,因为它们具有较大的Z值,因此在深度排序的显示列表中上升。

How do I fix this? 我该如何解决? I'm sure there are known depth sorting techniques if I can only get some practical help in programming them. 如果我只能在编程方面得到一些实际的帮助,我肯定有已知的深度排序技术。 I've build the render pipeline myself so I have access to all the required data - triangles, points, textures, UV coordinates, etc. 我自己构建了渲染管道,因此我可以访问所有需要的数据 - 三角形,点,纹理,UV坐标等。

Cathedral rendered in a 3D program 大教堂在3D程序中呈现

替代文字

Cathedral rendered in my 3D engine 大教堂在我的3D引擎中呈现

替代文字

You choices are either to: 您可以选择:

  1. Subdivide your meshes so that you can sort each polygon reliably (but there are still horrible edge cases that you may or may not see). 细分网格,以便可以可靠地对每个多边形进行排序(但是仍然存在可能或可能看不到的可怕边缘情况)。

  2. Use a Z-Buffer , which is supported by all graphics hardware and is essentially free. 使用Z-Buffer ,它受所有图形硬件支持,基本上是免费的。

You need to subdivide your triangles so that they are all roughly the same size - regardless of whether you do the sorting yourself or employ a z-buffer. 您需要细分三角形,使它们的大小大致相同 - 无论您是自己进行排序还是使用z缓冲区。 Unless, of course, the z-buffer algorithm also splits the long thin triangles up for you. 当然,除非z-buffer算法也为你分割长的细长三角形。

The problem is that if you've got some small compact triangles and some long thin ones (for example) the algorithm will miss classify the long thin ones more often than not. 问题在于,如果你有一些小的紧凑三角形和一些细长的三角形(例如),算法将会错过对长薄的三角形进行分类。 If you use the mid point of the triangle there will be view points where it will be regarded as "in front" of a more compact one when in fact if really is behind. 如果你使用三角形的中点,那么它将被视为一个更紧凑的“在前面”的视点,实际上如果真的落后了。 Take this top down view where + represents the mid point. 从上往下看, +表示中点。

            o

-+-            1
-----+------   2
         -+-   3

*

Looking from * to o the large triangle (2) could be interpreted as being in front of the small triangle (3) and hence be drawn on top of it. *o ,大三角形(2)可以解释为位于小三角形(3)的前面,因此可以在它上面绘制。

If (2) was split into 3 or 4 smaller triangles then the z-buffering would work more of the time. 如果(2)被分成3或4个较小的三角形,则z缓冲将更多地工作。

The corner case that complicates any triangle sorting algorithm is represented by the following diagram: 使任何三角形排序算法复杂化的角点情况如下图所示:

不整齐的三角形

Each of the triangles is in front of one triangle and behind the other. 每个三角形位于一个三角形的前面,而在另一个三角形的后面。 I had to do some very simple tricks in inkscape just to create this diagram. 我必须在inkscape中做一些非常简单的技巧才能创建这个图表。

It is not hard to arrange polygons in 3D such that you have a cycle in the "in front of" graph. 在3D中排列多边形并不困难,以便在“前面”图形中有一个循环。 To solve this problem your algorithm would need the ability to subdivide the triangles to break the cycle. 要解决此问题,您的算法需要能够细分三角形以打破循环。

This is one of the reasons Z buffers are so popular (that and they are easily accelerated in hardware). 这是Z缓冲区如此受欢迎的原因之一(它们很容易在硬件中加速)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM