CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Sep 2012

    Java 3D: Trouble with .mtl files

    So I've been trying to play around with Java 3D and, recently, I've been playing around with importing outside 3D models into a program. At this point, I can get the model into the program as an OBJ file, but for whatever reason, the program won't load the corresponding material file.

    This is the code I wrote:

    import com.sun.j3d.utils.universe.*;
    import com.sun.j3d.utils.geometry.*;
    import javax.media.j3d.*;
    import com.sun.j3d.loaders.objectfile.*;
    import com.sun.j3d.loaders.Scene;
    import java.awt.Color;
    import javax.vecmath.*;
    import javax.swing.*;
    import javax.imageio.IIOException;
    public class ModelLoadingTest {
        public static void main(String[] args) {
            SimpleUniverse universe = new SimpleUniverse();
            BranchGroup scene = new BranchGroup();
            ObjectFile loader = new ObjectFile(ObjectFile.RESIZE);
            Scene modelScene = null;
            Shape3D model = new Shape3D();
                modelScene = loader.load("Pterodactyl.OBJ");
                model = (Shape3D) modelScene.getSceneGroup().getChild(0);
                //JOptionPane.showMessageDialog(null, model.getAppearance().getMaterial());
            catch(Exception e){
                JOptionPane.showMessageDialog(null, e.toString());
            DirectionalLight lighting = new DirectionalLight(new Color3f(Color.WHITE), new Vector3f(0f, 0f, -1f));
            lighting.setInfluencingBounds(new BoundingSphere(new Point3d(0.0, 0.0, 1.0), 100));
    This is the beginning of the .obj file:

    # 3ds Max Wavefront OBJ Exporter
    mtllib Pterodactyl.mtl
    # object Pterodactyl
    v  121.2250 19.0041 66.8833
    v  116.1940 12.5999 63.5611
    v  135.5396 13.8324 64.7891
    v  137.5667 21.8641 68.2204
    v  121.5318 24.7219 64.6238
    v  170.6878 5.0353 69.7959
    And this is the corrosponding .mtl file:

    # 3ds Max Wavefront OBJ Exporter
    newmtl pterodactyl_MAT
    	Ns 10.0000
    	Ni 1.5000
    	d 1.0000
    	Tr 0.0000
    	Tf 1.0000 1.0000 1.0000 
    	illum 2
    	Ka 0.5880 0.5880 0.5880
    	Kd 0.5880 0.5880 0.5880
    	Ks 0.0000 0.0000 0.0000
    	Ke 0.0000 0.0000 0.0000
    	map_Ka Pterodactyl_D.tga
    	map_Kd Pterodactyl_D.tga
    Now, the code runs fine when the mtllib call in the .obj file is removed (save for the fact that the pterodactyl is white and has a material generated by default by the Java 3D library), but when the mtllib call is present and uncommented in the .obj file, the following error occurs when I run the program:

    com.sun.j3d.utils.image.ImageException: javax.imageio.IIOException: Can't read input file!
    Now, I'm not sure if I'm correct, but for this exception to occur in the first place, the program has to acknowledge that the .mtl file exists which begs the question of why it can't read it. Is it because the .mtl file is formatted wrong? Is it because the call in the .obj file is wrong or located in the wrong part of the file, or is it something to do with my coding?

    Sidenote: Before anyone suggests it as a fix, Pterodactyl.obj and Pterodactyl.mtl ARE in the same folder.

  2. #2
    Join Date
    May 2006

    Re: Java 3D: Trouble with .mtl files

    Don't know anything about Java3D so can't really help but I suggest you post the whole exception output with the stack trace. It may show something important to someone who knows Java3D.

    I only other thing, and this is a total guess, is Pterodactyl is with a capital P everywhere except the beginning of the mtl file where it is pterodactyl_MAT. I don't know if this loading mechanism is case sensitive but it may be worth changing it to Pterodactyl_MAT.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Sep 2012

    Re: Java 3D: Trouble with .mtl files

    Found the problem: Turns out I forgot to include the texture files the .mtl file tried to call and that the compiler can't seem to read .tga files.

  4. #4
    Join Date
    Mar 2013

    Re: Java 3D: Trouble with .mtl files

    dude. I dont know who you are, but right now you are practicly a god send to me. Ive been searching left and right on how to do what you did. Copying your code it saved my life, thank you some much. I think you could try and convert the mlt to a image than apply it as a texture.

  5. #5
    Join Date
    May 2009

    Re: Java 3D: Trouble with .mtl files

    Quote Originally Posted by fftk View Post
    Well, no one uses Java 3D anymore. It was abandoned by the dudes at Sun years ago after years of neglect.

    In fact Java 3D is the quintessential example of what happend when Java hype finally hit the ground. It flipped and tumbled and not even Oracle has managed to stop it from going into oblivition.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.