Faces with more than 3 vertices

Hi There. Just getting into OpenGL and have managed to read data from an .obj file and render it on screen. I’ve noticed though some .obj have 4 or various indices for the face like-
v -6.048903 1.856823 1.202985
v -6.028662 1.639770 1.364798

5797 vertices

g shroud
f 5049 5019 5021 5033 5035 4907 4909 5047
f 4951 4935 4937 4963 4965 4879 4881 4949
f 5124 5125 5123
f 5184 5185 5183
f 5214 5215 5213
f 5154 5155 5153
f 5077 5061 5063 4893 4895 5089 5091 5075
f 4993 4977 4979 4921 4923 5005 5007 4991
s 853
f 5127 5129 5126

I’m wondering what is the best way to handle these as I believe polygon primitives aren’t used anymore, neither are quads. I’ve done some Dr Google and it looks like a routine that does polygon to triangle is the way to go? Wondering if there is already a library or something within OpenGL?

Cheers
Peter

The best solution is to take this back to the modeling tool (or modeler) that gave it to you and have the modeling tool triangulate this mesh and spit out only triangles.

Failing that, if you can’t guarantee that the faces are convex, you end up having to:

[ol]
[li]triangulate the face geometry yourself (so you can rasterize triangles), or [/li][li]use a stencil-based method to fill concave polygons in OpenGL (where you rasterize the concave polygon using component possibly-overlapping triangles). [/li][/ol]
Note that the link only describes the base method for drawing one concave polygon. It is possible to batch multiple polygons using the same material together using double-sided stencil and stencil increment/decrement.

[QUOTE=palace;1292370]I’m wondering what is the best way to handle these as I believe polygon primitives aren’t used anymore, neither are quads. I’ve done some Dr Google and it looks like a routine that does polygon to triangle is the way to go? Wondering if there is already a library or something within OpenGL?
[/QUOTE]
If the polygon is convex (and if it isn’t, GL_POLYGON wouldn’t work even if you could use it), then you can treat it as a triangle fan. I.e. given N vertices:


for (int i = 0; i < N-2; i++)
    add_triangle(f[0], f[i+1], f[i+2]);

Note that in the case where N=3, this is equivalent to add_triangle(f[0], f[1], f[2]), and if N<3 it does nothing, so you can process every “f” line this way.

If it isn’t guaranteed to be convex, I’d follow Dark Photon’s suggestion to ask whoever created the model to fix it. There are algorithms to tessellate non-convex polygons into triangles (e.g. the GLU tessellator, which could be used in a stand-alone program which reads in an OBJ file and writes out a “fixed” OBJ file), but a human will typically do a better job.

Stencil-based rendering of non-convex polygons probably isn’t appropriate for the type of geometry you’re likely to get in an OBJ file.

Hi Guys. Thank you for the updates. I looked at doing simple triangulation as it looks like most of the models have standard convex polygons. The .obj models have just been downloaded randomly from online. Things like the teapot worked OK and it’s only when I was trying a few other “samples” where I saw the issue. I can probably import into Blender and export with triangulation, or I may just ignore anything with more than 3 indices or assume they have convex polygons and see how I go as I can see people doing “ear cut” type routines which is probably more than I need at the moment. Anyway it’s all a learning exercise.

Thanks again
Peter