将OBJ文件中的四极光转换为三角形? [英] Converting quadriladerals in an OBJ file into triangles?

查看:280
本文介绍了将OBJ文件中的四极光转换为三角形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,看起来很明显......在每个脸上找到2个三角形,找到4个索引,对吗?

含义如下:

  v 1.000000 1.000000 0.000000 
v -1.000000 1.000000 -0.000000
v 1.000000 -1.000000 0.000000
v -1.000000 -1.000000 -0.000000
f -4 -3 -2 -1

...反过来,需要转换成如下形式:

  v 1.000000 1.000000 0.000000 
v -1.000000 1.000000 -0.000000
v 1.000000 -1.000000 0.000000
v -1.000000 -1.000000 -0.000000
f -4 -3 -2
f -2 -3 -1

当然,这个特殊的例子会正确呈现。

然而,并非所有的情况都像把脸分割成两个脸一样简单(第一张脸包含原始脸的前三个顶点,第二张脸包含最后3个顶点,如上例所示)。例如:

  v 0.000000 1.000000 1.000000 
v 0.000000 0.000000 1.000000
v 1.000000 0.000000 1.000000
v 1.000000 1.000000 1.000000
v 0.000000 1.000000 0.000000
v 0.000000 0.000000 0.000000
v 1.000000 0.000000 0.000000
v 1.000000 1.000000 0.000000
f 1 2 3 4
f 8 7 6 5
f 4 3 7 8
f 5 1 4 8
f 5 6 2 1
f 2 6 7 3

在前面的例子中,这些面不能以相同的方式拆分...所以,我需要一些方法来知道如何将一个四边形面分成两个三角形面,同时使用第二张脸的正确指数...

这怎么能达到?
请注意我没有使用固定功能管道,因此使用GL_QUADS不是一种选择。我的渲染引擎几乎坚持使用GL_TRIANGLES。

解决方案

如果您有4个索引,例如:

  0 1 2 3 



<将两个三角形分割成前三个索引,第一个,第三个和第四个索引。在这个例子中:

  0 1 2 
0 2 3

让我们尝试一些ASCII艺术来说明这一点:

  3 ------- 2 
| / |
| / |
| / |
| / |
0 ------- 1

在这里您可以看到 0 1 2 3 作为四边形, 0 1 2 作为第一个三角形(右下角), 0 2 3 作为第二个三角形(左上角)。



更一般地说,对于 n 顶点,您生成三角形:

  0(i)(i + 1)[for i in 1 ..( n  -  2)] 

如果你不坚持单独的三角形,你也可以使用 GL_TRIANGLE_FAN 基元,它们仍然在核心OpenGL中。这样,您可以使用原始索引序列绘制具有三角形扇形的任何凸多边形。因此,一个顶点序列 0 1 2 3 的三角形风扇在这种情况下描述了四边形,并且很容易推广到具有四个以上顶点的面。



编辑:由于您仍然有问题,让我们看看这是如何适用于您的文章中的示例。我将列出每个面的四边形原始索引序列,以及分割四边形后两个三角形的索引序列。

  f 1 2 3 4  - > (1 2 3)(1 3 4)
f 8 7 6 5 - > (8 7 6)(8 6 5)
f 4 3 7 8 - > (4 3 7)(4 7 8)
f 5 1 4 8 - > (5 1 4)(5 4 8)
f 5 6 2 1 - > (5 6 2)(5 2 1)
f 2 6 7 3 - > (2 6 7)(2 7 3)

当我绘制立方体时,这看起来是正确的。请记住从索引中减去1以供您使用,因为这些索引是基于1的索引,并且几乎肯定需要基于0的索引。


At first, it seemed obvious... Make 2 triangles per face wherever 4 indices were found, right?
Meaning, the following:

v 1.000000 1.000000 0.000000
v -1.000000 1.000000 -0.000000
v 1.000000 -1.000000 0.000000
v -1.000000 -1.000000 -0.000000
f -4 -3 -2 -1

... would, in turn, need to be converted into something like:

v 1.000000 1.000000 0.000000
v -1.000000 1.000000 -0.000000
v 1.000000 -1.000000 0.000000
v -1.000000 -1.000000 -0.000000
f -4 -3 -2
f -2 -3 -1

This particular example, of course, would render correctly.
However, not all cases are as simple as splitting the face into two faces (where the first face contains the first three vertices of the original face, and the second face contains the last 3 vertices, as per the above example). Take the following cube, for example:

v 0.000000 1.000000 1.000000
v 0.000000 0.000000 1.000000
v 1.000000 0.000000 1.000000
v 1.000000 1.000000 1.000000
v 0.000000 1.000000 0.000000
v 0.000000 0.000000 0.000000
v 1.000000 0.000000 0.000000
v 1.000000 1.000000 0.000000
f 1 2 3 4
f 8 7 6 5
f 4 3 7 8
f 5 1 4 8
f 5 6 2 1
f 2 6 7 3

These faces cannot be split the same way in the previous example... So, I would need some way of knowing how to split a quadrilateral face into two triangle faces, whilst using the correct indices for the second face...

How can this be achieved? Please note that I am NOT using the fixed-function pipeline, and therefore, using GL_QUADS is NOT an option. My rendering engine is pretty much stuck on using GL_TRIANGLES only.

解决方案

If you have 4 indices, e.g.:

0 1 2 3

The division into two triangles would be one with the first 3 indices, and one with the first, third, and fourth. In this example:

0 1 2
0 2 3

Let's try some ASCII art to illustrate this:

3-------2
|      /|
|    /  |
|  /    |
|/      |
0-------1

Here you see 0 1 2 3 as the quad, 0 1 2 as the first triangle (bottom-right), and 0 2 3 as the second triangle (top left).

More generally, for faces with n vertices, you generate triangles:

0 (i) (i + 1)  [for i in 1..(n - 2)]

If you don't insist on separate triangles, you can also use GL_TRIANGLE_FAN primitives, which are still in core OpenGL. That way, you can draw any convex polygon with a triangle fan, using the original sequence of indices. So a triangle fan with vertex sequence 0 1 2 3 describes the quad in this case, and it very easily generalizes to faces with more than 4 vertices.

Edit: Since you still appear to have problems, let's see how this applies to the example in your post. I'll list the original index sequence of the quad for each face, and the index sequence for the two triangles after splitting the quad.

f 1 2 3 4 --> (1 2 3) (1 3 4)
f 8 7 6 5 --> (8 7 6) (8 6 5)
f 4 3 7 8 --> (4 3 7) (4 7 8)
f 5 1 4 8 --> (5 1 4) (5 4 8)
f 5 6 2 1 --> (5 6 2) (5 2 1)
f 2 6 7 3 --> (2 6 7) (2 7 3)

That looks correct to me when I draw the cube. Remember to subtract 1 from the indices for your use, since these are 1-based indices, and you will almost certainly need 0-based indices.

这篇关于将OBJ文件中的四极光转换为三角形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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