.obj file issues

I am having problems with my .obj file parser. Namely, the number of normals listed in the file is not equal to, nor is it a divisible by the number of vertices. The file looks like

# This file uses centimeters as units for non-parametric coordinates.

mtllib random.mtl
g default
v -3.130898 -2.031424 -4.913666
v -7.193745 -2.031424 -0.850819
v -3.130898 -2.031424 3.212028
v 0.931949 -2.031424 -0.850819
v -3.130898 2.031424 -0.850819
vt 0.500000 0.000000
vt 0.250000 0.250000
vt 0.500000 0.500000
vt 0.750000 0.250000
vt 0.250000 0.500000
vt 0.375000 0.500000
vt 0.500000 0.500000
vt 0.625000 0.500000
vt 0.750000 0.500000
vt 0.500000 1.000000
vn 0.000000 -1.000000 0.000000
vn 0.000000 -1.000000 0.000000
vn 0.000000 -1.000000 0.000000
vn 0.000000 -1.000000 0.000000
vn -0.577350 0.577350 -0.577350
vn -0.577350 0.577350 -0.577350
vn -0.577350 0.577350 -0.577350
vn -0.577350 0.577350 0.577350
vn -0.577350 0.577350 0.577350
vn -0.577350 0.577350 0.577350
vn 0.577350 0.577350 0.577350
vn 0.577350 0.577350 0.577350
vn 0.577350 0.577350 0.577350
vn 0.577350 0.577350 -0.577350
vn 0.577350 0.577350 -0.577350
vn 0.577350 0.577350 -0.577350
s off
g pPyramid1
usemtl initialShadingGroup
f 1/1/1 4/4/2 3/3/3 2/2/4
f 1/5/5 2/6/6 5/10/7
f 2/6/8 3/7/9 5/10/10
f 3/7/11 4/8/12 5/10/13
f 4/8/14 1/9/15 5/10/16

for a basic pyramid and

# This file uses centimeters as units for non-parametric coordinates.

mtllib 4sided.mtl
g default
v -7.497060 -0.000000 1.763534
v 5.051444 -0.000000 1.763534
v -7.497060 6.349456 1.763534
v 5.051444 6.349456 1.763534
v -7.497060 6.349456 -4.496893
v 5.051444 6.349456 -4.496893
v -7.497060 -0.000000 -4.496893
v 5.051444 -0.000000 -4.496893
vt 0.375000 0.000000
vt 0.625000 0.000000
vt 0.375000 0.250000
vt 0.625000 0.250000
vt 0.375000 0.500000
vt 0.625000 0.500000
vt 0.375000 0.750000
vt 0.625000 0.750000
vt 0.375000 1.000000
vt 0.625000 1.000000
vt 0.875000 0.000000
vt 0.875000 0.250000
vt 0.125000 0.000000
vt 0.125000 0.250000
vn 0.000000 0.000000 1.000000
vn 0.000000 0.000000 1.000000
vn 0.000000 0.000000 1.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 1.000000 0.000000
vn 0.000000 1.000000 0.000000
vn 0.000000 0.000000 -1.000000
vn 0.000000 0.000000 -1.000000
vn 0.000000 0.000000 -1.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 -1.000000 0.000000
vn 0.000000 -1.000000 0.000000
vn 1.000000 0.000000 0.000000
vn 1.000000 0.000000 0.000000
vn 1.000000 0.000000 0.000000
vn 1.000000 0.000000 0.000000
vn -1.000000 0.000000 0.000000
vn -1.000000 0.000000 0.000000
vn -1.000000 0.000000 0.000000
vn -1.000000 0.000000 0.000000
s off
g pCube1
usemtl initialShadingGroup
f 1/1/1 2/2/2 4/4/3 3/3/4
f 3/3/5 4/4/6 6/6/7 5/5/8
f 5/5/9 6/6/10 8/8/11 7/7/12
f 7/7/13 8/8/14 2/10/15 1/9/16
f 2/2/17 8/11/18 6/12/19 4/4/20
f 7/13/21 1/1/22 3/3/23 5/14/24

for a basic block.

