Creating the Scene


Using Loader3D

Loader3D can be called in two ways:

     java -cp "%CLASSPATH%;ncsa\portfolio.jar Loader3D" <filename>

or:

     java -cp "%CLASSPATH%;ncsa\portfolio.jar Loader3D" -c <filename>

The application searches the models/ subdirectory for the filename and loads the file. If the -c option is included, Loader3D will attempt to load the text file replaceable Coords.txt, which contains translation, rotation, and scaling values (called coords data) that should be applied to the model.

Figure 16-17 shows the Coolrobo.3ds model initially loaded into the application.

Figure 16-17. Coolrobo.3ds first loaded


Figure 16-18 shows the model after it has been moved, rotated, and scaled in various ways.

The user's viewpoint has been moved in Figures 16-17 and 16-18 to make the images bigger on-screen. The changes to the robot can be observed by comparing the model to the red square in both figures, which is centered at (0, 0) on the XZ plane.

The bottom half of the GUI pane in Figure 16-18 shows the current configuration: the (x, y, z) position is (-1.9, 3.9, 0), which is the distance of the model's center from its starting point. The rotation values are (0, 70, 0), which means a 70-degree positive rotation around the y-axis. The model has been scaled by a factor of 2.594.

Figure 16-18. Coolrobo.3ds after manipulation


When the Save Coords button is pressed, the current coords data is saved to a text file in the subdirectory models/. The contents of the file generated for Coolrobo.3ds (CoolroboCoords.txt) are:

     Coolrobo.3ds     -p -1.9 3.9 0     -r 3333333     -s 2.594

The -p line gives the (x, y, z) translation, the -r line contains a series of rotation numbers (explained later), and the -s value is for scaling.

The methods defined in Loader3D are given in Figure 16-19.

Figure 16-19. Loader3D methods


Loader3D creates its GUI control panel with initGUI( ). actionPerformed( ) handles the various GUI events triggered by pressing buttons and typing in the text field. Depending on the user request, actionPerformed( ) calls movePosn( ), rotate( ), scale( ), or saveCoordsFile( ) in the WrapLoader3D class to request changes to the model's position, rotation, scaling, or to save its coords data.

Here is a fragment of the method:

     // globals     // constants for specifying moves and rotations     private static final int X_AXIS = 0;     private static final int Y_AXIS = 1;     private static final int Z_AXIS = 2;     private static final int INCR = 0;     private static final int DECR = 1;     private WrapLoader3D w3d;    // the loader canvas     public void actionPerformed(ActionEvent e)     {       if (e.getSource( ) == saveBut)   // save coord info         w3d.saveCoordFile( );       else if (e.getSource( ) == xPosLeftBut)   // an X move         w3d.movePos(X_AXIS, DECR);       else if (e.getSource( ) == xPosRightBut)         w3d.movePos(X_AXIS, INCR);       else if (e.getSource( ) == yPosLeftBut)   // a Y move         w3d.movePos(Y_AXIS, DECR);       else if (e.getSource( ) == yPosRightBut)         w3d.movePos(Y_AXIS, INCR);        ...       // more branches dealing with a Z move, X rotation,       // Y rotation, Z rotation, and scaling       showPosInfo( );   // update on-screen display       showRotInfo( );       showScale( );     }

At the end of actionPerformed( ), showPosInfo( ), showRotInfo( ), and showScale( ) communicate with WrapLoader3D to obtain the current coords data and to update the GUI display. For example, here'sshowPosInfo( ):

     // global     private JTextField xyzTF;     private void showPosInfo( )     {       Vector3d loc = w3d.getLoc( );  // get coords data       xyzTF.setText("( " + df.format(loc.x) + ", " +           df.format(loc.y) + ", " + df.format(loc.z) + " )");     }



Killer Game Programming in Java
Killer Game Programming in Java
ISBN: 0596007302
EAN: 2147483647
Year: 2006
Pages: 340

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net