## Featured White Papers

##### Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

##### Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as \$1250 per TB. Ideal if you currently rely on servers/disks/JBODs

# 3D graphics programming in Java: Part 2, Advanced Java 3D

### Learn more about Java 3D performance, 3D content loaders, and the Java 3D VRML97 browser

• Print
• Feedback
Java 3D has the potential to allow Java programmers of all sorts to include vibrant 3D content in their applets and applications. This is fact, but not certainty, as Sun faces heavy competition from other technologies, especially OpenGL. Beyond the basics that every 3D API must provide, what does Java 3D have going for it?

Last month we began our exploration of Java 3D with static content and small scenes. This month we'll conclude our introduction by delving into loaders for larger scene graphs and giving our worlds behaviors and interactivity. We'll also discuss the interplay between Java 3D and VRML, review the performance characteristics of Java 3D, and set the stage for next month's look at Java OpenGL bindings.

### Java 3D transforms and placement

I received several reader questions about the placement of the 3D objects in last month's examples. One reader in particular asked why, when he modified the transform so the cube's z coordinate was positive, he could no longer see the cube. Let's reexamine the example code from last month's Example 3 to learn more about how things are situated in three dimensional space using Java 3D.

The critical line of code from this example is:

```
myTransform3D.setTranslation(new Vector3f(-0.5f,-0.5f,-2.3f));
```

The arguments in the `Vector3D` constructor are x, y, and z coordinates. The cube is placed by this transform at x = -0.5; y = -0.5; and z = -2.3 (in floating point values, hence the "f" suffixes).

It's critical to note that your viewing location can change. This is determined by the location of the associated `ViewPlatform`, which, in turn, is situated in the 3D world using the `TransformGroup` above it (actually, using a `Transform3D` node to transform the `TransformGroup` above it).

Also note that in Example 3, we don't move the `ViewPlatform`; we construct all of the view branch using the default constructors, which use an identity `Transform3D` node (more on identity later) in the `TransformGroup` above `ViewPlatform`. Thus, the `ViewPlatform` is located at x = 0.0; y = 0.0; and z = 0.0, the origin of the 3D coordinate system.

And finally, it's important to note that the coordinate system is aligned with your view into it. With the default view branch, the +x axis runs from left to right across the screen and the +y axis runs from bottom to top, which means, using the right-hand rule, that the +z axis runs out of the screen towards you. By default, your view into the world is looking in the -z direction.

Default Java 3D coordinate axes, as seen
from directly in front of the monitor. We are
looking in the -z axis direction, directly into
the monitor. The origin is located in the center
of the x-y plane.

Default Java 3D coordinate axes, as seen from
directly above the front of the monitor. We are
looking in the -y axis direction along the x-y plane.
The +z axis is sticking straight out of the monitor.

Because you're "sitting" at the origin (the view platform is located at 0.0, 0.0, 0.0) looking in the negative-z direction, anything in the positive-z section of the 3D world is behind you and you can't see it. Don't you just love 3D! (See Resources for more information on `Transform3D`.)

### Exploring the com.sun.j3d class archives

Now, let's return to the issue of the contents of Sun's private jar files. Last month I briefly mentioned that Sun's Java 3D implementation installs both standard Java 3D classes (as specified in the Java 3D API specification) and nonstandard (Sun implementation-specific) classes. These classes are made available in one of several jar archives placed within the new Java 2 platform (formerly known as Java 1.2) extensions directory (see last month's column for more information on this). We discussed the standard classes and jars in sufficient detail last month, but we still have some ground to cover on the Sun-specific ones.

As a reminder, Sun's archives include:

• `j3daudio.jar`, which archives the `com.sun.j3d.audio` packages, described last month
• `j3dutils.jar`, which encapsulates a variety of Sun utility classes in 16 total packages and subpackages underneath `com.sun.j3d` (more details in a moment)
• `j3dutilscontrib.jar`, which archives useful utilities contributed by others to Sun's efforts (again, more details in a moment)

When you explode the `j3dutils.jar` archive, you see the following subpackages of `com.sun.j3d`:

• `audioengines`
• `audioengines.javasound`
• `loaders`
• `loaders.lw3d`
• `loaders.objectfile`
• `utils`
• `utils.applet`
• `utils.audio`
• `utils.behaviors`
• `utils.behaviors.interpolators`
• `utils.behaviors.mouse`
• `utils.behaviors.picking`
• `utils.geometry`
• `utils.image`
• `utils.internal`
• `utils.universe`

Likewise, exploding `j3dutilscontrib.jar` reveals these additional `com.sun.j3d` subpackages:

• `loaders`
• `loaders.vrmlfile`
• `utils`
• `utils.behaviors`
• `utils.behaviors.keyboard`
• `utils.font3d`
• `utils.trackers`

Now that you know the basic layout of Sun's utility class archives, you can delve into them more deeply if you need any of the functionality they provide. For instance, if you're looking to capture mouse events to control interpolated behaviors, you need to load in LightWave 3D content. And if you're interested in 3D font support, you should review `com.sun.j3d.utils.behaviors.mouse`, `com.sun.j3d.utils.behaviors.interpolators`, `com.sun.j3d.loaders.lw3d`, and `com.sun.j3d.utils.font3d` more closely.

