










































Estude fácil! Tem muito documento disponível na Docsity
Ganhe pontos ajudando outros esrudantes ou compre um plano Premium
Prepare-se para as provas
Estude fácil! Tem muito documento disponível na Docsity
Prepare-se para as provas com trabalhos de outros alunos como você, aqui na Docsity
Encontra documentos específicos para os exames da tua universidade
Prepare-se com as videoaulas e exercícios resolvidos criados a partir da grade da sua Universidade
Responda perguntas de provas passadas e avalie sua preparação.
Ganhe pontos para baixar
Ganhe pontos ajudando outros esrudantes ou compre um plano Premium
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
1 / 50
Esta página não é visível na pré-visualização
Não perca as partes importantes!











































tutorial v1.4 (Java 3D API v1.1.2)
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
2
Creating Geometry
T(dx, dy, dz) =
1 0 0 dx 0 1 0 dy 0 0 1 dz 0 0 0 1
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.
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
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.
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.
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
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.
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.
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
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).
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.
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.
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.
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.
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.
public class ConeYoyo{
private BranchGroup yoyoBG;
// create Shape3D with geometry and appearance
//
public ConeYoyo() {
yoyoBG = new BranchGroup();
Transform3D rotate = new Transform3D();
Transform3D translate = new Transform3D();
Appearance yoyoAppear = new Appearance();
rotate.rotZ(Math.PI/2.0d);
TransformGroup yoyoTGR1 = new TransformGroup(rotate);
translate.set(new Vector3f(0.1f, 0.0f, 0.0f));
TransformGroup yoyoTGT1 = new TransformGroup(translate);
Cone cone1 = new Cone(0.6f, 0.2f);
cone1.setAppearance(yoyoAppear);
yoyoBG.addChild(yoyoTGT1);
yoyoTGT1.addChild(yoyoTGR1);
yoyoTGR1.addChild(cone1);
translate.set(new Vector3f(-0.1f, 0.0f, 0.0f));
TransformGroup yoyoTGT2 = new TransformGroup(translate);
rotate.rotZ(-Math.PI/2.0d);
TransformGroup yoyoTGR2 = new TransformGroup(rotate);
Cone cone2 = new Cone(0.6f, 0.2f);
cone2.setAppearance(yoyoAppear);
yoyoBG.addChild(yoyoTGT2);
yoyoTGT2.addChild(yoyoTGR2);
yoyoTGR2.addChild(cone2);
yoyoBG.compile();
} // end of ConeYoyo constructor
public BranchGroup getBG(){
return yoyoBG;
}
} // end of class ConeYoyo
Code Fragment 2-2 Class ConeYoyo From ConeYoyoApp.java Example Program
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);