How to parse or load .obj file in JOGL? [closed]

2019-06-10 19:50发布

问题:

I'm trying to load a car object made with 3DS Max into a JOGL program but I don't know how to do that. I have searched online but haven't gotten a good result.

I tried this but got no result.

Is there a tutorial on how to do that? or how to write your own loader? or an already written class that I can use?

回答1:

Here is a very limited loader for the OBJ format written in Java (but unfortunately using the lower-level LWJGL instead of JOGL) by Oskar Veerhoek as part of the Coding Universe website. You would pass the File object which references your OBJ file into that class using either the loadModel(File f) method or the loadTexturedModel(File f) method. The returned Model object contains the 3D model described by the OBJ file. You should then pass the Model object to the provided utility methods createTexturedDisplayList(Model m), createDisplayList(Model m), or createVBO(Model model). Those return a renderable vertex buffer object or display list which you can then use as part of your render. Note however that you could write your own Model to renderable method if the provided don't meet your needs, but I would highly recommend understanding/reading the provided methods first.

An alternative which is much more full-featured is the joglobj library written in Java using JOGL. This loader is more complete, but it doesn't correctly handle transparency and textures in some ways. Using this loader will require this dependency (which is used to parse the OBJ file).

Another alternative is the oObjLoader library, which "is a java library for parsing and loading WaveFront .OBJ files."

If none of these meet your needs please refer to other similar SO questions, such as this question from user skaffman. Additionally, the gaming development specific stack exchange is a good place to look for similar related questions . This is a question from that section which discusses correctly loading OBJ files.



回答2:

I advise you to read the Wikipedia article on Wavefront .obj file (and MTL), the external links are interesting too, especially the specification, it helps to understand this ASCII file format, its "commands" (v, vf, vn, vt, f, mtllib, usemtl, o, s, g, ...) and its structure.

The other examples I quote can help you to understand how to parse the files and to convert your data into something more "OpenGL friendly" by using a few JOGL classes or more high level utilities.

There are one OBJ loader in JOGL and another one in JOGL-Utils: ObjReader and WaveFrontLoader.

There is the LibGDX OBJ loader too. LibGDX has a non official backend based on JOGL which is actively maintained by @bgroenks, @xranby and me.

You can use JogAmp's Ardor3D Continuation too (which uses JOGL under the hood), you can look at this simple example, its WaveFront OBJ importer (it shows you how to parse an OBJ file) ant its storage (it shows how to store the data into some structures to use them later to draw with JOGL).

N.B: I'm responsible for engine support in the JogAmp community, I'm in the list of the official maintainers and I'm the main contributor of JogAmp's Ardor3D Continuation which isn't a fork of Ardor3D but a "continuation". The development of the legacy Ardor3D was stopped in March 2014 according to its main developer Renanse as stated here on his blog.