Getting Started with the Java 3D API - University of California, Los ...

[Pages:29]Getting Started with the Java 3DTM API

Chapter 1

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

Dennis J Bouvier K Computing

Getting Started with Java 3D

? 1999 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, ). 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.

Getting Started with Java 3D

Table of Contents

Chapter 1: Getting Started......................................................................................................................................1-1

1.1 What is Java 3D ........................................................................................................................1-1

1.2 The Java 3D API.......................................................................................................................1-2

1.3 Building a Scene Graph .............................................................................................................1-2 1.3.1 High Level Java 3D API Class Hierarchy ...........................................................................1-7

1.4 Recipe for Writing Java 3D Programs........................................................................................1-8 1.4.1 A Simple Recipe for Writing Java 3D Programs .................................................................1-9

1.5 Some Java 3D Terminology .....................................................................................................1-12

1.6 Simple Recipe Example: HelloJava3Da....................................................................................1-13 1.6.1 Java 3D Classes Used in HelloJava3Da ............................................................................1-16

1.7 Rotating the Cube....................................................................................................................1-19 1.7.1 Combination of Transformations Example: HelloJava3Db ................................................1-20

1.8 Capabilities and Performance...................................................................................................1-22 1.8.1 Compiling Contents..........................................................................................................1-23 1.8.2 Capabilities......................................................................................................................1-24

1.9 Adding Animation Behavior.....................................................................................................1-25 1.9.1 Specifying Animation Behavior ........................................................................................1-26 1.9.2 Time Varying Functions: Mapping a Behavior to Time.....................................................1-27 1.9.3 Scheduling Region ...........................................................................................................1-27 1.9.4 Behavior Example: HelloJava3Dc ....................................................................................1-28 1.9.5 Transformation and Behavior Combination Example: HelloJava3Dd.................................1-30

1.10 Chapter Summary....................................................................................................................1-33

1.11 Self Test..................................................................................................................................1-33

The Java 3D Tutorial

1-i

Getting Started with Java 3D

Figures

Figure 1-1 Symbols Representing Objects in Scene Graphs....................................................................1-4 Figure 1-2 First Scene Graph Example ..................................................................................................1-5 Figure 1-3 Example of an Illegal Scene Graph (not a DAG)...................................................................1-6 Figure 1-4 Fix for Illegal Scene Graph of Figure 1-3..............................................................................1-6 Figure 1-5 An Overview of the Java 3D API Class Hierarchy ................................................................1-8 Figure 1-6 Recipe for Writing Java 3D Programs .................................................................................1-9 Figure 1-7 A SimpleUniverse Object Provides a Minimal Virtual Universe...........................................1-10 Figure 1-8 Simple Recipe for Writing Java 3D Programs.....................................................................1-10 Figure 1-9 Conceptual Drawing of Image Plate and Eye Position in a Virtual Universe.........................1-11 Figure 1-10 Conceptual Renderer Process............................................................................................1-13 Figure 1-11 Scene Graph for HelloJava3Da Example ..........................................................................1-16 Figure 1-12 Image Produced by HelloJava3Da ....................................................................................1-16 Figure 1-13 Scene Graph for Content Branch Graph Created in Code Fragment 1-5.............................1-20 Figure 1-14 Scene Graph for HelloJava3Db Example ..........................................................................1-22 Figure 1-15 Image of the Rotated ColorCube Rendered by HelloJava3Db ............................................1-22 Figure 1-16 Conceptual Example of the Result of Compiling a Scene Graph ........................................1-23 Figure 1-17 Recipe for Adding Behaviors to a Java 3D Visual Objects.................................................1-26 Figure 1-18 Scene Graph for HelloJava3Dc Example ..........................................................................1-30 Figure 1-19 An Image of the ColorCube in Rotation as Rendered by HelloJava3Dc..............................1-30 Figure 1-20 Scene Graph for HelloJava3Dd Example ..........................................................................1-32 Figure 1-21 An Image of the ColorCube in Rotation as Rendered by HelloJava3Dd..............................1-32

The Java 3D Tutorial

1-ii

Getting Started with Java 3D

Code Fragments

Code Fragment 1-1 Class HelloJava3Da..............................................................................................1-14 Code Fragment 1-2 Method createSceneGraph for Class HelloJava3Da ...............................................1-14 Code Fragment 1-3 Main() Method of HelloJava3Da Invokes MainFrame............................................1-15 Code Fragment 1-4 Import Statements for HelloJava3Da.java..............................................................1-15 Code Fragment 1-5 One Rotation in the Content Branch Graph............................................................1-20 Code Fragment 1-6 Two Rotation Transformations in HelloJava3Db ...................................................1-21 Code Fragment 1-7 createSceneGraph method with RotationInterpolator Behavior...............................1-29 Code Fragment 1-8 Content Branch for Rotated Spinning ColorCube of HelloJava3Dd........................1-31

The Java 3D Tutorial

1-iii

Getting Started with Java 3D

Reference Blocks

