Question

I've been making a .obj file loader class. At one point I was trying to load obj-file containing multiple objects, but I couldn't get it to work.

Now I realised that when you export multiple objects from Blender into obj-file, only the first object has normals. Is there a way to make it give all the objects normals or do I have to start computing them myself?

Here's two cubes in a single obj-file. You can see that only the first one has "vn . . ." lines.

# Blender v2.67 (sub 1) OBJ File: ''
# www.blender.org
mtllib font.mtl
o Cube.004_Cube.005
v 3.100306 -2.415467 -4.025629
v 3.100306 -2.415467 -2.978886
v 2.053563 -2.415467 -2.978886
v 2.053564 -2.415467 -4.025629
v 3.100306 -1.368724 -4.025628
v 3.100305 -1.368724 -2.978886
v 2.053563 -1.368724 -2.978886
v 2.053564 -1.368724 -4.025629
vt 0.000000 0.000000
vt 1.000000 0.000000
vt 0.000000 1.000000
vt 1.000000 1.000000
vn 0.000000 -1.000000 0.000000
vn -0.000000 1.000000 0.000000
vn 1.000000 -0.000000 0.000000
vn -0.000000 -0.000000 1.000000
vn -1.000000 -0.000000 -0.000000
vn 0.000000 0.000000 -1.000000
vn 1.000000 0.000000 0.000001
usemtl Material1
s off
f 1/1/1 2/2/1 4/3/1
f 5/1/2 8/2/2 6/3/2
f 1/1/3 5/2/3 2/3/3
f 2/1/4 6/2/4 3/3/4
f 3/1/5 7/2/5 4/3/5
f 5/1/6 1/2/6 8/3/6
f 2/2/1 3/4/1 4/3/1
f 8/2/2 7/4/2 6/3/2
f 5/2/7 6/4/7 2/3/7
f 6/2/4 7/4/4 3/3/4
f 7/2/5 8/4/5 4/3/5
f 1/2/6 4/4/6 8/3/6
o Cube.003_Cube.004
v 2.891954 -3.616562 -2.073456
v 2.891954 -3.616562 -1.026714
v 1.845212 -3.616562 -1.026714
v 1.845212 -3.616562 -2.073456
v 2.891954 -2.569820 -2.073456
v 2.891954 -2.569820 -1.026713
v 1.845212 -2.569820 -1.026714
v 1.845212 -2.569820 -2.073456
vt 0.000000 0.000000
vt 1.000000 0.000000
vt 0.000000 1.000000
vt 1.000000 1.000000
usemtl Material2
s off
f 9/5/1 10/6/1 12/7/1
f 13/5/2 16/6/2 14/7/2
f 9/5/3 13/6/3 10/7/3
f 10/5/4 14/6/4 11/7/4
f 11/5/5 15/6/5 12/7/5
f 13/5/6 9/6/6 16/7/6
f 10/6/1 11/8/1 12/7/1
f 16/6/2 15/8/2 14/7/2
f 13/6/3 14/8/3 10/7/3
f 14/6/4 15/8/4 11/7/4
f 15/6/5 16/8/5 12/7/5
f 9/6/6 12/8/6 16/7/6
Was it helpful?

Solution

They both have normals, the second cube just references the same normals as the first one.

You have to realize that the numbering of v, vt and vn elements continues throughout the file, so

o Cube.004_Cube.005
v 3.100306 -2.415467 -4.025629  #v1
v 3.100306 -2.415467 -2.978886  #v2
v 2.053563 -2.415467 -2.978886  #v3
v 2.053564 -2.415467 -4.025629  #v4
vt 0.000000 0.000000            #vt1
vt 1.000000 0.000000            #vt2
vt 0.000000 1.000000            #vt3
vn 0.000000 -1.000000 0.000000  #vn1
usemtl Material1
s off
f 1/1/1 2/2/1 4/3/1

o Cube.003_Cube.004
v 2.891954 -3.616562 -2.073456 #v5
v 2.891954 -3.616562 -1.026714 #v6
v 1.845212 -3.616562 -1.026714 #v7
v 1.845212 -3.616562 -2.073456 #v8
vt 0.000000 0.000000           #vt4
vt 1.000000 0.000000           #vt5
vt 0.000000 1.000000           #vt6
usemtl Material2
s off
f 5/4/1 6/5/1 8/6/1

is exactly the same as

v 3.100306 -2.415467 -4.025629  #v1
v 3.100306 -2.415467 -2.978886  #v2
v 2.053563 -2.415467 -2.978886  #v3
v 2.053564 -2.415467 -4.025629  #v4
v 2.891954 -3.616562 -2.073456  #v5
v 2.891954 -3.616562 -1.026714  #v6
v 1.845212 -3.616562 -1.026714  #v7
v 1.845212 -3.616562 -2.073456  #v8
vt 0.000000 0.000000            #vt1
vt 1.000000 0.000000            #vt2
vt 0.000000 1.000000            #vt3
vt 0.000000 0.000000            #vt4
vt 1.000000 0.000000            #vt5
vt 0.000000 1.000000            #vt6
vn 0.000000 -1.000000 0.000000  #vn1

o Cube.004_Cube.005
usemtl Material1
s off
f 1/1/1 2/2/1 4/3/1

o Cube.003_Cube.004
usemtl Material2
s off
f 5/4/1 6/5/1 8/6/1

The only rule is that you define the v, vt or vn before you use its index in a f. So if your parser has problems with this, change your parser to read it correctly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top