.OBJ loader for Processing
Maintained by SAITO and Matt Ditton (AKA Polymonkey)
SAITO: http://users.design.ucla.edu/~tatsuyas/tools/objloader/index.htm
Polymonkey: http://www.polymonkey.com/blogger-polymonkey/
Updates
- 23/07/2009 : rev18 update to OBJTools to include void hackUVClampToZeroOne(OBJModel m) and void hackUVMapToZeroOne(OBJModel m) functions. These are "in case of emergency break UV's" functions. Added by request.
- 18/07/2009 : rev17 fixed materials not showing up when mtl file has extra tabs at the front of lines. Bug found by Noirmain Pierre.
- 16/06/2009 : rev16 added int getTotalFaceCount() to get the number of total faces. And PVector getFaceVertArray(int faceNumber) this returns an array of PVectors that make up the positions of the verts of the face. These two methods are mainly used for extracting position information from the model.
- 16/04/2009 : rev15 moved Vert, Normal, and UV's to be stored in the processing PVector object. Added getNormal methods and objTools. And bug fix for filling with diffuse color
- 12/07/2008 : rev14 added the first pass of the fast draw method
- 07/01/2007 : rev13 added some material features (by polymonkey)
- 05/29/2007 : rev12 fixed a texture coordinate bug. texture swapping (thanks to polymonkey)
- 03/10/2007 : rev11 fixed some major bugs.
- 01/30/2006 : rev10 fixed some bugs.
- 11/16/2005 : rev09 supported Processing 95+.
- 06/18/2005 : rev07 added functions for model transformation
- 04/05/2005 : rev06 fixed a few bugs, supports alpha value
- 28/04/2005 : rev05 applet execution support
- 26/04/2005 : rev04 fixed a texture mapping problem
- 25/04/2005 : added FAQ
- 24/04/2005 : rev03 fixed a few bugs, 3ds exported .obj support
- 19/04/2005 : rev02 .mtl support, normal vertex support, lighting support
- 16/04/2005 : rev01 launched
Description
This is Wavefront Alias .OBJ file loader for Processing. It loads .OBJ model file and renders the model onto a screen.
Download
http://code.google.com/p/saitoobjloader/downloads/list
Installation After uncompressing the file, copy 'objloader/' folder into 'libraries/' folder which you can find under the Processing folder. Since the archive file is compressed in MacOSX environment, it might have some needless hidden files. Notice that the file you need is only 'objloader' folder and the files in the folder.
The folder/file structure should be as follows.
Restart Processing. You should be able to import the objloader library from sketch menu.
Reference
This library contains a class for loading/rendering a .OBJ file (OBJModel) and a class for accessing each vertex (Vertex) which can be primarily used to addressing vertexes in the model file and transform it.
This is a list of all current functions in the obj loader. Example use can be seen in the above sample project.
- void load(String filename)
example -> model.load(filename.obj)
description -> loads a model file.
- void draw()
example -> model.draw()
description -> draws the model.
- void drawMode(int mode)
example -> model.drawMode(mode)
description -> set render mode (ex. TRIANGLES, POLYGON, LINES etc.)
Basically the argument to this function is same as the one given to beginShape(). POINTS mode isn't reliable in OPENGL. And you've got to turn stroke on to see LINES. TRIANGLES and QUADS are the most reliable but it depends on your export setting.
- void setTexture(PImage)
example -> model.setTexture(PImage)
description -> swap the model texture with the specified PImage.
- void enableTexture()
example -> model.enableTexture()
description -> enable texture rendering. This is on by default.
- void disableTexture()
example -> model.disableTexture()
description -> disable texture rendering.
- void originalTexture()
example -> model.originalTexture()
description -> turns on the original texture that is mentioned in the mtl file. This is a useful function once you've gone a little crazy with the setTexture function.
- void enableLocalTexture()
example -> model.enableLocalTexture()
description -> Use this function before the load function to get the texture from the DATA folder. Some packages (like XSI) save absolute paths into the texture name of the mtl file. This helper function removes the text before the texture filename. At the moment it uses the windows '/' as a separator. I don't have a Mac or run Linux so if anyone would like to throw an exported mtl file my way I'd be happy to make them work too.
- void enableMaterial()
example -> model.enableMaterial()
description -> enables the use of the exported material this is on by default.
- void disableMaterial()
example -> model.disableMaterial()
description -> disables the use of the exported material. Using this means that you can use fill() before the model draw to set the material to any crazy color.
- void clear()
example -> model.clear()
description -> clears all model data from the model. This way you can get rid of the loaded obj and reload another one.
- int getVertexsize()
example -> model.getVertexsize()
description -> returns the number of vertexes that the model contains.
- Vertex getVertex(int index)
example -> model.getVertex(pickNumber)
description -> returns the vertex data specified by array index.
FAQ
- What is .OBJ file?
.OBJ file is a standard 3D object file format by Alias.
Alias is the leading software company of 3D graphics technology for the film, video, game development, interactive media, industrial design, automotive industry and visualization softwares. Their .OBJ ASCII file format is widely accepted all over the world as a standard format for exchanging data between 3D graphics applications. OBJ files contain solids which are made up of 3 or 4 sided faces and material data such as texture is searatedly stored in .MTL file.
- What is .MTL file?
.OBJ file often has a link to .MTL file, which contains material information such as texture, color and surface reflection. To use the material information, you need to add .MTL file to the project. Currently, OBJModel class supports only texture information. Texture file images (.jpg) should be added to your project too.
Future updates will include support for other material information.
- Polygons are not correctly rendered. What should I do?
The first thing you should make sure is what type of polygons your model is composed of. If it is made of quads, the correct option for drawMode() is POLYGON. If it is made of triangles, drawMode(TRIANGLES) still works. drawMode(TRIANGLES) is recommended for Processing BETA 85 because of some rendering bugs (shapes drawn as POLYGONS sometimes don't show up.) More detailed info is available here (discource: Libraries, Tools - New Libraries: OBJ Loader, Google Web API )
If the model still doesn't show up, please send me the Processing source code and .OBJ model files you are trying to render. tatsuyas@ucla.edu
- Texture is not correctly shown. What should I do?
To render texture, three files have to be added to your project: .OBJ file, .MTL file, texture image file.
OBJModel uses standard Processing functions to render models. Inside the library, texture image is stored as PImage object, which means that the texture image file format has to be supported by PImage. Currently it supports .jpg.
- I still have a problem with online execution
Hmm.. I updated the library so that it can load models files by relative path whereever a sketch program is located. It might still have a bug. Please send me the model file and the sketch program you have a problem with.
Examples
- Simple rendering
// .OBJ Loader
// by SAITO <http://users.design.ucla.edu/~tatsuyas>
// Placing a virtual structure represented as mathematically
// three-dimensional object.
// OBJModel.load() reads structure data of the object stored
// as numerical data.
// OBJModel.draw() gives a visual form to the structure data.
// processing standard drawing functions can be used to manipulate
// the visual form to create deep visual experiences.
// Created 20 April 2005
import saito.objloader.*;
import processing.opengl.*;
OBJModel model;
float rotX;
float rotY;
void setup()
{
size(400, 400, OPENGL);
model = new OBJModel(this, "dma.obj");
}
void draw()
{
background(51);
noStroke();
lights();
pushMatrix();
translate(width/2, height/2, 0);
rotateX(rotY);
rotateY(rotX);
scale(20.0);
model.drawMode(POLYGON);
model.draw();
popMatrix();
}
void keyPressed()
{
if(key == 'a')
model.enableTexture();
else if(key=='b')
model.disableTexture();
}
void mouseDragged()
{
rotX += (mouseX - pmouseX) * 0.01;
rotY -= (mouseY - pmouseY) * 0.01;
}
- Model transformation
// .OBJ Loader transformation
// by SAITO <http://users.design.ucla.edu/~tatsuyas>
// Placing a virtual structure represented as mathematically
// three-dimensional object.
// OBJModel.getVertex() allows accessing to each vertex
// OBJModel.setVertex() allows transformation of a model
import saito.objloader.*;
OBJModel model;
OBJModel tmpmodel;
float rotX;
float rotY;
void setup()
{
size(600, 600, P3D);
model = new OBJModel(this, "dma.obj");
tmpmodel = new OBJModel(this, "dma.obj");
model.debugMode();
}
void draw()
{
background(255);
lights();
pushMatrix();
translate(width/2, height/2, 0);
rotateX(rotY);
rotateY(rotX);
scale(30.0);
// renders the temporary model
tmpmodel.draw();
popMatrix();
animation();
}
// transformation parameter
float k = 0.0;
// transforms the orignal model shape and stores transformed shape
// into temporary model storage
void animation(){
for(int i = 0; i < model.getVertexsize(); i++){
PVector orgv = model.getVertex(i);
PVector tmpv = new PVector();
tmpv.x = orgv.x * (abs(sin(k+i*0.04)) * 0.3 + 1.0);
tmpv.y = orgv.y * (abs(cos(k+i*0.04)) * 0.3 + 1.0);
tmpv.z = orgv.z * (abs(cos(k/5.)) * 0.3 + 1.0);
tmpmodel.setVertex(i, tmpv);
}
k+=0.1;
}
void mouseDragged()
{
rotX += (mouseX - pmouseX) * 0.01;
rotY -= (mouseY - pmouseY) * 0.01;
}
Etc.
Bugs, opinions and complains? Please drop a line here: http://code.google.com/p/saitoobjloader/w/list