how to load a .obj with mtl?

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

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
|

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
|

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
|

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
|

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
|

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
|

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
|

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
Reply | Threaded
Open this post in threaded view
|

Re: how to load a .obj with mtl?

jf Pauly
In reply to this post by Ususuus
I have also been  struggling (as you say) to get textures with WaveFront models.
So I have copied the objectfile package into my own stuff to be able to look at the code (and may be change it ).
I have coded several changes :
1- when an unknown token is encountered in ObjectFileMaterials.java, do not just crash, ignore the token (line 340)
2- sometimes (often??) the models I got do not include group tokens as they would do. So ObjectFile.java does not see the groups token and does not build shap3D's to assign appearances with textures. I have added code to generate groups.
Changed code apply this simple rule to find material and textures:

 * The folder which contains .obj also contains .mtl
 * It also contains a folder named textures with texture pictures.

To verify I suggest the following :

goto https://sourceforge.net/projects/cliper/ 
You could download the project and include the objectfile package it contains to test.
You can also download the dist file,  run the JAR and load models.
I am afraid that my late response is not usefull for you, but I hope that these corrections could help other people.
Regards
jf Pauly
 
Reply | Threaded
Open this post in threaded view
|

Re: how to load a .obj with mtl?

gouessej
Administrator
Thank you for your contribution but you cannot relicense our source code into "public domain", this class is under GPL version 2 with classpath exception. If you keep it in your project, you have to use the same license, otherwise you have to keep your project and your modified version of Java3D separated (and a pull request would be welcome of course).

P.S: I advise you to use a versioning system and to drive your project independent from Netbeans. I use Eclipse and I do my best to allow other people to build my projects with several build tools without forcing them to use a particular IDE. Seeing exactly what you modified is less easier than it should, I won't download a compressed archive to find that. Please make an effort. Providing only archives is what I did more than ten years ago when I didn't know CVS, Subversion and GIT.
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: how to load a .obj with mtl?

jf Pauly
Thank you for your response.
I am going to correct the license.
And learn a bit about GIT.
I start from scratch because I  does not know what is a pull request.
Thank you again for your help
jf Pauly