Docsity
Docsity

Prepare-se para as provas
Prepare-se para as provas

Estude fácil! Tem muito documento disponível na Docsity


Ganhe pontos para baixar
Ganhe pontos para baixar

Ganhe pontos ajudando outros esrudantes ou compre um plano Premium


Guias e Dicas
Guias e Dicas


Tutorial Java3D Capitulo 2 - Criando Geometrias, Notas de estudo de Análise de Sistemas de Engenharia

Java3D Tutorial - Criando Geometrias <br> Permission to use, copy, modify, and distribute this documentation for NON-COMMERCIAL purposes and without fee is hereby granted provided that this copyright notice appears in all copies.

Tipologia: Notas de estudo

Antes de 2010

Compartilhado em 10/04/2010

alessandro-borges-10
alessandro-borges-10 🇧🇷

3

(2)

12 documentos

1 / 50

Toggle sidebar

Esta página não é visível na pré-visualização

Não perca as partes importantes!

bg1
tutorial v1.4 (Java 3D API v1.1.2)
Getting Started with
the Java 3D API
Chapter 2
Creating Geometry
Dennis J Bouvier
K Computing
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32

Pré-visualização parcial do texto

Baixe Tutorial Java3D Capitulo 2 - Criando Geometrias e outras Notas de estudo em PDF para Análise de Sistemas de Engenharia, somente na Docsity!

tutorial v1.4 (Java 3D API v1.1.2)

Getting Started with

the Java 3D

API

Chapter 2

Creating Geometry

Dennis J Bouvier

K Computing

Getting Started with the Java 3D API Creating Geometry 2

The Java 3D Tutorial 2-i

© Sun Microsystems, Inc. 2550 Garcia Avenue, Mountain View, California 94043-1100 U.S.A

All Rights Reserved.

The information contained in this document is subject to change without notice.

SUN MICROSYSTEMS PROVIDES THIS MATERIAL "AS IS" AND MAKES NO WARRANTY OF ANY KIND, EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SUN MICROSYSTEMS SHALL NOT BE LIABLE FOR ERRORS CONTAINED HEREIN OR FOR INCIDENTAL OR CONSEQUENTIAL DAMAGES (INCLUDING LOST PROFITS IN CONNECTION WITH THE FURNISHING, PERFORMANCE OR USE OF THIS MATERIAL, WHETHER BASED ON WARRANTY, CONTRACT, OR OTHER LEGAL THEORY).

THIS DOCUMENT COULD INCLUDE TECHNICAL INACCURACIES OR TYPOGRAPHICAL ERRORS. CHANGES ARE PERIODICALLY MADE TO THE INFORMATION HEREIN; THESE CHANGES WILL BE INCORPORATED IN NEW EDITIONS OF THE PUBLICATION. SUN MICROSYSTEMS, INC. MAY MAKE IMPROVEMENTS AND/OR CHANGES IN THE PRODUCT(S) AND/OR PROGRAM(S) DESCRIBED IN THIS PUBLICATION AT ANY TIME.

Some states do not allow the exclusion of implied warranties or the limitations or exclusion of liability for incidental or consequential damages, so the above limitations and exclusion may not apply to you. This warranty gives you specific legal rights, and you also may have other rights which vary from state to state.

Permission to use, copy, modify, and distribute this documentation for NON-COMMERCIAL purposes and without fee is hereby granted provided that this copyright notice appears in all copies.

This documentation was prepared for Sun Microsystems by K Computing (530 Showers Drive, Suite 7-225, Mountain View, CA 94040, 770-982-7881, www.kcomputing.com). For further information about course development or course delivery, please contact either Sun Microsystems or K Computing.

Java, JavaScript, Java 3D, HotJava, Sun, Sun Microsystems, and the Sun logo are trademarks or registered trademarks of Sun Microsystems, Inc. All other product names mentioned herein are the trademarks of their respective owners.

The Java 3D Tutorial 2-ii

The Java 3D Tutorial 2-iii

  • Getting Started with the Java 3D API Creating Geometry
  • Creating Geometry................................................................................................................................2- Chapter 2:
    • 2.1 Virtual World Coordinate System .............................................................................................2-
    • 2.2 Visual Object Definition Basics ................................................................................................2-
      • 2.2.1 An Instance of Shape3D Defines a Visual Object 2-
      • 2.2.2 Node Components.......................................................................................................................... 2-
      • 2.2.3 Defining Visual Object Classes 2-
    • 2.3 Geometric Utility Classes..........................................................................................................2-
      • 2.3.1 Box 2-
      • 2.3.2 Cone 2-
      • 2.3.3 Cylinder......................................................................................................................................... 2-
      • 2.3.4 Sphere............................................................................................................................................ 2-
      • 2.3.5 More About Geometric Primitives 2-
      • 2.3.6 ColorCube.................................................................................................................................... 2-
      • 2.3.7 Example: Creating a Simple Yo-Yo From Two Cones.................................................................. 2-
      • Advanced Topic: Geometric Primitive 2-
    • 2.4 Mathematical Classes .............................................................................................................2-
      • 2.4.1 Point Classes................................................................................................................................ 2-
      • 2.4.2 Color Classes 2-
      • 2.4.3 Vector Classes.............................................................................................................................. 2-
      • 2.4.4 TexCoord Classes......................................................................................................................... 2-
    • 2.5 Geometry Classes ...................................................................................................................2-
      • 2.5.1 GeometryArray Class 2-
      • 2.5.2 Subclasses of GeometryArray 2-
      • 2.5.3 Subclasses of GeometryStripArray................................................................................................ 2-
      • 2.5.4 Subclasses of IndexedGeometryArray........................................................................................... 2-
      • 2.5.5 Axis.java is an Example of IndexedGeometryArray...................................................................... 2-
    • 2.6 Appearance and Attributes......................................................................................................2-
      • 2.6.1 Appearance NodeComponent 2-
      • 2.6.2 Sharing NodeComponent Objects................................................................................................. 2-
      • 2.6.3 Attribute Classes 2-
      • 2.6.4 Example: Back Face Culling 2-
    • 2.7 Self Test.................................................................................................................................2-
  • Getting Started with the Java 3D API Creating Geometry
  • Figure 2-1 Orientation of Axis in Virtual World ....................................................................................2- List of Figures
  • Figure 2-2 A Shape3D Object Defines a Visual Object in a Scene Graph. ..............................................2-
  • Figure 2-3 Partial Java 3D API Class Hierarchy Showing Subclasses of NodeComponent......................2-
  • Figure 2-4 Class Hierarchy for Utility Geometric Primitives: Box, Cone, Cylinder, and Sphere ..............2-
  • Figure 2-5 Class Hierarchy of ColorCube Utility Geometric Class .......................................................2-
  • Figure 2-6 Scene Graph for ConeYoyoApp..........................................................................................2-
  • Figure 2-7 Multiple Parent Exception While Attempting to Reuse a Cone Object .................................2-
  • Figure 2-8 An Image Rendered by ConeYoyoApp.java ........................................................................2-
  • Figure 2-9 Mathematical Classes Package and Hierarchy.....................................................................2-
  • Figure 2-10 Geometry Class Hierarchy................................................................................................2-
  • Figure 2-11 Axis Class in AxisApp.java Creates this Scene Graph.......................................................2-
  • Figure 2-12 Non-Indexed GeometryArray Subclasses ..........................................................................2-
  • Figure 2-13 GeometryArray Subclasses...............................................................................................2-
  • Figure 2-14 GeometryStripArray Subclasses .......................................................................................2-
  • Figure 2-15 Three Views of the Yo-yo.................................................................................................2-
  • Figure 2-16 Yo-yo with Colored Filled Polygons..................................................................................2-
  • Figure 2-17 Index and Data Arrays for a Cube ....................................................................................2-
  • Figure 2-18 IndexedGeometryArray Subclasses...................................................................................2-
  • Figure 2-19 An Appearance Bundle.....................................................................................................2-
  • Figure 2-20 Appearance Bundle Created by Code Fragment 2-9. .........................................................2-
  • Figure 2-21 Multiple Appearance Objects Sharing a Node Component.................................................2-
  • Figure 2-22 Twisted Strip with Back Face Culling...............................................................................2-
  • Figure 2-23 Twisted Strip without Back Face Culling..........................................................................2-
  • Figure 2-24 Determining the Front Face of Polygons and Strips ...........................................................2-
  • Table 2-1 Attribute Defaults................................................................................................................2- List of Tables
  • Code Fragment 2-1 Skeleton Code for a VisualObject Class ..................................................................2- List of Code Fragments
  • Code Fragment 2-2 Class ConeYoyo From ConeYoyoApp.java Example Program...............................2-
  • Code Fragment 2-3 Example ColorConstants Class .............................................................................2-
  • Code Fragment 2-4 GeometryArray Constructors ................................................................................2-
  • Code Fragment 2-5 Storing Data into a GeometryArray Object............................................................2-
  • Code Fragment 2-6 GeometryArray Objects Referenced by Shape3D Objects ......................................2-
  • Code Fragment 2-7 yoyoGeometry() Method Creates TriangleFanArray Object ...................................2-
  • Code Fragment 2-9 Using Appearance and ColoringAttributes NodeComponent Objects......................2-
  • Code Fragment 2-10 Disable Back Face Culling for the Twisted Strip .................................................2-
  • Getting Started with the Java 3D API Creating Geometry
  • Shape3D Constructors ..........................................................................................................................2- List of Reference Blocks
  • Shape3D Methods (partial list)..............................................................................................................2-
  • Shape3D Capabilities............................................................................................................................2-
  • Box Constructors (partial list) ...............................................................................................................2-
  • Box, Cone, and Cylinder Methods .........................................................................................................2-
  • Cone Constructors (partial list)..............................................................................................................2-
  • Cylinder Constructors (partial list) ........................................................................................................2-
  • Sphere Constructors (partial list) ...........................................................................................................2-
  • Sphere Methods ..................................................................................................................................2-
  • Primitive Methods (partial list)............................................................................................................2-
  • Tuple2f Constructors ..........................................................................................................................2-
  • Tuple2f Methods (partial list)..............................................................................................................2-
  • Point3f Methods (partial list)...............................................................................................................2-
  • Color* Classes ....................................................................................................................................2-
  • Vector3f Methods (partial list) ............................................................................................................2-
  • GeometryArray Constructor ................................................................................................................2-
  • GeometryArray Methods (partial list) ..................................................................................................2-
  • GeometryArray Methods (partial list, continued) .................................................................................2-
  • GeometryArray Subclass Constructors ................................................................................................2-
  • GeometryStripArray Subclass Constructors.........................................................................................2-
  • Triangulator Class ..............................................................................................................................2-
  • Constructor Summary .........................................................................................................................2-
  • Method Summary................................................................................................................................2-
  • IndexedGeometryArray and Subclasses Constructors...........................................................................2-
  • IndexedGeometryStripArray and Subclasses Constructors ...................................................................2-
  • IndexedGeometryArray Methods (partial list) ......................................................................................2-
  • Appearance Constructor......................................................................................................................2-
  • Appearance Methods (excluding lighting and texturing) .......................................................................2-
  • PointAttributes Constructors ...............................................................................................................2-
  • PointAttributes Methods......................................................................................................................2-
  • LineAttributes Constructors ................................................................................................................2-
  • LineAttributes Methods.......................................................................................................................2-
  • PolygonAttributes Constructors...........................................................................................................2-
  • PolygonAttributes Methods .................................................................................................................2-
  • ColoringAttributes Constructors..........................................................................................................2-
  • ColoringAttributes Methods ................................................................................................................2-
  • TransparencyAttributes Constructors ..................................................................................................2-
  • TransparencyAttributes Methods.........................................................................................................2-
  • RenderingAttributes Constructors........................................................................................................2-
  • RenderingAttributes Methods ..............................................................................................................2-

