For reasons of collision detection, I am attempting to iterate through all the triangles in my model.

However, how can I get them with an OBJ file?

OBJ files start each vertex position information with a "V". I cannot see how to get a triangle of three vertices from that, since I can't know which vertices make up the triangle.

I don't care what order the triangles are iterated through in, as long as I am able to get the triangle. How can I get it?

  • 4
    $\begingroup$ faces start with F and link to the vertices via index: so f 2 1 3 4 means quad from 2nd, 1s, 3rd and 4th vertex in vertex list. Its in wiki don't be lazy to look: en.wikipedia.org/wiki/Wavefront_.obj_file also this isn't about blender at all its offtopic.. $\endgroup$ – Jaroslav Jerryno Novotny Feb 16 '15 at 16:01
  • $\begingroup$ @Jerryno Okay. I won't ask similar questions like this in the future here. That has solved my issue. Thank you :). $\endgroup$ – Joehot200 Feb 16 '15 at 16:02
  • 3
    $\begingroup$ I'm voting to close this question as off-topic because it is not about blender but about the obj format. $\endgroup$ – Ray Mairlot Feb 16 '15 at 16:06
  • $\begingroup$ @Jerryno Whilst what you say is true, just bare in mind how you word things, it sounded a little harsh to me :) $\endgroup$ – Ray Mairlot Feb 16 '15 at 16:09
  • $\begingroup$ @RayMairlot It's all right, nobody can be on the internet for any length of time without learning to take a bit of flak. I actually believed that the OBJ format was something created by blender, which is why I thought that this was ontopic. Thank you for informing me. $\endgroup$ – Joehot200 Feb 16 '15 at 16:11

Apart from a list of vertices OBJ files also contain a list indices that make up a face. Look for lines that start with an "f". The vertices are enumerated by their occurrence in the file, starting with index 1. Be aware that faces can consist of more than three vertices. You will have to split quads and n-gons if you want tris.

f 1 2 3         # a triangle that consists of the first, second and third vertex
f 2 3 4 5       # a quad consisting of the vertices 2 to 5
f 1 2 5 6 10 11 # an n-gon with six vertices

OBJ also supports relative indices. These indices are negative. For example an index of -1 would address the last vertex at the position of the face. The following file would contain three triangles, consisting of the first three, the second three and the first, third and last vertex:

v  1.0 -1.0 -1.0
v  1.0 -1.0  1.0
v -1.0 -1.0  1.0
f -3 -2 -1
v -1.0 -1.0 -1.0
v  1.0  1.0 -1.0
v  1.0  1.0  1.0
f -3 -2 -1
f -6 -4 -1
| improve this answer | |
  • $\begingroup$ Mind explaining why all the faces have a - on them? Also, thanks a ton! $\endgroup$ – Joehot200 Feb 16 '15 at 17:28
  • $\begingroup$ I wrote it in the second paragraph. Negative indices are relative indices: The vertices are counted backwards from the line of the face. $\endgroup$ – maddin45 Feb 16 '15 at 17:36
  • $\begingroup$ But, I just have a list of vertices? How would I know which are which, since the array starts at a value of 0. Also, the faces in my .obj folder look like this: f 2271/29/448 2269/27/448 3936/28/448 $\endgroup$ – Joehot200 Feb 16 '15 at 18:02
  • $\begingroup$ Usually there is a list of vertices and after that a list of faces. The face definition you have also contains the indices for texture coordinates and normals. See the wikipedia article on OBJ, which Jerryno also linked in the comments to your question. $\endgroup$ – maddin45 Feb 16 '15 at 18:18

To answer my own question (reading through stuff - Also big thanks to @Jerryno), I can use the faces to get the position of the vertex in the array.

For example, to get the first vertex in a triangle (triangle is the same as face), I would use vertex.get(face[0]).

| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.