Introduction to Java 3D

Document Sample
Introduction to Java 3D Powered By Docstoc
					Introduction to Java 3D




                          1
1. Java 3D Overview
 API for 3D applications
 Portable




                            2
Some Application Areas

 Scientific/Medical/Datavisualization
 Geographical information systems (GIS)
 Computer-aided design (CAD)
 Simulation
 Computer-aided education (CAE)
 Games



                                           3
The Java 3D API

 Packages   related to the Java 3D API:
  –   Core classes: javax.media.j3d
  –   Utility classes:   com.sun.j3d.utils
  –   Math classes: javax.vecmath
  –   AWT classes: javax.swing




                                             4
Introduction

 Elements   of the rendering
  – Objects
  – Camera/View
  – Canvas/Screen




                                5
Introduction

 Rendering




 Camera   / objects may move in time…



                                         6
Scene Representation

 Scene
  – Representation of elements in the virtual world
  – How to represent?
  – What to represent?




                                                      7
The Virtual Universe

 Basics
 Scene Graph
 Locales
 Content Branch
 View Branch




                       8
2. What is a Scene Graph?
 scene  graph = tree-like
 stores,3D scene
 easier than OpenGL or DirectX.




                                   9
Scene Graph Symbols
Nodes and Node Components (objects)   Arcs (object relationships)


                Group =erase head     Parent-child link
                Leaf=cone head

                Node Component             Reference

                Other objects=block head




                                                                    10
       Java3D’s SceneGraph
                                                     VitualUniverse


                                                   Locale
                      Root group
                                      BG

                                  TG                           BG
                   robot arm               environment
                         TG                TG
Group =                                         sphere                view
                    TG         base
erase head               S                  S
             TG
                        bottom arm
                     S
                  up arm
             S

                                                                             11
The Virtual Universe
Objectives
 Know   what a virtual universe and a scene
  graph is.
 Understand the basic parts and elements of
  a scene graph.
 Know the basic recipe for a Java 3D
  program.



                                               12
                 Leaf=cone head   A Typical Scene Graph




Content Branch                     View Branch
                                                          13
The Virtual Universe
Basics
A  virtual universe can be seen as a 1:1
  representation of our own universe.
 Represent structures from astronomical to
  subatomic.
  – Floating point 3D space: 2^256 (!!) for each
    x,y,z.
  – Decimal point for 1 meter at 2^128
  – There is one VU-instance only (Singleton).

                                                   14
The Virtual Universe
Locales (1)
 How   to handle the huge extend of a virtual
  universe efficiently?
 Our virtual universe contains at least one
  Locale.
 The locale is a 3D-reference point inside the
  virtual universe.



                                                  15
The Virtual Universe
Locales (2)
 Standard Locale resides at (0,0,0) in the VU.
 We can have several Locales, eg:
    – One as reference point of the swiss coordinate system
      (located in Bordeaux, France).
    – A second as the architects reference point of a building
      plan.
    – The both are related, but depending on the point of
      view it is more convenient (and precise) to work with
      one or the other.


                                                                 16
The Virtual Universe
Content Branch (1)
 Contains  all „visible“ objects of our scene.
 Contains all transformations for those
  objects (displacement, animation, ...).
 We distinguish between group nodes and
  leaf nodes (see following slide).




                                                  17
The Virtual Universe
Content Branch (2)
 Themost common
 object types
                                     Branch
                                     Group


                                                ...
                                    Transform
                                      Group


                                                ...

                       Appearance   Shape3D


                                                      18
The Virtual Universe                       VirtualUniverse

Scene Graph (1)
                                                Locale
 Scene Graph is the graphical
  representation of the objects in
  our VU.                                       Group

 Directed Acyclic Graph (Tree)                 Link

  – Nodes, arcs, parents, childs,
    leaves, references and other                 Leaf
    objects.                        Reference


                   NodeComponent           Other objects

                                                             19
The Virtual Universe
Scene Graph (2)
A  Java 3D program may have more objects
  than those in the scene graph.
 A scene graph drawing is the correct
  documentation for the 3D part of a
  Java 3D program.




                                            20
The Virtual Universe
View Branch       The Canvas3D will be
                  inserted in our application or
                  applet -> most important!




                                                   21
The Virtual Universe
Recipe for a simple Program
 1.   Create a Frame & a
      Canvas3D instance
 2.   Create a SimpleUniverse
 3.   Construct the content branch.
 4.   Insert the content branch into the Locale
      of the SimpleUniverse.




                                                  22
