如何在 Android 中加载 3D 对象?
我有 .obj 文件和 .mtl 文件,但不知道如何导入它。我已经尝试过教程,它们不起作用。
I have the .obj file and .mtl file but don't know how to import it. I have tried the tutorials, they aren't working.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从 .obj 文件加载对象的一种方法是首先从文件获取输入流,然后遍历每一行,因为一行由一个顶点组成。然后,您可以使用 .obj 文件中的数据来实际绘制它,您可以轻松地在网上找到一堆教程,例如: http://www.droidnova.com/android-3d-game-tutorial-part-i,312.html 。如果您想更深入地了解 Opengl 并真正学到很多东西,您可以使用 Nehe 的全能在线教程,该教程涵盖了很多内容并且非常深入地介绍了 OpenGL 编程:http ://nehe.gamedev.net/ 。
不管怎样,.obj 文件加载可以这样开始:
对于除了以 'f' 开头的行之外的所有内容,它都非常简单,您只需将两个或三个值存储在某个东西中,我更喜欢矢量:
对于面部部分(其中行以“f”开头,您最好首先检查每个顶点由多少个“/”组成:
每个面由三个顶点组成,其中每个顶点索引为 v/vt/vn。
这就是如果你有v/vt。
现在您已经通过将索引添加到单独的向量或数组中来处理这个问题,
现在您应该拥有由坐标和由每种类型(v、vt 或)的索引组成的向量 。 vn)。
通过知道面数,您现在可以开始一个从 0 到面数的循环。
如果您现在创建由最终结果组成的新向量,则可以在此循环中将索引处的坐标从循环索引移动到结果向量中的循环索引。
这可能需要一些解释,所以这里是一个简单的例子:
现在您已经完成并从 .obj 文件中获得了所需的信息。因此,通过将 thoose 向量转换为 FloatBuffers 并创建一个像 0,1,2,3,4,5 ... 一样简单的 ShortBuffer,您可以使用 thoose 与 OpenGL-ES 来绘制您的对象。
(这并不完全是我写的,我使用了我在网上找到的一个基本概念,但现在找不到了,然后使它非常适合我想要的,并对其进行了一些调整以使其更容易理解)< /em>
这没有涵盖您还加载 .mtl 文件的部分,您在从程序导出对象时通常也会获得该文件。如果您也想实现这一点,您可以看看这个:http://people .sc.fsu.edu/~jburkardt/data/mtl/mtl.html
如果您在程序中构建一个对象并想要一个更精确的副本,那么这是一个很好的东西,这将给出您对如何处理材料的光的价值观以及。
如果您有任何疑问,请随时询问他们。
(我知道这可能不是加载 .obj 文件的最佳方式,因为我们最多使用 9 个不同的向量,但如果您也使用序列化,这实际上可能会变得相当快。)
One way to load an Object from an .obj file is to first get the inputstream from the file, then you go through each line, since one line consists of one vertex. The part where you then use youre data from the .obj file to acctually draw it is something that you easily can find a bunch of tutorials on online, one example: http://www.droidnova.com/android-3d-game-tutorial-part-i,312.html . If you want to get deeper into Opengl and acctually learn alot you can use Nehe the allmightys online tutorial that covers very much and goes quite deep into OpenGL programming: http://nehe.gamedev.net/ .
Anyway, the .obj file loading could start of by something like this:
For everything except when line starts with 'f' it's quite straight forward, you just store thoose two or three values in something, I preferred a Vector:
For the faces part (where line starts with 'f', you would preferably first check how many '/' each vertex consists of:
Each face is built up by three vertex, where each vertex indexes is v/vt/vn.
This is what could happen if you have v/vt. It stores the indices in seperate vectors.
Now you have handled this by adding the indices into sperarate Vectors or arrays.
Now you should have Vectors that consists of coordinates and vector that consists of indices for each type(v, vt or vn). You should as well have the number of faces.
By knowing the number of faces you can now start a loop that goes from 0 to number of faces.
If you now create new vectors that consists of the final result, you can in this loop move the coordinates at the index from the loop index to the loop index in the results vectors.
This may need some explenation maybe so here is a simple example:
Now your'e acctually done and have the information you need from the .obj file. So by converting thoose vectors to FloatBuffers and by creating a ShortBuffer that is as simple as 0,1,2,3,4,5 ... and so on you can use thoose with OpenGL-ES to draw your'e object.
(This is not completly written by me, I used a basic concept i found somewhere online, can't find now tho, and then made it fit nicely to what i wanted and adjusted it abit to get easier to understand)
What this doesn't cover is the part where you also load a .mtl file that you oftenly will get as well while exporting an Object from a program. If you want to implement that as well you could have a look at this: http://people.sc.fsu.edu/~jburkardt/data/mtl/mtl.html
This is something that can be good to have if your'e building an Object in a program and want a more exact replica where this will give you values for how light should be handled to materials as well.
If you have any questions, feel free to ask them.
(I know that this might not be the optimal way of loading a .obj file since we use up to 9 different vectors but if you use serialization as well this might acctually get quite fast.)
OpenGL 不是场景图形库。它为您提供了一套复杂的绘图工具,但仅此而已。加载3D模型、场景管理等都留给你自己实现,或者使用库来完成。
OpenGL is not a scene graph library. It provides you a set of sophisticated drawing tools, but nothing more. Loading 3D models, scene management, etc. are all left for you to implement, or use a library for.
现在已经很晚了......但仍然是有用的解决方案:
min3D 引擎帮助在 android 中加载 3d 模型
以下是 syeps:
1) 下载 Rajawali lib。
2)创建一个RenderActivity
私有Object3dContainerfaceObject3D;
私有 Object3dContainer objModel;
在 res/raw 目录中添加你的 3d 模型 obj 文件...这里是 monster_high
就是这样...在 Manifest 中注册活动并运行:)。
如果您想移动 3d 模型覆盖 updateScene(..):
更多信息请参见此处 http:// code.google.com/p/min3d/wiki/HowToLoadObjFile 尽情享受吧:)
it's far late now ...but still helpful solution :
min3D engine help to load 3d model in android
Following are syeps:
1) Download Rajawali lib.
2) Create an RenderActivity
private Object3dContainer faceObject3D;
private Object3dContainer objModel;
add ur 3d model obj file in res/raw directory...here it's monster_high
That's it...register activity in Manifest and run :) .
if you want to move 3d model override updateScene(..):
more on here http://code.google.com/p/min3d/wiki/HowToLoadObjFile Enjoy :)