Parsing .obj files

I’m trying to render models from SolidWorks in OpenGL, and I’ve been told that having the models exported from SolidWorks as .obj files would be best. However, I can’t seem to find any good .obj parsers for OpenGL. Does anyone know how I could go about doing this?

Do one yourself, begin by saving all vertex something like this

http://codepad.org/rqZIKFfQ

and then work it up =) to the indexes etc
sorry if the code is crappy and uncommented

Take a look at Nate Robins tutorial code. In the process, a file glm.c was created to load OBJ files ie pmodel = glmReadOBJ(“data/porsche.obj”); etc. It is self described as


/*
      glm.c
      Nate Robins, 1997
      [email]ndr@pobox.com[/email], http://www.pobox.com/~ndr/

      Wavefront OBJ model file format reader/writer/manipulator.

      Includes routines for generating smooth normals with
      preservation of edges, welding redundant vertices & texture
      coordinate generation (spheremap and planar projections) + more.

 */

ps. glm here is not to be confused with the different but very useful openGL Mathematics library.

It’s quite easy to write one, though it’ll take some work to make one that runs very fast and deals with the weird quirks of all exporters. In my research (I did one for a project) I found that it was much quicker to read the whole file into a buffer, and then parse it twice (once to count elements, once to fill the allocated arrays) than it was to grow my arrays as I went. By about 100x once the models get big.

Parsing files is technically not an OpenGL-related topic. However, it is close, I guess.

How general do you need to be? As previous posters have pointed out the .OBJ-format can be very simple, but also extend into madness.

Study the files you wish to parse, and if you are using C++ and speed is not an issue, remember that std::stringstream is your friend.

as a followup – maybe taking a look at Open Asset Import Library is worth while because it handles many more formats than OBJ alone. I tried it for the first time on Linux and it was easy to compile and install. The sample code they had also worked no problem.