Casey Reagan wrote:
>
> Can someone give me an example, or point me to one, of how to set up the
> view in Java3D without using simple universe. All of the examples that I
> have seen use this. I have an image but I want to be able to look at my
> world in different directions.
>
> Thank You,
> Casey R
Hi Casey,
attached to this mail there is a simple programm, it opens a frame and
initializes two simple 3d views and doesn't use the simple universe
class.
Hope it helps
Joerg
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.awt.Toolkit;
import com.sun.j3d.utils.behaviors.keyboard.*;
import com.sun.j3d.utils.behaviors.mouse.*;
import java.io.*;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.geometry.*;
import javax.media.j3d.*;
import javax.vecmath.*;
//show the scengraph in an x-tra window
//import SceneGraphViewer.ViewerLocale;
public class Universe extends Applet implements ActionListener{
BranchGroup contentroot;
BranchGroup sgroot;
BranchGroup viewgraphroot;
public Universe(){
setLayout(new GridLayout(0,2));
Canvas3D canvas_1 = new
Canvas3D(com.sun.j3d.utils.universe.SimpleUniverse.getPreferredConfiguration());
Canvas3D canvas_2 = new
Canvas3D(com.sun.j3d.utils.universe.SimpleUniverse.getPreferredConfiguration());
Button button1 = new Button("B1");
Button button2 = new Button("B2");
button1.addActionListener(this);
button2.addActionListener(this);
this.add(canvas_1);
this.add(canvas_2);
this.add(button1);
this.add(button2);
createSceneGraph(canvas_1,canvas_2);
}
public void createSceneGraph(Canvas3D c1, Canvas3D c2){
//Create a new Universe
VirtualUniverse universe = new VirtualUniverse();
//Create a new Locale & add it to the Universe
Locale locale = new Locale(universe);
//Debuging
//ViewerLocale locale = new ViewerLocale(universe);
//Create the root of the scenegraph & add it to locale
// sgroot will be used to attach content_branch_graph and view_branch_graph
sgroot = new BranchGroup();
sgroot.setCapability(BranchGroup.ALLOW_CHILDREN_EXTEND);
sgroot.setCapability(BranchGroup.ALLOW_CHILDREN_READ);
sgroot.setCapability(BranchGroup.ALLOW_CHILDREN_WRITE);
// Create the root of the content graph.
// Used to attach the geometricmodel of the scene
// Create the root of the viewgraph.
// Used to attach the different views
// Add the Stuff to the SceneGraphs Root
contentroot = new BranchGroup();
contentroot.setCapability(BranchGroup.ALLOW_DETACH);
viewgraphroot = new BranchGroup();
sgroot.addChild(contentroot);
sgroot.addChild(viewgraphroot);
createContent(contentroot);
createViews(viewgraphroot, c1, c2);
createLights(contentroot);
//Make the scenegraph "live"
locale.addBranchGraph(sgroot);
}
public void createMouseBehavior(TransformGroup tgr){
tgr.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
tgr.setCapability( TransformGroup.ALLOW_TRANSFORM_READ );
tgr.setCapability(TransformGroup.ENABLE_PICK_REPORTING);
BoundingSphere bounds =
new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
// Create the rotate behavior node fuer ObjTrans
MouseRotate behavior = new MouseRotate(tgr);
tgr.addChild(behavior);
behavior.setSchedulingBounds(bounds);
// Create the zoom behavior node fuer ObjTrans
MouseZoom behavior2 = new MouseZoom(tgr);
tgr.addChild(behavior2);
behavior2.setSchedulingBounds(bounds);
}
public void createViews(BranchGroup viewgraphroot, Canvas3D c1, Canvas3D c2){
//Create the Views and attach their canvases
View v1 = new View();
v1.addCanvas3D(c1);
View v2 = new View();
v2.addCanvas3D(c2);
//Create the viewplatforms,physical bodies & phys. Environments
//and attach the stuff to the views
ViewPlatform vp1 = new ViewPlatform();
v1.attachViewPlatform(vp1);
v1.setPhysicalBody(new PhysicalBody());
v1.setPhysicalEnvironment(new PhysicalEnvironment());
ViewPlatform vp2 = new ViewPlatform();
v2.attachViewPlatform(vp2);
v2.setPhysicalBody(new PhysicalBody());
v2.setPhysicalEnvironment(new PhysicalEnvironment());
//Create TransformGroups & Transformations for the diffrent views
//Attach the views to their belonging TG's
Transform3D t = new Transform3D();
Transform3D r = new Transform3D();
TransformGroup vp1tgr = new TransformGroup();
vp1tgr.addChild(vp1);
TransformGroup vp2tgr = new TransformGroup();
vp2tgr.addChild(vp2);
// Set view transformations
t.set(new Vector3f(0f, 0f, 10f));
vp1tgr.setTransform(t);
t.set(new Vector3f(0f, 13f, 10f));
r.rotX(-Math.PI/4d);
t.mul(t, r);
vp2tgr.setTransform(t);
//Finally attach the viewtgr's to the view graph root
viewgraphroot.addChild(vp1tgr);
viewgraphroot.addChild(vp2tgr);
}
private void createLights(BranchGroup graphRoot) {
// Create a bounds for the light source influence
BoundingSphere bounds =
new BoundingSphere(new Point3d(0.0,0.0,0.0), 1000.0);
// Set up the global, ambient light
Color3f alColor = new Color3f(0.1f, 0.1f, 0.1f);
AmbientLight aLgt = new AmbientLight(alColor);
aLgt.setInfluencingBounds(bounds);
graphRoot.addChild(aLgt);
// Set up the directional (infinite) light source
Color3f lColor1 = new Color3f(0.5f, 0.5f, 0.5f);
Vector3f lDir1 = new Vector3f(10f, -10f, -10f);
DirectionalLight lgt1 = new DirectionalLight(lColor1, lDir1);
lgt1.setInfluencingBounds(bounds);
graphRoot.addChild(lgt1);
}
public void createContent(BranchGroup contentroot){
TransformGroup contenttgr = new TransformGroup();
//Coordinate Axis
Appearance xapp = new Appearance();
Appearance yapp = new Appearance();
Appearance zapp = new Appearance();
ColoringAttributes xc = new ColoringAttributes();
ColoringAttributes yc = new ColoringAttributes();
ColoringAttributes zc = new ColoringAttributes();
xc.setColor(1f,0f,0f);
xapp.setColoringAttributes(xc);
Box x_axe = new Box(15.0f, 0.1f, 0.1f, xapp);
yc.setColor(0f,1f,0f);
yapp.setColoringAttributes(yc);
Box y_axe = new Box(0.1f, 15f, 0.1f, yapp);
zc.setColor(0f,0f,1f);
zapp.setColoringAttributes(zc);
Box z_axe = new Box(0.1f, 0.1f, 15f, zapp);
contenttgr.addChild(x_axe);
contenttgr.addChild(y_axe);
contenttgr.addChild(z_axe);
contenttgr.addChild(new Box());
contentroot.addChild(contenttgr);
createMouseBehavior(contenttgr);
}
public void actionPerformed(ActionEvent e){
String arg = e.getActionCommand();
if ("B1".equals(arg)){
contentroot.detach();
}
else if ("B2".equals(arg)){
sgroot.addChild(contentroot);
}
}
public static void main(String[] args){
Frame frame = new MainFrame(new Universe(), 500, 500);
}
}