Les textures en Java 3D
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Novembre 2001 */
import java.applet.Applet;
import java.awt.BorderLayout;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.geometry.Sphere;
import com.sun.j3d.utils.universe.*;
import java.awt.GraphicsConfiguration;
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.image.TextureLoader;
import javax.media.j3d.*;
import javax.vecmath.*;
import java.util.Enumeration;
import java.awt.*;
import java.net.*;
import java.awt.image.*;
public class SimpleTexture extends Applet {
static boolean applet = true ;
private String base ;
private Texture chercheTexture(String s) {
if ( applet ) {
try {
String adresse = base+s ;
URL u1 = new URL(adresse);
return(new TextureLoader(u1,this).getTexture()) ; }
catch (MalformedURLException e) { return(new Texture2D());} }
else
return(new TextureLoader(s,this).getTexture());
}
public BranchGroup createSceneGraph(SimpleUniverse u) {
BranchGroup objRoot = new BranchGroup();
BoundingSphere largeBounds;
largeBounds = new BoundingSphere(new Point3d(0.0,0.0,0.0),100.0);
{ Vector3f ldir = new Vector3f(1.0F,1.0F,-2.0F);
Color3f lcouldl = new Color3f(1.0F,1.0F,1.0F);
DirectionalLight dl = new DirectionalLight(lcouldl,ldir);
dl.setInfluencingBounds(largeBounds);
objRoot.addChild(dl); }
{ Color3f lcoulal = new Color3f(0.3F,0.3F,0.1F);
AmbientLight al = new AmbientLight(lcoulal);
al.setInfluencingBounds(largeBounds);
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();
Texture t = chercheTexture("Prairie.jpg");
a.setTexture(t);
objTrans.addChild(new Sphere(0.18F,Sphere.GENERATE_TEXTURE_COORDS,100,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();
Texture t = chercheTexture("Savon.jpg");
a.setTexture(t);
objTrans.addChild(new Cone(0.14F,0.28F,Cone.GENERATE_TEXTURE_COORDS,100,100,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();
Texture t = chercheTexture("Parquet.jpg");
a.setTexture(t);
objTrans.addChild(new Cylinder(0.14F,0.28F,Cylinder.GENERATE_TEXTURE_COORDS,100,100,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();
Texture t = chercheTexture("Bois.jpg");
a.setTexture(t);
objTrans.addChild(new Box(0.05F,0.13F,0.22F,Box.GENERATE_TEXTURE_COORDS,a)); }
objRoot.compile();
return objRoot;
}
public void lanceJava3D() {
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 void init() {
if ( applet ) {
URL url = this.getCodeBase();
base = url.toString();
lanceJava3D(); }
}
public SimpleTexture() {
if ( !applet ) {
lanceJava3D(); }
}
public static void main(String[] args) {
applet = false ;
new MainFrame(new SimpleTexture(),600,150);
}
}
Remarques,erreurs
nicolas.janey@univ-fcomte.fr