1:import java.applet.Applet;
   2:import java.awt.*;
   3:import com.sun.j3d.utils.applet.MainFrame;
   4:import com.sun.j3d.utils.universe.*;
   5:import com.sun.j3d.utils.geometry.*;
   6:import javax.media.j3d.*;
   7:import javax.vecmath.*;
   8:import com.sun.j3d.utils.behaviors.mouse.*;
   9:
  10:public class MorphExample extends Applet{
  11:        
  12:        private static float PI = (new Double(Math.PI)).floatValue();
  13:        
  14:        public MorphExample() {
  15:                setLayout(new BorderLayout());
  16:                GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
  17:                Canvas3D canvas3d = new Canvas3D(config);
  18:                add("Center", canvas3d);
  19:                BranchGroup scene = createSceneGraph();
  20:                scene.compile();
  21:                SimpleUniverse sU = new SimpleUniverse(canvas3d);
  22:                sU.getViewingPlatform().setNominalViewingTransform();
  23:                sU.addBranchGraph(scene);
  24:        }       
  25:        
  26:        public BranchGroup createSceneGraph(){
  27:                
  28:                //create GeometryArray[]
  29:                //each element in this array is one of the geometries to be interpolated
  30:                GeometryArray[] geomArray = new GeometryArray[2];
  31:                geomArray[0] = createGeometryArray0();
  32:                geomArray[1] = createGeometryArray1();
  33:                
  34:                //create morph object
  35:                Morph morphObj = new Morph(geomArray);
  36:                morphObj.setCapability(Morph.ALLOW_WEIGHTS_WRITE);
  37:                
  38:                //Appearance is created in the method createAppearance below
  39:                //the we set the appearance of the Morph object
  40:                Appearance app = createAppearance();
  41:                morphObj.setAppearance(app);
  42:                
  43:                //create alpha object
  44:                //maps a time interval into alpha in [0,1]
  45:                //default is a monotone increasing function of time
  46:                //first argument is loop count (-1 = infinity!)
  47:                //second argument is duration (10 sec)          
  48:                Alpha alpha = new Alpha(-1, 10000);
  49:                alpha.setIncreasingAlphaRampDuration(1000);
  50:                
  51:                //create morph driving behavior
  52:                //this is a class we have to write ourselves
  53:                //it will wake up immediately and keep looping
  54:                //it will change the weights in morphObj depending
  55:                //on the current value of alpha
  56:                MorphBehavior morphBehav = new MorphBehavior(morphObj, alpha);
  57:                morphBehav.setSchedulingBounds(new BoundingSphere());
  58:                
  59:                //move things back a bit
  60:                Transform3D t3d = new Transform3D();
  61:                t3d.set(new Vector3f(0f,0f,-2f));
  62:                TransformGroup translate = new TransformGroup(t3d);
  63:                
  64:                BranchGroup objRoot = new BranchGroup();
  65:                
  66:                //add a mouse rotation
  67:                TransformGroup objRotate = new TransformGroup();
  68:                objRotate.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
  69:                objRotate.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
  70:                
  71:                MouseRotate mR = new MouseRotate();
  72:                mR.setTransformGroup(objRotate);
  73:                mR.setSchedulingBounds(new BoundingSphere());
  74:                objRoot.addChild(mR);
  75:                
  76:                objRoot.addChild(translate);
  77:                translate.addChild(objRotate);
  78:                objRotate.addChild(morphObj);
  79:                objRoot.addChild(morphBehav);
  80:                
  81:                //Create ambient light
  82:                AmbientLight amb = new AmbientLight();
  83:                amb.setInfluencingBounds(new BoundingSphere());
  84:                translate.addChild(amb);
  85:                
  86:                // Create the directional light
  87:                Color3f white = new Color3f(1.0f,  1.0f,  1.0f);
  88:                Vector3f dir = new Vector3f(-1.0f, -1.0f, -1.0f);
  89:                DirectionalLight dirLight = new DirectionalLight(white, dir);
  90:                dirLight.setInfluencingBounds(new BoundingSphere());
  91:                translate.addChild(dirLight);
  92:                
  93:                return objRoot;
  94:        }       
  95:        
  96:        public GeometryArray createGeometryArray0(){
  97:                
  98:                float x,y;
  99:                float delta=0.25f;
 100:                
 101:                QuadArray quadArray = new QuadArray(256, QuadArray.COORDINATES | QuadArray.NORMALS );
 102:                int index = 0;
 103:                for(int i=0; i<8; i++){
 104:                        for(int j=0; j<8; j++){
 105:                                x = -1.0f+0.25f*j;
 106:                                y = -1.0f+0.25f*i;
 107:                                quadArray.setCoordinate(index,   new Point3f(x,      y,       0.0f));
 108:                                quadArray.setCoordinate(index+1, new Point3f(x+delta,y,       0.0f));
 109:                                quadArray.setCoordinate(index+2, new Point3f(x+delta,y+delta, 0.0f));
 110:                                quadArray.setCoordinate(index+3, new Point3f(x,      y+delta, 0.0f));
 111:                                
 112:                                quadArray.setNormal(index,     new Vector3f(0.0f,0.0f,0.1f));
 113:                                quadArray.setNormal(index+1,   new Vector3f(0.0f,0.0f,0.1f));
 114:                                quadArray.setNormal(index+2,   new Vector3f(0.0f,0.0f,0.1f));
 115:                                quadArray.setNormal(index+3,   new Vector3f(0.0f,0.0f,0.1f));
 116:                                
 117:                                index += 4;
 118:                        }
 119:                }
 120:                return quadArray;
 121:        }
 122:        
 123:        public GeometryArray createGeometryArray1(){
 124:                
 125:                float x,y;
 126:                float delta=0.25f;
 127:                
 128:                QuadArray quadArray = new QuadArray(256, QuadArray.COORDINATES | QuadArray.NORMALS );
 129:                int index = 0;
 130:                for(int i=0; i<8; i++){
 131:                        for(int j=0; j<8; j++){
 132:                                x = -1.0f+0.25f*j;
 133:                                y = -1.0f+0.25f*i;
 134:                                quadArray.setCoordinate(index,     new Point3f(cos(PI*x),        y,      sin(PI*x)));
 135:                                quadArray.setCoordinate(index+1,   new Point3f(cos(PI*(x+delta)),y,      sin(PI*(x+delta))));
 136:                                quadArray.setCoordinate(index+2,   new Point3f(cos(PI*(x+delta)),y+delta,sin(PI*(x+delta))));
 137:                                quadArray.setCoordinate(index+3,   new Point3f(cos(PI*x),        y+delta,sin(PI*x)));
 138:                                
 139:                                quadArray.setNormal(index,     new Vector3f(cos(PI*x),0.0f,sin(PI*x)));
 140:                                quadArray.setNormal(index+1,   new Vector3f(cos(PI*(x+delta)),0.0f,sin(PI*(x+delta))));
 141:                                quadArray.setNormal(index+2,   new Vector3f(cos(PI*(x+delta)),0.0f,sin(PI*(x+delta))));
 142:                                quadArray.setNormal(index+3,   new Vector3f(cos(PI*x),0.0f,sin(PI*x)));
 143:                                
 144:                                index += 4;
 145:                        }
 146:                }
 147:                return quadArray;
 148:        }
 149:        
 150:        
 151:        private float sin(float x){
 152:                return (new Double(Math.sin(new Double(x).doubleValue()))).floatValue();
 153:        }
 154:        
 155:        private float cos(float x){
 156:                return (new Double(Math.cos(new Double(x).doubleValue()))).floatValue();
 157:        }
 158:        
 159:        private Appearance createAppearance() {
 160:                
 161:                // Define colors
 162:                Color3f black        = new Color3f(0.0f,  0.0f,  0.0f);
 163:                Color3f red          = new Color3f(0.8f,  0.2f,  0.2f);
 164:                Color3f ambientRed   = new Color3f(0.8f,  0.05f, 0.0f);
 165:                Color3f specular     = new Color3f(0.7f,  0.7f,  0.7f);
 166:
 167:                
 168:                Material redMaterial = new Material(ambientRed, black, red, specular, 75.0f);
 169:                redMaterial.setLightingEnable(true);
 170:                TransparencyAttributes opaque = new TransparencyAttributes(TransparencyAttributes.NONE,0.0f);
 171:                Appearance redAppearance = new Appearance();
 172:                redAppearance.setMaterial(redMaterial);
 173:                redAppearance.setTransparencyAttributes(opaque);
 174:                PolygonAttributes bothFaces = new PolygonAttributes();
 175:                bothFaces.setCullFace(PolygonAttributes.CULL_NONE); //want to see the backs
 176:                redAppearance.setPolygonAttributes(bothFaces);
 177:                
 178:                return redAppearance;
 179:        }
 180:        
 181:        public static void main(String[] args){
 182:                Frame frame = new MainFrame(new MorphExample(), 256, 256);
 183:        }       
 184:        
 185:}       
previous  start  toc  next