Introduction to Java 3D (2) by xdu18397

VIEWS: 39 PAGES: 8

									           240-302, Computer Engineering Lab IV (Software)
                                                                                 Contents
             Introduction to Java 3D (2)                                             1.      Checkers3D Again
                                                                                     2.      The Floor
             Dr. Andrew Davison                                                      3.      Viewer Positioning and Movement
             Room 101, CoE                                                           4.      Loader Classes
             dandrew@ratree.psu.ac.th
                                                                                     5.      Behavior Objects
                                                                                     6.      Animation
                                                                                     7.      Textures
                                                                                     8.      Sound
            240-302 Comp. Eng. Lab IV. Java 3D                               1   240-302 Comp. Eng. Lab IV. Java 3D                                 2




            1. Checkers3D Again                                                  Scene Graph for Checkers3D
           The scene consists of
            – a dark green and blue tiled
              surface (and red center)
                                                                                                                                      view branch
            – labels along the X and Z axes                                                                                           not shown

            – a blue background
            – a floating sphere lit from two different
              directions
            – the user (viewer) can move through the scene
              by moving the mouse
            240-302 Comp. Eng. Lab IV. Java 3D                               3   240-302 Comp. Eng. Lab IV. Java 3D                                 4




            WrapChecker3D Constructor
                    private SimpleUniverse su;
                    private BranchGroup sceneBG; // for content branch
                    private BoundingSphere bounds;                                           createSceneGraph();
                             :                                                               initUserPosition();      // set user's viewpoint

                    public WrapCheckers3D()                                                  orbitControls(canvas3D);
                    { setLayout( new BorderLayout() );                                             // controls for moving the viewpoint
                      setOpaque( false );
                      setPreferredSize( new Dimension(PWIDTH, PHEIGHT));                     su.addBranchGraph( sceneBG );

                        GraphicsConfiguration config =                                   } // end of WrapCheckers3D()
                               SimpleUniverse.getPreferredConfiguration();
                        Canvas3D canvas3D = new Canvas3D(config);
                        add("Center", canvas3D);
                        canvas3D.setFocusable(true);
                        canvas3D.requestFocus();

                        su = new SimpleUniverse(canvas3D);
                                      :
            240-302 Comp. Eng. Lab IV. Java 3D                               5   240-302 Comp. Eng. Lab IV. Java 3D                                 6




240-302 Comp. Eng. Lab IV. Java 3D. Part 2.                                                                                                             1
            createSceneGraph()                                                     2. The Floor
                    private void createSceneGraph()
                    // initilise the scene below sceneBG
                    {
                                                                                       The floor is made of tiles created with the
                      sceneBG = new BranchGroup();                                     our ColouredTiles class, and axis labels
                      bounds = new BoundingSphere(new Point3d(0,0,0),
                                                        BOUNDSIZE);                    made with the Text2D utility class.
                        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()

            240-302 Comp. Eng. Lab IV. Java 3D                                7    240-302 Comp. Eng. Lab IV. Java 3D                          8




            Floor Branch of the Scene Graph                                        CheckerFloor Constructor
                                                   floorBG                                  // constants for various colours
                                                                                               :
                                                                                            private BranchGroup floorBG;


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


            240-302 Comp. Eng. Lab IV. Java 3D                                9    240-302 Comp. Eng. Lab IV. Java 3D                          10




                         boolean isBlue;
                         for(int z = -FLOOR_LEN/2;
                                                                                                floorBG.addChild(
                                    z <= (FLOOR_LEN/2)-1; z++) {
                                                                                                    new ColouredTiles(blueCoords, blue) );
                           isBlue = (z%2 == 0)? true : false;
                                                                                                floorBG.addChild(
                              // set colour for new row
                                                                                                    new ColouredTiles(greenCoords, green) );
                           for(int x = -FLOOR_LEN/2;
                                     x <= (FLOOR_LEN/2)-1; x++) {
                                                                                              addOriginMarker();
                             if (isBlue)
                                                                                              labelAxes();
                                createCoords(x, z, blueCoords);
                                                                                            } // end of CheckerFloor()
                             else
                                createCoords(x, z, greenCoords);
                             isBlue = !isBlue;
                           }
                                                                                            public BranchGroup getBG()
                         }
                                                                                            { return floorBG; }
                              :

            240-302 Comp. Eng. Lab IV. Java 3D                                11   240-302 Comp. Eng. Lab IV. Java 3D                          12




