PDA

View Full Version : Parsing .obj files



SiraRaven
07-30-2010, 07:32 PM
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?

Friktion
07-31-2010, 03:42 AM
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

marshats
07-31-2010, 06:57 AM
Take a look at Nate Robins tutorial code (http://www.gamedev.net/reference/articles/article839.asp). 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
ndr@pobox.com, 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.

NeXEkho
07-31-2010, 02:29 PM
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.

thinks
08-02-2010, 04:01 PM
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.

marshats
08-07-2010, 09:31 AM
as a followup -- maybe taking a look at Open Asset Import Library (http://assimp.sourceforge.net/lib_html/index.html) 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.