can someone please help me! I have a program with uses a DistanceLOD and
a Switch. I need to have many instances of my avatar. For some reason,
it will only load one. I dont know why. If i use a primitive like a
Sphere it loads them but then they all switch LOD at the same time. My
code is attatched , any help would be very much apprecaiated.
--
J. Stone
[EMAIL PROTECTED]
http://www.DevilInBlue.org
//---------------------------------------------------------------------------
//
// Class myHand
// 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 myHand
//
// Description: Builds an avatar of my hand
// Preconditions: none
// Postconditions: complete myHand 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{
Color3f theBgColor = new Color3f(0.0f, 0.1f, 0.4f); // ambient blue
Color3f dirColor = new Color3f(1f, 1f, 1f);
Color3f objColor = new Color3f(.8f, .6f, .5f);
Color3f black = new Color3f(0.0f, 0.0f, 0.0f);
Color3f white = new Color3f(1.0f, 1.0f, 1.0f);
//public int detail = 64;
public Avatar(TransformGroup theTran, 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);
theTran.addChild(theRoot);
}}
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 Avatar;
public class AvatarClass extends Applet {
private boolean spin = false;
private boolean noTriangulate = false;
private boolean noStripify = false;
private double creaseAngle = 60.0;
private String filename = null;
public BranchGroup createSceneGraph(SimpleUniverse theUniv, String[] args) {
// 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), 3000.0);
TransformGroup vpTrans =
theUniv.getViewingPlatform().getViewPlatformTransform();
KeyNavigatorBehavior keyNavigate = new KeyNavigatorBehavior(vpTrans);
keyNavigate.setSchedulingBounds(new BoundingSphere(new Point3d(),1000.0));
objRoot.addChild(keyNavigate);
//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(objTrans,100));
theSW.addChild(new Sphere(0.4f, Sphere.GENERATE_NORMALS, 10));
theSW.addChild(new Sphere(0.4f, Sphere.GENERATE_NORMALS, 4));
objTrans.addChild(theSW);
//add distance lod
float[] distance = new float[2];
distance[0] = Float.parseFloat( args[0]);
distance[1] = Float.parseFloat(args[1]);
DistanceLOD theLOD = new DistanceLOD(distance);
theLOD.addSwitch(theSW);
theLOD.setSchedulingBounds(bounds);
objTrans.addChild(theLOD);
Switch newSW = new Switch();
// newSW.duplicateNode(theSW, false);
newSW.setCapability(javax.media.j3d.Switch.ALLOW_SWITCH_WRITE);
newSW.setCapability(javax.media.j3d.Switch.ALLOW_SWITCH_READ);
newSW.addChild(new Avatar(objTrans,100));
newSW.addChild(new Sphere(0.4f, Sphere.GENERATE_NORMALS, 10));
newSW.addChild(new Sphere(0.4f, Sphere.GENERATE_NORMALS, 4));
theLOD.addSwitch(newSW);
theLOD.setSchedulingBounds(bounds);
TransformGroup aT = new TransformGroup();
aT.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
aT.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
Transform3D iTrans = new Transform3D();
iTrans.setIdentity();
iTrans.setTranslation(new Vector3d(3f, 0f, -6f));
aT.setTransform(iTrans);
objRoot.addChild(aT);
//aT.addChild(theLOD);
aT.addChild(newSW);
// Set up the background
//Color3f bgColor = new Color3f(0.05f, 0.05f, 0.5f);
Color3f bgColor = new Color3f(1f, 1f, 1f);
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;
}
private void usage()
{
System.out.println(
"Usage: java ObjLoad [-s] [-n] [-t] [-c degrees] <.obj file>");
System.out.println(" -s Spin (no user interaction)");
System.out.println(" -n No triangulation");
System.out.println(" -t No stripification");
System.out.println(
" -c Set crease angle for normal generation (default is 60 without");
System.out.println(
" smoothing group info, otherwise 180 within smoothing groups)");
System.exit(0);
} // End of usage
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);
// This will move the ViewPlatform back a bit so the
// objects in the scene can be viewed.
u.getViewingPlatform().setNominalViewingTransform();
u.addBranchGraph(scene);
}
//
// 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), 400, 400);
}
}