240-302 Comp. Eng. Lab IV. Java 3D. Part 2.                                                                                                         2
                                                                                ColouredTiles Class
             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)                                     The ColouredTiles class extends Shape3D,
             {
               // points created in counter-clockwise order                         and defines the geometry and appearance of
               Point3f p1 = new Point3f(x, 0.0f, z+1.0f);                           tiles with the same colour.
               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);
                                                                                    The geometry uses a QuadArray to represent
               coords.add(p1); coords.add(p2);
               coords.add(p3); coords.add(p4);              p4        p3            the tiles as a series of quadlilaterals.
             } // end of createCoords()                                                                                          v4        v3 v8    v7
                                                                       +x
                                                            p1    p2                                                            v1    v2 v5        v6
            240-302 Comp. Eng. Lab IV. Java 3D
                                                                 +z        13   240-302 Comp. Eng. Lab IV. Java 3D                                       14




            QuadArray Creation                                                  Filling the QuadArray
                                                                                           // coordinate data
                The constructor is:                                                        int numPoints = coords.size();
                      QuadArray(int vertexCount, int vertexFormat);                        Point3f[] points = new Point3f[numPoints];
                                                                                           coords.toArray( points );
                                                                                                    // ArrayList-->array
                                                                                           plane.setCoordinates(0, points);
                In ColouredTiles, the QuadArray plane is
                                                                                           // colour data
                created using:                                                             Color3f cols[] = new Color3f[numPoints];
                      plane = new QuadArray( coords.size(),                                for(int i=0; i < numPoints; i++)
                                      GeometryArray.COORDINATES |                            cols[i] = col;
                                       GeometryArray.COLOR_3 );                            plane.setColors(0, cols);



            240-302 Comp. Eng. Lab IV. Java 3D                             15   240-302 Comp. Eng. Lab IV. Java 3D                                       16




            Issues                                                              Unreflecting Colour
                Counter-clockwise specification of the                              You can specify a shape's colour in three
                vertices for each quad                                              ways:
                                                                                      – in the shape's material
                  – makes the top of the quad its 'front'
                                                                                                when the scene is illuminated

                Ensure that each quad is a convex, planar                             – in the shape's colouring attributes
                polygon.                                                                        used when the shape is unreflecting

                                                                                      – in the vertices of the shape's geometry
                Normals or no normals?                                                          also for unreflecting shapes (used here)

            240-302 Comp. Eng. Lab IV. Java 3D                             17   240-302 Comp. Eng. Lab IV. Java 3D                                       18




240-302 Comp. Eng. Lab IV. Java 3D. Part 2.                                                                                                                   3
            The Axes                                                               Positioning an Axis Value

                Each axis value is a Text2D object, which                               TransformGroup axisTG = new TransformGroup();
                specifies the string, colour, font, point size,                         Transform3D t3d = new Transform3D();
                                                                                        t3d.setTranslation( vertex );
                and font style:                                                            // vertex is the position for the label
                                                                                        axisTG.setTransform(t3d);

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




            240-302 Comp. Eng. Lab IV. Java 3D                              19     240-302 Comp. Eng. Lab IV. Java 3D                            20




            3. Viewer Positioning                                                  View Branch for Checkers3D

                A simple way of positioning the viewer (the
                camera) is with the lookAt() method. It
                requires:                                                        apply
                  – the viewer’s intended position;                              lookAt()
                                                                                 here
                  – the point which he is looking at;
                  – a vector specifying the upward direction.                                                                  steerTG




            240-302 Comp. Eng. Lab IV. Java 3D                              21     240-302 Comp. Eng. Lab IV. Java 3D                            22




            Code Fragment
                                                                                   Apply lookAt() positioning (inverted since
                The view branch is created by the                                  the position is relative to the viewer):
                SimpleUniverse utility class:                                           Transform3D t3d = new Transform3D();
                      su = new SimpleUniverse(canvas3D);                                steerTG.getTransform(t3d);

                                                                                        t3d.lookAt(                     new Point3d(0,5,20),
                                                                                                                        new Point3d(0,0,0),
                Access the TransformGroup node for the                                                                  new Vector3d(0,1,0) );
                ViewPlatform:                                                           t3d.invert();
                      ViewingPlatform vp = su.getViewingPlatform();
                      TransformGroup steerTG =                                          steerTG.setTransform(t3d);
                             vp.getViewPlatformTransform();


            240-302 Comp. Eng. Lab IV. Java 3D                  continued   23     240-302 Comp. Eng. Lab IV. Java 3D                            24




