Hi,

I'm trying to set the projection policy to parallel.

But if I change the view policy from perspective to
parallel, my scene vanish. Might have something to
do with the viewAll method that tries to resize the
scene so that it's all visible. Have anyone encountered
similiar problems?

Here's the code:

---

package com.decim;

import javax.media.j3d.*;
import javax.swing.*;
import javax.vecmath.*;
import java.awt.*;
import java.io.File;

public class Designer extends JFrame
{
  public Designer()
  {
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setTitle("Designer");

    this.setSize(640, 400);
  }

  public void addCanvas(Canvas3D canvas)
  {
    getContentPane().add(canvas, BorderLayout.CENTER);
  }

  public void displayFrame(boolean center)
  {
    this.validate();

    if (center == true)
    {
      Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
      Dimension frameSize = this.getSize();

      if (frameSize.height > screenSize.height)
      {
        frameSize.height = screenSize.height;
      }

      if (frameSize.width > screenSize.width)
      {
        frameSize.width = screenSize.width;
      }

      this.setLocation((screenSize.width - frameSize.width) / 2,
                       (screenSize.height - frameSize.height) / 2);
    }

    this.setVisible(true);
  }

  public static void main(String args[])
  {
    AppScene scene = new AppScene();

    Designer frame = new Designer();
    frame.addCanvas(scene.createCanvas());

    scene.createScene();

    frame.displayFrame(true);
  }
}

class AppScene
{
  private Canvas3D canvas;
  private View view;

  public AppScene()
  {
  }

  public Canvas3D createCanvas()
  {
    GraphicsConfigTemplate3D template = new GraphicsConfigTemplate3D();
    GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice device = env.getDefaultScreenDevice();
    GraphicsConfiguration config = device.getBestConfiguration(template);

    this.canvas = new Canvas3D(config);

    return this.canvas;
  }

  public void createScene()
  {
    SceneManager scene = new SceneManager();

    BoundingSphere bounds = new BoundingSphere(new Point3d(), 100.0);

    BranchGroup textGroup = new BranchGroup();
    String message = "Application startup";

    float length = message.length();

    Transform3D transform = new Transform3D();
    TransformGroup scale = new TransformGroup();

    transform.setScale(1.0);
    transform.rotY(-0.75);
    scale.setTransform(transform);
    textGroup.addChild(scale);

    Background bg = new Background(0.5f, 0.5f, 1.0f);
    bg.setApplicationBounds(bounds);
    textGroup.addChild(bg);

    Font3D f3d = new Font3D(new Font("Courier", Font.PLAIN, 3), new
FontExtrusion());
    Text3D txt = new Text3D(f3d, message, new Point3f(-length / 1.5f, -1f,
-1f));
    Shape3D sh = new Shape3D();
    sh.setGeometry(txt);
    sh.setAppearance(createAppearance());
    scale.addChild(sh);

    scene.addCamera(createCamera(new BoundingSphere(sh.getBounds())));
    scene.makeLive();

    Color3f ambientColor = new Color3f(1, 0, 0);
    AmbientLight ambientLightNode = new AmbientLight(ambientColor);
    ambientLightNode.setColor(new Color3f(1, 1, 1));
    ambientLightNode.setInfluencingBounds(bounds);
    textGroup.addChild(ambientLightNode);

    textGroup.setCapability(BranchGroup.ALLOW_DETACH);
    textGroup.setCapability(Node.ALLOW_BOUNDS_READ);

    scene.changeWorldGroup(textGroup);
  }

  private Appearance createAppearance()
  {
    Appearance appearance = new Appearance();
    Material material = new Material();

    material.setAmbientColor ( new Color3f( 0.57f, 0.40f, 0.00f ) );
    material.setDiffuseColor ( new Color3f( 0.22f, 0.15f, 0.00f ) );
    material.setSpecularColor( new Color3f( 0.71f, 0.70f, 0.56f ) );
    material.setShininess( 0.16f );

    appearance.setMaterial(material);

    return appearance;
  }

  public TransformGroup createCamera(BoundingSphere bounds)
  {
    PhysicalEnvironment env;
    PhysicalBody body;

    ViewPlatform platform = new ViewPlatform();

    Group head = new Group();
    head.setCapability(Group.ALLOW_CHILDREN_EXTEND);

    TransformGroup transform = new TransformGroup();
    transform.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    transform.addChild(platform);
    transform.addChild(head);

    body = new PhysicalBody();
    env = new PhysicalEnvironment();

    view = new View();
    view.setBackClipDistance(100);
    view.setPhysicalBody(body);
    view.setPhysicalEnvironment(env);
    view.attachViewPlatform(platform);

    /**
     * If I change the view policy from perspective to
     * parallel, my view vanish. Probably due to some
     * resizing problem. Any ideas why?
     */
    view.setProjectionPolicy(View.PERSPECTIVE_PROJECTION);
    // view.setProjectionPolicy(View.PARALLEL_PROJECTION);

    view.addCanvas3D(canvas);

    Transform3D location = viewAll(bounds);
    transform.setTransform(location);

    return transform;
  }

  public Transform3D viewAll(BoundingSphere bounds)
  {
    Transform3D viewTrans = new Transform3D();

    Point3d center = new Point3d();
    bounds.getCenter(center);
    double radius = bounds.getRadius();

    double eyeDist = 1.2 * radius / Math.tan(view.getFieldOfView() / 2.0);

    Vector3d up = new Vector3d(0, 1, 0);
    Point3d eyePos = new Point3d(center);
    eyePos.z += eyeDist;

    viewTrans.lookAt(eyePos, center, up);
    viewTrans.invert();

    if ( view.getBackClipDistance() < eyeDist )
    {
      view.setBackClipDistance(eyeDist);
      view.setFrontClipDistance(eyeDist / 3000);
    }

    return viewTrans;
  }
}

class SceneManager extends VirtualUniverse
{
  private Locale locale;
  private BranchGroup viewGroup;
  private BranchGroup worldGroup;

  public SceneManager()
  {
    this.locale = new Locale(this);

    this.viewGroup = new BranchGroup();
    this.viewGroup.setCapability(Group.ALLOW_CHILDREN_EXTEND);

    this.worldGroup = new BranchGroup();
    this.worldGroup.setCapability(BranchGroup.ALLOW_DETACH);
  }

  public void addCamera(TransformGroup cam)
  {
    this.viewGroup.addChild(cam);
  }

  public void makeLive()
  {
    this.viewGroup.compile();
    this.worldGroup.compile();

    this.locale.addBranchGraph(this.viewGroup);
    this.locale.addBranchGraph(this.worldGroup);
  }

  public synchronized void changeWorldGroup(BranchGroup newGroup)
  {
    newGroup.compile();

    this.locale.replaceBranchGraph(this.worldGroup, newGroup);
    this.worldGroup = newGroup;
  }
}

===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JAVA3D-INTEREST".  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".

Reply via email to