Irrlicht with Java
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.