eScience Lectures Notes : Introduction to Java3D


Slide 1 : 1 / 35 : Introduction to Java 3D

Introduction to Java 3D

 


Slide 2 : 2 / 35 : Introduction to Java 3D

Introduction to Java 3D

What is Java 3D

"Write once, run anywhere"

Of course, this is theoritical. In practice, it is not yet available everywhere


Java 3DTM API on Other Platforms

Java 3DTM Available for IBM`s AIX Platform

IBM has released the Java 3D API for AIX.

HP Licenses the Java 3DTM API!

Hewlett-Packard Company has released the latest version of Java 3D on HP-UX. This brings the Java 3D API one step closer to platform ubiquity. Read the Press Release for more information.

Java 3DTM for the Linux Platform : Blackdown

Blackdown has been working on porting the Java 3DTM API to Linux. For more information on these please their Java 3D for Linux page.

Java 3DTM for SGI IRIX® Platform

SGI has made available a developer release of the Java 3D API, version 1.1.3, based on the Java 2 SDK, v 1.2.2. This now enables customers with SGI IRIX systems to develop and execute JavaTM programs written with the Java 3D API. This software can be used with IRIX® 6.2 through 6.5.

 

THE Java 3D API is an application programming interface used for writing three-dimensional graphics applications and applets. It gives developers high-level constructs for creating and manipulating 3D geometry and for constructing the structures used in rendering that geometry. Application developers can describe very large virtual worlds using these constructs, which provide Java 3D with enough information to render these worlds efficiently.

Java 3D delivers Java's "write once, run anywhere" benefit to developers of 3D graphics applications. Java 3D is part of the JavaMedia suite of APIs, making it available on a wide range of platforms. It also integrates well with the Internet because applications and applets written using the Java 3D API have access to the entire set of Java classes.

The Java 3D API draws its ideas from existing graphics APIs and from new technologies. Java 3D's low-level graphics constructs synthesize the best ideas found in low-level APIs such as Direct3D, OpenGL, QuickDraw3D, and XGL. Similarly, its higher-level constructs synthesize the best ideas found in several scene graph-based systems. Java 3D introduces some concepts not commonly considered part of the graphics environment, such as 3D spatial sound. Java 3D's sound capabilities help to provide a more immersive experience for the user.


Slide 3 : 3 / 35 : Java 3D API Goals

Java 3D API Goals

Java 3D was designed with several goals in mind. Chief among them is high performance. Several design decisions were made so that Java 3D implementations can deliver the highest level of performance to application users. In particular, when trade-offs were made, the alternative that benefited runtime execution was chosen.
Other important Java 3D goals are to

Provide a rich set of features for creating interesting 3D worlds, tempered by the need to avoid nonessential or obscure features. Features that could be layered on top of Java 3D were not included.

Provide a high-level object-oriented programming paradigm that enables developers to deploy sophisticated applications and applets rapidly.

Provide support for runtime loaders. This allows Java 3D to accommodate a wide variety of file formats, such as vendor-specific CAD formats, interchange formats, and VRML97.


Slide 4 : 4 / 35 : A Higher Level API

A Higher Level API

Raise the programming floor

Think


Slide 5 : 5 / 35 : Why Use Java 3D?

Why Use Java 3D?

... Because you won't have to learn still another langage !

... Because you are learning 3D programming

A good option : openGL


What's the difference between Java 3D and OpenGL/Direct3D/PHIGS/, etc? Java3D is another 3D programming API that exists on a similar level to OpenGL, Direct3D, PHIGS and similar systems. It is designed to use hardware accelaration wherever possible based on the underlying graphics architecture of the OS. That is, J3D provides a 3D rendering API for the Java language, but at the same time it may use OpenGL to do the interface to the hardware. J3D does not require direct hardware device driver support like the other APIs because it could rely on them to build its functionality. Sun is encouraging graphics board vendors to provide native implementations of J3D, but nothing has beenseen yet.
For unix users Sun's Java3D is implemented on top of OpenGL. For Win32 users Java3D is available for OpenGL and Direct3D.
There are Java bindings to the other APIs. GL4Java is an OpenGL binding written in Java.


Slide 6 : 6 / 35 : Programming Paradigm

Programming Paradigm

Java 3D is an object-oriented API. Applications construct individual graphics elements as separate objects and connect them together into a treelike structure called a scene graph. The application manipulates these objects using their predefined accessor, mutator, and node-linking methods.

Java 3D's scene graph-based programming model provides a simple and flexible mechanism for representing and rendering scenes. The scene graph contains a complete description of the entire scene, or virtual universe. This includes the geometric data, the attribute information, and the viewing information needed to render the scene from a particular point of view. Chapter 3, "Scene Graph Basics," provides more information on the Java 3D scene graph programming model.

The Java 3D API improves on previous graphics APIs by eliminating many of the bookkeeping and programming chores that those APIs impose. Java 3D allows the programmer to think about geometric objects rather than about triangles-about the scene and its composition rather than about how to write the rendering code for efficiently displaying the scene.

Java 3D includes three different rendering modes: immediate mode, retained mode, and compiled-retained mode. Each successive rendering mode allows Java 3D more freedom in optimizing an application's execution. Most Java 3D applications will want to take advantage of the convenience and performance benefits that the retained and compiled-retained modes provide.

Immediate Mode

Immediate mode leaves little room for global optimization at the scene graph level. Even so, Java 3D has raised the level of abstraction and accelerates immediate mode rendering on a per-object basis. An application must provide a Java 3D draw method with a complete set of points, lines, or triangles, which are then rendered by the high-speed Java 3D renderer. Of course, the application can build these lists of points, lines, or triangles in any manner it chooses.

Retained Mode

Retained mode requires an application to construct a scene graph and specify which elements of that scene graph may change during rendering. The scene graph describes the objects in the virtual universe, the arrangement of those objects, and how the application animates those objects.

Compiled-Retained Mode

Compiled-retained mode, like retained mode, requires the application to construct a scene graph and specify which elements of the scene graph may change during rendering. Additionally, the application can compile some or all of the subgraphs that make up a complete scene graph. Java 3D compiles these graphs into an internal format. The compiled representation of the scene graph may bear little resemblance to the original tree structure provided by the application, however, it is functionally equivalent. Compiled-retained mode provides the highest performance.


Slide 7 : 7 / 35 : A Conceptual Scene Graph

A Conceptual Scene Graph


Slide 8 : 8 / 35 : Overview of Java 3D Architecture

Overview of Java 3D Architecture

 

 


Slide 9 : 9 / 35 : An Application in Brief

An Application in Brief


Slide 10 : 10 / 35 : What does Java 3D do?

What does Java 3D do?


Slide 11 : 11 / 35 : First look at the Scene Graph

First look at the Scene Graph


The scene graph consists of superstructure components-a VirtualUniverse object and a Locale object-and a set of branch graphs. Each branch graph is a subgraph that is rooted by a BranchGroup node that is attached to the superstructure.

  A VirtualUniverse object defines a named universe. Java 3D permits the creation of more than one universe, though the vast majority of applications will use just one. The VirtualUniverse object provides a grounding for scene graphs. All Java 3D scene graphs must connect to a VirtualUniverse object to be displayed.

Below the VirtualUniverse object is a Locale object. The Locale object defines the origin, in high-resolution coordinates, of its attached branch graphs. A virtual universe may contain as many Locales as needed. In this example, a single Locale object is defined with its origin at (0.0, 0.0, 0.0).

The scene graph itself starts with the BranchGroup nodes. A BranchGroup serves as the root of a subgraph, called a branch graph, of the scene graph. Only BranchGroup objects can attach to Locale objects.

In this example there are two branch graphs and, thus, two BranchGroup nodes. Attached to the left BranchGroup are two subgraphs. One subgraph consists of a user-extended Behavior leaf node. The Behavior node contains Java code for manipulating the transformation matrix associated with the object's geometry.

The other subgraph in this BranchGroup consists of a TransformGroup node that specifies the position (relative to the Locale), orientation, and scale of the geometric objects in the virtual universe. A single child, a Shape3D leaf node, refers to two component objects: a Geometry object and an Appearance object. The Geometry object describes the geometric shape of a 3D object (a cube in our simple example). The Appearance object describes the appearance of the geometry (color, texture, material reflection characteristics, and so forth).

The right BranchGroup has a single subgraph that consists of a TransformGroup node and a ViewPlatform leaf node. The TransformGroup specifies the position (relative to the Locale), orientation, and scale of the ViewPlatform. This transformed ViewPlatform object defines the end user's view within the virtual universe.

Finally, the ViewPlatform is referenced by a View object that specifies all of the parameters needed to render the scene from the point of view of the ViewPlatform. Also referenced by the View object are other objects that contain information, such as the drawing canvas into which Java 3D renders, the screen that contains the canvas, and information about the physical environment.

 


Slide 12 : 12 / 35 : Process Overview

Process Overview

NB : SimpleUniverse Utility is not part of Java API !


Slide 13 : 13 / 35 : Recipe for a Java 3D Program

Recipe for a Java 3D Program

The following steps are taken by the example program to create the scene graph elements and link them together. Java 3D will then render the scene graph and display the graphics in a window on the screen:

  1. Create a Canvas3D object and add it to the Applet/Frame panel.

  2. Create a BranchGroup as the root of the scene branch graph.

  3. Construct a Shape3D node with a TransformGroup node above it.

  4. Attach a RotationInterpolator behavior to the TransformGroup.

  5. Call the simple universe utility function to do the following:

    1. Establish a virtual universe with a single high-resolution Locale

    2. Create the PhysicalBody, PhysicalEnvironment, View, and ViewPlat-form objects.

    3. Create a BranchGroup as the root of the view platform branch graph.

    4. Insert the view platform branch graph into the Locale.

  6. Insert the scene branch graph into the simple universe's Locale.

The Java 3D renderer then starts running in an infinite loop.


NB :

SimpleUniverse Utility is not part of Java API

Most Java 3D programs build an identical set of superstructure and view branch objects, so the Java 3D utility packages provide a universe package for constructing and manipulating the objects in a view branch. The classes in the universe package provide a quick means for building a single view (single window) application. Listing 2-3 shows a code fragment for using the SimpleUniverse class. Note that the SimpleUniverse constructor takes a Canvas3D as an argument, in this case referred to by the variable myCanvas.

Code for Constructing a Scene Graph Using the Universe Package

------------------------------------------------------------------------
   import com.sun.j3d.utils.universe.*;
   Shape3D myShape1 = new Shape3D(myGeometry1, myAppearance1);
   Shape3D myShape2 = new Shape3D(myGeometry2, myAppearance2);
   BranchGroup myBranch = new BranchGroup();
   myBranch.addChild(myShape1);
   myBranch.addChild(myShape2);
   myBranch.compile();
   SimpleUniverse myUniv = new SimpleUniverse(myCanvas);
   myUniv.addBranchGraph(myBranch);


Slide 14 : 14 / 35 : A Conceptual Java 3D Renderer

A Conceptual Java 3D Renderer

while (true) {
   Process input
   if (exit request) break
   Check for collisions
   Perform behaviors
   Start playing appropriate sounds
   Traverse scene graph and render objects
}
Cleanup


Slide 15 : 15 / 35 : A Java 3D Scene Graph

A Java 3D Scene Graph


Slide 16 : 16 / 35 : Scene Graph : Constructing an Application

Scene Graph : Constructing an Application

Your application creates 3D content, arranged in a scene graph

Groups of content within groups, and so on to create a family tree of 3D content

The scene graph contains everything to draw

 

 


Slide 17 : 17 / 35 : Terminology : VirtualUniverse

Terminology

VirtualUniverse

A VirtualUniverse object defines a named universe. Java 3D permits the creation of more than one universe, though the vast majority of applications will use just one. The VirtualUniverse object provides a grounding for scene graphs. All Java 3D scene graphs must connect to a VirtualUniverse object to be displayed.


 

JAVA 3D's superstructure consists of one or more VirtualUniverse objects, each of which contains a set of one or more high-resolution Locale objects. The Locale objects, in turn, contain collections of subgraphs that comprise the scene graph

The Virtual Universe

Java 3D defines the concept of a virtual universe as a three-dimensional space with an associated set of objects. Virtual universes serve as the largest unit of aggregate representation, and can also be thought of as databases. Virtual universes can be very large, both in physical space units and in content. Indeed, in most cases a single virtual universe will serve an application's entire needs.
Virtual universes are separate entities in that no node object may exist in more than one virtual universe at any one time. Likewise, the objects in one virtual universe are not visible in, nor do they interact with objects in, any other virtual universe.
To support large virtual universes, Java 3D introduces the concept of Locales that have high-resolution coordinates as an origin. Think of high-resolution coordinates as "tie-downs" that precisely anchor the locations of objects specified using less precise floating-point coordinates that are within the range of influence of the high-resolution coordinates.
A Locale, with its associated high-resolution coordinates, serves as the next level of representation down from a virtual universe. All virtual universes contain one or more high-resolution-coordinate Locales, and all other objects are attached to a Locale. High-resolution coordinates act as an upper-level translation-only transform node. For example, the coordinates of all objects that are attached to a particular Locale are all relative to the location of that Locale's high-resolution coordinates.


The Virtual Universe


While a virtual universe is similar to the traditional computer graphics concept of a scene graph, a given virtual universe can become so large that it is often better to think of a scene graph as the descendant of a high-resolution-coordinate Locale.


Slide 18 : 18 / 35 : Terminology : View

Terminology

View

The right BranchGroup has a single subgraph that consists of a TransformGroup node and a ViewPlatform leaf node. The TransformGroup specifies the position (relative to the Locale), orientation, and scale of the ViewPlatform. This transformed ViewPlatform object defines the end user's view within the virtual universe.

Finally, the ViewPlatform is referenced by a View object that specifies all of the parameters needed to render the scene from the point of view of the ViewPlatform. Also referenced by the View object are other objects that contain information, such as the drawing canvas into which Java 3D renders, the screen that contains the canvas, and information about the physical environment.


Slide 19 : 19 / 35 : Terminology : ViewPlatform

Terminology

ViewPlatform

The right BranchGroup has a single subgraph that consists of a TransformGroup node and a ViewPlatform leaf node. The TransformGroup specifies the position (relative to the Locale), orientation, and scale of the ViewPlatform. This transformed ViewPlatform object defines the end user's view within the virtual universe.

Finally, the ViewPlatform is referenced by a View object that specifies all of the parameters needed to render the scene from the point of view of the ViewPlatform. Also referenced by the View object are other objects that contain information, such as the drawing canvas into which Java 3D renders, the screen that contains the canvas, and information about the physical environment.


Slide 20 : 20 / 35 : Terminology : Canvas3D

Terminology

Canvas3D


Slide 21 : 21 / 35 : Terminology : Locale

Terminology

Locale

Below the VirtualUniverse object is a Locale object. The Locale object defines the origin, in high-resolution coordinates, of its attached branch graphs. A virtual universe may contain as many Locales as needed. By default, a single Locale object is defined with its origin at (0.0, 0.0, 0.0).


Slide 22 : 22 / 35 : Terminology : Branch graph

Terminology : BranchGroup

Branch graph

A BranchGroup serves as the root of a subgraph, called a branch graph, of the scene graph. Only BranchGroup objects can attach to Locale objects.


Slide 23 : 23 / 35 : The View Branch

Terminology : The View Branch


Slide 24 : 24 / 35 : The Content Branch

Terminology : The Content Branch


Slide 25 : 25 / 35 : Terminology : Live

Terminology

Live


Slide 26 : 26 / 35 : Terminology : Compiled

Terminology

Compiled


Slide 27 : 27 / 35 : Building Applications and Applets

Building Applications and Applets

Applications require a main method:

public static void main( String[] args ) 

Applets require a public constructor:

public classname( )

To make an application work as an applet too, write the main method to use MainFrame

public static void main( String[] args )	{
   new MainFrame( new HelloUniverse( ), 640, 480 );
}


Slide 28 : 28 / 35 : The View Branch again

The View Branch again


Slide 29 : 29 / 35 : Get a Canvas3D

Get a Canvas3D, and set up  the Scene Graph

How to get a good GraphicsConfiguration to construct a Canvas3D ?

GraphicsConfigTemplate3D gcTemplate = new GraphicsConfigTemplate3D();

GraphicsEnvironment local = GraphicsEnvironment.getLocalGraphicsEnvironment();
// there could be more than one screen
GraphicsDevice screen = local.getDefaultScreenDevice();

GraphicsConfiguration configuration = screen.getBestConfiguration(gcTemplate);

// Get a Canvas3D and call its constructor with the configuration
mainCanvas3D = new Canvas3D(configuration);

// Add the canvas to a JPanel (which is part of a JFrame ...)
mainCanvas3D.setSize(200,200);   // and / or
setLayout(new Border Layout)
add("Center", mainCanvas3D)

// The general Scene graph
VirtualUniverse myUniverse = new VirtualUniverse();
Locale myLocale = new Locale(myUniverse);

// Create the two main branches (with your functions)
BranchGroup myScene = myCreateSceneBranch();
BranchGroup myView = myCreateViewBranch(mainCanvas3D); // a canvas3D is needed here !

// Add the two branches to the Locale ... AT THE END !!!
myLocale.addBranchGraph(myView);
myLocale.addBranchGraph(myScene);

 


Slide 30 : 30 / 35 : Construct the View Branch

Construct the View Branch

Again : SimpleUniverse is not part of the Java3D API

One could use instead home made function ...

 


Slide 31 : 31 / 35 : Construct the View Branch (2)

Construct the View Branch (2)

/**
* This function builds the view branch of the scene
* graph.  It creates a branch group and then creates the
* necessary view elements to give a useful view of our
* content.
* @param c Canvas3D that will display the view
* @return BranchGroup that is the root of the view elements
*/
    protected BranchGroup myCreateViewBranch(Canvas3D c) {
        //This is the root of our view branch
        BranchGroup viewBranch = new BranchGroup();
		
        //The transform that will move our view
        //back 5 units along the z-axis
        Transform3D viewXfm = new Transform3D();
        viewXfm.set(new Vector3f(0.0f,0.0f,5.0f));
		
        //The transform group that will be the parent
        //of our view platform elements
        TransformGroup viewXfmGroup = new TransformGroup(viewXfm);
        viewTG.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
		
        // Deafult View and ViewPlateform
        ViewPlatform myViewPlatform = new ViewPlatform();
        View myView = new View();
		
        //Next the physical elements are created
        PhysicalBody myBody = new PhysicalBody();
        PhysicalEnvironment myEnvironment = new PhysicalEnvironment();
		
        //Then we put it all together
        myView.addCanvas3D(c);
        myView.setPhysicalBody(myBody);
        myView.setPhysicalEnvironment(myEnvironment);
		
        myView.attachViewPlatform(myViewPlatform);
		
        viewXfmGroup.addChild(myViewPlatform);
        viewBranch.addChild(viewXfmGroup);


        return viewBranch;
	}


Slide 32 : 32 / 35 : The View Branch

The View Branch again (2)


Slide 33 : 33 / 35 : Construct the Content Branch

Construct the Content Branch

Construct a content branch graph (or graphs), making:

In this example there are two branch graphs and, thus, two BranchGroup nodes. Attached to the left BranchGroup are two subgraphs. One subgraph consists of a user-extended Behavior leaf node. The Behavior node contains Java code for manipulating the transformation matrix associated with the object's geometry.
The other subgraph in this BranchGroup consists of a TransformGroup node that specifies the position (relative to the Locale), orientation, and scale of the geometric objects in the virtual universe. A single child, a Shape3D leaf node, refers to two component objects: a Geometry object and an Appearance object. The Geometry object describes the geometric shape of a 3D object (a cube in our simple example). The Appearance object describes the appearance of the geometry (color, texture, material reflection characteristics, and so forth).


Slide 34 : 34 / 35 : The Content Branch

The Content Branch again


Slide 35 : 35 / 35 : Application Code Flow

Application Code Flow

Optionally spawn threads