
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.GraphicsConfiguration;

import javax.media.j3d.Appearance;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Background;
import javax.media.j3d.Transform3D;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.Material;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.AmbientLight;
import javax.media.j3d.PointLight;
import javax.media.j3d.TexCoordGeneration;
import javax.media.j3d.Texture;
import javax.media.j3d.TextureAttributes;
import javax.vecmath.Color3f;
import javax.vecmath.Color4f;
import javax.vecmath.Point3d;
import javax.vecmath.Point3f;
import com.sun.j3d.utils.geometry.Sphere;
import com.sun.j3d.utils.geometry.Primitive;
import com.sun.j3d.utils.image.TextureLoader;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.universe.SimpleUniverse;

public class MagicSphere extends Applet
{
	
	private static final long serialVersionUID = 1L;
	
	private SimpleUniverse u = null;
	
	public BranchGroup createSceneGraph()
	{
		BranchGroup bg = new BranchGroup();
		
		Color3f weiss = new Color3f(1.0f, 1.0f, 1.0f);
		Color3f blau = new Color3f(0.0f, 0.0f, 0.7f);
		Color3f schwarz = new Color3f(0.0f, 0.0f, 0.0f);
		Color3f hellblau = new Color3f(0.3f, 0.3f, 1.0f);
		
		Material matSphere = new Material(blau, schwarz, hellblau, weiss, 100.0f);
		Appearance appSphere = new Appearance();
		appSphere.setMaterial(matSphere);
		BoundingSphere bounds = new BoundingSphere (new Point3d(1.0, 1.0, 1.0),100.0);
		AmbientLight licht = new AmbientLight();
		licht.setInfluencingBounds(bounds);
		Point3f lichtQuelle = new Point3f(-0.5f, -0.5f, 1.5f);
		Point3f abnahme = new Point3f(0.4f, 0.0f, 1.0f);
		PointLight pl = new PointLight(weiss, lichtQuelle, abnahme);
		pl.setInfluencingBounds(bounds);
		Sphere sphere = new Sphere((float) 0.3, Sphere.GENERATE_NORMALS, 80, appSphere);
		bg.addChild(sphere);
		bg.addChild(licht);
		bg.addChild(pl);
		bg.compile();
		return bg;
	}
		public BranchGroup createBackground()
		{
			BranchGroup bg = new BranchGroup();
			Background back = new Background();
			back.setApplicationBounds(getBoundingSphere());
			BranchGroup bgGeometry = new BranchGroup();
			Appearance app = new Appearance();
			Texture tex = new TextureLoader("d:/eclipse/textout/background.jpg", null).getTexture();
			app.setTexture(tex);
			app.setTexCoordGeneration(new TexCoordGeneration(
					TexCoordGeneration.SPHERE_MAP,TexCoordGeneration.TEXTURE_COORDINATE_2));
			app.setTextureAttributes(new TextureAttributes(TextureAttributes.REPLACE,
					new Transform3D(), new Color4f(),
					TextureAttributes.FASTEST));
			Sphere sphere = new Sphere(1.0f, Primitive.GENERATE_TEXTURE_COORDS |
					Primitive.GENERATE_NORMALS_INWARD, 40, app);
			bgGeometry.addChild(sphere);
			back.setGeometry(bgGeometry);
			bg.addChild(back);
			return bg;
		}
		BoundingSphere getBoundingSphere()
		{
			return new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 200);
		}
		public void init()
		{
			this.setLayout(new BorderLayout());
			GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
			Canvas3D c = new Canvas3D(config);
			this.add("Center", c);
			u = new SimpleUniverse(c);
			u.getViewingPlatform().setNominalViewingTransform();
			u.addBranchGraph(this.createBackground());
			u.addBranchGraph(this.createSceneGraph());
		}
		public void destroy()
		{
			u.cleanup();
		}
		public static void main(String[] argv)
		{
			new MainFrame(new MagicSphere(), 512, 512);
		}

}
