• fullscreen
  • Loading_3D_OBJ_Model.pde
  • import codeanticode.glgraphics.*;
    
    //import processing.opengl.*;
    //import javax.media.opengl.*;
    import saito.objloader.*;
    
    // declare that we need a OBJModel and we'll be calling it "model"
    OBJModel model;
    
    float rotX;
    float rotY;
    PImage tex;
    // these booleans will be used to turn on and off bits of the OBJModel
    boolean bTexture = true;
    boolean bStroke = false;
    boolean bMaterial = false;
    
    void setup()
    {
        size(800, 800, P3D);
      
        // making an object called "model" that is a new instance of OBJModel
        model = new OBJModel(this,"Lancer.obj", "relative", POLYGON);
       
        // turning on the debug output (it's all the stuff that spews out in the black box down the bottom)
        model.enableDebug();
        model.translateToCenter();
        model.scale(.9);
        model.printModelInfo();
        noStroke();
    }
    
    void draw()
    {
        background(255);
        lights();
    
        //this will do nothing until the model material is turned off
      //  fill(255,0,255);
    
        pushMatrix();
        translate(width/2, height/2, 0);
        rotateX(rotY);
        rotateY(rotX);
        model.draw();
        popMatrix();
    }
    
    
    
    void keyPressed() {
        // turns on and off the texture listed in .mtl file
        if(key == 't') {
            if(!bTexture) {
                model.enableTexture();
                bTexture = true;
            } 
            else {
                model.disableTexture();
                bTexture = false;
            }
        }
    
        else if(key == 'm') {
            // turns on and off the material listed in .mtl file
            if(!bMaterial) {
                model.enableMaterial();
                bMaterial = true;
            } 
            else {
                model.disableMaterial();
                bMaterial = false;
            }
        }
    
        else if(key == 's') {
            if(!bStroke) {
                stroke(10, 10, 10);
                bStroke = true;
            } 
            else {
                noStroke();
                bStroke = false;
            }
        }
    
        // the follwing changes the render modes
        // POINTS mode is a little flakey in OPENGL (known processing bug)
        // the best one to use is the one you exported the obj as
        // when in doubt try TRIANGLES or POLYGON
        else if(key=='1') {
            stroke(10, 10, 10);
            bStroke = true;
            model.shapeMode(POINTS);
        }
    
        else if(key=='2') {
            stroke(10, 10, 10);
            bStroke = true;
            model.shapeMode(LINES);
        }
    
        else if(key=='3') {
            model.shapeMode(TRIANGLES);
        }
    
        else if(key=='4') {
            model.shapeMode(POLYGON);
        }
    
        else if(key=='5') {
            model.shapeMode(TRIANGLE_STRIP);
        }
    
        else if(key=='6') {
            model.shapeMode(QUADS);
        }
    
        else if(key=='7') {
            model.shapeMode(QUAD_STRIP);
        }
    }
    void mouseDragged()
    {
        rotX += (mouseX - pmouseX) * 0.01;
        rotY -= (mouseY - pmouseY) * 0.01;
    }
    
    

    code

    tweaks (9)

    about this sketch

    This sketch is running as Java applet, exported from Processing.

    license

    advertisement

    archana

    Loading_3D_OBJ_Model

    Add to Faves Me Likey@! 2
    You must login/register to add this sketch to your favorites.

    I simply use (OBJLoader) SAITO Library to import 3D Model. Press & Drag the mouse to rotate the model.

    Press 1-7 to check the ShapeMode...
    Also Press m-for material,
    t- for texture,
    s- for Stroke...

    PROCESSJW
    8 Apr 2013
    Hi there im glad i found your sketch, i've been stuck on my own for a long time, been trying to get so the obj can load all my textures as they are exported along with the obj as separate jpegs, how did you result with a .bmp file that contains all your textures unwrapped? I am really new at this and needing to find a way to do this because i am determined to learn for my project. I currently have my .obj and mtl and jpegs that go with it, how do i change it so my images combined into one jpeg/image file similar to yours?

    I hope you can help

    Thanks
    Archana
    10 Apr 2013
    HI jwang,

    Actually I have faced the similar problem when an object has more texture files.
    In this example there is only one texture file.

    But m trying to load the texture files on object with the help of objImport Library . And its worked fine . For me the problem releated to texture is reduced now. So if a 3D object has more than one texture files, the problem of UV mapped can be solved through this way . This library imports .obj files and supports groups, material references, vertex-, normal-, and texture-coordinates. You can get this library from here : http://www.pixelnerve.com/processing/libraries/objimport/

    Thanks
    PROCESSJW
    10 Apr 2013
    I downloaded the OBJimport library and was trying to put mine in but the example code was very complicated, i think it was an example to load an entire scene? so i wasn't able to figure how to load just a simple obj with that library because I am still learning the code, do u know the code to load a simple obj into the library? My 3D model does not have a uv map just jpegs that the .obj and .mtl are linked with.

    Do you know the code for how to simply display the 3D model with multiple textures with this objimport library?

    Thanks so much for the reply! I may be able to solve my problem if this works.
    Archana
    11 Apr 2013
    Hello jwang,

    Can you please provide me your email Id, so I can send you the simplest code for this.
    Sanjana
    14 Oct 2013
    hey even m facing the same problem as PROCESSJW can u resolve my doubt too? please
    archana
    15 Oct 2013
    Hi Sanjana ..
    give me your email add... want to discuss it with you..
    d.1
    27 Dec 2013
    hello , how can i make the .obj file ..the 3d object to rotate smoothly ?
    You need to login/register to comment.