Java - 加载 .OBJ 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7233987/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 19:13:57  来源:igfitidea点击:

Java - Loading .OBJ files

java.obj

提问by will

I want to be able to get information (like names of sub-components) from an .obj file. Is there a loader out there somewhere to do this? Java3D has an ObjectFile, but the list of methods I've seen makes me think it is not exactly what I want. Would I be better off just parsing the file myself?

我希望能够从 .obj 文件中获取信息(如子组件的名称)。有没有装载机可以做到这一点?Java3D 有一个 ObjectFile,但是我所看到的方法列表让我觉得它不是我想要的。我自己解析文件会更好吗?

Thanks -Will

谢谢-威尔

回答by 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;

几个月前我写了一个 java obj 文件格式加载器,它应该很容易满足你的需求。它在 git hub 上;

https://github.com/seanrowens/oObjLoader

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.

请注意,自述文件说它是 pre-alpha - 这主要是因为几乎没有其他人看过它。

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.

它解析大部分 .obj 格式以及 .mtl 格式。现在它对解析的内容做了什么......好吧,我尝试采用类似 SAX 的方法。有一个解析器,它使用每个解析的元素调用 BuilderInterface 上的方法。有一个 BuilderInterface 的简单示例实现,还有一个简单的 LWJGL 查看应用程序将它们联系在一起。

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.

它目前对更奇特的几何语句没有任何用处,即除了顶点和多边形之外的任何东西,换句话说,它忽略除顶点和多边形之外的任何几何图形。它解析和捕获 .mtl 文件,但实际的查看器除了非常非常简单的纹理之外没有实现任何东西。

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.

这段代码是我自己,Sean R. Owens,在 Guild dot net 编写的,并被发布到公共领域。分享和享受。由于有些人认为不可能将软件发布到公共领域,因此您也可以在任何版本的 GPL、LPGL、Apache 或 BSD 许可下自由使用此代码,或者联系我以使用其他许可。

回答by Momchil Atanasov

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

我编写了一个 Java 库,可以帮助您解析 OBJ 和 MTL 资源。

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

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 OutOfMemoryexceptions.
  • 所有顶点的列表 ( v)。
  • 所有法线的列表 ( vn)。
  • 所有纹理坐标的列表 ( vt)。
  • 所有材料库参考列表 mtllib
  • 所有对象 ( o) 及其名称的列表。
    • 按材料对对象的面进行分组 ( usemtl)
    • 材料组中所有面的列表。
      • 构成面的所有顶点、法线、纹理坐标索引的列表。
  • 所有材料的清单 ( newmtl) 及其名称
    • 漫反射颜色 ( Kd)
    • 镜面颜色 ( Ks)
    • 环境色 ( Ka)
    • 透明度 ( d)
    • 漫反射纹理参考 ( map_Kd)
    • 其他...
  • 能够对正在解析的 OBJ 和 MTL 资源的大小设置限制。
    有助于防止OutOfMemory异常。

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.

它是通过 Maven 构建的,因此很容易集成到 Maven 项目中。(您需要在本地存储库中手动注册它)。如果您的项目不是 Maven 项目 - 别担心,这应该不是问题。