2

Creating Geometry

T(dx, dy, dz) =

1 0 0 dx 0 1 0 dy 0 0 1 dz 0 0 0 1

Chapter Objectives

After reading this chapter, you’ll be able to:

  • Use geometric primitive utility classes
  • Write classes to define visual objects
  • Specify geometry using core classes
  • Specify appearance for visual objects

hapter 1 explores the basic concepts of building a Java 3D virtual universe, concentrating on specifying transforms and simple behaviors. The HelloJava3D examples in Chapter 1 use the ColorCube class for the only visual object. With ColorCube, the programmer doesn't specify shape or color. The ColorCube class is easy to use but can not be used to create other visual objects.

There are three major ways to create new geometric content. One way uses the geometric utility classes for box, cone, cylinder, and sphere. Another way is for the programmer to specify the vertex coordinates for points, line segments, and/or polygonal surfaces. A third way is to use a geometry loader. This chapter demonstrates creating geometric content the first two ways.

The focus of this chapter is the creation of geometric content, that is, the shape of visual objects. A few topics related to geometry are also covered, including math classes and appearance. Before describing how to create geometric content, more information on the virtual universe coordinate system is presented in section 2.1.

2.1 Virtual World Coordinate System

As discussed in Chapter 1, an instance of VirtualUniverse class serves as the root of the scene graph in all Java 3D programs. The term virtual universe commonly refers to the three dimensional virtual space Java 3D objects populate. Each Locale object in the virtual universe establishes a virtual world Cartesian coordinate system.

