in a wavefront object file (.obj) how am i supposed to render faces with more than 4 vertices in opengl?

后端 未结 3 963
别跟我提以往
别跟我提以往 2021-01-12 16:27

So using a Wavefront object file how am i supposed to render faces that have more than 4 vertices in OpenGL?

I understand that if it has 3 vertices I use GL_TR

相关标签:
3条回答
  • 2021-01-12 17:00

    First, you should tell any exporting tool to not export faces with that many vertices. Faces should have 3 vertices, period.

    If your exporting tool can't do that, then your loading tool should break the polygons down into 3 vertex faces. I'm fairly certain that the Asset Importer library can do that.

    0 讨论(0)
  • 2021-01-12 17:12

    OBJ exporters will export the vertices in a sane order for each face (anti-/clockwise), and long as your faces are coplanar and convex (which they bloody should be!) - you can use GL_TRIANGLE_FAN.

    I disagree with Nicol Bolas' point that faces should always have 3 vertices, although fool proof, if your polygons follow the above rules, using GL_TRIANGLE_FAN simplifies your code and reduces system memory consumption. Nothing will change GPU side as the polygons will be decomposed to triangles anyway.

    0 讨论(0)
  • 2021-01-12 17:14

    In practice, most wavefront-obj faces are coplanar and convex, but I can't find anything in the original OBJ specification saying this is guaranteed.

    If the face is coplanar and convex, you can either use GL_TRIANGLE_FAN, or you can use GL_TRIANGLE and manually evaluate the fan yourself. A fan has all triangles share the first vertex. Like this:

    // manually generate a triangle-fan
    for (int x = 1; x < (faceIndicies.Length-1); x++) {
        renderIndicies.Add(faceIndicies[0]);
        renderIndicies.Add(faceIndicies[x]);
        renderIndicies.Add(faceIndicies[x+1]);
    }
    

    If the number of vertices in an n-gon is large, using GL_TRIANGLE_STRIP, or manually forming your own triangle strips, can produce better visual results. But this is very uncommon in wavefront OBJ files.

    If the face is co-planar but concave, then you need to triangulate the face using an algorithm, such as the Ear-Clipping Method..

    http://en.wikipedia.org/wiki/Polygon_triangulation#Ear_clipping_method

    If the verticies are not-coplanar, you are screwed, because OBJ doesn't preserve enough information to know what shape tessellation was intended.

    0 讨论(0)
提交回复
热议问题