Title: Network Blitz
Hi Friends:
 
I am sorry for sending this letter again as I have not got any idea yet. 
 
I am trying to render an image with raster.  However, the rastered image seems not being able to locate at the center of the screen and also it's rotating axis is centered at (x=0, y=0, z=0).  I have attached my file here for your reference, which renders an image and a ColorCube (ColorCube is for comparison purpose).  When you press the left and right keys, the image should be able to move left and right.  When you press the up and down keys, the image should be able to move forwards and backwards.  When you press any of those keys with Control key pressed, the image should move in some more types (try it to see what happens!).  However, this is true only for the ColorCube.  The raster image moves in a funny way.  Could you please let me know what I should do to get the rastered image working exactly as the ColorCube?  Thank you!
 
 
Guang Bin Liu
VTHRC, University of Queensland
St Lucia, Brisbane
Qld 4067
Australia

package testraster;

import com.sun.j3d.loaders.objectfile.ObjectFile;
import com.sun.j3d.loaders.*;
import java.applet.Applet;

import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import javax.swing.ImageIcon;
import java.io.*;
import com.sun.j3d.utils.image.TextureLoader;
import com.sun.j3d.utils.behaviors.vp.*;
import java.awt.image.BufferedImage;
import java.applet.Applet;
import java.awt.*;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.loaders.ParsingErrorException;
import com.sun.j3d.loaders.IncorrectFormatException;

import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.j3d.utils.behaviors.mouse.MouseRotate;

public class TestRaster extends Applet
{
  VirtualUniverse universe;
  Locale locale;
  TransformGroup vpTrans;
  View view;
  Bounds bounds;
  PhysicalBody body;
  static Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

  public BranchGroup createSceneGraph()
    {
     // Create the root of the branch graph; this will be returned
        BranchGroup objRoot = new BranchGroup();
        TransformGroup geoTG = new TransformGroup();
        geoTG.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        geoTG.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);

        BoundingSphere bounds =
                        new BoundingSphere(new Point3d(0.0,0.0,0.0), 1000.0);

        MouseRotate mouseBeh = new MouseRotate(geoTG);
        geoTG.addChild(mouseBeh);
        geoTG.addChild(new ColorCube(0.6));
        TextureLoader icon=new TextureLoader("earth.jpg", null);
        ImageComponent2D image=icon.getImage();
        Raster raster=new Raster();
        raster.setImage(image);
        raster.setCapability(Raster.ALLOW_IMAGE_WRITE|Raster.ALLOW_IMAGE_READ);
        raster.setCapability(Raster.ALLOW_SIZE_WRITE|Raster.ALLOW_SIZE_READ);
        raster.setType(Raster.RASTER_COLOR );

        raster.setPosition( new Point3f(-(float)(image.getWidth()/screenSize.getWidth()),
                                        (float)(image.getHeight()/screenSize.getHeight()),
                                        0.8f ));
        raster.setSize( image.getWidth(), image.getHeight() );

        Shape3D shape=new Shape3D(raster, new Appearance());
        geoTG.addChild(shape);
        Alpha rotationAlpha = new Alpha(-1, 6000);
        RotationInterpolator rotator =
                 new RotationInterpolator(rotationAlpha, geoTG);
        rotator.setSchedulingBounds(bounds);
        rotator.getAxisOfRotation().rotX(Math.PI/2);
        rotator.getAxisOfRotation().rotY(Math.PI/2);

       geoTG.addChild(rotator);
       objRoot.addChild(geoTG);
       mouseBeh.setSchedulingBounds(bounds);

