how to load a .obj with mtl?

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

how to load a .obj with mtl?

Ususuus
I'm struggling this for 3 days, i can load .obj files, but the materials never works, can anyone give some tips?

package com.java3d.dennist.loader;

import javax.media.j3d.BranchGroup;
import com.sun.j3d.loaders.Scene;
import com.sun.j3d.loaders.objectfile.ObjectFile;
public class ObjFileReader extends BranchGroup{
    private double creaseAngle = 60.0;
    public ObjFileReader(String filePath){
        BranchGroup branchGroup = new BranchGroup();
        int flags = ObjectFile.RESIZE;
        ObjectFile objFile = new ObjectFile(flags, (float)(creaseAngle*Math.PI)/180);
        Scene scenen = null;
        try {
            scenen = objFile.load(filePath);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("OBJ load Err:" + e.getMessage());
        }
        branchGroup.addChild(scenen.getSceneGroup());
        this.addChild(branchGroup);
    }
}

package com.java3d.dennist.loader;

import java.applet.Applet;
import java.awt.BorderLayout;

import javax.media.j3d.Alpha;
import javax.media.j3d.Background;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.DirectionalLight;
import javax.media.j3d.RotationInterpolator;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.Vector3f;

import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.universe.SimpleUniverse;
public class JavaModelObjLoaderApp extends Applet{
    private static final long serialVersionUID = 5841679659336190804L;
    public BranchGroup createSceneGraph(){
        BranchGroup group = new BranchGroup();
        TransformGroup transGroup = new TransformGroup();
        Transform3D trans3d = new Transform3D();
        trans3d.setScale(0.8);
        transGroup.setTransform(trans3d);
        group.addChild(transGroup);
       
        BoundingSphere bound= new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
        Color3f bgColor = new Color3f(0.05f,0.05f,0.2f);
        Background bg = new Background(bgColor);
        bg.setApplicationBounds(bound);
        group.addChild(bg);
        Color3f lightColor = new Color3f(1.0f,1.0f,0.9f);
        Vector3f lightDirection = new Vector3f(4.0f,-7.0f,-12.0f);
        DirectionalLight light = new DirectionalLight(lightColor, lightDirection);
        light.setInfluencingBounds(bound);
        group.addChild(light);  
        TransformGroup objTrans = new TransformGroup();
        objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);

        objTrans.addChild(new ObjFileReader("F:/guanyu.obj"));

        transGroup.addChild(objTrans);

        group.compile();
       
        return group;
    }
   
   
    public JavaModelObjLoaderApp(){
       
        setLayout(new BorderLayout());
        Canvas3D canvas = new Canvas3D(null);
        add("Center",canvas);
        BranchGroup scene = createSceneGraph();
       
        SimpleUniverse universe = new SimpleUniverse(canvas);
        universe.getViewingPlatform().setNominalViewingTransform();
        universe.addBranchGraph(scene);
    }
   
   
    public static void main(String[] args) {
        new MainFrame(new JavaModelObjLoaderApp(), 360,360);
    }
}
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: how to load a .obj with mtl?

gouessej
Administrator
Hi

Your MTL file must be in the same directory as your OBJ file, otherwise use ObjectFile.setBasePath().
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: how to load a .obj with mtl?

Ususuus
my .mtl is with .obj files, the same obj works well with win 10 3D builder.

the best situation is that, i can see the obj color, but always only one of the mtl colors.

BTW, forgive my expressions, no good at English..
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: how to load a .obj with mtl?

Ususuus
This post was updated on .
In reply to this post by gouessej

updated~~~, I just get understanding that, 1.6 is not just a upgrade from 1.5..., it's working now, and working well~~
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: how to load a .obj with mtl?

gouessej
Administrator
A very few things have changed in the public APIs from Java3D 1.5 to Java3D 1.6 but there are more changes in the way it's packaged. I'm glad to learn that it works :)
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: how to load a .obj with mtl?

philjord
Ususuus,
Thanks for an excellent problem description with full code, that really is a great thing and much appreciated.

Your English is good, don't worry.

If you had a moment it would be good to get some more description of what fixed your problem, so if others are converting from 1.5 to 1.6 and happen to hit the same issue they can read here for ideas. If it was a mistake you made it will still help others if they make the same mistake, and if it's something that's unclear in the API or even a bug it'll help us to improve it.

Thanks,
Phil.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: how to load a .obj with mtl?

Ususuus
i didn't fix what i ask first,bu do get understand why the errors come out.
java3d 1.6 was not an oracle release any more,i tried 3 whole days with no progress at 1.5,and what's worse was that there'@ very limited resourse for
 java 3d. so i start thinking to update to 1.6(or even 1.7). at that time i have no idea about jogl at all,so i got these errors.
the mtl issue was fixed just because i turned out to jogl,it's quiet obvious that i can google a lot more than java3d.

 
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: how to load a .obj with mtl?

gouessej
Administrator
Most of the information is here:
http://gouessej.wordpress.com/2012/08/01/java-3d-est-de-retour-java-3d-is-back/
http://jogamp.org/wiki/index.php/Downloading_and_installing_Java3D

There are still tons of obsolete and misleading contents about Java3D on Internet.
Julien Gouesse | Personal blog | Website
Loading...