< Zurück | Inhalt | Weiter >

From KeyNavigateTest.java


//walls are black pixels

private final int m_ColorWall = new Color( 0,0,0 ).getRGB();


//guards are red pixels

private final int m_ColorGuard = new Color( 255,0,0 ).getRGB();


//lights are yellow pixels

private final int m_ColorLight = new Color( 255,255,0 ).getRGB();


//bookcases are green pixels

private final int m_ColorBookcase = new Color( 0,255,0 ).getRGB();


//water is blue pixels

private final int m_ColorWater = new Color( 0,0,255 ).getRGB();


image


The createMap routine loops over the pixels in the image and delegates responsibility for creating the individual elements of the 3D world to the createMapItem method.


//create elements of the Map and add to the Group g public Group createMap( Group g )

{

System.out.println( "Creating map items" );


Group mapGroup = new Group(); g.addChild( mapGroup );


//load the map image using the texture loader

Texture tex = new TextureLoader( m_szMapName, this).getTexture(); m_MapImage = ((ImageComponent2D) tex.getImage( 0 )).getImage();


//query the size of the map image

float imageWidth = m_MapImage.getWidth(); float imageHeight = m_MapImage.getHeight();


//the size of our world is related to the size of the image FLOOR_WIDTH = imageWidth * 8;

FLOOR_LENGTH = imageHeight * 8;


//loop over each pixel in the image and call createMapItem 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) + "%" );

}


//finally, create the external perimeter wall all around the world createExternalWall( mapGroup );


return mapGroup;

}


The createMapItem method compares the color of the incoming pixels with the colors of the predefined classes of objects and delegates object creation as necessary.


//create an object from a pixel location based on the color of

//the pixel

void createMapItem( Group mapGroup, int nPixelX, int nPixelY )

{

//get the color of the pixel

int color = m_MapImage.getRGB( (int) nPixelX, (int) nPixelY );


//compare with our defined colors and delegate the object creation if( color == m_ColorWall )

createWall( mapGroup, nPixelX, nPixelY );


else if( color == m_ColorGuard ) createGuard( mapGroup, nPixelX, nPixelY );

else if( color == m_ColorLight ) createLight( mapGroup, nPixelX, nPixelY );


else if( color == m_ColorBookcase ) createBookcase( mapGroup, nPixelX, nPixelY );


else if( color.hashCode() == m_ColorWater.hashCode() ) createWater( mapGroup, nPixelX, nPixelY );

}


For example, the createWall method is used to create a cuboidal section of wall every time a black pixel is encountered.


//create a section of wall at a given pixel location

void createWall( Group mapGroup, int nPixelX, int nPixelY )

{

//get the world coordinates for the center of the pixel location Point3d point = convertToWorldCoordinatesPixelCenter(

nPixelX, nPixelY );


//use a shared Appearance for all Wall objects to minimize

//texture memory

if( m_WallAppearance == null ) m_WallAppearance = new Appearance();


//get the size in world coordinates of a pixel in the image Vector3d squareSize = getMapSquareSize();


//create a Cuboid object that models a section of Wall and

//assign an Appearance. Cuboid is defined in the

//org.selman.java3d.book package

Cuboid wall = new Cuboid( this, mapGroup, ComplexObject.GEOMETRY |

ComplexObject.TEXTURE );

wall.createObject( m_WallAppearance,

new Vector3d( point.x, m_kFloorLevel, point.z ), new Vector3d( squareSize.x/2, m_kCeilingHeight/2, squareSize.z/2), "wall.gif", null, null );

}


Storing the world description in an image is very convenient and allows several worlds to be quickly generated using a standard bitmap editor. Three maps are included with the example: small_map.gif (32 × 32 pixels), large_map (64 × 64 pixels), and huge_map (128 × 128 pixels).