Hello World




              23
Hello World
Source Code
 public class HelloJava3Da extends Applet {
     public HelloJava3Da() {
         setLayout(new BorderLayout());
         Canvas3D canvas3D = new Canvas3D(null);
         add("Center", canvas3D);

         SimpleUniverse simpleU = new SimpleUniverse(canvas3D);
         simpleU.getViewingPlatform().setNominalViewingTransform();

         BranchGroup scene = createSceneGraph();
         simpleU.addBranchGraph(scene);
     }

     public BranchGroup createSceneGraph() {
         BranchGroup objRoot = new BranchGroup();
         objRoot.addChild(new ColorCube(0.4));

         return objRoot;
     }

     public static void main(String[] args) {
         Frame frame = new MainFrame(new HelloJava3Da(), 256, 256);
     }
 }
                                                                      24
Hello World
Class Diagram of HelloJava3D




                               25
Hello World
What happens when running?
while (true) {
   Process input
   Perform Behaviours //none at the moment
   Traverse scene graph and render visual objects
   if (request to exit) break
}
Cleanup and exit




                                                    26
Scene Graph




              27
       Java3D (cont.)

• The scene graph is a tree
• Elements of the scene that
have some data component,
such as viewable objects, light
sources, camera locations and
so forth are contained in the
graph nodes.




From: “The Java 3D Tutorial” by Dennis J Bouvier at java.sun.com/products/java-media/3D/collateral
and       http://www.itk.ilstu.edu/faculty/javila/ITk356/Java3D/scene_graph_basics.htm               28
Symbols representing Scene Graph Objects:
     Virtual Universe (V)

     Locale (L)

     Group (B or T)

     Leaf (Lf)

     Node Component (N)

     Other Objects




                                            29
       Scene Graph Viewing Objects


                                                              Canvas3D, Screen3D,
                                                              View, PhysicalBody,
                                                              PhysicalEnvironment.




From: “The Java 3D Tutorial” by Dennis J Bouvier at java.sun.com/products/java-media/3D/collateral
and       http://www.itk.ilstu.edu/faculty/javila/ITk356/Java3D/scene_graph_basics.htm               30
   Java3D (cont.)

Terminology         The renderer




                                   31
    Code (create robot arm)
   public void init() // applet’s init() function
     {
        setLayout(new BorderLayout());
        GraphicsConfiguration config =
           SimpleUniverse.getPreferredConfiguration();

              Canvas3D c = new Canvas3D(config);
              add("Center", c);

         // create simple universe( camera, lights, ...)
               u = new SimpleUniverse(c);

         // This will move the ViewPlatform back a bit so the
         // objects in the scene can be viewed.
         u.getViewingPlatform().setNominalViewingTransform();

         viewTrans =
               u.getViewingPlatform().getViewPlatformTransform();
         // Create a robot arm
         createSceneGraph();
         // add root group
         u.addBranchGraph(rootGroup);
                                                                     32
     }
   public void createSceneGraph()
   {
     // create root
        rootGroup = new BranchGroup();
                rootTrans = new TransformGroup();
                rootTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
                rootTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        // create bounds
        BoundingSphere bounds =
                   new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
       // create Appearance
       Appearance greenLook = new Appearance();
       ColoringAttributes greenColorAttr = new ColoringAttributes();
       greenColorAttr.setColor(new Color3f(0.0f,1.0f,0.0f));
       greenLook.setColoringAttributes(greenColorAttr);

       // create Sphere
       sphereTrans = new TransformGroup();
       sphere = new Sphere(0.1f,redLook);





                                                                                  33
   // transform sphere
   Transform3D sphereT3d = new Transform3D();
   sphereT3d.setTranslation(new Vector3f(0.5f,0.0f,0.0f));
   sphereTrans.setTransform(sphereT3d);

   // add sphere into scenegraph
   sphereTrans.addChild(sphere);
   rootTrans.addChild(sphereTrans);

    // create arm
    …..
   } // end of createSceneGraph()




                                                              34
3. A Java 3D Skeleton

 Allour Java 3D programs embed a scene
 inside a JPanel, which is part of a JFrame
  – this allows Swing GUI controls to be utilized
    along side the Java 3D panel (if necessary)


 We will develop an example called
 Checkers3D during the course of these slides.



                                                    35
  Checkers3D
 The   scene consists of
  – a dark green and blue tiled
    surface (and red center)
  – labels along the X and Z axes
  – a blue background
  – a floating sphere lit from two different
    directions
  – the user (viewer) can move through the scene
    by moving the mouse
                                                   36
