Has anyone had luck loading .obj files?

I started writing an obj file loader last night and mostly got it working. My problem isn’t with the loader, but how the normals are assigned in the file. When I load the faces, I get vertex indices and normal vertex indices. The problem is that the normal indices get reassigned.

For an example, here is a short obj file I’m using:

# Blender3D v248 OBJ File: 
# www.blender3d.org
v 0.437500 0.500000 0.468750
v -0.437500 0.500000 0.468750
v -0.437500 0.500000 -0.468750
v 0.437500 0.500000 -0.468750
v -0.437500 -0.500000 0.468750
v 0.437500 -0.500000 0.468750
v -0.437500 -0.500000 -0.468750
v 0.437500 -0.500000 -0.468750
vn 1.000000 0.000000 -0.000000
vn -1.000000 0.000000 0.000000
vn 0.000000 -0.000000 1.000000
vn -0.000000 -1.000000 -0.000000
vn 0.000000 1.000000 0.000000
vn -0.000000 0.000000 -1.000000
usemtl (null)
s off
f 2//1 5//1 7//1
f 1//2 8//2 6//2
f 3//1 2//1 7//1
f 3//3 7//3 8//3
f 3//3 8//3 4//3
f 8//2 1//2 4//2
f 1//4 3//4 4//4
f 1//4 2//4 3//4
f 6//5 8//5 7//5
f 5//5 6//5 7//5
f 2//6 1//6 6//6 5//6

For those that are unfamiliar with the file format, the “f x//y” translates to “face vertex index//normal vertex index”. In this case, there are 10 triangles and one quad.

Now look at face #1. The first vertex index is 2 and the first normal index is 1. If I assign that vertex that normal, when I get down to face #8, I end up reassigning vertex #2 with normal #4. At face #11, it is reassigned again to normal #6.

Am I doing the assignment wrong or is the .obj exporter that comes with Blender screwed up?

I’m sorry that this isn’t exactly on topic, but I will be using GL to do the rendering. I need to load from a file because some of the models are quite complex and I do not want to hard code the objects.


Never mind. Upon further inspection, it is a problem with the exporter and I’ll need to take it up Blender’s developers. It should be exporting 8 normals and is only exporting 6.

Each normal is per vertex per polygon, meaning you should think the face list as the actual geometry, not the vertex list.

Correct, however, the exporter is exporting face normals instead of vertex normals. The model I presented is a rectangular prism. Look at the normals. They are axis aligned. If they were vertex normals, they wouldn’t be.

well that depends if you are using vertex smoothing or not, if not then they are most likely to be axis aligned, especially if you have an axis aligned rectangular prism like you have.
obviously you haven’t since you have the line “s off” there (which means normal smoothing off).
to get smoothing select the object and then press “set smooth” in the lower left corner.

and it only looks like it’s exporting face normals since it only exports unique normals.

Blender creates normals for each vertex and face. You can view them and Ive seen them, so I know for certain that the calculations are being performed. I’ve read over the file format specifications (not official, but what I could find) and it only mentions vertex normals, which, correct me if I’m wrong, are averaged face normals. I cannot find any reference to face normals in what references I could find online.

Other than that, you’re saying that the face list does not share vertices? Again, what examples I could find show otherwise. I’m really thinking this is an exporter issue.

Well i actually tried the exporter and it works just fine, i could replicate what you got and also where the normals are smoothed out, it worked as expected, and i don’t think they would screw up on such an important feature, at least not in v248, so i think it’s more of an pebcac problem.

Vertex normals can be the same as face normals, it depends on how sharp of an edge you want, in the case where you want sharp edges the vertex normals are not averaged and instead just kept as they where.
There are also instances where the vertex normals are averaged but are still not the same as all their counterparts, but that’s whole other issue.
(i.e. they don’t have to be averaged.)

I set the faces to smooth and got the desired results. Although this whole discussion may be moot. I do not want the polys smoothed. I just need the normals for GL. This means that each panel needs to be separate. Oh well… At least I know what was going on and why.