package unit5;
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.GraphicsConfiguration;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.geometry.Sphere;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.vecmath.*;
public class SimpleLight extends Applet{
Appearance createAppearance() {
Appearance appear = new
Appearance();
Material material = new
Material();
appear.setMaterial(material);
return
appear;
}
public BranchGroup createSceneGraph() {
BranchGroup sceneRoot = new
BranchGroup();
BoundingSphere bounds =new BoundingSphere(new Point3d(0.0,0.0,0.0),
100.0);
// Set up the background
Color
Color3f bgColor = new Color3f(1.0f, 1.0f,
1.0f);
Background bg = new Background(bgColor);
bg.setApplicationBounds(bounds);
sceneRoot.addChild(bg);
//环境光的使用
//聚光灯的使用
SpotLight lightS=new SpotLight
();
// lightS.setEnable
(true);
lightS.setPosition (new
Point3f(0.5f,0.0f,10.0f));
lightS.setColor (new
Color3f(0.0f,1.0f,1.0f));
lightS.setAttenuation (new
Point3f(1.0f,0.0f,0.0f));
lightS.setDirection (new
Vector3f(0.0f,0.0f,-1.0f));
lightS.setSpreadAngle (15.0f *
(float)Math.PI / 180.0f);
lightS.setConcentration
(100);
lightS.setInfluencingBounds
(bounds);
sceneRoot.addChild
(lightS);
Sphere ball1=new Sphere(0.5f,Sphere.GENERATE_NORMALS,createAppearance()); Sphere ball2=new Sphere(0.5f,Sphere.GENERATE_NORMALS,createAppearance()); Transform3D tr = new
Transform3D();
tr.setScale(0.8);
tr.setTranslation(new
Vector3f(0.5f, 0.0f, 0.0f));
TransformGroup objTransr = new
TransformGroup(tr);
objTransr.addChild(ball1);
Transform3D tl = new
Transform3D();
tl.setScale(0.8);
tl.setTranslation(new
Vector3f(-0.5f, 0.0f, 0.0f));
TransformGroup objTransl = new
TransformGroup(tl);
objTransl.addChild(ball2);
sceneRoot.addChild(objTransr);
sceneRoot.addChild
(objTransl);
System.out.println("hello");
return
sceneRoot;
}
public SimpleLight() {
setLayout(new
BorderLayout());
GraphicsConfiguration
config=SimpleUniverse.getPreferredConfiguration();
Canvas3D c = new
Canvas3D(config);
add("Center", c);
BranchGroup scene =
createSceneGraph();
SimpleUniverse u = new
SimpleUniverse(c);
u.getViewingPlatform().setNominalViewingTransform();
u.addBranchGraph(scene);
}
public static void main(String[] args) {
new MainFrame(new
SimpleLight(), 512, 256);
}
}