[ACCEPTED]-Converting quadriladerals in an OBJ file into triangles?-wavefront

Accepted answer
Score: 22

If you have 4 indices, e.g.:

0 1 2 3

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

0 1 2
0 2 3

Let's try some ASCII 22 art to illustrate this:

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

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

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

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

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

Edit: Since 10 you still appear to have problems, let's 9 see how this applies to the example in your 8 post. I'll list the original index sequence 7 of the quad for each face, and the index 6 sequence for the two triangles after splitting 5 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 4 draw the cube. Remember to subtract 1 from 3 the indices for your use, since these are 2 1-based indices, and you will almost certainly 1 need 0-based indices.

Score: 0

Writing my own obj loader and reading the 8 spec very carefully, the details on the 7 'f' parameter are very vague, especially 6 seeing some files contain 'f' lines with 5 > 4 arguments on them.

Turns out that these 4 are actually a triangle strip in an odd 3 order. Correct conversion to triangles is 2 as follows (psuedo code):

n = 0;
triangles[n++] = [values[0], values[1], values[2]];
for(i = 3; i < count(values); ++i)
  triangles[n++] = [
    values[i - 3],
    values[i - 1],
    values[i]
  ];

Example:

f: A B C D E F G

Would be 1 the following 5 triangles:

A B C
A C D
B D E
C E F
D F G

More Related questions