Checkers3D.java
 public class Checkers3D extends JFrame
 { public Checkers3D()
   { super("Checkers3D");
     Container c = getContentPane();
     c.setLayout( new BorderLayout() );
   WrapCheckers3D w3d = new WrapCheckers3D();
            // panel holding the 3D scene
     c.add(w3d, BorderLayout.CENTER);
     setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
     pack();
     setResizable(false);    // fixed size display
     show();
   } // end of Checkers3D()

   public static void main(String[] args)
   { new Checkers3D(); }
 } // end of Checkers3D class
                                                         37
Scene Graph for Checkers3D


                         view branch
                         not shown




                                       38
Building the Scene Graph

 The VirtualUniverse, Locale, and view
 branch graph often have the same structure
 across different applications
  – programmers usually create them with the
    SimpleUniverse utility class




                                               39
WrapChecker3D Constructor
  private SimpleUniverse su;
  private BranchGroup sceneBG; // for content branch
  private BoundingSphere bounds;
           :

  public WrapCheckers3D()
  { setLayout( new BorderLayout() );
    setOpaque( false );
    setPreferredSize( new Dimension(PWIDTH, PHEIGHT));

  GraphicsConfiguration config =
           SimpleUniverse.getPreferredConfiguration();
    Canvas3D canvas3D = new Canvas3D(config);
    add("Center", canvas3D);
    canvas3D.setFocusable(true);
    canvas3D.requestFocus();

  su = new SimpleUniverse(canvas3D);
                  :                                      40
createSceneGraph();
  initUserPosition();      // set user's viewpoint

  orbitControls(canvas3D);
        // controls for moving the viewpoint

  su.addBranchGraph( sceneBG );

} // end of WrapCheckers3D()




                                                     41
createSceneGraph()
  private void createSceneGraph()
  // initilise the scene below sceneBG
  {
    sceneBG = new BranchGroup();
    bounds = new BoundingSphere(new Point3d(0,0,0),
                                      BOUNDSIZE);

    lightScene();         // add the light
    addBackground();      // add the sky
    sceneBG.addChild( new CheckerFloor().getBG() );
                          // add the floor

    floatingSphere();     // add the floating sphere

    sceneBG.compile();   // fix the scene
  } // end of createSceneGraph()

                                                       42
  1. Checkers3D Again
 The   scene consists of
  – a dark green and blue tiled
    surface (and red center)
  – labels along the X and Z axes
  – a blue background
  – a floating sphere lit from two different
    directions
  – the user (viewer) can move through the scene
    by moving the mouse
                                                   43
Scene Graph for Checkers3D


                         view branch
                         not shown




                                       44
WrapChecker3D Constructor
  private SimpleUniverse su;
  private BranchGroup sceneBG; // for content branch
  private BoundingSphere bounds;
              :

  public WrapCheckers3D()
  { setLayout( new BorderLayout() );
    setOpaque( false );
    setPreferredSize( new Dimension(PWIDTH, PHEIGHT));

   GraphicsConfiguration config =
    SimpleUniverse.getPreferredConfiguration();
   Canvas3D canvas3D = new Canvas3D(config);
   add("Center", canvas3D);
   canvas3D.setFocusable(true);
   canvas3D.requestFocus();

    su = new SimpleUniverse(canvas3D);
                       :




                                                         45
createSceneGraph();
  initUserPosition();      // set user's viewpoint

  orbitControls(canvas3D);
        // controls for moving the viewpoint

  su.addBranchGraph( sceneBG );

} // end of WrapCheckers3D()




                                                     46
createSceneGraph()
  private void createSceneGraph()
  // initilise the scene below sceneBG
  {
    sceneBG = new BranchGroup();
    bounds = new BoundingSphere(new Point3d(0,0,0),
                                                 BOUNDSIZE);

    lightScene();         // add the light
    addBackground();      // add the sky
    sceneBG.addChild( new CheckerFloor().getBG() );
            // add the BranchGroup for the floor

    floatingSphere();     // add the floating sphere

    sceneBG.compile();   // fix the scene
  } // end of createSceneGraph()




                                                               47