        return objRoot;
     }

   public BranchGroup createViewGraph()
   {
        BranchGroup objRoot = new BranchGroup();
        Transform3D t = new Transform3D();
        t.setTranslation(new Vector3f(0.0f, 0.0f,10.0f));
        ViewPlatform vp = new ViewPlatform();

        vpTrans = new TransformGroup();
        vpTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        vpTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
        vpTrans.setTransform(t);

         DisparityBehavior db = new DisparityBehavior(body);
         NavigationBehavior nav = new NavigationBehavior(vpTrans);

         db.setSchedulingBounds(bounds);
         nav.setSchedulingBounds(bounds);

         vpTrans.addChild(nav);
         vpTrans.addChild(db);
         vpTrans.addChild(vp);
         view.attachViewPlatform(vp);

         objRoot.addChild(vpTrans);
         return objRoot;
   }

    public TestRaster()
    {
        this.setLayout(new BorderLayout());
        GraphicsConfigTemplate3D g3d = new GraphicsConfigTemplate3D();
        g3d.setStereo(GraphicsConfigTemplate3D.REQUIRED);
        GraphicsConfiguration gc = GraphicsEnvironment.
                                   getLocalGraphicsEnvironment().
                                   getDefaultScreenDevice().
                                   getBestConfiguration(g3d);

        Canvas3D c = new Canvas3D(gc);
        add("Center", c);
        universe = new VirtualUniverse();

        locale = new Locale(universe);
        body = new PhysicalBody();
        PhysicalEnvironment environment = new PhysicalEnvironment();
        body.setLeftEyePosition(new Point3d(-0.001, -0.0, -0.0));
        body.setRightEyePosition(new Point3d(0.001, 0.0, 0.0));
        view = new View();
        Background bk=new Background();
        bk.setColor(0.6f, 0.6f, 0.6f);
        c.setStereoEnable(true);
        view.addCanvas3D(c);

        view.setPhysicalBody(body);
        view.setPhysicalEnvironment(environment);

        // Create a simple scene and attach it to the virtual universe
         bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
         BranchGroup scene = createSceneGraph();
         BranchGroup vgraph = createViewGraph();

        locale.addBranchGraph(vgraph);
        locale.addBranchGraph(scene);
    }
/*
  The following allows HelloUniverse to be run as an application
  as well as an applet
*/
    public static void main(String[] args)
    {
        new MainFrame(new TestRaster(),
                          screenSize.width,
                          screenSize.height-10);
    }
}
package testraster;

import java.awt.AWTEvent;
import java.awt.event.*;
import java.util.Enumeration;
import javax.media.j3d.*;
import javax.vecmath.*;

import com.sun.j3d.utils.universe.*;

public class NavigationBehavior extends Behavior
{
  protected static final double FAST_SPEED = 2.0;
  protected static final double NORMAL_SPEED = 1.0;
  protected static final double SLOW_SPEED = 0.5;

  private TransformGroup transformGroup;
  private Transform3D transform3D;
  private WakeupCondition keyCriterion;

  private final static double TWO_PI = (2.0 * Math.PI);
  private double rotateXAmount = Math.PI / 16.0;
  private double rotateYAmount = Math.PI / 16.0;
  private double rotateZAmount = Math.PI / 16.0;

  private double moveRate = 0.3;
  private double speed = NORMAL_SPEED;

  private int forwardKey = KeyEvent.VK_UP;
  private int backKey = KeyEvent.VK_DOWN;
  private int leftKey = KeyEvent.VK_LEFT;
  private int rightKey = KeyEvent.VK_RIGHT;
  private int QuitKey = KeyEvent.VK_ESCAPE;

  public NavigationBehavior( TransformGroup tg )
  {
    transformGroup = tg;
    transform3D = new Transform3D();
  }

  public void initialize()
  {
    WakeupCriterion[] keyEvents = new WakeupCriterion[2];
    keyEvents[0] = new WakeupOnAWTEvent( KeyEvent.KEY_PRESSED );
    keyEvents[1] = new WakeupOnAWTEvent( KeyEvent.KEY_RELEASED );
    keyCriterion = new WakeupOr( keyEvents );
    wakeupOn( keyCriterion );
  }



