codice:
/* Copyright (C) 2001 Daniel Selman */
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.util.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.image.TextureLoader;
import com.sun.j3d.utils.behaviors.keyboard.KeyNavigatorBehavior;
import org.selman.java3d.book.common.*;
public class PlatformTest extends Applet implements ActionListener
{
// table used to map the name of a Viewer to its KeyNavigatorBehavior
Hashtable m_KeyHashtable = null;
BoundingSphere m_Bounds = null;
private static int m_kWidth = 300;
private static int m_kHeight = 300;
private static final String m_szMapName = new String( "large_map.gif" );
private float FLOOR_WIDTH = 256;
private float FLOOR_LENGTH = 256;
private final int m_ColorWall = new Color( 0,0,0 ).getRGB( );
private final int m_ColorLight = new Color( 255,255,0 ).getRGB( );
private final int m_ColorBookcase = new Color( 0,255,0 ).getRGB( );
private final int m_ColorWater = new Color( 0,0,255 ).getRGB( );
private Vector3d m_MapSquareSize = null;
private Appearance m_WallAppearance = null;
private Appearance m_BookcaseAppearance = null;
private Appearance m_WaterAppearance = null;
private BufferedImage m_MapImage = null;
private final double m_kFloorLevel = -20;
private final double m_kCeilingHeight = 50;
private final double m_kCeilingLevel = m_kFloorLevel + m_kCeilingHeight;
private Vector3d m_Translation = new Vector3d( );
public PlatformTest( )
{
m_KeyHashtable = new Hashtable( );
m_Bounds = new BoundingSphere( new Point3d( 0, 0, 0 ), 100 );
// get the graphics configuration for the graphics device
GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration( );
// create the first canvas, this is the top-down view
// …
// create the third canvas, this is used for "You's" Viewer
Canvas3D c3 = new Canvas3D( config );
c3.setSize( m_kWidth, m_kHeight );
add( c3 );
// Create the simple environment
BranchGroup scene = createSceneGraph( );
// create the first Viewer, this is a static top-down view
// create a ViewingPlatform with 2 TransformGroups above the ViewPlatform
ViewingPlatform vp = new ViewingPlatform( 2 );
// create the Viewer and attach to the first canvas
Viewer viewer = new Viewer( c );
// rotate and position the first Viewer above the environment
// …
MultiTransformGroup mtg = vp.getMultiTransformGroup( );
mtg.getTransformGroup( 0 ).setTransform( t3d );
// create a SimpleUniverse from the ViewingPlatform and Viewer
SimpleUniverse u = new SimpleUniverse( vp, viewer );
// add the geometry to the scenegraph
u.addBranchGraph( scene );
// add two more Viewers to the scenegraph
u.getLocale( ).addBranchGraph( createViewer( c3, "You", new Color3f( 1.0f, 0.1f, 0.1f ), 2, -8 ) );
}
protected int getCanvas3dWidth( Canvas3D c3d )
{
return m_kWidth - 10;
}
protected int getCanvas3dHeight( Canvas3D c3d )
{
return m_kHeight - 10;
}
protected double getBackClipDistance( )
{
return 20.0;
}
protected double getFrontClipDistance( )
{
return 0.05;
}
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 TransformGroups 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( true );
// add the KeyNavigatorBehavior to the ViewingPlatform
vp2.addChild( key );
// set the ViewingPlatform for the Viewer
viewer2.setViewingPlatform( vp2 );
return vp2;
}
// create a tiled environment from -12 to +12. The environment
// is created from a QuadArray. The environment is surrounded by a ColorCube
// "wall" that is 2 units high (from Z = -1 to Z = 1).
public BranchGroup createSceneGraph( )
{
BranchGroup objRoot = new BranchGroup( );
TransformGroup objTrans = new TransformGroup( );
objTrans.setCapability( TransformGroup.ALLOW_TRANSFORM_WRITE );
objTrans.setCapability( TransformGroup.ALLOW_TRANSFORM_READ );
createMap( objTrans );
createFloor( objTrans );
objRoot.addChild( objTrans );
return objRoot;
}
public Group createFloor( Group g )
{
System.out.println( "Creating floor" );
Group floorGroup = new Group( );
Land floorTile = null;
// use a shared Appearance so we only store 1 copy of the texture
Appearance app = new Appearance( );
g.addChild( floorGroup );
final double kNumTiles = 6;
for( double x = -FLOOR_WIDTH + FLOOR_WIDTH/(2 * kNumTiles); x < FLOOR_WIDTH; x = x + FLOOR_WIDTH/kNumTiles )
{
for( double z = -FLOOR_LENGTH + FLOOR_LENGTH/(2 * kNumTiles); z < FLOOR_LENGTH; z = z + FLOOR_LENGTH/kNumTiles )
{
floorTile = new Land( this, g, ComplexObject.GEOMETRY | ComplexObject.TEXTURE );
floorTile.createObject( app,
new Vector3d( x,m_kFloorLevel,z ),
new Vector3d( FLOOR_WIDTH/(2*kNumTiles),1, FLOOR_LENGTH/(2*kNumTiles) ),
"floor.gif", null, null );
}
}
return floorGroup;
}
public Group createMap( Group g )
{
System.out.println( "Creating map items" );
Group mapGroup = new Group( );
g.addChild( mapGroup );
Texture tex = new TextureLoader( m_szMapName, this ).getTexture( );
m_MapImage = ((ImageComponent2D) tex.getImage( 0 )).getImage( );
float imageWidth = m_MapImage.getWidth( );
float imageHeight = m_MapImage.getHeight( );
FLOOR_WIDTH = imageWidth * 8;
FLOOR_LENGTH = imageHeight * 8;
for( int nPixelX = 1; nPixelX < imageWidth-1; nPixelX++ )
{
for( int nPixelY = 1; nPixelY < imageWidth-1; nPixelY++ )
createMapItem( mapGroup, nPixelX, nPixelY );
float percentDone = 100 * (float) nPixelX / (float) (imageWidth-2);
System.out.println( " " + (int) (percentDone) + "%" );
}
createExternalWall( mapGroup );
return mapGroup;
}
void createMapItem( Group mapGroup, int nPixelX, int nPixelY )
{
int color = m_MapImage.getRGB( (int) nPixelX, (int) nPixelY );
if( color == m_ColorWall )
createWall( mapGroup, nPixelX, nPixelY );
else if( color == m_ColorLight )
createLight( mapGroup, nPixelX, nPixelY );
else if( color == m_ColorBookcase )
createBookcase( mapGroup, nPixelX, nPixelY );
else if( color == m_ColorWater )
createWater( mapGroup, nPixelX, nPixelY );
}
Vector3d getMapSquareSize( )
{
if( m_MapSquareSize == null )
{
double imageWidth = m_MapImage.getWidth( );
double imageHeight = m_MapImage.getHeight( );
m_MapSquareSize = new Vector3d( 2 * FLOOR_WIDTH / imageWidth, 0, 2 * FLOOR_LENGTH / imageHeight );
}
return m_MapSquareSize;
}
void createWall( Group mapGroup, int nPixelX, int nPixelY )
{
// … codice per la creazione dei muri
}
void createExternalWall( Group mapGroup )
{
// … codice per la creazione dei muri esterni
}
void createLight( Group mapGroup, int nPixelX, int nPixelY )
{
// … codice per la creazione delle torce
}
void createBookcase( Group mapGroup, int nPixelX, int nPixelY )
{
Point3d point = convertToWorldCoordinatesPixelCenter( nPixelX, nPixelY );
if( m_BookcaseAppearance == null )
m_BookcaseAppearance = new Appearance( );
Vector3d squareSize = getMapSquareSize( );
Cuboid bookcase = new Cuboid( this, mapGroup, ComplexObject.GEOMETRY | ComplexObject.TEXTURE );
bookcase.createObject( m_BookcaseAppearance,
new Vector3d( point.x, m_kFloorLevel, point.z ),
new Vector3d( squareSize.x/2, m_kCeilingHeight/2.7, squareSize.z/2 ),
"bookcase.gif", null, null );
}
void createWater( Group mapGroup, int nPixelX, int nPixelY )
{
// … codice per la creazione delle pozzanghere
}
…
}
Point3d convertToWorldCoordinatesPixelCenter( int nPixelX, int nPixelY )
{
Point3d point3d = convertToWorldCoordinate( nPixelX, nPixelY );
Vector3d squareSize = getMapSquareSize( );
point3d.x += squareSize.x/2;
point3d.z += squareSize.z/2;
return point3d;
}
Point3d convertToWorldCoordinate( int nPixelX, int nPixelY )
{
Point3d point3d = new Point3d( );
Vector3d squareSize = getMapSquareSize( );
// range from 0 to 1
point3d.x = nPixelX * squareSize.x;
point3d.x -= FLOOR_WIDTH;
point3d.z = nPixelY * squareSize.z;
point3d.z -= FLOOR_LENGTH;
point3d.y = 0;
return point3d;
}
Point2d convertToMapCoordinate( Vector3d worldCoord )
{
Point2d point2d = new Point2d( );
Vector3d squareSize = getMapSquareSize( );
point2d.x = (worldCoord.x + FLOOR_WIDTH)/ squareSize.x;
point2d.y = (worldCoord.z + FLOOR_LENGTH)/ squareSize.z;
return point2d;
}
ViewerAvatar createViewerAvatar( String szText, Color3f objColor )
{
// … codice per la creazione del “cono” che si vede dall’alto
}
PlatformGeometry createPlatformGeometry( String szText )
{
// … codice per rasterizzare il nome dell’avatar
}
private Shape3D createLabel( String szText, float x, float y, float z )
{
// … altro codice per rasterizzare il nome dell’avatar
}
public void actionPerformed( ActionEvent event )
{
KeyNavigatorBehavior key = (KeyNavigatorBehavior) m_KeyHashtable.get( event.getActionCommand( ) );
Object[] keysArray = m_KeyHashtable.values( ).toArray( );
}
public static void main( String[] args )
{
new MainFrame( new PlatformTest( ), (int) (m_kWidth * 3.5), (int) (m_kHeight * 1.1) );
}
}
Per ora è tutto, tornerò a disturbarvi presto... intanto auguro a tutti buona giornata.