Creating 3D-headers files for Blender-Models

I thought my little program that translates an “obj”-file made in blender to a c-header file that can be imported in an iPhone project might be useful to someone else. There is already a script available at Jeff Lamarches blog but the calculation of normals didn’t work that well for me. He also provided another example here which I started my work from, but the issue of normals wasn’t addressed back then.

Blender works with face normals, that is , a normal is saved for each triangle.

In OpenGL ES we need all 3D-data vertex-vice, so we need a normal vector for each vertex.

In Blender I don’t even save the normals into the obj-file, since they can be inferred from the geometry of a triangle and its orientation. Calculating a normal on a plane was something we learned in school, did we?

So all that is left in the obj-file is an array of vertices of dimension float[3] and an array of faces of dimension int[3]. It looks like this:

# Blender3D v249 OBJ File: humanjunk.blend
v -1.047877 -1.680641 0.324353
v -1.615929 0.175786 0.596449
v -2.184968 -0.105407 -0.072403
v -1.329265 -1.819690 -0.006392
v 1.302033 -1.445303 -0.310020
v 1.780988 -1.268360 -0.386480
v 2.087248 -1.902097 -0.058468
v 1.574263 -2.091612 0.023424
v 1.498691 -0.991348 0.229323
v 2.132722 -1.468048 0.367573
v 1.382529 -1.745196 0.487334
v 2.039944 -1.366277 -0.153916
v 1.241436 -1.661274 -0.026442
v 1.958126 -0.973603 0.242287
v 1.700415 -1.370000 0.616674
v 1.159618 -1.268600 0.369761
v 1.828412 -1.823354 0.369309
v 1.543827 -0.992686 0.508959
usemtl Red
s 1
f 3 1 2
f 4 1 3
f 14 9 18
f 5 16 9
f 16 18 9
f 13 8 11
f 7 17 8
f 17 11 8
f 17 15 11
f 7 10 17

So a triangle is just an ordered set of tree vertices

triangle = { v_i, v_j, v_k }

and we may define the star of some vertex v as the set of all triangles that v is a member of:

star(v_n) = { triangle : v_n in triangle }

Now we may calculate the normal of a triangle using the Cross product

In our case we have to be careful to use the right order of vertices because if the normal points in the wrong direction OpenGL ES will light triangles surrounding vertex on th wrong side!

So for a triangle {v_1, v_2, v_3} the normal is

n := (v_2 – v_1) x (v_3 – v_1)

So now we calculated a normal for one triangle from the coordinates of the vertices in the mesh and all we have left to do to calculate the normal of a vertex v is to take the average of all normals in the star of v.

The Code

Most iPhone developers are used to Objective-C projects so I just did one for this purpose. Some lines are taken from Jeff Lamarches example project here , especially the parsing of the obj-file and loading of the input data part at the beginning of the method “(void)makeHeaderFile”.

To have a look at my Code, download this file , extract it and open it in Xcode. Then you may drag a Blender-created *.obj file into the resources, add the described lines in the “ObjParserAppDelegate.m” under “applicationDidFinishLaunching” , build&run and finally open the debug-folder to get your *.h file holding the geometries information.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: