< Zurück | Inhalt | Weiter >

8.2 Primitives











Figure 8.1

Geometric objects derived from Primitive: sphere, box, cylinder, and cone. Note the different resolution options and how the Primitives are triangulated when drawn as wire frames

Primitive is not part of the Java 3D package (javax.media.j3d) but has been defined in the Java 3D utilities package (com.sun.java.j3d.utils). The Primitive class serves as the base class for several simple geometric shapes that can act as building blocks for Java 3D developers. The Java 3D Primitive−derived classes have been illustrated in figure 8.1.

Primitive is derived from Group—that is, it is not a Shape3D object. This is important—a Primitive is not a shape or geometric object, but is rather a collection of Shape3D objects. Primitive provides methods to access the subcomponents of the Group. There is therefore an important distinction between modifying the characteristics of the Primitive (Group settings) and modifying the properties of the child Shape3D objects (Shape3D settings such as Material, Appearance, etc.)

Because of serious class design issues regarding the Primitive base class, I cannot recommend that any of the Primitive−derived classes be used in a Java 3D application of any complexity. The fundamental issue is that the Primitive class defines a “has−a” relationship for its geometry. That is, a single Primitive is defined such that it is a container for its geometric parts. For example, Box has six geometric parts:








. To facilitate this relationship the Primitive class is derived from Group.

The Java 3D scenegraph, however, can only have Leaf−derived geometric objects as leaf nodes, and many operations, such as picking for mouse−based selection, are performed on Shape3D−derived objects. This lack of compatibility between Shape3D objects and Primitive objects is something that Sun will address with future releases of Java 3D.

Additionally, it is not generally possible to derive a class from Primitive because the class is not designed to be extensible. For example Primitive.java contains the following code:

static final int SPHERE = 0x01; static final int CYLINDER = 0x02; static final int CONE = 0x04; static final int BOX = 0x08;

This extremely non−object−oriented approach to differentiating between instances of objects derived from Primitive essentially makes it impossible for application developers to safely create new Primitive−derived classes. In general, classes at the top of a class hierarchy should not have knowledge of derived classes.

Example code for all of the Primitive−derived classes is defined in the Sun ConicWorld Java 3D demo directory. The ConicWorld demo creates a variety of Primitive objects with various lighting, material, and texture attributes.

The following subsections describe the simple geometric Primitives defined in



8.2.1 Box
8.2.2 Cone
8.2.3 Cylinder
8.2.4 Sphere
8.2.5 Primitive flags
8.2.6 Primitives and the geometry cache