Extending Processing.js with a OBJ Importer Part 4 January 5, 2012
Posted by Andor Salga in Open Source, Processing, Processing.js, Processing.js OBJ Support, webgl.trackback
Run Me!

This sketch demonstrates more .obj file support for Processing.js.
This blog post is the continuation of a series of blogs [1, 2, 3] related to adding .obj file support to Processing.js. This code I’m working on is important since it will allow developers to easily load 3D models from files and it will increase the performance of rendering 3D objects in Processing.js.
Since my last blog, I have added some small but critical changes to the code, some of which I outline here.
Interface Change
I contacted one of the developers of Processing, Andrés Colubri, who is reworking most of the OpenGL code. Some of his rework includes making Saito’s .obj loader native in Processing. This is great for Processing, but it means that all the time I spent making the Processing.js .obj loader work like Saito’s was wasted ): On the other hand, it means that pushing this code in the next release of Processing.js might actually be done! (:
The sketch below is a simple example of using Saito’s .obj extension, which my code expected.
OBJModel obj;
void setup(){
size(100, 100, 3D);
obj = new OBJModel();
obj.load("object.obj");
}
void draw(){
obj.drawMode(POLYGON);
}
The problem was that I had no idea what loading 3D models was supposed to look like natively. So I asked Andréas for a simple sketch that worked in Processing and that I could emulate in Processing.js.
After receiving the sketch, I was glad to see it wasn’t much different.
PShape obj;
void setup() {
size(100, 100, 3D);
obj = loadShape("object.obj");
}
void draw() {
shape(obj);
}
I was able to quickly add a few hacks to make Processing.js work with the new interface. I didn’t want to rewrite my entire parser just yet since all my tests rely on the old method. I also don’t want to rewrite my code a third time (:
Triangulation
I found that many 3D authoring tools export .obj models with triangle fans. In my last blog about .obj importing I wrote about the lack of support in my code for this scenario, but I recently wrote a patch that fixes the issue. It was not difficult to write, but because of this fix, many more models can now be properly parsed. This includes the 3D model at the top of this post.
Testing, testing, …
I found a few more issues with the parser so I fixed them and added reference tests. I’m finding these tests invaluable since I’m often tweaking the parser as I go. I have just over 30 right now, but I hope to have many more since I expect the code will go through many more transformations.

Feedback
If you are using my ‘extension’ and you find a file that isn’t being properly loaded, please send me your file so that I can fix it and add a test.
Nice work, good to hear about the progress. I’m still patiently waiting for the release
Hey Matas,
Just fixed another small issue. Don’t wait for the release, start using it now!
https://raw.github.com/asalga/processing-js/bug608/processing.js
It would be awesome if you could report any bugs back to me so I can fix them.
Hey Andor, this is totally amazing, already have some 3d models working! This is just great. I will definitely report bugs if I’ll find something.
Awesome, good to hear!
Hey Andor, is it possible to orient shapes according to their coordinate system, for example say setOrigin(PVector), setAxisX(PVector), setAxisY, setAxisZ? I understand that it could be done in local scope by translating world coordinates, but this might be more intuitive and would allow shapes to appear in my own coordinate spaces.
Sorry to have asked this before investigating the problem deeper, in two days I’ve got it working and it is a fantastic object loader, will share results and experiences a bit later… No bugs till now and everything works!
Hi Matas,
If I implement functionality which isn’t present in the Processing loader, my patch will likely get rejected. If you find a solution to the problem, post your sketch online so others can benefit.
Hello Andor,
You will find latest post in my blog http://fucture.org where I give url to try out coordinate system driven 3d environment written in processingjs. Code is slow and messy at the moment, so I will share it later, of course you can track it down from pde, but I suggest waiting for cleaner examples. I am an architect, not programmer, thus code is probably not professional, but it works and it is good for now…
Although it is obvious, I forgot to mention that it uses your model loader for all 3d shapes and I’m really thankful for your initiative
my hosting provider has some problems, so new link is here, hope it won’t stop working… http://feese.clod5.com/