ホームに戻る
 光源

図07_00:手前からの赤い光を全反射

/*
*   JAVA3D で光源アプレット
*/

import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.GraphicsConfiguration;
import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.j3d.utils.universe.*;

/* <applet code="java3Dtest07" width=300 height=300></applet> */

public class java3Dtest07 extends Applet{
  private SimpleUniverse universe = null;

  public BranchGroup createSceneGraph(){
    BranchGroup root = new BranchGroup();

    Transform3D t3d = new Transform3D();
    t3d.rotY(Math.PI / 4);

    TransformGroup trans = new TransformGroup(t3d);
	trans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
	root.addChild(trans);

    AmbientLight a_light = new AmbientLight(new Color3f(0.0f, 0.0f, 0.0f));

    DirectionalLight d_light = new DirectionalLight(true, new Color3f(1.0f, 0.0f, 0.0f), new Vector3f(0.0f, 0.0f, 1.0f));

    Point3d point = new Point3d(0.0d, 0.0d, 0.0d);
    BoundingSphere bounds = new BoundingSphere(point, 100.0d);

    a_light.setInfluencingBounds(bounds);
    d_light.setInfluencingBounds(bounds);

    root.addChild(a_light);
    root.addChild(d_light);

    Point3d[] vertices = new Point3d[3];

    vertices[0] = new Point3d(0.0, 0.5, 0.0);
    vertices[1] = new Point3d(-0.433, -0.25, 0.0);
    vertices[2] = new Point3d(0.433, -0.25, 0.0);

    TriangleArray geometry = new TriangleArray(vertices.length, GeometryArray.COORDINATES | GeometryArray.NORMALS);
    geometry.setCoordinates(0, vertices);

    Vector3f normal = new Vector3f(0.0f, 0.0f, -1.0f);
    Vector3f[] normals = {normal, normal, normal};

    geometry.setNormals(0, normals);

    Shape3D shape = new Shape3D(geometry);
    
    Appearance app = new Appearance();

    Material mat = new Material();
    mat.setDiffuseColor(1.0f, 1.0f, 1.0f);
    mat.setAmbientColor(0.0f, 0.0f, 0.0f);

    app.setMaterial(mat);

    shape.setAppearance(app);

    trans.addChild(shape);

    Alpha rotationAlpha = new Alpha(-1, 0);
    RotationInterpolator rotator = new RotationInterpolator(rotationAlpha, trans);

    root.addChild(rotator);

    root.compile();

    return root;
  }

  public void init(){
    GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
    Canvas3D canvas3d = new Canvas3D(config);
    this.setLayout(new BorderLayout());
    this.add(canvas3d, BorderLayout.CENTER);

    BranchGroup scene = createSceneGraph();

    universe = new SimpleUniverse(canvas3d);

    universe.getViewingPlatform().setNominalViewingTransform();
    universe.addBranchGraph(scene);
  }

  public void destroy(){
    universe.cleanup();
  }
}

inserted by FC2 system