问题
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_TRIANGLES
, if it has 4 I use GL_QUADS
, but if it has 5 or more, what am I supposed to use? Is there a standard?
回答1:
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.
回答2:
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.
回答3:
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.
来源:https://stackoverflow.com/questions/9147197/in-a-wavefront-object-file-obj-how-am-i-supposed-to-render-faces-with-more-th