Displaying a Model


Loaders in Java 3D

Before WrapLoaderInfo3D can traverse or change the external model's scene graph, the model has to be loaded. Java 3D supports external model loading through its Loader interface and the Scene class.Loader takes as input the model's filename and flags for enabling and disabling the loading of certain elements of the model, such as light nodes, sound nodes, and view graphs.

Java 3D's utilities package includes two subclasses of Loader aimed at particular file formats: Lw3dLoader handles Lightwave 3D scene files, and ObjectFile processes Wavefront OBJ files. A third subclass, LoaderBase, implements the Loader interface in a generic way to encourage the building of loaders for other 3D formats through subclassing.

The Scene class uses a Loader object to extract details about a model, the most significant being its BranchGroup, usually for the purpose of adding it to the application scene. Information about other aspects of the model may be available, including the model's light nodes, object names, viewpoints, and behavior nodes. However, not all loaders supply this information, i.e., the relevant methods may return nothing.

There's a wide range of Java 3D loaders for different file formats, written by third-party developers. A good list is maintained at http://www.j3d.org/utilities/loaders.html.

Loaders specifically aimed at gaming are listed in Chapter 14.


In this and other chapters, I employ the loaders in the NCSA Portfolio package (available from http://www.ncsa.uiuc.edu/~srp/Java3D/portfolio/ or at the web site for this book: http://fivedots.coe.psu.ac.th/~ad/jg/portfolio.jar). Using a single ModelLoader interface, the package supports many formats, including 3D Studio Max (3DS files), AutoCAD (DXF), Digital Elevation Maps (DEMs), TrueSpace (COB), and VRML 97 (WRL). The drawbacks of Portfolio are its advanced age (the current version is 1.3, from 1998) and its relatively simple support of the formats: often only the geometry and shape colors are loaded, without textures, behaviors, or lights. Portfolio offers more than just loaders, though, since it has interfaces for several kinds of input devices and makes it easy to take snapshots of the 3D canvas and convert them into video clips.

Alternate Loaders

Inspector3ds is an up-to-date 3DS loader, developed by John Wright at Starfire Research (http://www.starfireresearch.com). The loader handles geometry, materials, textures, and normals.

The popular modeling package ac3d (http://www.ac3d.org) has a loader written by Jeremy Booth, available at http://www.newdawnsoftware.com/.

Programmers wishing to utilize a modern VRML loader should consider the Xj3D loader (http://www.web3d.org), which is actively being developed and covers most of the VRML 2.0 standard. The actual aim is to load X3D files, which extend VRML with XML functionality.

For the artistically impaired (e.g., yours truly), many web sites offer 3D models. A good starting point is the Google directory on 3D models http://directory.google.com/Top/Computers/Software/Graphics/3D/Models/. One site with many free models is 3D Cafe (http://www.3dcafe.com/asp/freestuff.asp).


Using NCSA Portfolio Loaders

The ModelLoader interface is used by WrapLoaderInfo3D in loadModel( ). First, a Loader object is obtained, which loads a Scene object. If this is successful, then a call to getSceneGroup( ) extracts the model's BranchGroup (into loadedBG).

     import ncsa.j3d.loaders.*;     // Portfolio loaders     import com.sun.j3d.loaders.Scene;     private Scene loadedScene = null;   // globals     private BranchGroup loadedBG = null;     public void loadModel(String fn)     {       FileWriter ofw = null;       System.out.println( "Loading: " + fn );       try {         ModelLoader loader = new ModelLoader( );         loadedScene = loader.load(fn);   // the model's scene         if(loadedScene != null ) {           loadedBG = loadedScene.getSceneGroup( );  // model's BG           // code to manipulate the model         }       }       catch( IOException ioe )       { System.err.println("Could not find object file: " + fn); }     } // end of loadModel( )

The compilation of the LoaderInfo3D classes requires portfolio.jar which contains the Portfolio packages:

     javac -classpath "%CLASSPATH%;ncsa\portfolio.jar" *.java



Killer Game Programming in Java
Killer Game Programming in Java
ISBN: 0596007302
EAN: 2147483647
Year: 2006
Pages: 340

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net