Load .obj file out of Blender

眉间皱痕 提交于 2019-12-08 04:03:25

take a look at the source in libgdx, and take a look at the objloader. He built the lib to be able to load obj files, and is supposed to work across multiple platforms (including ios). From reading the source you will see that creating an object loader for just the vertices is really simple, but becomes more complicated when you start caring about the normals and texture coords. Here is a simple algorithm for scraping an obj file (I will leave parsing the associated tpl file for the reader's own research):

  1. Read in the lines of the file, throwing out all lines that start with # (comments)
  2. Vertices look like: v 1.000 1.000 1.000, so, if line starts with 'v ' split the line on the spaces and store (and convert to float) the 3 floats as a vertice.
  3. Normals look like: vn 1.000 1.000 1.000, so, if the line starts with 'vn ' do the same as number 2, but store as a normal.
  4. Texture coords look like vt 1.000 1.000 with a possible 3rd [w] value, split and store the line in the same way.
  5. Now it gets tricky, there is the face descriptions that look like f 1/1/1 2/2/2 3/3/3 these are describing 3 vertices/textcoords/normals (in that order) for each of the vertices of a shape (normally they are triangles) by index. The hardest part about this is that the obj file type uses three indexes instead of one like opengl or direct3d. So you will have to shuffle around the order of your vertexes/coords/normals so that you can utilize indexed drawing on the sources.

E.g. Basically you have to get the f 1/300/30 40/22/400 20/30/10 to become more like f 1/1/1 2/2/2 40/40/40 through reshuffling the order.

This site gives you an idea of this same algorithm and shows you an example of how to go about this in a high level algorithm (go about midway down on the page), and the source code he references for you to check out can be found here.

Anyways, let me know if you need anymore assistance. :)

Edit:

By the way if you see something like this: f 1//4 2//5 3//7 don't be alarmed, this is a valid file as intended and just means (in this instance) that there are no texture coords

I used the GLEssentials sample app as a starting point. It is really bare bones, but its kind of what you want to start with so you can really understand the format for when you decide to add to it later.

https://developer.apple.com/library/mac/#samplecode/GLEssentials/Introduction/Intro.html

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