A Locale object serves as the reference point for visual objects in a virtual universe. With one Locale in a SimpleUniverse, there is one coordinate system in the virtual universe.

C H A P T E R

C

The coordinate system of the Java 3D virtual universe is right-handed. The x-axis is positive to the right, y-axis is positive up, and z-axis is positive toward the viewer, with all units in meters. Figure 2-1 shows the orientation with respect to the viewer in a SimpleUniverse.

viewer position^ image plate

y

z

x

Figure 2-1 Orientation of Axis in Virtual World

2.2 Visual Object Definition Basics

Section 2.2.1 presents the Shape3D class. A general discussion of the NodeComponent class follows in section 2.2.2. After discussing geometry primitives defined in the utility package, the rest of the chapter covers Geometry and Appearance node components.

2.2.1 An Instance of Shape3D Defines a Visual Object

A Shape3D scene graph node defines a visual object^1. Shape3D is one of the subclasses of Leaf class; therefore, Shape3D objects can only be leaves in the scene graph. The Shape3D object does not contain information about the shape or color of a visual object. This information is stored in the NodeComponent objects referred to by the Shape3D object. A Shape3D object can refer to one Geometry node component and one Appearance node component.

In the HelloJava3D scene graphs in Chapter 1, the generic object symbol (rectangle) was used to represent the ColorCube object. The simple scene graph in Figure 2-2 shows a visual object represented as a Shape3D leaf (triangle) and two NodeComponents (ovals) instead of the generic rectangle^2.

(^1) Shape3D objects define the most common visual objects of a virtual universe, but there are other ways.

(^2) This scene graph is not correct for a ColorCube object. ColorCube does not use an Appearance NodeComponent.

This is an example of a typical visual object.

Reading Reference Blocks

The reference blocks in this tutorial do not list all of the constructors, methods, and capabilities for each Java 3D API class. For example, the Shape3D methods reference block (above) does not list all the methods of the Shape3D class. Two of the methods not listed are the "get-methods" that match the "set- methods" shown. That is, Shape3D has getGeometry() and getAppearance() methods. Each of these methods returns a reference to the appropriate NodeComponent.

Since many Java 3D API classes have many methods, not all are listed. The ones listed in the reference blocks in this tutorial are the ones that pertain to the tutorial topics. Also, many classes have get-methods that match set-methods. The get-methods are not listed in the reference blocks in this tutorial to reduce the length of the reference blocks.

The following reference block shows the capabilities of Shape3D objects. This reference block introduces a shorthand notation for listing capabilities. Each line in the reference block lists two capabilities instead of one. There is an ALLOW_GEOMETRY_READ and an ALLOW_GEOMETRY_WRITE capability in each Shape3D object. Quite often there are read and write pairs of capabilities. To reduce the size of the reference blocks, capability reference blocks list the matched read and write capability pairs together in the short hand notation.

Consult the API specification for the complete list of constructors, methods, and capabilities.

Shape3D Capabilities

