Using Xj3D as a Loader

This was a post originally sent by Noah Ternullo to the Java3D interest list. There are a lot of people that use the Loader interface for bringing VRML content into their application. This is an edited version of his original post with some more info added.


There are a couple of ways to get VRML content into your Java3D application. The simplest way, I feel, is to find a loader that conforms to Sun's com.sun.j3d.loaders.Loader interface. I know of 3 such loaders and this description focuses on the XJ3D loader.

1) Obtain the appropriate jar files.

You can find all of the files you need on the Xj3D site. http://www.web3d.org/x3d/applications/xj3d/

Since I already have JDK1.4 and Java3D 1.3 I just downloaded the smaller version. I got the tar ball even though I use a windows system because all I wanted were the applicable jar files for VRML loading, not the entire application. You can get them through the .exe file as well, but there was an installation process involved and I didn't want to install any applications, just get the tar files. The link I used to the tar ball is here:

http://www.web3d.org/TaskGroups/source/Xj3D-unix-M5.tar.gz

I spent a long time determining which of the distributions jar files that came with I needed, and I provided that list below. However, if you want to be absolutely sure that you get all the functionality you need to load the specific VRML file you're interested in, put *all* of the jar files in your classpath. My application already included all jar files for XML parsing, so I did not need to include them, and haven't listed them below.

[If you know what jar files are specifically required just for loading a VRML97 file please respond to this post, and update this information].

For compilation:

For Running: Note: If you do not already have the appropriate jar files for XML parsing in your project, you will need to include the XJ3D ones. If you are using JDK 1.4, you will already have the required APIs.

2) Writing the Code

Once you've found all of the appropriate jars, and included them in your classpath, its a simple thing to attempt to use the loader. Here is the code I used:

import all of the appropriate packages for Loader, Scene, BranchGroup etc I didn't bother importing the Xj3D VRML loader but called it specifically.

Loader myFileLoader = null;             //holds the file loader
Scene  myVRMLScene  = null;             //holds the loaded scene
BranchGroup myVRMLModel = null;         //BG of the VRML scene

try {
        //create an instance of the Loader
        myFileLoader = new org.web3d.j3d.loaders.VRML97Loader();
        //Load the scene from your VRML97 file
        myVRMLScene  = myFileLoader.load("YOURFILENAMEHERE.wrl");
        //Obtain the root BranchGroup for the Scene
        myVRMLModel = myVRMLScene.getSceneGroup();
} catch (Exception e) {
        //in case there was a problem, print the stack out
        e.printTraceStack();
        //we still need a model, even if we can't load the right
        //one, I use a color cube just in case
        myVRMLModel = new ColorCube();
}
This code is actually pretty standard for loading anything from a Loader. Only the names of the loaders tend to change. The Scene interface provides a lot of functionality to get at different types of nodes within the VRML content that you loaded (such as lights, etc). I recommend examining the Java3D API documentation for more info.

3) The Actual VRML File.

XJ3D Handles VRML97 files. It doesn't read VRML1.0 files. If you need to read a simple VRML1.0 file I recommend the application vrml1tovrml2 which you can download for free here:

http://synapses.bu.edu/tools/vrml2/vrml1to2.zip I actually had a bit of difficulty getting some files I thought were VRML97 compliant loaded via the XJ3D loader. The best experience I actually had was converting a simple VRML1.0 file to VRML2.0 file using the above filter and loading that. I urge you to go to the XJ3D project home page and look at the formats and specific tags that XJ3D supports (here).


[ Xj3D Homepage | Xj3D @ Web3d | Screenshots | Dev Releases | Conformance | Contributors | Getting Started ]
Last updated: $Date: 2004-04-30 04:50:25 $