Please note that while some documentation is provided for these packages in the general Java 3D documentation from Sun, the quality of documentation varies from package to package and from class to class. I recommend that you monitor the java3d-interest mailing list for information on the particular utility classes you need to use.

### Behaviors and interpolators

Interactivity results from being able to program an object to react to certain stimuli. In Java 3D, these behaviors are scheduled when the view platform crosses the stimulus bounds, a region of space defined by the programmer. Bounds assign a spatial scope to objects. There are bounds for both sounds and behaviors in Java 3D.

The advantage of requiring bounds for objects is that the runtime can safely disregard (not render) things that are out of bounds. If you build behaviors into your Java 3D applications, you will be required to set bounds in order to have the Java 3D runtime scheduler prepare your behaviors for execution. The scene graph includes a set of nodes for setting up and scheduling behaviors and bounds. In addition, several of the utilities described above help you to more easily deal with interactivity, including the aptly named "behaviors" subpackages.

• Print
• Feedback

### More from CITEworld

Resources
• Sun has made its "Java 3D 1.1 Performance Guide" available on its product site. This guide provides some short tips and tricks developers can heed to speed up their Java 3D applications. http://www.sun.com/desktop/java3d/collateral/j3d_perfguide.html
• The home page for the Java 3D and VRML working group provides links to download the Java 3D VRML browser. http://www.vrml.org/WorkingGroups/vrml-java3d/
• You can download the Java 3D VRML97 browser from the Java Developer Connection (free registration required). http://developer.java.sun.com/developer/earlyAccess/java3D/index.html
• VRML97 is an international standard, officially designated ISO/IEC 14772-1:1997. The complete standard specification is available from the VRML Consortium Web site. http://www.vrml.org/Specifications/VRML97/
• Get information on Transform3D with the API spec and documentation. API specification
javadocs
• If you need more information on how transforms are actually applied using matrix mathematics, I recommend the classic computer graphics primer "Introduction to Computer Graphics" (Addison-Wesley, ISBN0201609215). http://www1.clbooks.com/asp/bookinfo/bookinfo.asp?theisbn=0201609215
• The Java 3D Archive links to loaders and shapes for use with Java 3D. You can use Java 3D loader software to read in models from common 3D file formats and translate them into a scene graph that the Java 3D runtime can render. The archive was assembled through the efforts of Matt Robinson, who has since passed its maintenance on to me. http://reality.sgi.com/bday/Java3DArchives/index.html
• NCSA's Portfolio provides a consistent interface for loading many popular 3D modeling formats into the Java 3D runtime. It also supports domain-specific loaders for formats such as Protein Data Bank (PDB). Portfolio provides an implementation of Canvas3D, which is able to save snapshots into JPEG files. These files can then be used to make MPEG movies of Java 3D worlds. http://havefun.ncsa.uiuc.edu/Java3D/portfolio/
• For more on the Java 3D API performance-oriented features, see "Introduction to Programming with Java 3D." http://www.sdsc.edu/~nadeau/Courses/SDSCjava3d/
• JavaWorld has published several good VRML-related articles. In addition to the two below, be sure to visit the JavaWorld Topical Index for more VRML-related information.
• "3D computer graphicsGetting the hang of VRML"
• "Why Java and VRML?"
• http://www.javaworld.com/topicalindex/jw-ti-vrml.html
• The Java 3D mailing list archive stores lots of useful tips and tricks. Please search here before posting new questions to the list. http://java.sun.com/products/java-media/mail-archive/3D/index.html
• Allen McPherson has made some example code available to help in the visual debugging of normal problems. http://java.sun.com/products/java-media/mail-archive/3D/1860.html
• Learn how to get the frame rate from the Java 3D rendering engine. http://java.sun.com/products/java-media/mail-archive/3D/0093.html
• Tips on coaxing Java 3D applets to run inside your Web browser. Includes step-by-step instructions for installing and configuring the Java 2 platform (formerly Java 1.2) and Java 3D to work with Netscape's browser. http://java.sun.com/products/java-media/mail-archive/3D/1212.html
• Need to know the bounding volumes used by the Java 3D VRML97 browser? Try this sample code. http://java.sun.com/products/java-media/mail-archive/3D/1063.html
• Read about an interesting real-world use of Java 3D and VRML, Nearlife's Virtual Fishtank. The article contains a special sidebar on Java 3D and the fishtank. http://java.sun.com/features/1998/11/fishtank.html
• Download source code and classes for this column. http://www.javaworld.com/jw-01-1999/media/jw-01-media.jar
• I've archived Media Programming resources on my Web site. This archive contains the up-to-date media.jar file with code fixes for all the examples in the column. http://reality.sgi.com/bday/Work/index.html
• Read all my previous Media Programming columns. http://www.javaworld.com/topicalindex/jw-ti-media.html

RESEARCH CENTERS: Java Standard Edition | Java Enterprise Edition | Java Micro Edition | Development Tools
Copyright, 2006-2008 Network World, Inc. All rights reserved.