  public void processStimulus( Enumeration criteria )
  {
    WakeupCriterion wakeup;
    AWTEvent[] event;

    while( criteria.hasMoreElements() )
    {
      wakeup = (WakeupCriterion) criteria.nextElement();
      if( !(wakeup instanceof WakeupOnAWTEvent) )
        continue;

      event = ((WakeupOnAWTEvent)wakeup).getAWTEvent();
      for( int i = 0; i < event.length; i++ )
      {
        if( event[i].getID() == KeyEvent.KEY_PRESSED )
        {
          processKeyEvent((KeyEvent)event[i]);
        }
      }
    }
    wakeupOn( keyCriterion );
  }

  protected void processKeyEvent(KeyEvent event)
  {
    int keycode = event.getKeyCode();

    if(event.isShiftDown()) speed = FAST_SPEED;
    else speed = NORMAL_SPEED;
    if( event.isAltDown() )
      altMove(keycode);
    else if( event.isControlDown())
      controlMove(keycode);
    else
      standardMove(keycode);
  }



  //moves forward backward or rotates left right
  private void standardMove(int keycode)
  {
    if(keycode == forwardKey)
      moveForward();
    else if(keycode == backKey)
      moveBackward();
    else if(keycode == leftKey)
      rotLeft();
    else if(keycode == rightKey)
      rotRight();
    else if (keycode==QuitKey)
      System.exit(0);
  }

  //moves left right, rotate up down
  protected void altMove(int keycode)
  {
    if(keycode == forwardKey)
      rotUp();
    else if(keycode == backKey)
      rotDown();
    else if(keycode == leftKey)
      moveLeft();
    else if(keycode == rightKey)
      moveRight();
  }

  //move up down, rot left right
  protected void controlMove(int keycode)
  {
    if(keycode == forwardKey)
      moveUp();
    else if(keycode == backKey)
      moveDown();
    else if(keycode == leftKey)
      rollLeft();
    else if(keycode == rightKey)
      rollRight();
  }

  private void moveForward()
  {
    doMove(new Vector3d(0.0,0.0, -getMovementRate()));
  }

  private void moveBackward()
  {
    doMove(new Vector3d(0.0,0.0, getMovementRate()));
  }

  private void moveLeft()
  {
    doMove(new Vector3d( -getMovementRate() ,0.0,0.0));
  }

  private void moveRight()
  {
    doMove(new Vector3d(getMovementRate(),0.0,0.0));
  }

  private void moveUp()
  {
    doMove(new Vector3d(0.0, getMovementRate() ,0.0));
  }

  private void moveDown()
  {
    doMove(new Vector3d(0.0, -getMovementRate() ,0.0));
  }

  protected void rotRight()
  {
    doRotateY(getRotateRightAmount());
  }

  protected void rotUp()
  {
    doRotateX(getRotateUpAmount());
  }

  protected void rotLeft()
  {
    doRotateY(getRotateLeftAmount());
  }

  protected void rotDown()
  {
    doRotateX(getRotateDownAmount());
  }

  protected void rollLeft()
  {
     doRotateZ(getRollLeftAmount());
  }

  protected void rollRight()
  {
     doRotateZ(getRollRightAmount());
  }

  protected void doRotateY(double radians)
  {
    transformGroup.getTransform(transform3D);
    Transform3D toMove = new Transform3D();
    toMove.rotY(radians);
    transform3D.mul(toMove);
    transformGroup.setTransform(transform3D);
  }
  protected void doRotateX(double radians)
  {
    transformGroup.getTransform(transform3D);
    Transform3D toMove = new Transform3D();
    toMove.rotX(radians);
    transform3D.mul(toMove);
    transformGroup.setTransform(transform3D);
  }

  protected void doRotateZ(double radians)
  {
    transformGroup.getTransform(transform3D);
    Transform3D toMove = new Transform3D();
    toMove.rotZ(radians);
    transform3D.mul(toMove);
    transformGroup.setTransform(transform3D);
  }
  protected void doMove(Vector3d theMove)
  {
    transformGroup.getTransform(transform3D);
    Transform3D toMove = new Transform3D();
    toMove.setTranslation(theMove);
    transform3D.mul(toMove);
    transformGroup.setTransform(transform3D);
  }
  protected double getMovementRate()
  {
    return moveRate * speed;
  }