Note how the vertices have a number of normals equal to how many edges/faces connect to them, and yet all the normals are the same for each vertex. I cannot think of any simple way of reading these normals in. Unity wants one normal per vertex and that information is there, but once I read the first normal for that vertex how can i calculate how many to skip in order to get to the next one? Knowing how many edges connect to a vertex requires complex processing of the face information which I would rather not get into. While we are on the subject why does the file need so many identical normals listed anyway?

it seems you didn’t triangulate the model.
Due to this you get groups of X on the normal front where X is the number of vertices in that n-gon so n

on the pyramid you have a 4 corner ground + 3 corner sides → 4,3,3,3,3
on the block, you have 4 corners on each n-gon → 4,4,4,4,4,4 (as there are 6 sides)

if the model contained smoothing or triangulation, it would look differently.

If you want to import models manually I highly recommend using the X file format, it defines such stuff significantly cleaner and especially linear parser friendlier (you can load and parse X files with far less temp memory allocation required for cross lookups etc and it contains all information in the model, not model + material, while at the same time supporting multi texturing and bone animation in a clearly defined standardized form)

As I’m a lazy ass, I would recommend to look at the obj importer script already present on the wiki if you want a starting point or invest into Eric5h5 ones if you want one that “just works” :slight_smile:

First I should thank dreamora, .x files are really much better. Having converted my project to this format I suddenly had the same problem again. Well not the same. This time the shape has 80 vertices and only 41 normals. There is a list of indices after the normals though which may be the solution but I can’t find a description of exactly what they mean.

MeshNormals {
			41;
			 0.000000,-1.000000, 0.000000;,
			 0.000000,-1.000000, 0.000000;,
			 0.000000,-1.000000, 0.000000;,
			 0.000000,-1.000000, 0.000000;,
			 0.000000,-1.000000, 0.000000;,
			 0.000000,-1.000000, 0.000000;,
			 0.000000,-1.000000, 0.000000;,
			 0.000000,-1.000000, 0.000000;,
			 0.000000,-1.000000, 0.000000;,
			 0.000000,-1.000000, 0.000000;,
			 0.000000,-1.000000, 0.000000;,
			 0.000000,-1.000000, 0.000000;,
			 0.000000,-1.000000, 0.000000;,
			 0.000000,-1.000000, 0.000000;,
			 0.000000,-1.000000, 0.000000;,
			 0.000000,-1.000000, 0.000000;,
			 0.000000,-1.000000, 0.000000;,
			 0.000000,-1.000000, 0.000000;,
			 0.000000,-1.000000, 0.000000;,
			 0.000000,-1.000000, 0.000000;,
			 0.739483, 0.628836, 0.240272;,
			 0.629041, 0.628836, 0.457025;,
			-0.000000, 1.000000,-0.000000;,
			 0.457025, 0.628836, 0.629041;,
			 0.240272, 0.628836, 0.739482;,
			-0.000000, 0.628836, 0.777538;,
			-0.240273, 0.628836, 0.739482;,
			-0.457025, 0.628836, 0.629041;,
			-0.629041, 0.628836, 0.457025;,
			-0.739482, 0.628836, 0.240272;,
			-0.777538, 0.628836,-0.000000;,
			-0.739482, 0.628836,-0.240272;,
			-0.629041, 0.628836,-0.457025;,
			-0.457025, 0.628836,-0.629041;,
			-0.240272, 0.628836,-0.739482;,
			 0.000000, 0.628836,-0.777538;,
			 0.240273, 0.628836,-0.739482;,
			 0.457025, 0.628836,-0.629041;,
			 0.629041, 0.628836,-0.457025;,
			 0.739483, 0.628836,-0.240272;,
			 0.777538, 0.628836,-0.000001;;
			21;
			20;19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0;,
			3;22,21,20;,
			3;22,23,21;,
			3;22,24,23;,
			3;22,25,24;,
			3;22,26,25;,
			3;22,27,26;,
			3;22,28,27;,
			3;22,29,28;,
			3;22,30,29;,
			3;22,31,30;,
			3;22,32,31;,
			3;22,33,32;,
			3;22,34,33;,
			3;22,35,34;,
			3;22,36,35;,
			3;22,37,36;,
			3;22,38,37;,
			3;22,39,38;,
			3;22,40,39;,
			3;22,20,40;;
		}