Jump to content
  • Advertisement
Sign in to follow this  
codpp

OpenGL obj file format

This topic is 1497 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I tried to load information about 3d model from a .obj file using opengl. But I noticed that some of them contain more than four vertices for a single face in f section.

Does that mean that face is a rectangle not triangle? If so, is it possible to render it out? A portion of the obj file that contain four vertices for a face is below:

 
s off
g polySurface11 polySurface12
usemtl initialShadingGroup
f 1/1/1 15/25/2 16/27/3 3/3/4
f 3/3/5 16/27/6 46/78/7 19/31/8
f 5/5/9 14/24/10 7/7/11  
f 15/26/12 1/9/13 20/33/14 21/34/15 2/10/16 
 
Thanks.

Share this post


Link to post
Share on other sites
Advertisement

OBJ files can contain polygons with an arbitrary amount of vertices (so yes, those are quadrilaterals). You need to convert them to triangles first if you want to render them using OpenGL.

 

The easiest and dumb way is to just treat them like a triangle fan, but that assumes all polygons are convex (which is likely the case for most models anyway). If that works out for your data then just do that and don't worry anymore. Otherwise, you can try loading the model in a 3D editor and reexport the model ensuring all faces are turned into triangles (Blender has a checkbox that says "triangulate" if I remember correctly, don't know about other editors).

Share this post


Link to post
Share on other sites

Personally I'd re-export them as triangles in a modeling tool, if possible.

Share this post


Link to post
Share on other sites
The normal method is to treat them as triangle fans, and it is trivial to convert them to triangles.
If you have 5 points then you have 3 triangles:
Fan: 0 1 2 3 4
Triangles:
0 1 2
0 2 3
0 3 4


L. Spiro

Share this post


Link to post
Share on other sites

I'd second Promit's advice: .obj is fine as an interchange format but for actual production use you're doing slow (and error-prone) text parsing which is just going to give you complex code and annoyed users.

 

The ideal model format is one where you can just memory-map a file and pass the resulting pointer directly to a glBufferData call.  You really shouldn't be doing anything more complex.

Share this post


Link to post
Share on other sites

I don't think Promit ever suggested to not use OBJ, just to use a 3D editor to turn the quads into triangles.

 

In any case there's always the question of what to do with those OBJ files, because even if you don't use them directly you still need to convert them somehow. Ideally you'd have the conversion tool directly as a script for the editor itself (so you have access to all the original data). Or you could just use something like assimp in your game and avoid having to make your own parser.

Share this post


Link to post
Share on other sites

Have a look at polytrans.

 

Not only is it an excellent tool for handling 3D meshes, it has options to triangulate meshes.

 

It can do all the hard work for you and then you can save the edited mesh in a format that is easy to parse in the game (or editor)

 

http://www.okino.com/conv/conv.htm

Edited by Stainless

Share this post


Link to post
Share on other sites

Using OBJ as source for GPU related APIs has at least these well-known issues:

1.) support for polygons with more than 3 vertices,

2.) usage of independent indices for positions, normals, tex-coords,

3.) is text based.

 

All of the above issues cause an essential amount of work to be done during import. Moreover, OBJ is AFAIK meaningful useable for static meshes only. With this in mind, OBJ is not a well suited format for use in game engines. It has limitations even in its use as input to tool chains. So, even if the mesh is exported with triangles only, not all problems are solved. OBJ seems to be a relatively simple format, and it actually is during the process of parsing, but it turns into headache if one wants to use the resulting mesh with  modern graphics APIs.

Edited by haegarr

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!