To create 3D graphics, we have to build graphics or visual objects and position them appropriately in a virtual scene. In general, there are three possible approaches for doing this (Java 3D geometry, 2006). One approach is to make use of geometry utility classes to create basic geometric shapes or primitives. The basic shapes are boxes, spheres, cones, and cylinders. Another approach is to employ commercial modeling tools, such as 3D studio max, and have the results loaded into Java 3D. Lastly, custom geometrical shapes or objects can also be created by defining their vertices. While using utility classes or commercial modeling tools may be simpler and less time consuming, creating objects based on specifying vertices corresponds to the most general method. From a certain angle, the latter can in fact be regarded as the foundation from which the other approaches are based. The main thrust in this chapter will thus be on how objects can be built from their vertices, with some brief discussion on using utility classes presented toward the end of the chapter.
As specified in the constructor of Figure 1, the most basic method for creating and specifying the geometry and appearance of an object is through the Shape3D class. The geometry component of this class hold the geometric data for the 3D object to be created, while the appearance component defines physical properties like color and line styles.
Specifying geometry and appearance of Shape3D objects
After its definition, a Shape3D class can be instantiated to create one or several Shape3D objects. These objects are usually added as child nodes to a BranchGroup, TransformGroup or any appropriate group node. The node components of the Shape3D object include objects under the Geometry and Appearance classes. The latter is optional, but will most likely be present.
Figure 2 shows the structure of a created Shape3D object and its components under a TransformGroup. The corresponding code segment is shown in Figure 3. Specifically, in line 17, objRoot is an instance of the branch group. Line 18 creates an object objTrans and add it as a child of objRoot in the scene graph. In line 20, the constructor for the createShape3D subclass is invoked and an instance of the Shape3D class is created and added to the TransformGroup. Using the setGeometry method, line 32 specifies the geometry for the created Shape3D object to that from a private createGeometry method. This private method specifies the geometrical shape of the visual object to be rendered. How this specification can be written will be discussed in subsequent sections.
A Shape3D object and its components
Figure 3. Top
Code segment structure for setting up a scene with a single Shape3D node
As discussed earlier in the first section, the shape of the visual object to be rendered is specified by a geometry object, which is a component under a Shape3D object. The creation of a geometry object can be done using the geometry class, which is an abstract superclass.
Under the geometry class (Java 3D API specification, 2006), a useful abstract subclass is the GeometryArray class and the class hierarchy is shown in Figure 4. Figure 5 illustrates the geometry subclasses.
GeometryArray class hierarchy