Shape3D objects inherit capabilities from SceneGraphObject, Node, and Leaf classes. They are not listed here. Refer to section 1.8.2 for more information on Capabilities.

ALLOW_GEOMETRY_READ | WRITE

ALLOW_APPEARANCE_READ | WRITE

ALLOW_COLLISION_BOUNDS_READ | WRITE

2.2.2 Node Components

NodeComponent objects contain the exact specification of the attributes of a visual object. Each of the several subclasses of NodeComponent defines certain visual attributes. Figure 2-3 shows part of the Java 3D API hierarchy containing the NodeComponent class and its descendants. Section 2.5 presents the Geometry NodeComponent. Section 2.6 presents the Appearance NodeComponent.

SceneGraphObject

NodeComponent

Geometry

Material

Node

Texture

Attributes*

Appearance

Group

Leaf

Background

Behavior

Fog

Light

Morph

Shape3D

Sound

ViewPlatform

*There are several attribute classes.

Figure 2-3 Partial Java 3D API Class Hierarchy Showing Subclasses of NodeComponent.

2.2.3 Defining Visual Object Classes

The same visual object will quite often appear many times in a single virtual universe. It makes sense to define a class to create the visual object instead of constructing each visual object from scratch. There are several ways to design a class to define a visual object.

Code Fragment 2-1 shows the skeleton code of VisualObject class as an example of one possible organization for a generic visual object class. The methods are empty in the code. The code of VisualObject does not appear in the examples distribution because is it not particularly useful as is.

  1. public class VisualObject extends Shape3D{
  2. private Geometry voGeometry;
  3. private Appearance voAppearance;
  4. // create Shape3D with geometry and appearance
  5. // the geometry is created in method createGeometry
  6. // the appearance is created in method createAppearance
  7. public VisualObject() {
  8. voGeometry = createGeometry();
  9. voAppearance = createAppearance();
  10. this.setGeometry(voGeometry);
  11. this.setAppearance(voAppearance);
  12. }

everything about a ColorCube is fixed, except its size^3. The size of a ColorCube is only specified when the object is created.

A primitive object provides more flexibility by specifying shape without specifying color. In a geometric primitive utility class, the programmer cannot change the geometry, but can change the appearance^4. The primitive classes give the programmer the flexibility to have multiple instances of the same geometric primitive where each can have a different appearance by having a reference to different Appearance NodeComponents.

The Box, Cone, Cylinder and Sphere utility classes are defined in the com.sun.j3d.utils.geometry package. Details of the Box, Cone, Cylinder, and Sphere classes are presented in Sections 2.3.1 through 2.3.4, respectively. The superclass of these primitives, Primitive, is discussed in Section 2.3.5. The portion of the com.sun.j3d.utils.geometry package hierarchy that contains the primitive classes is shown in Figure 2-4.

com.sun.j3d.utils.geometry.Primitive

com.sun.j3d.utils.geometry.Box

com.sun.j3d.utils.geometry.Cone

com.sun.j3d.utils.geometry.Cylinder

com.sun.j3d.utils.geometry.Sphere

java.lang.Object

javax.media.j3d.SceneGraphObject

javax.media.j3d.Node

javax.media.j3d.Group

Figure 2-4 Class Hierarchy for Utility Geometric Primitives: Box, Cone, Cylinder, and Sphere

2.3.1 Box

The Box geometric primitive creates 3D box visual objects^5. The defaults for length, width, and height are 2 meters, with the center at the origin, resulting in a cube with corners at ( -1, -1, -1) and ( 1, 1, 1). The

(^3) The Geometry NodeComponent referenced by a ColorCube object can be changed, but then it wouldn't appear as

a ColorCube. (^4) Just like with ColorCube, the Geometry NodeComponent referenced by a primitive object can be changed, but

then it wouldn't appear as the primitive. (^5) Technically, a box is a six-sided polyhedron with rectangular faces.

length, width, and height can be specified at object creation time. Of course, TransformGroup along the scene graph path to a Box can be used to change the location and/or orientation of instances of Box and other visual objects.

Box Constructors (partial list)

Package: com.sun.j3d.utils.geometry

Box extends Primitive, another class in the com.sun.j3d.utils.geometry package.

Box() Constructs a default box of 2.0 meters in height, width, and depth, centered at the origin.

Box(float xdim, float ydim, float zdim, Appearance appearance) Constructs a box of a given dimension and appearance, centered at the origin.

While the constructors differ by class, Box, Cone, and Cylinder classes share the same methods. The following reference block lists the methods for these classes.

Box, Cone, and Cylinder Methods

Package: com.sun.j3d.utils.geometry

These methods are defined in each of the Primitive classes: Box, Cone, and Cylinder. These primitives are composed of multiple Shape3D objects in a group.

Shape3D getShape(int id) Gets one of the faces (Shape3D) from the primitive that contains the geometry and appearance. Box, Cone, and Cylinder objects are composed of more than one Shape3D object, each with its own Geometry node component. The value used for partid specifies which of the Geometry node components to get.

void setAppearance(Appearance appearance) Sets appearance of the primitive (for all of the Shape3D objects).

2.3.2 Cone

The Cone class defines capped, cone shaped objects centered at the origin with the central axis aligned along the y-axis. The default for radius is 1.0 and 2.0 for height. The center of the cone is defined to be the center of its bounding box rather than its centroid.

Sphere Methods

Package: com.sun.j3d.utils.geometry

As an extention of Primitive, a Sphere is a Group object that has a single Shape3D child object.

Shape3D getShape() Gets the Shape3D that contains the geometry and appearance.

Shape3D getShape(int id) This method is included for compatibility with the other Primitive classes: Box, Cone, and Cylinder. However, since a Sphere has only one Shape3D object, it can be called only with id = 1.

void setAppearance(Appearance appearance) Sets appearance of the sphere.

2.3.5 More About Geometric Primitives

The geometry of a primitive utility class does not define color. Geometry that does not define color derives its color from its Appearance node component. Without a reference to an Appearance node component, the visual object will be white, the default appearance color. Color is first discussed in Section 2.4.2 and added to geometry in Section 2.5.1. Section 2.6 presents the details of Appearance node components.

The Primitive class defines default values common to Box, Cone, Cylinder, and Sphere. For example, Primitive defines the default value for the number of polygons used to represent surfaces. Section 2.3. presents some of the details of the Primitive class. Since the default values defined by Primitive are fine for most applications, Java 3D programs can be written without even using the Primitive class. For this reason, the section describing the Primitive class is considered an advanced topic (which can be skipped). You will recognize advanced sections when you get there by the Duke figure hanging from the double-line outline.

2.3.6 ColorCube

The ColorCube class is presented here to contrast with the geometric primitive classes of Box, Cone, Cylinder, and Sphere. The ColorCube class extends a different hierarchy than the graphic primitive classes. It is a subclass of Shape3D. This hierarchy for ColorCube is shown in Figure 2-5. Chapter 1 contains the reference blocks for ColorCube.

javax.media.j3d.Shape3D

com.sun.j3d.utils.geometry.ColorCube

java.lang.Object

javax.media.j3d.SceneGraphObject

javax.media.j3d.Node

javax.media.j3d.Leaf

Figure 2-5 Class Hierarchy of ColorCube Utility Geometric Class

ColorCube is the only class distributed with the Java 3D API that allows a programmer to ignore the issues of colors and lights. For this reason, ColorCube class is useful for quickly assembling scene graphs for testing or prototyping.

2.3.7 Example: Creating a Simple Yo-Yo From Two Cones

This section presents a simple example that uses the Cone class: ConeYoyoApp.java. The goal of the program is to render a yo-yo. Two cones are used to form the yo-yo. Java 3D API behaviors could be used to make the yo-yo move up and down, but that is beyond the scope of this Chapter. The program spins the yo-yo so the geometry can be appreciated. The scene graph diagram in Figure 2-5 shows the designs for the ConeYoyo and ConeYoyoApp classes in the ConoYoyoApp example program.

The default position of a Cone object is with its bounding box centered at the origin. The default orientation is with the tip of the Cone object in the direction of the positive y-axis. The yo-yo is formed of two cones that are rotated about the z-axis and translated along the x-axis to bring the tips of the cones together at the origin. Other combinations of rotation and translation transformations could bring the tips of the Cone objects together.

descend from Group, the same Cone (or other Primitive object) can not be used more than once in a scene graph. Figure 2-7 shows an example error message produced when attempting to use the same Cone object in a single scene graph. This error does not exist in the example program distributed with this tutorial.

Exception in thread "main" javax.media.j3d.MultipleParentException: Group.addChild: child already has a parent at javax.media.j3d.GroupRetained.addChild(GroupRetained.java:246) at javax.media.j3d.Group.addChild(Group.java:241) at ConeYoyoApp$ConeYoyo.(ConeYoyoApp.java:89) at ConeYoyoApp.createSceneGraph(ConeYoyoApp.java:119) at ConeYoyoApp.(ConeYoyoApp.java:159) at ConeYoyoApp.main(ConeYoyoApp.java:172)

Figure 2-7 Multiple Parent Exception While Attempting to Reuse a Cone Object

Figure 2-8 An Image Rendered by ConeYoyoApp.java

Figure 2-8 shows one of the possible images rendered by ConeYoyoApp.java as the ConeYoyo object spins. ConeYoyoApp.java is found in the example/Geometry subdirectory. The ConeYoyo class in the program is reproduced here in Code Fragment 2-2.

Lines 14 through 21 create the objects of one half of the yo-yo scene graph. Lines 23 through 25 create the relationships among these objects. The process is repeated for the other half of the yo-yo on lines 27 through 38.

Line 12 creates yoyoAppear , an Appearance node component with default values, to be used by the Cone objects. Lines 21 and 34 set the appearance for the two cones.

  1. public class ConeYoyo{

  2. private BranchGroup yoyoBG;

  3. // create Shape3D with geometry and appearance

  4. //

  5. public ConeYoyo() {

  6. yoyoBG = new BranchGroup();

  7. Transform3D rotate = new Transform3D();

  8. Transform3D translate = new Transform3D();

  9. Appearance yoyoAppear = new Appearance();

  10. rotate.rotZ(Math.PI/2.0d);

  11. TransformGroup yoyoTGR1 = new TransformGroup(rotate);

  12. translate.set(new Vector3f(0.1f, 0.0f, 0.0f));

  13. TransformGroup yoyoTGT1 = new TransformGroup(translate);

  14. Cone cone1 = new Cone(0.6f, 0.2f);

  15. cone1.setAppearance(yoyoAppear);

  16. yoyoBG.addChild(yoyoTGT1);

  17. yoyoTGT1.addChild(yoyoTGR1);

  18. yoyoTGR1.addChild(cone1);

  19. translate.set(new Vector3f(-0.1f, 0.0f, 0.0f));

  20. TransformGroup yoyoTGT2 = new TransformGroup(translate);

  21. rotate.rotZ(-Math.PI/2.0d);

  22. TransformGroup yoyoTGR2 = new TransformGroup(rotate);

  23. Cone cone2 = new Cone(0.6f, 0.2f);

  24. cone2.setAppearance(yoyoAppear);

  25. yoyoBG.addChild(yoyoTGT2);

  26. yoyoTGT2.addChild(yoyoTGR2);

  27. yoyoTGR2.addChild(cone2);

  28. yoyoBG.compile();

  29. } // end of ConeYoyo constructor

  30. public BranchGroup getBG(){

  31. return yoyoBG;

  32. }

  33. } // end of class ConeYoyo

Code Fragment 2-2 Class ConeYoyo From ConeYoyoApp.java Example Program

2.3.8 Advanced Topic: Geometric Primitive

The class hierarchy of Figure 2-4 shows Primitive as the superclass of Box, Cone, Cylinder, and Sphere classes. It defines a number of fields and methods common to these classes, as well as default values for the fields.

The Primitive class provides a way to share Geometry node components among instances of a primitive of the same size. By default, all primitives of the same size share one geometry node component. An example of a field defined in the Primitive class is the GEOMETRY_NOT_SHARED integer. This field specifies the geometry being created will not be shared by another. Set this flag to prevent the geometry from being shared among primitives of the same parameters (e.g., spheres with radius 1).

myCone.setPrimitiveFlags(Primitive.GEOMETRY_NOT_SHARED);