240-302 Comp. Eng. Lab IV. Java 3D. Part 2.                                                                                                           4
            Viewer Movement                                               4. Loader Classes

            The OrbitBehavior class allows a range of                         Loader

            moves, rotations, and zooming of the viewer                         – specifies the elements that should be loaded
            position:                                                             from a file written in a given 3D format

                  OrbitBehavior orbit =
                   new OrbitBehavior(c,OrbitBehavior.REVERSE_ALL);            Scene
                  orbit.setSchedulingBounds(bounds);                            – extracts Java 3D scene graph information from
                  ViewingPlatform vp = su.getViewingPlatform();                   the loaded file
                  vp.setViewPlatformBehavior(orbit);



            240-302 Comp. Eng. Lab IV. Java 3D                       25   240-302 Comp. Eng. Lab IV. Java 3D                        26




            Loader Subclasses                                             Other Loaders
                Lw3dLoader                                                    A list of loaders for different file formats:
                  – for Lightwave 3D scene files                                    http://www.j3d.org/utilities/loaders.html
                ObjectFile
                                                                              NCSA Portfolio
                  – for Wavefront .obj files                                    – supports a wide range of formats
                                                                                – oldish, currently unsupported, simple
                LoaderBase
                  – implements the Loader interface in a generic              See chapter 9 of my online book:
                    way to encourage the building of loaders for                    http://fivedots.coe.psu.ac.th/~ad/jg/ch9/
                    other 3D formats through subclassing
            240-302 Comp. Eng. Lab IV. Java 3D                       27   240-302 Comp. Eng. Lab IV. Java 3D                        28




            ObjLoad Demo                                                  Loader Example
                                                                               // OBJ Loader classes
                The ObjLoad.java example in the Java 3D                        import com.sun.j3d.loaders.objectfile.ObjectFile;
                                                                               import com.sun.j3d.loaders.Scene;
                demo collection shows how to load a .obj                           :

                file                                                           // load OBJ file
                                                                               ObjectFile of = new ObjectFile(ObjectFile.RESIZE);
                  – in <JAVAHOME>\demo\java3d\ObjLoad                          Scene scene = null;
                  – java ObjLoad galleon.obj                                   try {
                                                                                 scene = of.load("galleon.obj");
                                                                               }
                                                                               catch (Exception e) {
                                                                                 System.err.println(e);
                                                                                 System.exit(1);
                                                                               }
                                                                               // add loaded model to scene
                                                                               BranchGroup modelBG = scene.getSceneGroup();
                                                                               sceneBG.addChild( modelBG );
            240-302 Comp. Eng. Lab IV. Java 3D                       29   240-302 Comp. Eng. Lab IV. Java 3D                        30




240-302 Comp. Eng. Lab IV. Java 3D. Part 2.                                                                                              5
                 5. Behavior Objects                                              Some Behavior Classes
                                                               BG

                                                                                      KeyNavigatorBehavior
             Both interaction and animation
                                                                                      MouseBehavior
             are specified with Behavior objects
                                                               TG
             A Behavior object changes the scene                                      User-defined Behavior classes
                                                                                        – triggered by WakeupCondition objects
             graph in response to events                              Behavior

               – key presses, mouse moves,                                        – PickMouseBehavior
                                                              Shape
                 object collisions,                                               – Interpolator
                 passage of time, etc.


                 240-302 Comp. Eng. Lab IV. Java 3D                          31   240-302 Comp. Eng. Lab IV. Java 3D                          32
          Interactions




                 WakeupConditions                                                 Picking
                                                                                                                       Object in
                         mouse, keyboard input                                                                         the world
                         collision
                         time and frame change                                                   Mouse
                                                                                    User         pointer
                         object movement                                                                                           Pick ray
                         camera movement                                                                    Image
                         sensor activity (input devices)                                                     plane




                 240-302 Comp. Eng. Lab IV. Java 3D                          33   240-302 Comp. Eng. Lab IV. Java 3D                          34




                 6. Animation                                                     Alpha

                         Animation is implemented using
                         time-based interpolation (or morphing)
                         – time is specified using the Alpha class




                 240-302 Comp. Eng. Lab IV. Java 3D                          35   240-302 Comp. Eng. Lab IV. Java 3D                          36




240-302 Comp. Eng. Lab IV. Java 3D. Part 2.                                                                                                        6
            Interpolator Behavior                                          Interpolator Examples

                Position, Rotation, Scale
                  – PositionInterpolator, RotationInterpolator,
                    ScaleInterpolator

                Color and Transparency
                  – ColorInterpolator
                  – TransparencyInterpolator

                Object
                  – SwitchValueInterplator




            240-302 Comp. Eng. Lab IV. Java 3D                    37        240-302 Comp. Eng. Lab IV. Java 3D                               38




            Morphing                                                       7. Textures
                                                                                   Create the illusion of detail by wraping a
                Morphing is the process of gradually                               complex image (texture) around a simple
                deforming a shape into another one.                                geometry
                For example:                                                         – realism is increased without the need for a
                  – changing a pyramid into a cube                                     complex shape
                  – changing a cat into a dog
                  – making a hand grasp

                See the Morph class

            240-302 Comp. Eng. Lab IV. Java 3D                    39        240-302 Comp. Eng. Lab IV. Java 3D                   continued   40
                                                                       Texture Mapping




                                                                           8. Sound
                2D and 3D textures are supported
                                                                              Sounds are obtained from audio files
                Textures an be attached to shapes in a                          – typically “.au” or “.wav”
                variety of predefined ways, or linked to
                specific coordinates in the shape.                            Possible to modify the volume, left/right
                                                                              balance, and intramural delay of the audio
                                                                              emission
                                                                                – but... this is a buggy part of Java 3D


            240-302 Comp. Eng. Lab IV. Java 3D                    41        240-302 Comp. Eng. Lab IV. Java 3D                   continued   42




240-302 Comp. Eng. Lab IV. Java 3D. Part 2.                                                                                                       7
             PointSound
               – radiate sound uniformly in all direction

             ConeSound
               – extend PointSound to create a sound that can be
                 directed along a 3D vector

             BackgroundSound
               – similar to PointSound but does not have a
                 location in space
            240-302 Comp. Eng. Lab IV. Java 3D                     43




240-302 Comp. Eng. Lab IV. Java 3D. Part 2.                             8

								
To top