Java - Loading .OBJ files

坚强是说给别人听的谎言 提交于 2019-12-05 18:06:20
Sean R Owens

I wrote a java obj file format loader a few months ago that should easily do what you need. It's on git hub at;

https://github.com/seanrowens/oObjLoader

Note that the README says it's pre-alpha - this is primarily because pretty much nobody else has seen it yet.

It parses most all of the .obj format as well as the .mtl format. Now what it DOES with what it parses... well, I tried to take a SAX like approach. There is a Parser that calls methods on a BuilderInterface with each parsed element. There is a simple sample implementation of the BuilderInterface and also a simple LWJGL viewing app that ties it all together.

It doesn't currently do anything useful with more exotic geometry statements, i.e. anything other than vertices and polygons, in other words it ignores any geometry except for vertices and polygons. It parses and captures .mtl files but the actual viewer doesn't implement anything except very very very simple textures.

I'd more than happy to hear any and all feedback and advice. The license is extremely liberal;

This code was written by myself, Sean R. Owens, sean at guild dot net, and is released to the public domain. Share and enjoy. Since some people argue that it is impossible to release software to the public domain, you are also free to use this code under any version of the GPL, LPGL, Apache, or BSD licenses, or contact me for use of another license.

I have written a Java library that should help you parse OBJ and MTL resources.

https://github.com/mokiat/java-data-front

Here are some of the features it supports:

  • List of all vertices (v).
  • List of all normals (vn).
  • List of all texture coordinates (vt).
  • List of all material library references mtllib
  • List of all objects (o) and their names.
    • Grouping of object's faces by material (usemtl)
    • List of all the faces in a material group.
      • List of all the vertex, normal, texture coordinate indices that make a face.
  • List of all materials (newmtl) and their names
    • Diffuse color (Kd)
    • Specular color (Ks)
    • Ambient color (Ka)
    • Transparency (d)
    • Diffuse texture reference (map_Kd)
    • others...
  • Ability to set limits on the size of OBJ and MTL resources being parsed.
    Helps prevent OutOfMemory exceptions.

The implementation is backed up by a sufficient number of tests. The library has no runtime dependencies so it should be fairly easy to integrate into your project.

It is built via Maven so it will be easy to integrate in Maven projects. (You will need to manually register it in your local repository). If your project is not a Maven one - don't worry, it should not be a problem.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!