Whenever you want to use 3D objects modeled with a software such as Blender, 3ds Max or Cinema 4D in your iPhone Application or any other OpenGL project, you somehow have to make the designed data accessible to you program. To do this you can either load and interpret the files your models are saved into or you could directly provide the needed in-memory representation your program expects.
OBJ2OPENGL does the latter and acts as a converter from model files to C/C++ headers that describe vertices of the faces, normals and texture coordinates as simple arrays of floats.
OBJ2OPENGL is a Perl script that reads a Wavefront OBJ file describing a 3D object and writes a C/C++ include file describing the object in a form suitable for use with OpenGL ES. It is compatible with Objective C and the libraries of the iPhone SDK.
The original idea and code base of this script comes from Margaret Geroch who kindly allowed me to enhance and republish this version. It now supports texture mapping and stored normals (instead of recalculating them). I have changed the structure of the generated arrays to work with glDrawArrays instead of glDrawElements. This is particularly needed for normals and textures where a vertex holds different information for some shapes. Also, this version includes several command line options to better control its behavior (such as scaling and moving the origin or setting the object’s and output file’s name).
If you can go with the defaults the conversion is as simple as
To include the converted object all you have to do is
// include generated arrays #import "banana.h" // set input data to arrays glVertexPointer(3, GL_FLOAT, 0, bananaVerts); glNormalPointer(GL_FLOAT, 0, bananaNormals); glTexCoordPointer(2, GL_FLOAT, 0, bananaTexCoords); // draw data glDrawArrays(GL_TRIANGLES, 0, bananaNumVerts);
These are two models and generated sample output that is included in the provided archive: