I dont think you want to add it to the TransformGroup that has the
ViewingPlatform. I would add in the BranchGroup root or the
TransformGroup attatched to the root. Check out my code.
Dennis Goetz wrote:
>
> When I try to add a behavior to a viewpoint transform, I get the following
> error:
> javax.media.j3d.RestrictedAccessException: Group: only a BranchGroup
> node may be added
>
> What exactly must be done to add a behavior to the viewpoint transform? The
> following is most of the relevant code for what I am trying to do:
> TransformGroup vpTrans;
> SimpleUniverse universe = new SimpleUniverse(canvas3D);
> universe.getViewingPlatform().setNominalViewingTransform();
> universe.addBranchGraph(scene3D);
> ....
> ....
> vpTrans = universe.getViewingPlatform().getViewPlatformTransform();
> CameraBehaviour cameraBehaviour = new CameraBehaviour(vpTrans,20);
> cameraBehaviour.setSchedulingBounds(bounds);
> vpTrans.addChild(cameraBehaviour);
>
> Thanks,
> Dennis Goetz
>
> ===========================================================================
> 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".
--
J. Stone
[EMAIL PROTECTED]
http://www.DevilInBlue.org
//---------------------------------------------------------------------------
//
// Class AvatarClass
// CSC 471 Program # 1
//
// Jerry J. Stone
// www.devilinblue.org
//
// CSC 476-01 Spring 2000
// Computer Science Dept.
// Calif. Polytechnic State Univ.
// San Luis Obispo, CA, 93407 USA
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// class AvatarClass
//
// Description: Builds a scene with per command line arguements
// Preconditions: values are entered for the distanceLOD and how many avatars
// Postconditions: complete AvatarScene scene graph has been built
//
//---------------------------------------------------------------------------
import com.sun.j3d.loaders.objectfile.ObjectFile;
import com.sun.j3d.loaders.ParsingErrorException;
import com.sun.j3d.loaders.IncorrectFormatException;
import com.sun.j3d.loaders.Scene;
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.event.*;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import java.io.*;
import com.sun.j3d.utils.behaviors.mouse.*;
import com.sun.j3d.utils.behaviors.keyboard.*;
import com.sun.j3d.utils.image.TextureLoader;
import com.sun.j3d.utils.geometry.*;
import java.util.*;
import Avatar;
public class AvatarClass extends Applet {
public BranchGroup createSceneGraph(SimpleUniverse theUniv, String[] args) {
//creates an int for avatar population
int howMany = Integer.parseInt(args[2]);
//set up distance array
float[] distance = new float[2];
distance[0] = Float.parseFloat( args[0]);
distance[1] = Float.parseFloat(args[1]);
//set up avatar population array
TransformGroup avatarTG[] = new TransformGroup[howMany];
// Create the root of the branch graph
BranchGroup objRoot = new BranchGroup();
// 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. Add it to the
// root of the subgraph.
TransformGroup objTrans = new TransformGroup();
objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
objRoot.addChild(objTrans);
BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0), 1000.0);
TransformGroup vpTrans =
theUniv.getViewingPlatform().getViewPlatformTransform();
KeyNavigatorBehavior keyNavigate = new KeyNavigatorBehavior(vpTrans);
keyNavigate.setSchedulingBounds(new BoundingSphere(new Point3d(),1000.0));
objTrans.addChild(keyNavigate);
//populates the scene with avatars
for(int count = 0; count < howMany; count ++){
objRoot.addChild(avatarTG[count] = createLOD(distance));}
// Set up the background
Color3f bgColor = new Color3f(0.05f, 0.05f, 0.5f);
Background bgNode = new Background(bgColor);
bgNode.setApplicationBounds(bounds);
objRoot.addChild(bgNode);
// Set up the ambient light
Color3f ambientColor = new Color3f(0.1f, 0.1f, 0.1f);
AmbientLight ambientLightNode = new AmbientLight(ambientColor);
ambientLightNode.setInfluencingBounds(bounds);
objRoot.addChild(ambientLightNode);
// Set up the directional lights
Color3f light1Color = new Color3f(1.0f, 1.0f, 0.9f);
Vector3f light1Direction = new Vector3f(4.0f, -7.0f, -12.0f);
Color3f light2Color = new Color3f(0.3f, 0.3f, 0.4f);
Vector3f light2Direction = new Vector3f(-6.0f, -2.0f, -1.0f);
DirectionalLight light1
= new DirectionalLight(light1Color, light1Direction);
light1.setInfluencingBounds(bounds);
objRoot.addChild(light1);
DirectionalLight light2
= new DirectionalLight(light2Color, light2Direction);
light2.setInfluencingBounds(bounds);
objRoot.addChild(light2);
objRoot.compile();
return objRoot;
}
public AvatarClass(String args[]) {
setLayout(new BorderLayout());
Canvas3D c = new Canvas3D(null);
add("Center", c);
// Create a simple scene and attach it to the virtual universe
SimpleUniverse u = new SimpleUniverse(c);
BranchGroup scene = createSceneGraph(u, args);
BranchGroup floor = createFloor();
// This will move the ViewPlatform back a bit so the
// objects in the scene can be viewed.
u.getViewingPlatform().setNominalViewingTransform();
u.addBranchGraph(scene);
u.addBranchGraph(floor);
}
public BranchGroup createFloor(){
//creates the floor branchgroup node
BranchGroup objRoot2 = new BranchGroup();
//sets up the floor texture
Appearance theFloor = new Appearance();
TextureLoader tex = new TextureLoader(new String("stone.jpg"), this);
ImageComponent2D image = tex.getImage();
Texture2D texture = new Texture2D(Texture.BASE_LEVEL, Texture.RGBA,
image.getWidth(), image.getHeight());
texture.setImage(0, image);
theFloor.setTexture(texture);
//scale and rotate the box primitive for the floor
TransformGroup makeFloor = new TransformGroup();
Transform3D scaleFloor = new Transform3D();
scaleFloor.setScale(new Vector3d(1000f, .1f, 1000f));
scaleFloor.setRotation(new AxisAngle4f(1,0,0,.01f));
scaleFloor.setTranslation(new Vector3d(0,-1f, 0));
makeFloor.setTransform(scaleFloor);
objRoot2.addChild(makeFloor);
Box box = new Box(1.0f, 1f,1f,Primitive.GENERATE_TEXTURE_COORDS, theFloor);
makeFloor.addChild(box);
objRoot2.compile();
return objRoot2;
}
public TransformGroup createLOD(float[] distance){
//shift avatar
TransformGroup moveIt = new TransformGroup();
Transform3D where = new Transform3D();
where.setScale(new Vector3d(.5f, .5f, .5f));
Random getIt = new Random();
float x = (getIt.nextFloat()*4f);
float y = (getIt.nextFloat());
float z = (getIt.nextFloat()*3f);
where.setTranslation(new Vector3d(-x, -y ,z));
moveIt.setTransform(where);
//create a boundingsphere
BoundingSphere aBounds = new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
//create a switch for the lod
Switch theSW = new Switch();
theSW.setCapability(javax.media.j3d.Switch.ALLOW_SWITCH_WRITE);
//theSW.setCapability(javax.media.j3d.Switch.ALLOW_SWITCH_READ);
//create the different levels
theSW.addChild(new Avatar(150));
theSW.addChild(new Avatar(30));
theSW.addChild(new Avatar(5));
moveIt.addChild(theSW);
//add distance lod
DistanceLOD theLOD = new DistanceLOD(distance);
theLOD.addSwitch(theSW);
theLOD.setSchedulingBounds(aBounds);
moveIt.addChild(theLOD);
return moveIt;
}
//
// The following allows ObjLoad to be run as an application
// as well as an applet
//
public static void main(String[] args) {
new MainFrame(new AvatarClass(args), 600, 600);
}
}
//---------------------------------------------------------------------------
//
// Class Avatar
// CSC 471 Program # 1
//
// Jerry J. Stone
// www.devilinblue.org
//
// CSC 476-01 Spring 2000
// Computer Science Dept.
// Calif. Polytechnic State Univ.
// San Luis Obispo, CA, 93407 USA
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// class Avatar
//
// Description: Builds an avatar of my hand
// Preconditions: none
// Postconditions: complete Avatar scene graph has been built
//
//---------------------------------------------------------------------------
import java.applet.Applet;
import javax.media.j3d.*;
import javax.vecmath.*;
import java.io.*;
import java.awt.*;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.geometry.*;
public class Avatar extends TransformGroup{
public Avatar( int detail){
//identity transform
Transform3D iTrans = new Transform3D();
//set up the root
TransformGroup theRoot = new TransformGroup();
//set up the palm of the hand
TransformGroup scaleSphere = new TransformGroup();
iTrans.setIdentity();
iTrans.setScale(new Vector3d(.3f, .4f, .1f));
scaleSphere.setTransform(iTrans);
theRoot.addChild(scaleSphere);
Sphere aSphere = new Sphere(1f, Sphere.GENERATE_NORMALS, detail);
scaleSphere.addChild(aSphere);
//scale a finger
TransformGroup scaleFinger1 = new TransformGroup();
iTrans.setIdentity();
iTrans.setScale(new Vector3d(.050f, .2f, .06f));
iTrans.setTranslation(new Vector3d(-.30f,.2f,0));
scaleFinger1.setTransform(iTrans);
theRoot.addChild(scaleFinger1);
Sphere bSphere = new Sphere(1f, Sphere.GENERATE_NORMALS, detail);
scaleFinger1.addChild(bSphere);
//scale a finger
TransformGroup scaleFinger2 = new TransformGroup();
iTrans.setIdentity();
iTrans.setScale(new Vector3d(.050f, .3f, .06f));
iTrans.setTranslation(new Vector3d(-.10,.55f,0));
scaleFinger2.setTransform(iTrans);
theRoot.addChild(scaleFinger2);
Sphere cSphere = new Sphere(1f, Sphere.GENERATE_NORMALS, detail);
scaleFinger2.addChild(cSphere);
//scale a finger
TransformGroup scaleFinger3 = new TransformGroup();
iTrans.setIdentity();
iTrans.setScale(new Vector3d(.050f, .3f, .06f));
iTrans.setTranslation(new Vector3d(.010f,.6f,0));
scaleFinger3.setTransform(iTrans);
theRoot.addChild(scaleFinger3);
Sphere dSphere = new Sphere(1f, Sphere.GENERATE_NORMALS, detail);
scaleFinger3.addChild(dSphere);
//scale a finger
TransformGroup scaleFinger4 = new TransformGroup();
iTrans.setIdentity();
iTrans.setScale(new Vector3d(.050f, .3f, .06f));
iTrans.setTranslation(new Vector3d(.120,.50f,0));
scaleFinger4.setTransform(iTrans);
theRoot.addChild(scaleFinger4);
Sphere eSphere = new Sphere(1f, Sphere.GENERATE_NORMALS, detail);
scaleFinger4.addChild(eSphere);
//scale a finger
TransformGroup scaleFinger5 = new TransformGroup();
iTrans.setIdentity();
iTrans.setScale(new Vector3d(.050f, .2f, .06f));
iTrans.setTranslation(new Vector3d(.240,.35f,0));
scaleFinger5.setTransform(iTrans);
theRoot.addChild(scaleFinger5);
Sphere fSphere = new Sphere(1f, Sphere.GENERATE_NORMALS, detail);
scaleFinger5.addChild(fSphere);
this.addChild(theRoot);
}}