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

3

7

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?

Brainbrainard answered 5/2, 2012 at 5:40 Comment(1)
GL_POLYGON ? I don't think that OBJs typically have concave polygons, if they do, you need to tessellate it first. For tessellating you can use the gluTesselator: glprogramming.com/red/chapter11.htmlMaharanee
U
8

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.

Ulbricht answered 5/2, 2012 at 9:26 Comment(6)
Nicol Bolas is right. Faces should consist of 3 vertices each. Triangles are the only non-ambigous primitives in 3D space. Anything else you can't specify in a sane form. And rasterizers work on triangles only, so they have to break it down. But in which way shall they do it? Only by using triangles in the first place you have the full control over this.Romeliaromelle
If a polygon is specified with a proper winding order (which the OBJ format is specific about), is concave, and coplanar; I see no way the GPU could create visible artifacts - so what's the point limiting your importer?Ulbricht
The OBJ format doesn't enforce coplanarity. How do you render the following quad: (-1, -1, -1), (1, 1, -1), (1, -1, 1), (-1, 1, 1) There's no unambigous way to subdivide it. I for one fall back into a triangle fan around the mean of the given points, but that's not necessarily what the artist intended.Romeliaromelle
I'm in agreement with @cbamber85 on this one. There should be some sort of default way of handling these types of situations because other programs can handle them just fine (an object file i export from sketchup is fully renderable in photoshop with faces that have more than 4 vertices). And if you think about it, both triangles and quads are triangle fans in reality, so why wouldn't it be true for the rest? I'll give triangle fan a shot and report my findings later.Brainbrainard
datenwolf and Nicol Bolas are not wrong, it's just that in the real world people expect minimum standards. You could make a polygon non-coplanar like in datenwolf's example - but why would you do that? If it is animated and could become non-coplanar, then break it up accordingly. There are many ways to make a bad mesh, but that is the responsibility of the artist, not the importer. Out of all the world's industrial strength OBJ importers (3ds max, Maya, AutoCAD, Modo, etc.), not a single one enforces triangles only.Ulbricht
The wikipedia entry on wavefront-obj says faces must be coplanar and convex, but I can't find any such declaration in the original OBJ specification.Airtoair
A
9

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.

Arluene answered 5/2, 2012 at 6:0 Comment(5)
Faces should have 3 triangles, period. You mean 3 vertices, right?Maharanee
@Maharanee I never said that. And I certainly didn't just edit my post to change that. ...everyone believed that, right?Arluene
@NicolBolas Any idea on how to do that with google sketchup or photoshop. Or have any recomendations for tools that will allow me to do that?Brainbrainard
@DerickF What do you use to generate your meshes? Sketchup should already be able to do that.Fredric
@NicolBolas Ah, i found it. It's under the export options when i'm selecting the file type i want to export and i need to check the box for triangulate all faces. That does solve my problem, however i'm still curious about the how one is supposed to deal with faces with more than 4 vertices, if i open up the object file with photoshop, it will show the object perfectly fine, which leads me to believe that there is a standard.Brainbrainard
U
8

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.

Ulbricht answered 5/2, 2012 at 9:26 Comment(6)
Nicol Bolas is right. Faces should consist of 3 vertices each. Triangles are the only non-ambigous primitives in 3D space. Anything else you can't specify in a sane form. And rasterizers work on triangles only, so they have to break it down. But in which way shall they do it? Only by using triangles in the first place you have the full control over this.Romeliaromelle
If a polygon is specified with a proper winding order (which the OBJ format is specific about), is concave, and coplanar; I see no way the GPU could create visible artifacts - so what's the point limiting your importer?Ulbricht
The OBJ format doesn't enforce coplanarity. How do you render the following quad: (-1, -1, -1), (1, 1, -1), (1, -1, 1), (-1, 1, 1) There's no unambigous way to subdivide it. I for one fall back into a triangle fan around the mean of the given points, but that's not necessarily what the artist intended.Romeliaromelle
I'm in agreement with @cbamber85 on this one. There should be some sort of default way of handling these types of situations because other programs can handle them just fine (an object file i export from sketchup is fully renderable in photoshop with faces that have more than 4 vertices). And if you think about it, both triangles and quads are triangle fans in reality, so why wouldn't it be true for the rest? I'll give triangle fan a shot and report my findings later.Brainbrainard
datenwolf and Nicol Bolas are not wrong, it's just that in the real world people expect minimum standards. You could make a polygon non-coplanar like in datenwolf's example - but why would you do that? If it is animated and could become non-coplanar, then break it up accordingly. There are many ways to make a bad mesh, but that is the responsibility of the artist, not the importer. Out of all the world's industrial strength OBJ importers (3ds max, Maya, AutoCAD, Modo, etc.), not a single one enforces triangles only.Ulbricht
The wikipedia entry on wavefront-obj says faces must be coplanar and convex, but I can't find any such declaration in the original OBJ specification.Airtoair
A
5

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.

Airtoair answered 8/9, 2013 at 5:58 Comment(1)
"The wikipedia entry on wavefront-obj says faces must be coplanar and convex"...where?Reception

© 2022 - 2024 — McMap. All rights reserved.