Chapter 7. Introducing Java 3D
Total Page:16
File Type:pdf, Size:1020Kb
Java Prog. Techniques for Games. Chapter 7. Java 3D Intro. Draft #1 (22nd July '04) Chapter 7. Introducing Java 3D The next fifteen or so chapters will be about 3D games programming using Java 3D, Java's scene graph API which can run on top of OpenGL or DirectX. I'll summarise the main elements of Java 3D, leaving program examples aside for the moment. Then, as in chapter 00, I'll examine Java 3D's suitability for games programming by considering the main criticisms leveled against it. There's been an explosion in alternative ways of programming in 3D with Java. I'll list the main ones under the headings of Java bindings for OpenGL, scene graph APIs, and game engine bindings. 1. Java 3D The Java 3D API provides a collection of high-level constructs for creating, rendering, and manipulating a 3D scene graph composed of geometry, materials lights, sounds, and more. Java 3D was developed by Sun Microsystems, and the most recent stable release is version 1.3.1. (There is a version 1.3.2, but it's a bug fix release which is still under review as I write this in July 2004.) In June 2004, Java 3D became a Sun community project, which allows external developers to submit modifications and new features. There are two Java 3D variants: one implemented on top of OpenGL, the other above DirectX Graphics. The low-level API handles the native rendering at the vertex and pixel levels, while the 3D scene, application logic, and scene interactions are carried out by Java code. This dual approach encourages application portability, hardware independence, and high-speed rendering. Intended application areas include visualization, CAD/CAM, virtual reality, simulation, and gaming. Java 3D 1.3.1. can be downloaded from http://java.sun.com/products/java-media/3D/, together with ample documentation and a long tutorial. 1.1. The Scene Graph Java 3D’s scene graph is a directed acyclic graph (a DAG), so there's a parent-child relationship between most of its nodes, and the graph doesn't contain loops. It is possible for nodes to be shared in a graph, for example to duplicate geometry details. 1 Andrew Davison 2004 Java Prog. Techniques for Games. Chapter 7. Java 3D Intro. Draft #1 (22nd July '04) A simplified scene graph for an office is shown in Figure 1. The office group contains nodes for a desk and two chairs. Each node utilizes shape and colour node components, and the chair shape information is shared. Figure 1. Scene Graph for an Office. The choice of symbols in Figure 1 comes from Figure 2. Figure 2. Scene Graph Symbols. A shape node employs Shape3D (or its subclasses) as a container for Geometry and Appearance node components. The main geometry class is GeometryArray, used for drawing points, lines, and polygons. There are many Appearance subclasses, including ones for colour, texture, and transparency. Environment nodes handle environmental concerns, such as lighting, sound, and behaviours that change the virtual world. Group nodes are containers for other groups or leaf nodes. Leaf nodes are usually shape or environmental nodes. The Group class supports node positioning and orientation for its children, and is subclassed in various ways. For instance, 2 Andrew Davison 2004 Java Prog. Techniques for Games. Chapter 7. Java 3D Intro. Draft #1 (22nd July '04) BranchGroup allows children to be added or removed from the graph at run time, while TransformGroup permits the position and orientation of its children to be changed. The standard first example for Java 3D programmers is HelloUniverse (it appears in chapter 1 of the tutorial). It displays a rotating coloured cube, as in Figure 3. Figure 3. A Rotating Coloured Cube. The scene graph for this application is given in Figure 4. Figure 4. Scene Graph for HelloUniverse. VirtualUniverse is the top node in every scene graph, and represents the virtual world space and its coordinate system. Locale acts as the scene graph's location in the virtual world. 3 Andrew Davison 2004 Java Prog. Techniques for Games. Chapter 7. Java 3D Intro. Draft #1 (22nd July '04) Below the Locale node there are always two subgraphs – the left branch is the content branch graph, holding program-specific content such as geometry, lighting, textures, and the background. The content branch graph differs significantly from one application to another. The ColorCube is composed from a Shape3D node and associated geometry and appearance components. Its rotation is carried out by a Behavior node which affects the TransformGroup parent of the ColorCube's shape. The right hand branch below Locale is the view branch graph, and specifies the user’s position, orientation, and perspective as they look into the virtual world from the physical world (e.g. from in front of a monitor). The ViewPlatform node holds the viewer’s position in the virtual world; the View node states how to turn what the viewer sees into a physical world image (e.g. a 2D picture on the monitor). The Canvas3D node is a Java GUI component that allows the 2D image to be placed inside a Java application or applet. The VirtualUniverse, Locale, and view branch graph often have the same structure across different applications, since most programs use a single Locale and view the virtual world through a 2D image on a monitor. For these applications, the relevant nodes can be created with Java 3D's SimpleUniverse utility class, relieving the programmer of a lot of graph construction work. 2. Java 3D Strengths The Scene Graph. Java 3D’s scene graph makes programming much easier for novices (and even for experienced programmers) because it emphasizes scene design rather than rendering, by hiding the underlying graphics pipeline. A scene graph naturally supports complex graphical elements such as 3D geometries, behaviours, lighting modes, picking, and collision detection. At the Java 3D implementation level, the scene graph can be used to group shapes with common properties, carry out view culling, occlusion culling, level of detail selection, execution culling, and behaviour pruning – all optimizations which must be coded directly by the programmer in lower-level APIs. Java 3D utilizes Java’s multithreading to carry out parallel graph traversal and rendering, both very useful optimizations. Performance. Java 3D is designed with performance in mind, which it achieves at the high-level by scene graph optimizations, and at the low-level by being built on top of OpenGL or DirectX. Some programmer-specified scene graph optimizations are available through capability bits, which state what operations can/cannot be carried out at run time (e.g. prohibiting a shape from moving). Java 3D also permits the programmer to bypass the scene graph, either totally by means of an immediate mode, or partially by using a combination of immediate and retained modes (called mixed mode). Immediate mode gives the programmer greater control over rendering and scene management. Novel Features. Java 3D’s view model separates the virtual and physical worlds (through the ViewPlatform and View nodes). This makes it straightforward to 4 Andrew Davison 2004 Java Prog. Techniques for Games. Chapter 7. Java 3D Intro. Draft #1 (22nd July '04) reconfigure an application to utilize a range of output device, from a monitor, to stereo glasses, to CAVES. Virtual world behaviour is coded with Behaviour nodes in the scene graph, and triggered by events. Among other things, this offers a different style of animation based on responding to events, instead of the usual update-redraw cycle. The core Java 3D API package, javax.media.j3d, supports basic polygons and triangles within a scene graph, while the com.sun.j3d packages add a range of utility classes including ColorCube and SimpleUniverse, mouse and keyboard navigation behaviours, audio device handling, and loaders for several 3D file formats. Geometry compression is possible, often reducing size by an order of magnitude. When this is combined with Java’s NIO and networking, it facilitates the ready transfer of large quantities of data between applications such as multiplayer games. Java 3D allows 2D and 3D audio output, ambient and spatialized sound. Unfortunately, there are bugs in the sound system. In the long term, it will probably be replaced by JOAL, a Java binding for a 3D audio API called OpenAL which is supported in hardware on many sound cards. Java Integration. Java 3D is Java, so offers object orientation (classes, inheritance, polymorphism), threads, exception handling, and more. Java 3D can easily make use of other Java APIs, such as JMF and JAI. Portability. Java 3D’s implementation in Java and OpenGL or DirectX gives it a high degree of portability, with related advantages such as reduced development time and cost. Documentation and Examples. The Java 3D distribution comes with about 40 small- to-medium examples. They are a great help, but somewhat lacking in documentation. Some of them need rewriting to use more recent features of Java 3D and the utility classes. The Java 3D tutorial is available online at http://java.sun.com/products/java- media/3D/collateral/. The tutorial is a good introduction to Java 3D but sometimes rather confusing for beginners. I recommend two Java 3D textbooks: • Java 3D API Jump-Start Aaron E. Walsh, Doug Gehringer Prentice Hall, 2001 ISBN: 0-1303-4076-6 http://vig.prenhall.com:8081/catalog/academic/product/0,1144,0130340766,00.ht ml • Java 3D Programming Daniel Selman Manning Pub, 2002 ISBN: 1-9301-1035-9 http://www.manning.com/selman/ 5 Andrew Davison 2004 Java Prog. Techniques for Games. Chapter 7. Java 3D Intro. Draft #1 (22nd July '04) The Walsh and Gehringer text is an excellent overview, using code snippets rather than page after page of listings.