Sunday 3 April 2011

PHP ObjLoader

Just finished writing a PHP Obj Loader for WebGL.

The basic requirement of the loader was to read in an .obj file, and turn it into something useful for use with WebGL. In order to do this, I wanted to write a PHP backend that could parse the information into JSON.

Also, I chose to format the dataset into Triangles, with no merged verts so I could tell GL to draw in GL_TRIANGLE mode with no worries.

The current version of the loader allows three lines to be called in order to output a .js version of the geometry:

$objLoader = new ObjLoader("geo/test.obj");
$objLoader->unpackForGl();
$objLoader->writeUnpackedToJson("geo/test.obj.js");


These lines will output a file in the following format.

var vertices = [
 -0.500000,-0.500000,0.500000,
 0.500000,-0.500000,0.500000,
 -0.500000,0.500000,0.500000
]

var texCoords = [
 -0.500000,-0.500000,0.500000,
 0.500000,-0.500000,0.500000,
 -0.500000,0.500000,0.500000
]

var normals = [
 -0.500000,-0.500000,0.500000,
 0.500000,-0.500000,0.500000,
 -0.500000,0.500000,0.500000
]



Now when setting up my buffers, I can simply include the script in the <head> tag and refer to these values  to send on for shading.


I have begun to learn WebGL using the tutorials at http://learningwebgl.com/blog/?p=28

So to test this loader, I used the first tutorial (with some tweaks) and have rendered an obj successfully.

2 comments: