Skip to main content

Loading in .obj & .mtl files

4 replies [Last post]
necro_ignis
Offline
Joined: 2005-07-23

HI all, I'm quite new to Java3D and 3D graphics and I'm struggling to understand how to load in models correctly. I have the latest Java3D binaries, Java 6, and I'm using Flux Studio 2.1 (as I'm also interested in X3D).

Ok the problem. I can import an .obj file into Java3D and have it displayed on the canvas. The object seems to require a texture to be visible; if my knowledge serves me correctly without a texture it may not have a material which enables lighting (could be wrong). Anyway. Loading in a single cube with a brick texture is fine. The moment I make a simply roof with a different texture and attached to the cube (the walls), the geometry loads in Java3D but it uses the first texture (the brick) to cover both the walls (cube) and roof! I don't understand why this second texture has vanished.

I've never looked at an .obj file before but after having opened up the .mtl file it looks like a line is missing:

# File produced by

newmtl dad_Roof
Ns 4
d 1
illum 2
Kd 1.000000 0.000000 0.000000
Ka 0.200000 0.200000 0.200000
Ks 0.000000 0.000000 0.000000

newmtl dad_Walls
Ns 4
d 1
illum 2
Kd 1.000000 0.000000 0.000000
Ka 0.200000 0.200000 0.200000
Ks 0.000000 0.000000 0.000000
map_Kd stonetile10.gif

There is no map_Kd entry for the dad_Roof which is being referenced in the .obj file.

I have tried putting in my own map_Kd entry, but it does nothing. Help! :-)

Incase it's something to do with my Java3D code, here's a very simple example of what I am using:

ObjectFile objFileLoader = new ObjectFile(ObjectFile.RESIZE);
File modelFile = new File("D:\\Profiles\\aan025\\My Documents\\test.obj");
URL url = null;
try {
url = modelFile.toURI().toURL();
} catch(MalformedURLException exp) {
exp.printStackTrace();
}
try {
Scene scene = objFileLoader.load(url);
branchGroup = scene.getSceneGroup();
analyseBG(branchGroup);
} catch(FileNotFoundException exp) {
exp.printStackTrace();
}
return branchGroup;

Thanks very much :-)

P.S. X Posted on the java.sun.com forums, I'm not sure which gets the most traffic.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
necro_ignis
Offline
Joined: 2005-07-23

Looking further into the problem of loading .obj files via ObjectFile into Java3D, if I generate three shapes and give each an individual textures the .mtl file is such:

# File produced by

newmtl dad_Box1
Ns 4
d 1
illum 2
Kd 1.000000 0.000000 0.000000
Ka 0.200000 0.200000 0.200000
Ks 0.000000 0.000000 0.000000

newmtl dad_Cone1
Ns 4
d 1
illum 2
Kd 1.000000 0.000000 0.000000
Ka 0.200000 0.200000 0.200000
Ks 0.000000 0.000000 0.000000

newmtl dad_Sphere1
Ns 4
d 1
illum 2
Kd 1.000000 0.000000 0.000000
Ka 0.200000 0.200000 0.200000
Ks 0.000000 0.000000 0.000000
[b]map_Kd D:\Profiles\aan025\My Documents\141_1920.jpg[/b]

So when producing anymore than two objects, the exporting for FluxStudio 2.0 only puts one texture inside the .mtl file.

Is this a limitation on .obj files? Does anyone have any experience in this at all, I'm simply trying to load in an .obj file of a house with two different textures. Looking at it further, however, it appears as though Java3D is treating the complex shape (a set of walls and a roof) as a single Shape3D object.

Help! Thanks

robroche
Offline
Joined: 2010-05-19

Hey,

I have had a similar problem. I believe Java3D's implementation of the *.obj files is not complete and has some holes. I have also found that when loading a shape with an associated material files(.mtl) Java3D will only load the 1st texture found(even if the files contains more than 1 texture). I believe there is also a problem with loading materials and textures if they are not in the same base directory.

Personally I think that *.obj files are a poor choice for Java3D because you cannot save lights, and other Java3D-only fields. It's purely shapes and some materials.

necro_ignis
Offline
Joined: 2005-07-23

Has anyone had any luck in loading multiple textures from an .obj file?

Take one obj file, along with a .mtl file. Load it via ObjectFile into the latest Java3D. The scene should be two shapes with a different textures each i.e., two different .jpg files. Can this be done? Or is it one texture per Shape3D loaded in from the .obj and .mtl file?

thoa070988
Offline
Joined: 2010-08-24

can you please sent to me an example load file obj+mtl (source code). Thanks...