SimpleUniverse Constructors ..............................................................................................................1-11 ViewingPlatform setNominalViewingTransform() Method ..................................................1-12 SimpleUniverse Methods (partial list)..................................................................................................1-12 BranchGroup compile() Method....................................................................................................1-13 SceneGraphObject Methods (partial list) .............................................................................................1-13 MainFrame Constructor (partial list) ...................................................................................................1-15 BranchGroup Default Constructor.......................................................................................................1-17 Canvas3D Constructor........................................................................................................................1-17 Transform3D Default Constructor.......................................................................................................1-17 Transform3D Methods (partial list) .....................................................................................................1-18 TransformGroup Constructors ............................................................................................................1-18 TransformGroup setTransform() Method...................................................................................1-18 Vector3f Constructors .........................................................................................................................1-19 ColorCube Constructors......................................................................................................................1-19 SceneGraphObject Methods (partial list) .............................................................................................1-24 TransformGroup Capabilities (partial list) ...........................................................................................1-24 Group Capabilities (partial list) ...........................................................................................................1-25 RotationInterpolator Constructor (partial list) ......................................................................................1-26 Alpha Constructor...............................................................................................................................1-27 Behavior setSchedulingBounds method................................................................................................1-28 Bounding Sphere Constructors (partial list) .........................................................................................1-28

Preface to Chapter 1 This document is the first part of a tutorial on using the Java 3D API. Additional chapters and the full preface to this material is presented in the Module 0 document available at:

The Java 3D Tutorial

1-iv

Getting Started with Java 3D

Chapter 1. Getting Started

CHAPTER 1 Getting Started

T(dx, dy, dz) =

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

Chapter Objectives

After reading this chapter, you will:

? Be able to explain in general terms what Java 3D is ? Be able to describe the basic structure of Java 3D programs ? Recognize many classes from the Java 3D API ? Be able to write some simple animated Java 3D programs

The Java 3D API is an interface for writing programs to display and interact with three-dimensional

graphics. Java 3D is a standard extension to the Java 2 JDK. The API provides a collection of high-level constructs for creating and manipulating 3D geometry and structures for rendering that geometry. Java 3D provides the functions for creation of imagery, visualizations, animations, and interactive 3D graphics application programs.

1.1 What is the Java 3D API?

The Java 3D API is a hierarchy of Java classes which serve as the interface to a sophisticated threedimensional graphics rendering and sound rendering system. The programmer works with high-level constructs for creating and manipulating 3D geometric objects. These geometric objects reside in a virtual universe, which is then rendered. The API is designed with the flexibility to create precise virtual universes of a wide variety of sizes, from astronomical to subatomic. Despite all this functionality, the API is still straightforward to use. The details of rendering are handled automatically. By taking advantage of Java threads, the Java 3D renderer is capable of rendering in parallel. The renderer can also automatically optimize for improved rendering performance. A Java 3D program creates instances of Java 3D objects and places them into a scene graph data structure. The scene graph is an arrangement of 3D objects in a tree structure that completely specifies the content of a virtual universe, and how it is to be rendered.

The Java 3D Tutorial

1-1

Getting Started with Java 3D

Chapter 1. Getting Started

Java 3D programs can be written to run as stand alone applications, as applets in browsers which have been extended to support Java 3D, or both1.

1.2 The Java 3D API

Every Java 3D program is at least partially assembled from objects from the Java 3D class hierarchy. This collection of objects describes a virtual universe, which is to be rendered. The API defines over 100 classes presented in the javax.media.j3d package. These classes are commonly referred to as the Java 3D core classes.

There are hundreds of fields and methods in the classes of the Java 3D API. However, a simple virtual universe that includes animation can be built with only a few classes. This chapter describes a minimal set of objects and their interactions to render a simple virtual universe.

This chapter includes the development of one simple but complete Java 3D program called HelloJava3D, which displays a rotating cube. The example program is developed incrementally, and presented in multiple versions, to demonstrate each part of the Java 3D programming process. All of the programs used in this tutorial are available electronically. See the "Getting This Tutorial" section in the Preface for more information.

In addition to the Java 3D core package, other packages are used in writing Java 3D programs. One such package is the com.sun.j3d.utils package that is commonly referred to as the Java 3D utility classes. The core class package includes only the lowest-level classes necessary in Java 3D programming. The utility classes are convenient and powerful additions to the core.

The utility classes fall into four major categories: content loaders, scene graph construction aids, geometry classes, and convenience utilities. Future functionality, such as nurbs, likely would be added as utility classes, not in the Java 3D core package. Some utility classes may be moved to the core package in future versions of the Java 3D API.

Using utility classes significantly reduces the number of lines of code in a Java 3D program. In addition to the Java 3D core and utility class packages, every Java 3D program uses classes from the java.awt package and javax.vecmath package. The java.awt package defines the Abstract Windowing Toolkit (AWT). AWT classes create a window to display the rendering. The javax.vecmath package defines vector math classes for points, vectors, matrices, and other mathematical objects.

In the rest of the text, the term visual object is used to refer to an `object in the scene graph' (e.g., a cube or a sphere). The term object is used only to refer to an instance of a class. The term content is used to refer to visual objects in a scene graph as a whole.

1.3 Building a Scene Graph

A Java 3D virtual universe is created from a scene graph. A scene graph is created using instances of Java 3D classes. The scene graph is assembled from objects to define the geometry, sound, lights, location, orientation, and appearance of visual and audio objects.

1 Browser support for Java 3D is available through the Java Plugin, which can be downloaded from java.. All of the example programs in this tutorial are written as applications.

The Java 3D Tutorial

1-2

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download