-----Original Message-----
From: Cao, Dat
Sent: Wednesday, July 12, 2000 9:42 AM
To: '[EMAIL PROTECTED]'
Cc: '[EMAIL PROTECTED]'
Subject: Need Help Relocating ViewPlatform

I modified the TexturedSphere demo ( demo\java3d\ConicWorld\TexturedSphere.java ) to display a textured sphere with radius equals to the radius of the earth.  When I ran the program, the sphere disappeared.  Can someone please explain what I did wrong from the following changes made in TexturedSphere.java:
 
1. Create a textured sphere with radius equals to the radius of the earth

   // Sphere SphereObj = new Sphere(1.0f, Sphere.GENERATE_NORMALS |Sphere.GENERATE_TEXTURE_COORDS,45);
   
Sphere SphereObj = new Sphere(EARTHRADIUS, Sphere.GENERATE_NORMALS |Sphere.GENERATE_TEXTURE_COORDS, 100);
 
2.  Translate the view platform to 10 units in front of the sphere, so I can see it.  I think??? 
 
 // u.getViewingPlatform().setNominalViewingTransform();
    TransformGroup tg = 
u.getViewingPlatform().getViewPlatformTransform();
    Transform3D transform = new Transform3D();
    transform.setTranslation(new Vector3f(0.0f, 0.0f, EARTHRADIUS + 10.0f));
    tg.setTransform(transform);
 
3.  Resize the bounding sphere to accomodate the textured sphere and the view platform
 
 // BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0),100);
    BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0),EARTHRADIUS * 2.0);
 
EARTHRADIUS is defined as
private final float EARTHRADIUS = 6378000.0f;  // earth radius
 
 
 
 
 
This is the complete code
 
import com.sun.j3d.utils.image.TextureLoader;
import com.sun.j3d.utils.behaviors.mouse.*;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.vecmath.*;
 
public class TexturedSphere extends Applet {
 
  private java.net.URL texImage = null;
  private final float EARTHRADIUS = 6378000.0f;  // earth radius
 
  public BranchGroup createSceneGraph() {
    // Create the root of the branch graph
    BranchGroup objRoot = new BranchGroup();
 
    // Create a Transformgroup to scale all objects so they
    // appear in the scene.
    TransformGroup objScale = new TransformGroup();
    Transform3D t3d = new Transform3D();
    t3d.setScale(0.4);
    objScale.setTransform(t3d);
    objRoot.addChild(objScale);
 
    // Create the transform group node and initialize it to the
    // identity.  Enable the TRANSFORM_WRITE capability so that
    // our behavior code can modify it at runtime.
    TransformGroup objTrans = new TransformGroup();
    // read/write-enable for behaviors
    objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
    objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    objScale.addChild(objTrans);
    
    // Create a Sphere
    // Sphere SphereObj = new Sphere(1.0f, Sphere.GENERATE_NORMALS |
    //     Sphere.GENERATE_TEXTURE_COORDS, 45);
    Sphere SphereObj = new Sphere(EARTHRADIUS, Sphere.GENERATE_NORMALS |
      Sphere.GENERATE_TEXTURE_COORDS, 100);
   
    Appearance ap = SphereObj.getAppearance();
 
    System.out.println("Created sphere with tris " +
         SphereObj.getNumTriangles() +
         " verts " + SphereObj.getNumVertices());
 
    // add it to the scene graph.
    objTrans.addChild(SphereObj);
 

    TextureLoader tex = new TextureLoader(texImage, "RGB", this);
    ap.setTexture(tex.getTexture());
 

    // BoundingSphere bounds =
    // new BoundingSphere(new Point3d(0.0,0.0,0.0),100);
    BoundingSphere bounds =
      new BoundingSphere(new Point3d(0.0,0.0,0.0),EARTHRADIUS * 2.0);
 
    // Create the rotate behavior node
    MouseRotate behavior = new MouseRotate(objTrans);
    objTrans.addChild(behavior);
    behavior.setSchedulingBounds(bounds);
 
    // Create the zoom behavior node
    MouseZoom behavior2 = new MouseZoom(objTrans);
    objTrans.addChild(behavior2);
    behavior2.setSchedulingBounds(bounds);
 
    // Create the translate behavior node
    MouseTranslate behavior3 = new MouseTranslate(objTrans);
    objTrans.addChild(behavior3);
    behavior3.setSchedulingBounds(bounds);
 
    // Shine it with two lights.
    Color3f lColor1 = new Color3f(0.7f, 0.7f, 0.7f);
    Color3f lColor2 = new Color3f(0.2f, 0.2f, 0.1f);
    Vector3f lDir1  = new Vector3f(-1.0f, -1.0f, -1.0f);
    Vector3f lDir2  = new Vector3f(0.0f, 0.0f, -1.0f);
    DirectionalLight lgt1 = new DirectionalLight(lColor1, lDir1);
    DirectionalLight lgt2 = new DirectionalLight(lColor2, lDir2);
    lgt1.setInfluencingBounds(bounds);
    lgt2.setInfluencingBounds(bounds);
    objScale.addChild(lgt1);
    objScale.addChild(lgt2);
 
    // Let Java 3D perform optimizations on this scene graph.
    objRoot.compile();
 
    return objRoot;
  }
 
  public TexturedSphere (){
  }
 
  public TexturedSphere(java.net.URL url) {
    texImage = url;
  }
 
  public void init() {
    if (texImage == null) {
      // the path to the image for an applet
      try {
 texImage = new java.net.URL(getCodeBase().toString() +
        "../images/earth.jpg");
      }
      catch (java.net.MalformedURLException ex) {
 System.out.println(ex.getMessage());
 System.exit(1);
      }
    }
 
    setLayout(new BorderLayout());
    GraphicsConfiguration config =
       SimpleUniverse.getPreferredConfiguration();
 
    Canvas3D c = new Canvas3D(config);
    add("Center", c);
   
    BranchGroup scene = createSceneGraph();
    SimpleUniverse u = new SimpleUniverse(c);
 
    // This will move the ViewPlatform back a bit so the
    // objects in the scene can be viewed.
    // u.getViewingPlatform().setNominalViewingTransform();
    TransformGroup tg =
 u.getViewingPlatform().getViewPlatformTransform();
    Transform3D transform = new Transform3D();
    transform.setTranslation(new Vector3f(0.0f, 0.0f, EARTHRADIUS + 10.0f));
    tg.setTransform(transform);
   
   
    u.addBranchGraph(scene);
  }
 
  public static void main(String args[])
  {
    java.net.URL url = null;
    if (args.length > 0) {
      try {
 url = new java.net.URL("file:" + args[0]);
      }
      catch (java.net.MalformedURLException ex) {
 System.out.println(ex.getMessage());
 System.exit(1);
      }
    }
    else {
      // the path to the image file for an application
      try {
 url = new java.net.URL("file:../images/earth.jpg");
      }
      catch (java.net.MalformedURLException ex) {
 System.out.println(ex.getMessage());
 System.exit(1);
      }
    }
   
    new MainFrame(new TexturedSphere(url), 500, 500);
  }
}
 
 
 
 
Thanks in advance for your help
 
 
 

Reply via email to