< Zurück | Inhalt | Weiter >

From PlatformTest.java


//This method creates the SimpleUniverse View and ViewPlatform

//scenegraph elements to create an avatar that has an associated

//Canvas3D for rendering, a PlatformGeometry, ViewerAvatar, and a

//KeyNavigator to allow movement of the ViewerAvatar with the

//keyboard.

ViewingPlatform createViewer( Canvas3D c, String szName, Color3f objColor, double x, double z )

{

//create a Viewer and attach to its canvas. A Canvas3D can

//only be attached to a single Viewer. Viewer viewer2 = new Viewer( c );


//create a ViewingPlatform with 1 TransformGroup above the

//ViewPlatform

ViewingPlatform vp2 = new ViewingPlatform( 1 );


//create and assign the PlatformGeometry to the Viewer vp2.setPlatformGeometry( createPlatformGeometry( szName ) );


//create and assign the ViewerAvatar to the Viewer viewer2.setAvatar( createViewerAvatar( szName, objColor ) );


//set the initial position for the Viewer Transform3D t3d = new Transform3D(); t3d.setTranslation( new Vector3d( x, 0, z ) ); vp2.getViewPlatformTransform().setTransform( t3d );


//set capabilities on the TransformGroup so that the

//KeyNavigatorBehavior can modify the Viewer's position vp2.getViewPlatformTransform().setCapability(

TransformGroup.ALLOW_TRANSFORM_WRITE ); vp2.getViewPlatformTransform().setCapability(

TransformGroup.ALLOW_TRANSFORM_READ );


//attach a navigation behavior to the position of the viewer KeyNavigatorBehavior key = new KeyNavigatorBehavior(

vp2.getViewPlatformTransform() ); key.setSchedulingBounds( m_Bounds ); key.setEnable( false );


//add the KeyNavigatorBehavior to the ViewingPlatform vp2.addChild( key );


//set the ViewingPlatform for the Viewer viewer2.setViewingPlatform( vp2 );


return vp2;

}


//creates and positions a simple Cone to represent the Viewer.

//The Cone is aligned and scaled such that it is similar to a 3D

//"turtle".

ViewerAvatar createViewerAvatar( String szText, Color3f objColor )

{

ViewerAvatar viewerAvatar = new ViewerAvatar();


//rotate the Cone so that it is lying down and the sharp end

//is pointed toward the Viewer's field of view. TransformGroup tg = new TransformGroup();

Transform3D t3d = new Transform3D();

t3d.setEuler( new Vector3d( Math.PI / 2.0, Math.PI, 0 ) ); tg.setTransform( t3d );


//create appearance and material for the Cone Appearance app = new Appearance();

Color3f black = new Color3f(0.4f, 0.2f, 0.1f); app.setMaterial(new Material(objColor, black, objColor,

black, 90.0f));


//create the Primitive and add to the parent BranchGroup tg.addChild( new Cone( 1, 3, Primitive.GENERATE_NORMALS, app ) ); viewerAvatar.addChild( tg );


return viewerAvatar;

}


//create a simple Raster text label used to help identify

//the viewer.

PlatformGeometry createPlatformGeometry( String szText )

{

PlatformGeometry pg = new PlatformGeometry(); pg.addChild( createLabel( szText, 0f, 2f, 0f ) );


return pg;

}


//creates a simple Raster text label (similar to Text2D) private Shape3D createLabel( String szText, float x,

float y, float z )

{

BufferedImage bufferedImage = new BufferedImage( 25, 14, BufferedImage.TYPE_INT_RGB );

Graphics g = bufferedImage.getGraphics(); g.setColor( Color.white );

g.drawString( szText, 2, 12 );


ImageComponent2D imageComponent2D = new ImageComponent2D( ImageComponent2D.FORMAT_RGB, bufferedImage );


//create the Raster for the image

javax.media.j3d.Raster renderRaster = new javax.media.j3d.Raster( new Point3f( x, y, z ), javax.media.j3d.Raster.RASTER_COLOR,

0, 0,

bufferedImage.getWidth(), bufferedImage.getHeight(), imageComponent2D,

null );


return new Shape3D( renderRaster );

}


image


6.3.1 Avatars and platform geometry

The SimpleUniverse class allows you to attach geometry to the viewer of your 3D scene. There are two methods of attaching viewer geometry using the SimpleUniverse class: ViewingPlatform.setPlatformGeometry or Viewer.setAvatar. The ViewingPlatform can be accessed from the SimpleUniverse by calling getViewingPlatform or calling getViewer to access the Viewer object. There does not seem to be any difference between these two methods.

There is nothing magical about the geometry that represents the viewer; it may be easier, and more consistent in your application, to represent the viewer using a separate BranchGroup attached to the root of your scenegraph. SimpleUniverse is defined in the Java 3D utilities package (com.sun.java.j3d.utils) along with the Viewer, ViewerAvatar, and ViewingPlatform classes. These classes are merely defined for convenience; it may be safer to use the core Java 3D scenegraph management classes.


That said, here is a short example of using setPlatformGeometry to assign geometry for the Viewer; the output is shown in figures 6.6–6.7.


image


Figure 6.6 A frame rendered by AvatarTest. The viewer’s avatar is the large cube to the right of the frame

image


Figure 6.7 A frame rendered by AvatarTest. The viewer’s avatar is the large cube in the center of the frame