Importing a model

So we can now generate a basic geometry if we need to, lets look at making ourselves a model. I have already done this, creating a little purple dude. I don’t think I’m giving anything away at this point in the game if I tell you that I’m planning for him to walk.

To create my little purple dude I used milkshape (http://www.swissquake.ch/chumbalum-soft/ ) which can be downloaded on a 30 day trial for free and costs very little to buy if you like it. This has been the most intuitive design program I have used and I recommend it thoroughly, but the beauty of Java3D is that it will let you import files from pretty much any 3d modelling program. It does this by using Loaders, which implement a standard interface so you can use them in pretty much the same way no matter which object format you are coming from. There is a complete list of available loaders to be found on the broadly useful j3d.org site, here: (http://www.j3d.org/utilities/loaders.html )

I won’t go into the details of the creation of the Little Purple Dude because there are plenty of tutorials out there if you are using MilkShape and if you’re not then it won’t be relevant anyways. However I will include the three models that I use in this tutorial in the supporting files zip, they will be in the “models”directory.

So, I’ll assume that you have a model to work with and that you are ready to load it up –in this example I use the Milkshape loader available here: http://www.duling.us/kevin/Java3D/Milkshape/

The main code for the ShowDude applet is exactly the same as the ShowLand example we have just discussed so I will only go through the differences, which all occur in the “createSceneGraph()”method:


private BranchGroup createSceneGraph()
{
BranchGroup objRoot = new BranchGroup();

String filePath="file://localhost/"+System.getProperty("user.dir")+"/";

MS3DLoader loader = new MS3DLoader();
Scene ourScene;
try
{
java.net.URL loadLocate = new java.net.URL(filePath+"models/msFigureStanding.ms3d");
try {
ourScene = loader.load(loadLocate);
BranchGroup b = ourScene.getSceneGroup();
Shape3D newShape = Shape3D) b.getChild(0);
b.removeChild(0);
Appearance app = new Appearance();
Color3f objColor = new Color3f(0.7f, 0.2f, 0.8f);
Color3f black = new Color3f(0.0f, 0.0f, 0.0f);
app.setMaterial(new Material(objColor, black, objColor, black, 80.0f));

newShape.setAppearance(app);

objRoot.addChild(newShape);

}
catch (Exception e)
{
System.err.println("could not load file! "+filePath);
e.printStackTrace();
}
}
catch (Exception e)
{
System.err.println("ShowDude.createSceneGraph error: "+filePath+" not a URL");
}

This is, by and large, surprisingly self-explanatory. The loader creates a Scene (from the com.sun.j3d.loaders package) which is a class that exists purely to interface between the loader and the Scenegraph, allowing you to access elements of the recently loaded scene in terms that Java3D can understand. All we do here is create a SceneGraph from it and remove the only child of that SceneGraph, which is our 3d object. We have to remove it after we have made it into a local Shape3D because a node can only ever have one parent- the same Shape3D cannot belong to two separate BranchGroups.

Having loaded the shape from the scene we simply create an appearance to make it purple (you could change this to be whatever appearance you want, but it would hardly be a little purple dude if you did) and then we add it to the branchGroup. The rest of the method, adding lights and so on, is literally identical to the previous example so I won’t repeat it here, I’m sure you’ve got the idea by now. Remember that you need to change “main”to create a ShowDude, as well. That one always gets me, no matter how often I try it, and then I’m baffled by why the window shows something different to what I thought it should.

When you run this remember that you will need to have the loader jar in your classpath - I run it from the command line with an instruction like java –cp $CLASSPATH;MS3Dloader-1.0.8.jar;.\; ShowDude (or java -classpath .:MSDLoader-1.0.8.jar BasicWalkDude for users of proper operating systems) and that seems to work fine . In the end we have something that looks a lot like this:

Previous: That Other Landscape | Back to the Index | Next: The Mobile Manikin