With the new JNLP Applet functionality in Java, SWT and Ogre4J, it is possible to embed Ogre in a webpage.
DOWNLOAD THE SOURCE CODE
VIEW THE DEMO
Since the advent of Flash, web based games have always been popular. Easy distribution and installation make them instantly accessible, and ideal for those looking for a quick break.
Lately 3D games have been making their way onto browsers too. Shiva, Unity and Torque all offer plugins that put high end 3D inside the browser. These solutions are fully features, but come at a cost. There are a number of Flash 3D engines, most of which are free, but their performance is limited.
Ogre is a popular C++ cross platform 3D engine, with bindings to a number of other languages, including Java via the Ogre4J project. With the introduction of the Next Generation Java Plugin it is quite easy to distribute Java applets that reference native libraries, thanks to the new JNLP support. And with SWT, and specifically the SWT_AWT bridge class, it is possible to host a native window in an applet, and then render to that window with Ogre. The end result is effectively Ogre embedded in a webpage.
Download and extract the SWT Java library from here.
Download and extract the Ogre4J library from here.
Download and install the Ogre SDK from here.
You need to make sure that you are downloading the Ogre SDK that matches the Ogre4J release. At the time of writing the latest version of Ogre4J supported Ogre 1.6.2, whereas the latest release of Ogre was 1.6.4.
You also need to make sure that you download the version of Ogre compiled against the same Visual C++ runtime library that Ogre4J was compiled against. Ogre4J 1.6.2 requires OgreSDKSetup1.6.2_VC80.exe.
The EngineManager class will create the Java applet, the SWT GUI elements, and initialise the Ogre engine. This file can be found in the source code download.
public void init()
{
Thread thread = new Thread(new Runnable()
{
public void run()
{
try
{
loadSystemLibraries();
setupGUI();
setupOgre();
loadResources();
setupScene();
enterRenderLoop();
}
catch (Exception ex)
{
}
}
});
thread.start();
}
The init function is called to initialise the Java applet. Here we create a new thread, and run call the various functions that will initialise the applet. We run Ogre in a separate thread so the applet is responsive even while we are in the Ogre render loop.
public void stop()
{
if (display != null && !display.isDisposed())
{
display.syncExec(new Runnable()
{
public void run()
{
if (swtParent != null && !swtParent.isDisposed())
swtParent.dispose();
swtParent = null;
display.dispose();
display = null;
}
});
remove(awtParent);
awtParent = null;
}
}
The stop function is called when the Applet is shutdown. We use the syncExec function to dispose the SWT windowing system. To interact with the SWT system from a thread separate to the one that created the it (remember that the GUI is setup in a new thread in the init function) you have to use either syncExec or asyncExec.