How to get proper number of vertices in OBJ file from DCC tools such as Blender for use in OpenGL ES?

StackOverflow https://stackoverflow.com/questions/9026758

  •  19-04-2021
  •  | 
  •  

Question

I am told that for a simple cube I need 36 vertices when I want to have colors/textures etc. for OpenGL ES application but when I export the colored cube to OBJ format using Blender, I only get 8 vertices and also I don't even get color data in the OBJ not to mention I only get 8 normals in OBJ file but I need normal for each vertex in each triangle ( a total of 36 normals).

This is what I get as the content of the OBJ file for a cube that has been colored with different colors on all the faces:

# Blender v2.56 (sub 0) OBJ File: ''
# www.blender.org
mtllib untitled.mtl
o Cube
v 1.000000 1.000000 -1.000000
v 1.000000 -1.000000 -1.000000
v -1.000000 -1.000000 -1.000000
v -1.000000 1.000000 -1.000000
v 1.000000 0.999999 1.000000
v 0.999999 -1.000001 1.000000
v -1.000000 -1.000000 1.000000
v -1.000000 1.000000 1.000000
vn 0.666646 0.666646 0.333323
vn 0.408246 0.408246 -0.816492
vn -0.408246 0.816492 -0.408246
vn -0.666646 0.333323 0.666646
vn -0.577349 -0.577349 -0.577349
vn -0.577349 -0.577349 0.577349
vn 0.816492 -0.408246 -0.408246
vn 0.333323 -0.666646 0.666646
usemtl Material
s 1
f 5//1 1//2 4//3
f 5//1 4//3 8//4
f 3//5 7//6 8//4
f 3//5 8//4 4//3
f 2//7 6//8 3//5
f 6//8 7//6 3//5
f 1//2 5//1 2//7
f 5//1 6//8 2//7
f 5//1 8//4 6//8
f 8//4 7//6 6//8
f 1//2 2//7 3//5
f 1//2 3//5 4//3

This is the content of MTL file:

# Blender MTL File: ''
# Material Count: 1
newmtl Material
Ns 96.078431
Ka 0.000000 0.000000 0.000000
Kd 0.640000 0.640000 0.640000
Ks 0.500000 0.500000 0.500000
Ni 1.000000
d 1.000000
illum 2
Was it helpful?

Solution

36 vertices for a cube is not right. Possible but unnecessary.

A vertex is a kind of coordinate in space, consisting of 3 parameters, x, y, z.
As a cube has 8 corners then there should be 8 vertices only.

Following vertices, there are texture coordinates which is obtained after UV mapping in Blender.

After texture coordinates, there are indices. They are the order of connecting vertices which determines in what order your cube is drawn.

And at last, there are normals for lighting effects.



While exporting in Blender, make sure you highlight only those:

Context:
all scenes

Output Options:
triangulate, materials, UVs, normals, HQ

Blender objects as OBJ:
objects

This gives you two files: OBJ and MTL

MTL contains texture image info
and OBJ contains:

vertices in form of:
v x, y, z
v x, y, z

texture coordinates in form of:
vt x, y
vt x, y

indices in form of:
f i/j k/l m/n
f i/j k/l m/n

After you successfully get your exported OBJ and MTL files, add them to your project with the texture image and use OpenGLOBJLoader class to render them in iOS.

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