/* Auteur: Nicolas JANEY */ /* nicolas.janey@univ-fcomte.fr */ /* Novembre 2001 */ import java.applet.Applet; import java.awt.*; import com.sun.j3d.utils.applet.MainFrame; import com.sun.j3d.utils.universe.*; import com.sun.j3d.utils.geometry.*; import javax.media.j3d.*; import javax.vecmath.*; public class SimpleMaterial extends Applet { public BranchGroup createSceneGraph(SimpleUniverse u) { BranchGroup objRoot = new BranchGroup(); BoundingSphere bounds; bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0),100.0); Vector3f ldir = new Vector3f(1.0F,1.0F,-1.0F); Color3f lcouldl = new Color3f(1.0F,1.0F,1.0F); DirectionalLight dl = new DirectionalLight(lcouldl,ldir); dl.setInfluencingBounds(bounds); objRoot.addChild(dl); Color3f lcoulal = new Color3f(0.3F,0.3F,0.1F); AmbientLight al = new AmbientLight(lcoulal); al.setInfluencingBounds(bounds); objRoot.addChild(al); { TransformGroup objTrans = new TransformGroup(); objRoot.addChild(objTrans); Transform3D t1 = new Transform3D(); t1.setTranslation(new Vector3d(-0.7,0.0,0.0)) ; Transform3D t2 = new Transform3D(); t1.setRotation(new AxisAngle4d(1.0,1.0,0.0,Math.PI/5.0)) ; t1.mul(t2) ; objTrans.setTransform(t1); Appearance a = new Appearance(); Material m = new Material(); a.setMaterial(m); objTrans.addChild(new Sphere(0.18F,a)); } { TransformGroup objTrans = new TransformGroup(); objRoot.addChild(objTrans); Transform3D t1 = new Transform3D(); t1.setTranslation(new Vector3d(-0.25,0.0,0.0)) ; Transform3D t2 = new Transform3D(); t1.setRotation(new AxisAngle4d(1.0,1.0,0.0,Math.PI/5.0)) ; t1.mul(t2) ; objTrans.setTransform(t1); Appearance a = new Appearance(); Material m = new Material(); m.setDiffuseColor(0.5f,1.0f,0.2f); m.setAmbientColor(0.5f,0.6f,0.8f); a.setMaterial(m); objTrans.addChild(new Cone(0.14F,0.28F,1,a)); } { TransformGroup objTrans = new TransformGroup(); objRoot.addChild(objTrans); Transform3D t1 = new Transform3D(); t1.setTranslation(new Vector3d(0.25,0.0,0.0)) ; Transform3D t2 = new Transform3D(); t1.setRotation(new AxisAngle4d(1.0,1.0,0.0,Math.PI/5.0)) ; t1.mul(t2) ; objTrans.setTransform(t1); Appearance a = new Appearance(); Material m = new Material(); m.setDiffuseColor(0.1f,0.1f,0.9f); m.setSpecularColor(1.0f,0.0f,0.0f); m.setAmbientColor(0.9f,0.9f,0.9f); m.setShininess(25.0f); a.setMaterial(m); objTrans.addChild(new Cylinder(0.14F,0.28F,a)); } { TransformGroup objTrans = new TransformGroup(); objRoot.addChild(objTrans); Transform3D t1 = new Transform3D(); t1.setTranslation(new Vector3d(0.7,0.0,0.0)) ; Transform3D t2 = new Transform3D(); t1.setRotation(new AxisAngle4d(1.0,1.0,0.0,Math.PI/5.0)) ; t1.mul(t2) ; objTrans.setTransform(t1); Appearance a = new Appearance(); Material m = new Material(); m.setDiffuseColor(0.9f,0.8f,0.4f); m.setAmbientColor(0.6f,0.0f,0.6f); a.setMaterial(m); objTrans.addChild(new Box(0.05F,0.13F,0.22F,a)); } objRoot.compile(); return objRoot; } public SimpleMaterial() { setLayout(new BorderLayout()); GraphicsConfiguration config; config = SimpleUniverse.getPreferredConfiguration(); Canvas3D c = new Canvas3D(config); add("Center",c); SimpleUniverse u = new SimpleUniverse(c); BranchGroup scene = createSceneGraph(u); u.getViewingPlatform().setNominalViewingTransform(); u.addBranchGraph(scene); } public static void main(String[] args) { new MainFrame(new SimpleMaterial(),600,150); } }