Forum Stats

  • 3,875,869 Users
  • 2,266,977 Discussions
  • 7,912,360 Comments

Discussions

HelloUniverse example - Cube suddenly disappears when zoom-out the scene

843799
843799 Member Posts: 49,999
edited Dec 25, 2009 3:51PM in Java 3D
Hi,

I modified the HelloUniverse example from the j3dexamples by adding an OrbitBahviour and removed also some code to have only the static cube.
But when I now zoom-out in the scene, the cube gets smaller and smaller and suddenly disappears. Why does this happen?

I would like to extend the scene with a lot of cubes, so when I want to see the whole scene I have to be able to zoom-out quite a lot...
import java.awt.GraphicsConfiguration;

import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.TransformGroup;
import javax.vecmath.Point3d;

import com.sun.j3d.utils.behaviors.vp.OrbitBehavior;
import com.sun.j3d.utils.geometry.ColorCube;
import com.sun.j3d.utils.universe.SimpleUniverse;

/**
 * Simple Java 3D example program to display a cube.
 */
public class HelloUniverse extends javax.swing.JFrame {

	private SimpleUniverse univ = null;
	private BranchGroup scene = null;
	
	private javax.swing.JPanel drawingPanel;

	
	/**
	 * Creates new form HelloUniverse
	 */
	public HelloUniverse() {
		// Initialize the GUI components
		initComponents();

		// Create Canvas3D and SimpleUniverse; add canvas to drawing panel
		Canvas3D c = createUniverse();
		drawingPanel.add(c, java.awt.BorderLayout.CENTER);

		// Create the content branch and add it to the universe
		scene = createSceneGraph();
		univ.addBranchGraph(scene);

		OrbitBehavior orbit = new OrbitBehavior(c);
		orbit.setSchedulingBounds(new BoundingSphere(new Point3d(0.0, 0.0, 0.0), Double.MAX_VALUE));
		univ.getViewingPlatform().setViewPlatformBehavior(orbit);
	}
	
	
	public BranchGroup createSceneGraph() {
		// Create the root of the branch graph
		BranchGroup objRoot = new BranchGroup();

		TransformGroup objTrans = new TransformGroup();
		objTrans.addChild(new ColorCube(0.4));
		objRoot.addChild(objTrans);

		// Have Java 3D perform optimizations on this scene graph.
		objRoot.compile();

		return objRoot;
	}

	
	private Canvas3D createUniverse() {
		// Get the preferred graphics configuration for the default screen
		GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();

		// Create a Canvas3D using the preferred configuration
		Canvas3D c = new Canvas3D(config);

		// Create simple universe with view branch
		univ = new SimpleUniverse(c);

		// This will move the ViewPlatform back a bit so the
		// objects in the scene can be viewed.
		univ.getViewingPlatform().setNominalViewingTransform();

		// Ensure at least 5 msec per frame (i.e., < 200Hz)
		univ.getViewer().getView().setMinimumFrameCycleTime(5);

		return c;
	}

	
	private void initComponents() {
		drawingPanel = new javax.swing.JPanel();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		setTitle("HelloUniverse");
		drawingPanel.setLayout(new java.awt.BorderLayout());

		drawingPanel.setPreferredSize(new java.awt.Dimension(500, 500));
		getContentPane().add(drawingPanel, java.awt.BorderLayout.CENTER);

		pack();
	}
	
	
	/**
	 * @param args the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new HelloUniverse().setVisible(true);
			}
		});
	}
}
Thx!

Comments

  • 843799
    843799 Member Posts: 49,999
    well the reason is that you didn't define different bounds for your BranchGroup, try using setBounds new SphericalBounds or something like that, and the BranchGroup uses a default, does this happens only on zoom out?? what happens when you go backwards, far enough from the cube??
  • 843799
    843799 Member Posts: 49,999
    By zoom-out I mean moving away from the cube so it gets smaller and smaller...

    I added the following line of code and now I can zoom-out a lot more.
    univ.getViewer().getView().setBackClipDistance(100.0);
    Thx!
This discussion has been closed.