< Zurück | Inhalt | Weiter >

Based on Java3dApplet.java


//create the ViewPlatform BranchGroup BranchGroup vpBranchGroup = new BranchGroup();


//create a TransformGroup to scale the ViewPlatform

//(and hence View)

TransformGroup tg = new TransformGroup();


//create the ViewPlatform ViewPlatform vp = new ViewPlatform();

vp.setViewAttachPolicy( View.RELATIVE_TO_FIELD_OF_VIEW );


//attach the ViewPlatform to the TransformGroup tg.addChild( vp );


//attach the TransformGroup to the BranchGroup vpBranchGroup.addChild( tg );


//finally, add the ViewPlatform BranchGroup to the Locale locale.addBranchGraph( vpBranchGroup );


image


Note that the TransformGroup created just before the ViewPlatform can be used to scale, translate, or rotate the scene rendered by the View attached to the ViewPlatform. For example:


//Move the camera BACK a little. Note that Transformation

//matrices above the ViewPlatform are inverted by the View

//renderer prior to rendering. By moving the camera back 20

//meters, you can see geometry objects that are positioned at 0,0,0. Transform3D t3d = new Transform3D();

t3d.setTranslation( new Vector3d( 0.0, 0.0, 20.0 ) ); tg.setTransform( t3d );


Now we need to create the View object itself and attach it to the ViewPlatform that was added to the scenegraph.


//create the View object View view = new View();

//create the PhysicalBody and PhysicalEnvironment for the View

//and attach to the View

PhysicalBody pb = new PhysicalBody(); PhysicalEnvironment pe = new PhysicalEnvironment(); view.setPhysicalEnvironment( pe ); view.setPhysicalBody( pb );


//attach the View to the ViewPlatform view.attachViewPlatform( vp );


//set the near and far clipping planes for the View view.setBackClipDistance( 110 );

view.setFrontClipDistance( 10 );


Finally, create a Canvas3D component (an AWT object) and add it to the View’s list of Canvases to be rendered into.


//create the Canvas3D that the View will render into.

//get the graphics capabilities of the system and create

//the best Canvas3D possible.

GraphicsConfigTemplate3D gc3D = new GraphicsConfigTemplate3D(); gc3D.setSceneAntialiasing( GraphicsConfigTemplate.PREFERRED ); GraphicsDevice gd[] = GraphicsEnvironment.

getLocalGraphicsEnvironment().getScreenDevices();


Canvas3D c3d = new Canvas3D( gd[0].getBestConfiguration( gc3D ) );


//set the size of the Canvas3D c3d.setSize( 512, 512 );


//add the Canvas3D to the View so that it is rendered into view.addCanvas3D( c3d );


//add the Canvas3D component to a parent AWT or Swing Panel add( c3d );