2

Today, I started to implement a wavefront obj loader and the thing is that I don't know why there are so many obj files on the internet containing 4 vertices despite the fact that OpenGL no longer supports GL_QUADS and deprecates its use.

Is there any workaround on this? I mean, everytime I try to draw a face containing 4 vertices using GL_TRIANGLE_FAN or GL_TRIANGLE_STRIP, it shows tragic result. So in the end I have no option but to issue draw command on each face and as you know, it shows bad performance. I want to draw it issuing only one command on each group or smoothing group.

Any ideas?

Windforces
  • 313
  • 4
  • 12
  • 2
    Answering the first part, because other 3D rendering APIs are not limited to triangles. For example, the basic rendering primitive in Pixar RenderMan is a four vertex "micro polygon" (bilinear patch) of which quads are a subset. Anyone doing film/TV CGI probably wants quads, not triangles. – Hugh Fisher Jul 09 '16 at 09:44
  • Understood. Thanks for the comment! – Windforces Jul 09 '16 at 09:45

2 Answers2

5

Because OBJ is a file format that is completely unrelated to OpenGL.

Face statements in OBJ files can contain any number of vertices, not only 3 or 4. And it's not unusual for them to contain more than 4. So if you want to be able to read a reasonable variety of OBJ files, you need to be prepared to read any number of vertices per face.

This really does not add much difficulty. Unless you use an algorithm to analyze connectivity, and build meshes, you get a separate triangle per face statement anyway, even for files with only 3 vertices per face. So in that case, you create one triangle per face, and draw them with primitive type GL_TRIANGLES.

So all that changes with more than 3 vertices per face is that you generate multiple triangles per face. You will need n - 2 triangles for a face with n vertices. My answer to this question explains in more detail how this is done: Converting quadriladerals in an OBJ file into triangles?.

Another option is that you treat each face as a triangle fan, and render them with the GL_TRIANGLE_FAN primitive type. A convex polygon (and all of this would break down if faces in the file are non-convex polygons) can always be drawn as a triangle fan, while keeping the vertices exactly in their original order. To treat each face as a separate triangle fan, you can use the primitive restart feature that has been available since OpenGL 3.1, using calls like:

glEnable(GL_PRIMITIVE_RESTART);
glPrimitiveRestartIndex(...);

It's easy to tell that OBJ is not targeting OpenGL rendering. The much more cumbersome aspect is that OBJ uses separate indices for positions, normals, and texture coordinates, while OpenGL only supports a single set of indices which are used for all vertex attributes. This tends to cause much more difficulties for people writing parsers. See for example here: OpenGL - Index buffers difficulties.

Community
  • 1
  • 1
Reto Koradi
  • 53,228
  • 8
  • 93
  • 133
2

If you have a modelling program that can open/export objs like Maya or Blender, I'd recommend opening the model there, triangulating it using the appropriate menu option, and then re-exporting it.

If you don't have access to any of those, it should be relatively simple to write a program that reads in the obj data, triangulates it, and writes it to a file. You'll need a bit of a deeper understanding of the obj format, but it won't require any additional software.

budjmt
  • 135
  • 1
  • 9
  • Do you think it's necessary for OpenGL developer to equip such kinda software? Everytime I try to do some work with OpenGL, I need some tools to visualize any 3D file somehow. – Windforces Jul 09 '16 at 06:57
  • Oh it turns out that Blender is free! I didn't know that! – Windforces Jul 09 '16 at 06:58
  • 1
    If you're going to develop applications with 3d graphics, I'd definitely recommend having external modelling software of some kind. It's a good way to sanity check your work early on, and later if you need to make a model quickly or export to a different format, it'll be pretty simple. Blender is free, but if you're a student you can grab any Autodesk product, including Maya and 3ds Max, for free. EDIT: yup, exactly – budjmt Jul 09 '16 at 06:59