-
Java 3D Mouse Behaviour
Hi, I've created two simple objects (box and sphere) in a bounding sphere that is declared globally with a size of 200. I also have amdient and directional lighting added to the same bounding sphere. The problem I'am getting is that when I zoom out from the objects using the middle mouse button, the box and sphere disappear. Can anyone suggest why and how the problem could be fixed??
<CODE>
import javax.swing.*;
import java.awt.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.j3d.utils.geometry.Box;
import com.sun.j3d.utils.geometry.Sphere;
import com.sun.j3d.utils.behaviors.mouse.*;
public class Test extends JFrame
{
protected Canvas3D yourCanvas3D = new Canvas3D(null);//Canvas 3D to render to
BoundingSphere boundingSphere = new BoundingSphere(new Point3d(0.0,0.0,0.0), 200.0);
public Test()
{
super("creating_scenes"); //set the JFrame title
VirtualUniverse yourUniverse = new VirtualUniverse(); //J3D create default universe
Locale yourLocale = new Locale(yourUniverse); //J3D create default locale
//J3D Use the functions to construct the branch graphs and attach to scene graph
yourLocale.addBranchGraph(constructViewBranchGroup(yourCanvas3D));
yourLocale.addBranchGraph(constructContentBranchGroup(yourShape3D()));
Container yourContainer = getContentPane(); // get content pane and name it
yourContainer.setLayout(new BorderLayout()); // use border layout
yourContainer.add(yourCanvas3D, BorderLayout.CENTER);//J3D add Canvas3D to JFrame
pack();
setSize(1000, 800); //set default size of JFrame to width=500 height=350
//Center the window
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = getSize();
if (frameSize.height > screenSize.height)
{
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width)
{
frameSize.width = screenSize.width;
}
setLocation((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);
setVisible(true); //display the JFrame
}
protected BranchGroup constructViewBranchGroup(Canvas3D yourCanvas3D)
{
BranchGroup viewBranch = new BranchGroup();// create BranchGroup
Transform3D viewObjectFrom = new Transform3D();
viewObjectFrom.set(new Vector3f(0.0f, 0.0f, 30.0f));//view position x=0,y=0,z=10(backwards)
TransformGroup viewObjectFromGroup = new TransformGroup(viewObjectFrom);
ViewPlatform yourViewPlatform = new ViewPlatform();
PhysicalBody yourBody = new PhysicalBody();
PhysicalEnvironment yourEnvironment = new PhysicalEnvironment();
viewObjectFromGroup.addChild(yourViewPlatform);
viewBranch.addChild(viewObjectFromGroup);
View yourView = new View(); //create View object
yourView.addCanvas3D(yourCanvas3D);
yourView.attachViewPlatform(yourViewPlatform);
yourView.setPhysicalBody(yourBody);
yourView.setPhysicalEnvironment(yourEnvironment);
return viewBranch;
}
protected BranchGroup constructContentBranchGroup(Node shape)
{
BranchGroup contentBranch = new BranchGroup(); //create content branch
TransformGroup mouseGroup = new TransformGroup();
mouseGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
mouseGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
MouseRotate mouseRotate = new MouseRotate();
mouseRotate.setTransformGroup(mouseGroup);
mouseGroup.addChild(mouseRotate);
mouseRotate.setSchedulingBounds(boundingSphere);
MouseZoom mouseZoom = new MouseZoom();
mouseZoom.setTransformGroup(mouseGroup);
mouseGroup.addChild(mouseZoom);
mouseZoom.setSchedulingBounds(boundingSphere);
MouseTranslate mouseTranslate = new MouseTranslate();
mouseTranslate.setTransformGroup(mouseGroup);
mouseGroup.addChild(mouseTranslate);
mouseTranslate.setSchedulingBounds(boundingSphere);
mouseGroup.addChild(shape);
contentBranch.addChild(mouseGroup);
addLights(contentBranch);
return contentBranch;
}
protected Node yourShape3D()//or protected Shape3D yourShape3D
{
BranchGroup yourScene = new BranchGroup();
Appearance groundAppearance = new Appearance();
Color3f groundAmbColor = new Color3f(0.2f, 0.5f, 0.0f);
Color3f groundEmissColor = new Color3f(0.0f, 0.0f, 0.0f);
Color3f groundDiffColor = new Color3f(0.2f, 0.5f, 0.0f);
Color3f groundSpecColor = new Color3f(0.5f, 0.5f, 0.5f);
float groundShininess = 12.0f;
Material groundMaterial = new Material(groundAmbColor, groundEmissColor, groundDiffColor, groundSpecColor, groundShininess);
groundAppearance.setMaterial(groundMaterial);
Box ground = new Box(37.0f, 0.05f, 18.6f, groundAppearance);
Transform3D groundT3D = new Transform3D();
groundT3D.set(new Vector3f(0.0f, -1.0f, 0.0f));
TransformGroup groundTGroup = new TransformGroup(groundT3D);
groundTGroup.addChild(ground);
yourScene.addChild(groundTGroup);
Appearance materialAppearance = new Appearance();
Color3f ambientColor = new Color3f(0.0f, 1.0f, 0.0f);
Color3f emissiveColor = new Color3f(1.0f, 0.0f, 0.0f);
Color3f diffuseColor = new Color3f(0.0f, 0.0f, 1.0f);
Color3f specularColor = new Color3f(1.0f, 1.0f, 1.0f);
float shininess = 32.0f;
Material material = new Material(ambientColor, emissiveColor, diffuseColor, specularColor, shininess);
materialAppearance.setMaterial(material);
Sphere shape = new Sphere(1.0f, materialAppearance);
yourScene.addChild(shape);
return yourScene;
}
protected void addLights(BranchGroup lightContentBranch)
{
AmbientLight ambientLight = new AmbientLight();
ambientLight.setInfluencingBounds(boundingSphere);
lightContentBranch.addChild(ambientLight);
DirectionalLight directionalLight = new DirectionalLight();
Vector3f lightDirection = new Vector3f(-1.0f, -1.0f, -1.0f);
directionalLight.setDirection(lightDirection);
directionalLight.setInfluencingBounds(boundingSphere);
lightContentBranch.addChild(directionalLight);
}
public static void main(String[] args)
{
Test test = new Test();//instantiate Graphics3DJFrame object
test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //close frame Swing way
}
}
</CODE>
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks