Irrlicht is a C++ open source 3D engine that has significantly grown in popularity since its inception. Because it is written in C++ Irrlicht has all the speed benefits that compiling to native code can provide. The Jirr project has used JNI to allow Java to interface with Irrlicht, giving Java developers the same access to the Irrlicht engine that their C++ brethren enjoy. I will take advantage of this to create a 3D Java application that will be launched by WebStart.
As an introduction to Jirr and Irrlicht I will create a simple application that will display an animated 3D model. I will use the pattern established in the Flash and Silverlight development tutorials by creating a class called EngineManager, which will contain the code necessary to manage the underlying 3D engine, and ApplicationManager, which will control the logic of the application itself.
EngineManager.java Source Code
As you can see the EngineManager class is quite straight forward, maybe with the exception of the loadResources function. We initialize the Irrlicht engine in the startupEngineManager function, enter the game loop in the enterGameLoop function, and finally clean everything up in the shutdown function.
The loadResources function is used to overcome a limitation that was recently imposed with Java 1.6. In previous versions the developer could get the filename of the local cached JAR file saved by WebStart. Since JAR files are just ZIP files, and Irrlicht can access the contents foa ZIP file directly, it would have been possible to allow Irrlict to access resources (like meshes, textures etc) directly from the same JAR file that was used by WebStart to launch the application.
Unfortunately this is no longer possible with Java 1.6. Instead the getResourcesAsStream fuction can be used to get access to a file included in the JAR archive. The loadReasources function makes use of this to get access to the media.zip file included in the JAR archive, extract it to a temporary location, and then point Irrlicht to the temporary ZIP file. The file is then deleted in the shutdown function.
ApplicationManager.java Source Code
The startApplicationManager function in ApplicationManager is called to create a GUI label and load, texture and animate a 3D model.
Start.java Source Code
The Start class hosts the main entry point function, and is used to startup and shutdown the EngineManager and ApplicationManager classes. Note that we also make a few calls to System.loadLibrary, which load the native Irrlicht and Jirr DLL's.
In order to package this application in a JAR files that can be launched with WebStart you first need to extract the jirr142.jar file, which is part of the Jirr package. Jar files use the standard zip compression, and a free tool like 7-ZIP can be used to extract them. You then need to call the jar.exe file from you Java SDK directory to create the JAR file that will be distributed with WebStart. The command I use is shown below. It assumes that the manifest file and the media.zip file (which will include all the models, textures and fonts used by Irrlicht) are in the current directory, the compiled Java class files are in a subdirectory called bin, and that the extracted jirr142.jar files are located in C:\Development\Libraries\jirr_1_4_2\1.4.2\lib\jirr142.
"C:\Program Files\Java\jdk1.6.0_07\bin\jar.exe" cfm output.jar manifest.txt media.zip -C bin . -C C:\Development\Libraries\jirr_1_4_2\1.4.2\lib\jirr142 net
The manifest.txt file has one line, Main-Class: Start, which tells Java to look for the main function in the Start class.
(Note: you could publish the jirr142.jar file separately - WebStart does have the ability to download multiple JAR files. However I prefer to keep everything in one JAR file for convenience.)
The DLL files that are supplied with the Jirr package will be placed in their own JAR file called native.jar. The reason for this is that these DLL files need to be extracted in a location where System.loadLobrary can load them. WebStart has this functionality built in, as you will see later on.
The Sun website has detailed information regarding the use of the jar.exe application here.