  protected double getRollLeftAmount()
  {
    return rotateZAmount * speed;
  }
  protected double getRollRightAmount()
  {
    return -rotateZAmount * speed;
  }

  protected double getRotateUpAmount()
  {
    return rotateYAmount * speed;
  }

  protected double getRotateDownAmount()
  {
    return -rotateYAmount * speed;
  }

  protected double getRotateLeftAmount()
  {
    return rotateYAmount * speed;
  }

  protected double getRotateRightAmount()
  {
    return -rotateYAmount * speed;
  }

  public void setRotateXAmount(double radians)
  {
    rotateXAmount = radians;
  }

  public void setRotateYAmount(double radians)
  {
    rotateYAmount = radians;
  }

  public void setRotateZAmount(double radians)
  {
    rotateZAmount = radians;
  }

  public void setMovementRate(double meters)
  {
    moveRate = meters; // Travel rate in meters/frame
  }

  public void setForwardKey(int key)
  {
    forwardKey = key;
  }

  public void setBackKey(int key)
  {
    backKey = key;
  }

  public void setLeftKey(int key)
  {
    leftKey = key;
  }

  public void setRightKey(int key)
  {
    rightKey = key;
  }
}
package testraster;

import java.awt.AWTEvent;
import java.awt.event.*;
import java.util.Enumeration;
import javax.media.j3d.*;
import javax.vecmath.*;

import com.sun.j3d.utils.universe.*;

public class DisparityBehavior extends Behavior
{
  private WakeupCondition keyCriterion;
  private int leftKey = KeyEvent.VK_SPACE;
  private int rightKey = KeyEvent.VK_BACK_SPACE;
  PhysicalBody body;

  public DisparityBehavior(PhysicalBody body)
  {
    this.body = body;
  }

  public void initialize()
  {
    WakeupCriterion[] keyEvents = new WakeupCriterion[2];
    keyEvents[0] = new WakeupOnAWTEvent( KeyEvent.KEY_PRESSED );
    keyEvents[1] = new WakeupOnAWTEvent( KeyEvent.KEY_RELEASED );
    keyCriterion = new WakeupOr( keyEvents );
    wakeupOn( keyCriterion );
  }

  public void processStimulus( Enumeration criteria )
  {
    WakeupCriterion wakeup;
    AWTEvent[] event;
    while( criteria.hasMoreElements() )
    {
      wakeup = (WakeupCriterion) criteria.nextElement();
      if( !(wakeup instanceof WakeupOnAWTEvent) )
        continue;

      event = ((WakeupOnAWTEvent)wakeup).getAWTEvent();
      for( int i = 0; i < event.length; i++ )
      {
        if( event[i].getID() == KeyEvent.KEY_PRESSED )
        {
          processKeyEvent((KeyEvent)event[i]);
        }
      }
    }
    wakeupOn( keyCriterion );
  }

  protected void processKeyEvent(KeyEvent event)
  {
    int keycode = event.getKeyCode();
    //event.VK_ESCAPE;
    Point3d currentLEyePos = new Point3d();
    body.getLeftEyePosition(currentLEyePos);

    Point3d currentREyePos = new Point3d();
    body.getRightEyePosition(currentREyePos);
    if(keycode == leftKey)
    {
     body.setLeftEyePosition(new Point3d(currentLEyePos.x-.001,currentLEyePos.y, currentLEyePos.z));
     body.setRightEyePosition(new Point3d(currentREyePos.x+.001,currentREyePos.y, currentREyePos.z));
    }
    else if (keycode == rightKey)
    {
     body.setLeftEyePosition(new Point3d(currentLEyePos.x+.001,currentLEyePos.y, currentLEyePos.z));
     body.setRightEyePosition(new Point3d(currentREyePos.x-.001,currentREyePos.y, currentREyePos.z));
    }
  }
  //moves forward backward or rotates left right
}

<<attachment: earth.jpg>>

Reply via email to