Problem loading Wavefront textures in Java3D

Hi all,

I am using the Wavefront loader that comes with Java3D to load an .obj model. The model has a reference to a .mtl file that in turn has multiple references to a single .jpg texture file.

When I load the .obj file using PoseRay, both the geometry and the texture load just fine. However, when I load the .obj using ObjectFile in Java3D, only the geometry loads–no textures.

I went into the .obj and .mtl files to tweak the references and see whether they have the expected effect on the model when loading it into PoseRay. The tweaks have exactly the expected effect. For example, if inside the .mtl file I break a reference to the .jpg file, then the corresponding texture disappears from the loaded model in PoseRay. So I am comfortable that the .obj and .mtl files are at least what PoseRay expects.

Note that the .obj and .mtl files were originally generated by PoseRay from a .3ds file. I used PoseRay to load a .3ds model and export as .obj. When PoseRay exports to .obj it uses absolute file paths for the .jpg references. I changed those to relative file paths and then loaded them back into PoseRay just to make sure that PoseRay would still be able to find the .jpg. It was able to.

Also, I tried leaving in the absolute file paths and putting the .jpg file at the required location, and then loading using Java3D. No luck either with absolute or relative paths to the .jpg. I’m assuming that if I have the .obj, .mtl, and .jpg in the same directory, then I can reference the .mtl from .obj using the filename alone, and that I can reference the .jpg file from the .mtl file using the filename alone.

So: Does the ObjectLoader support loading the referenced textures? ;D

Here is the Java code I am using:

    ObjectFile objFile = new ObjectFile();
    InputStream is = ClassLoader.getSystemResourceAsStream("path/to/model.obj"); // This works; I see the geom
    Scene scene = objFile.load(new InputStreamReader(is));
    Map<String, Shape3D> map = scene.getNamedObjects();
    BranchGroup root = scene.getSceneGroup();
    root.removeAllChildren();
    Shape3D shape = map.get("medievalHouse");

Also, I saw this thread, which appears to describe a problem similar to the one I’m having, but it doesn’t seem that I should have to use LWJGL to solve the problem:

http://www.java-gaming.org/forums/index.php?topic=13753.0

Thanks for any help that people may be able to offer.

Willie

I just saw this post, which claims that Wavefront models loaded by ObjectFile are untextured:

http://www.java-gaming.org/forums/index.php?topic=15114.0

Is this accurate?

Thanks,
Willie

I’m going through the forums and looking for more related posts, partly to try to solve my problem and partly to consolidate information for the benefit of others who may in the future have the same issue. Here is a post from a long time ago:

http://www.java-gaming.org/forums/index.php?topic=123.0

The thing on the minimart is that the texture files are type .int, .inta, and some others. No .jpg. Yet according to the following page, .jpg is a supported texture file format (see the bottom):

http://java3d.j3d.org/utilities/loaders/obj/sun.html

So I use the VRML97 Loader for my projects. I have noticed that relative paths don’t work. Meaning, if my texture file is called texture.jpg then inside my VRML file, I have to remove the path to this file (which is automatically set by my 3D Modeler program). I then have to copy the texture file to the same directory as the .wrl file.

So the director would look like this:
data/object.wrl
data/texture.jpg

Have you tried something like that?

Thanks Conzar for the reply. Yup, that is exactly the approach that I have been trying to make work. :slight_smile: (But with .obj files rather than .wrl files.) The modeler tool outputs the materials and texture files to the same directory as the model file (good), but uses absolute file paths in the model file (bad). So I go in and replace the absolute file paths with relative file paths. The modeler tool still finds the materials and textures. But Java3D–specifically, ObjectFile it would seem–doesn’t find them, or doesn’t care about them, or something. And Java3D doesn’t find them with the absolute paths either.

Hey. Try to not use paths at all.

Meaning, put your textures in the same directory as what they are applied to.

Inside the model file, point the texture path just to the file name itself

Did you try that?

Yeah, that’s the approach I’ve been taking all along. I just put the three files in the same directory and just used filenames for references. PoseRay was able to resolve the refs just fine but no such luck with Java3D. I suspect that ObjectFile doesn’t know how to handle the materials reference. :’(

Anyway you can convert to another file format?

That’s a good idea. I guess I’ll try that then. Either that or maybe look for a better OBJ loader…

Well, somehow the Java3D OBJ loader can load textures. :slight_smile: I just tried using the demo ObjLoad demo program, which uses the ObjectFile Wavefront loader, and it correctly loaded both the geometry and the textures. So I just haven’t figured it out yet. I’ll crack open the source code and when I figure out what I’ve been doing incorrectly I’ll post it.

Hooray, I figured out what was wrong, and now the model has textures:

The problem was related to my having tried to use an InputStream and InputStreamReader to load the model instead of using a URL. With the InputStream, apparently ObjectFile did not know the base directory for resolving the reference to the materials file. Once I switched over to using a URL, ObjectFile was able to figure it out…

Thanks Conzar for your help.