2. The Floor

 Thefloor is made of tiles created with the
 our ColouredTiles class, and axis labels
 made with the Text2D utility class.




                                               48
Floor Branch of the Scene Graph
                 floorBG




                           axisTG




                                    49
CheckerFloor Constructor
  // constants for various colours
     :
  private BranchGroup floorBG;


  public CheckerFloor()
  // create tiles, add origin marker,
  // then the axes labels
  {
    ArrayList blueCoords = new ArrayList()
    ArrayList greenCoords = new ArrayList();
    floorBG = new BranchGroup();
                    :




                                               50
        boolean isBlue;
        for(int z = -FLOOR_LEN/2;
                  z <= (FLOOR_LEN/2)-1; z++) {
          isBlue = (z%2 == 0)? true : false;
             // set colour for new row
          for(int x = -FLOOR_LEN/2;
                   x <= (FLOOR_LEN/2)-1; x++) {
            if (isBlue)
         createCoords(x, z, blueCoords);
            else
         createCoords(x, z, greenCoords);
            isBlue = !isBlue;
    }
}
        :




                                                  51
 floorBG.addChild(
      new ColouredTiles(blueCoords, blue) );
  floorBG.addChild(
      new ColouredTiles(greenCoords, green) );

  addOriginMarker();
  labelAxes();
} // end of CheckerFloor()



public BranchGroup getBG()
{ return floorBG; }




                                                 52
private void createCoords(int x, int z,
                                  ArrayList coords)
// Coords for a single blue or green square,
// its left hand corner at (x,0,z)
{
  // points created in counter-clockwise order
  Point3f p1 = new Point3f(x, 0.0f, z+1.0f);
  Point3f p2 = new Point3f(x+1.0f,0.0f,z+1.0f);
  Point3f p3 = new Point3f(x+1.0f, 0.0f, z);
  Point3f p4 = new Point3f(x, 0.0f, z);

  coords.add(p1); coords.add(p2);
  coords.add(p3); coords.add(p4);
} // end of createCoords()

                                                 p4        p3

                                                            +x
                                                 p1    p2
                                                      +z        53
ColouredTiles Class

 The ColouredTiles  class extends Shape3D,
 and defines the geometry and appearance of
 tiles with the same colour.

 The geometry uses a QuadArray to represent
 the tiles as a series of quadlilaterals.
                             v4    v3 v8    v7

                            v1    v2 v5    v6
                                                 54
QuadArray Creation

   The constructor is:
      QuadArray(int vertexCount, int vertexFormat);



   In ColouredTiles, the QuadArray plane is created
    using:
      plane = new QuadArray( coords.size(),
                       GeometryArray.COORDINATES |
                       GeometryArray.COLOR_3 );




                                                       55
Filling the QuadArray
   // coordinate data
   int numPoints = coords.size();
   Point3f[] points = new Point3f[numPoints];
   coords.toArray( points );
            // ArrayList-->array
   plane.setCoordinates(0, points);

   // colour data
   Color3f cols[] = new Color3f[numPoints];
   for(int i=0; i < numPoints; i++)
     cols[i] = col;
   plane.setColors(0, cols);




                                                56
Issues
 Counter-clockwise  specification of the
 vertices for each quad
  – makes the top of the quad its 'front'

       that each quad is a convex, planar
 Ensure
 polygon.

 Normals   or no normals?

                                            57
Unreflecting Colour
   You can specify a shape's colour in three ways:
    – in the shape's material
          when the scene is illuminated

    – in the shape's colouring attributes
          used when the shape is unreflecting

    – in the vertices of the shape's geometry
          also for unreflecting shapes (used here)




                                                      58
The Axes

   Each axis value is a Text2D object, which specifies
    the string, colour, font, point size, and font style:

      Text2D message =
        new Text2D("...", white, "SansSerif",
                     36, Font.BOLD );
             // 36 point bold Sans Serif




                                                            59
Positioning an Axis Value

 TransformGroup axisTG = new TransformGroup();
 Transform3D t3d = new Transform3D();
 t3d.setTranslation( vertex );
    // vertex is the position for the label
 axisTG.setTransform(t3d);

 axisTG.addChild( message );




                                                 60
Create a color cube

 Prompt  the user for an image, list unique
  colors in the image and then
 Create a sphere for each color such that:
  – The sphere has that color
  – The sphere is positioned in 3space according to
    its color
  – The color space is an RGB space.


                                                      61