The Java 3D API Specification |
C H A P T E R1 |
Introduction to Java 3D |
1.1
Java 3D was designed with several goals in mind. Chief among them is high performance. Several design decisions were made so that Java 3D implementations can deliver the highest level of performance to application users. In particular, when trade-offs were made, the alternative that benefited runtime execution was chosen.Goals Other important Java 3D goals are to
- Provide a rich set of features for creating interesting 3D worlds, tempered by the need to avoid nonessential or obscure features. Features that could be layered on top of Java 3D were not included.
- Provide a high-level object-oriented programming paradigm that enables developers to deploy sophisticated applications and applets rapidly.
- Provide support for runtime loaders. This allows Java 3D to accommodate a wide variety of file formats, such as vendor-specific CAD formats, interchange formats, and VRML97.
1.2
Java 3D is an object-oriented API. Applications construct individual graphics elements as separate objects and connect them together into a treelike structure called a scene graph. The application manipulates these objects using their predefined accessor, mutator, and node-linking methods.Programming Paradigm
1.2.1
Java 3D's scene graph-based programming model provides a simple and flexible mechanism for representing and rendering scenes. The scene graph contains a complete description of the entire scene, or virtual universe. This includes the geometric data, the attribute information, and the viewing information needed to render the scene from a particular point of view. Chapter 3, "Scene Graph Basics," provides more information on the Java 3D scene graph programming model.The Scene Graph Programming Model
1.2.2
Java 3D includes three different rendering modes: immediate mode, retained mode, and compiled-retained mode (see Chapter 13, "Execution and Rendering Model"). Each successive rendering mode allows Java 3D more freedom in optimizing an application's execution. Most Java 3D applications will want to take advantage of the convenience and performance benefits that the retained and compiled-retained modes provide.Rendering Modes
1.2.2.1
Immediate mode leaves little room for global optimization at the scene graph level. Even so, Java 3D has raised the level of abstraction and accelerates immediate mode rendering on a per-object basis. An application must provide a Java 3D draw method with a complete set of points, lines, or triangles, which are then rendered by the high-speed Java 3D renderer. Of course, the application can build these lists of points, lines, or triangles in any manner it chooses.Immediate Mode
1.2.2.2
Retained mode requires an application to construct a scene graph and specify which elements of that scene graph may change during rendering. The scene graph describes the objects in the virtual universe, the arrangement of those objects, and how the application animates those objects.Retained Mode
1.2.2.3
Compiled-retained mode, like retained mode, requires the application to construct a scene graph and specify which elements of the scene graph may change during rendering. Additionally, the application can compile some or all of the subgraphs that make up a complete scene graph. Java 3D compiles these graphs into an internal format. The compiled representation of the scene graph may bear little resemblance to the original tree structure provided by the application, however, it is functionally equivalent. Compiled-retained mode provides the highest performance.Compiled-Retained Mode
1.2.3
Most Java 3D classes expose only accessor and mutator methods. Those methods operate only on that object's internal state, making it meaningless for an application to override them. Therefore, Java 3D does not provide the capability to override the behavior of Java 3D attributes. To make Java 3D work correctly, applications must call "Extensibility super.setXxxxx
" for any attribute state set method that is overridden.Java 3D does provide hooks for mixing Java 3D-controlled scene graph rendering and user-controlled rendering using Java 3D's immediate mode constructs (see Section 14.1.2, "Mixed-Mode Rendering"). Alternatively, the application can stop Java 3D's renderer and do all its drawing in immediate mode (see Section 14.1.1, "Pure Immediate-Mode Rendering").
Behaviors require applications to extend the Behavior object and to override its methods with user-written Java code. These extended objects should contain references to those scene graph objects that they will manipulate at run time. Chapter 10, "Behaviors and Interpolators," describes Java 3D's behavior model.
1.3
Java 3D's programming model allows the Java 3D API to do the mundane tasks, such as scene graph traversal, managing attribute state changes, and so forth, thereby simplifying the application's job. Java 3D does this without sacrificing performance. At first glance, it might appear that this approach would create more work for the API; however, it actually has the opposite effect. Java 3D's higher level of abstraction changes not only the amount but, more important, also the kind of work the API must perform. Java 3D does not need to impose the same type of constraints as do APIs with a lower level of abstraction, thus allowing Java 3D to introduce optimizations not possible with these lower-level APIs.High Performance
1.3.1
Besides optimizations at the scene graph level, one of the more important factors that determines the performance of Java 3D is the time it takes to render the visible geometry. Java 3D implementations are layered to take advantage of the native, low-level API that is available on a given system. In particular, Java 3D implementations that use Direct3D and OpenGL are available. This means that Java 3D rendering will be accelerated across the same wide range of systems that are supported by these lower-level APIs.Layered Implementation
1.3.2
Java 3D is aimed at a wide range of 3D-capable hardware and software platforms, from low-cost PC game cards and software renderers at the low end, through midrange workstations, all the way up to very high-performance specialized 3D image generators.Target Hardware Platforms
1.4
Java 3D neither anticipates nor directly supports every possible 3D need. Instead it provides support for adding those features through Java code.Support for Building Applications and Applets
1.4.1
Today's Internet browsers support 3D content by passing such data to plug-in 3D viewers that render into their own window. It is anticipated that, over time, the display of 3D content will become integrated into the main browser display. In fact, some of today's 3D browsers display 2D content as 2D objects within a 3D world.Browsers
1.4.2
Developers of 3D game software have typically attempted to wring out every last ounce of performance from the hardware. Historically they have been quite willing to use hardware-specific, nonportable optimizations to get the best performance possible. As such, in the past, game developers have tended to program below the level of easy-to-use software such as Java 3D. However, the trend in 3D games today is to leverage general-purpose 3D hardware accelerators and to use fewer "tricks" in rendering.Games
1.5
Java 3D defines several basic classes that are used to construct and manipulate a scene graph and to control viewing and rendering. Figure 1-1 shows the overall object hierarchy used by Java 3D. Subsequent chapters provide more detail for specific portions of the hierarchy.Overview of Java 3D Object Hierarchy
1.6
This section illustrates how a developer might structure a Java 3D application. The simple application in this example creates a scene graph that draws an object in the middle of a window and rotates the object about its center point.Structuring the Java 3D Program
1.6.1
The scene graph for the sample application is shown in Figure 1-2.Java 3D Application Scene Graph The scene graph consists of superstructure components-a VirtualUniverse object and a Locale object-and a set of branch graphs. Each branch graph is a subgraph that is rooted by a BranchGroup node that is attached to the superstructure. For more information, see Chapter 3, "Scene Graph Basics."
Figure 1-1 Java 3D Object Hierarchy
Figure 1-2 Application Scene Graph
A VirtualUniverse object defines a named universe. Java 3D permits the creation of more than one universe, though the vast majority of applications will use just one. The VirtualUniverse object provides a grounding for scene graphs. All Java 3D scene graphs must connect to a VirtualUniverse object to be displayed. For more information, see Chapter 4, "Scene Graph Superstructure."
The scene graph itself starts with the BranchGroup nodes (see Section 5.2, "BranchGroup Node"). A BranchGroup serves as the root of a subgraph, called a branch graph, of the scene graph. Only BranchGroup objects can attach to Locale objects.
1.6.2
The following steps are taken by the example program to create the scene graph elements and link them together. Java 3D will then render the scene graph and display the graphics in a window on the screen:Recipe for a Java 3D Program 1. Create a Canvas3D object and add it to the Applet panel.
The Java 3D renderer then starts running in an infinite loop. The renderer conceptually performs the following operations:2. Create a BranchGroup as the root of the scene branch graph.
3. Construct a Shape3D node with a TransformGroup node above it.
4. Attach a RotationInterpolator behavior to the TransformGroup.
5. Call the simple universe utility function to do the following:
a. Establish a virtual universe with a single high-resolution Locale (see Chapter 3, "Scene Graph Basics").
6. Insert the scene branch graph into the simple universe's Locale.b. Create the PhysicalBody, PhysicalEnvironment, View, and ViewPlat-form objects.
c. Create a BranchGroup as the root of the view platform branch graph.
d. Insert the view platform branch graph into the Locale.
while(true) { Process input If (request to exit) break Perform Behaviors Traverse the scene graph and render visible objects } Cleanup and exit1.6.3
Following are code fragments from a simple program,HelloUniverse: A Sample Java 3D Program HelloUniverse.java
, that creates a cube and a RotationInterpolator behavior object that rotates the cube at a constant rate of /2 radians per second.
1.6.3.1
The HelloUniverse class, on the next page, creates the branch graph that includes the cube and the RotationInterpolator behavior. It then adds this branch graph to the Locale object generated by the SimpleUniverse utility.HelloUniverse Class
public class HelloUniverse extends Applet { public BranchGroup createSceneGraph() { // Create the root of the branch graphBranchGroup objRoot = new BranchGroup(); // Create the TransformGroup node and initialize it to the // identity. Enable the TRANSFORM_WRITE capability so that // our behavior code can modify it at run time. Add it to // the root of the subgraph.TransformGroup objTrans = new TransformGroup(); objTrans.setCapability( TransformGroup.ALLOW_TRANSFORM_WRITE); objRoot.addChild(objTrans); // Create a simple Shape3D node; add it to the scene graph.objTrans.addChild(new ColorCube(0.4)); // Create a new Behavior object that will perform the // desired operation on the specified transform and add // it into the scene graph.Transform3D yAxis = new Transform3D(); Alpha rotationAlpha = new Alpha(-1, 4000); RotationInterpolator rotator = new RotationInterpolator( rotationAlpha, objTrans, yAxis, 0.0f, (float) Math.PI*2.0f); BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0); rotator.setSchedulingBounds(bounds); objRoot.addChild(rotator); // Have Java 3D perform optimizations on this scene graph. objRoot.compile(); return objRoot; } public HelloUniverse() { <set layout of applet, construct canvas3d, add canvas3d> // Create the scene; attach it to the virtual universeBranchGroup scene = createSceneGraph(); SimpleUniverse u = new SimpleUniverse(canvas3d); u.getViewingPlatform().setNominalViewingTransform(); u.addBranchGraph(scene); } }
The Java 3D API Specification |