Convert quadriladerals in obj file to triangles?

At first it seemed like it was obvious ... Make 2 triangles on the face where 4 indexes were found, right? Value:

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 

..., in turn, needs to be converted to 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, will display correctly.
However, not all cases are as simple as splitting a 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 shown in the example above). 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 shared equally in the previous example ... So, I need some way to find out how to split a quadrangle into two faces of a triangle using correct indexes for the second person ...

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

+7
source share
2 answers

If you have 4 indexes, for example:

 0 1 2 3 

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

 0 1 2 0 2 3 

Try using some ASCII art to illustrate this:

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

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

In the general case, for faces with vertices n you create triangles:

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

If you do not insist on individual triangles, you can also use the GL_TRIANGLE_FAN primitives, which are still mostly OpenGL. Thus, you can draw any convex polygon with a triangle fan using the original index sequence. Thus, a triangle with a vertex sequence of 0 1 2 3 describes a quad in this case and is very easy to generalize to a face with more than four vertices.

Edit: since you still have problems, let's see how this applies to the example in your post. I will list the initial quad index sequence for each face and the index sequence for two triangles after splitting the quadrant.

 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) 

It looks right to me when I draw a cube. Remember to subtract 1 from the indexes for your use, as these are 1-based indexes, and you will almost certainly need 0-based indexes.

+12
source

Writing your own obj loader and reading the specification is very careful, the details in the "f" parameter are very vague, especially if some files contain the "f" line with 4 arguments to them.

It turns out that this is actually a triangular strip in an odd order. The correct conversion to triangles is 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: ABCDEFG 

There would be the following 5 triangles:

 ABC ACD BDE CEF DFG 
+1
source

Source: https://habr.com/ru/post/969470/


All Articles