My favorites | Sign in
Project Logo
                
Feeds:
People details
Project owners:
  mattditton, tatsuyas

.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

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.

example -> model.load(filename.obj)
description -> loads a model file.
example -> model.draw()
description -> draws the model.

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.
example -> model.setTexture(PImage)
description -> swap the model texture with the specified PImage.
example -> model.enableTexture()
description -> enable texture rendering. This is on by default.
example -> model.disableTexture()
description -> disable texture rendering.
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.
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.
example -> model.enableMaterial()
description -> enables the use of the exported material this is on by default.
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.

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.
example -> model.getVertexsize()
description -> returns the number of vertexes that the model contains.
example -> model.getVertex(pickNumber)
description -> returns the vertex data specified by array index.

FAQ

.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.

.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

// .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









Hosted by Google Code