`
kennyluo
  • 浏览: 78217 次
  • 性别: Icon_minigender_1
  • 来自: 珠海
社区版块
存档分类
最新评论

LightScopeApp creates a scene that is paritally light

阅读更多
import java.applet.Applet;
import java.awt.BorderLayout;

import javax.media.j3d.AmbientLight;
import javax.media.j3d.Appearance;
import javax.media.j3d.Background;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.DirectionalLight;
import javax.media.j3d.GeometryArray;
import javax.media.j3d.Material;
import javax.media.j3d.PointLight;
import javax.media.j3d.PolygonAttributes;
import javax.media.j3d.QuadArray;
import javax.media.j3d.Shape3D;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.media.j3d.TriangleStripArray;
import javax.vecmath.Color3f;
import javax.vecmath.Point3f;
import javax.vecmath.Vector3f;

import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.geometry.Box;
import com.sun.j3d.utils.universe.SimpleUniverse;

/**
 * LightScopeApp creates a scene that is paritally light by a light using the
 * scoping feature.
 */
public class LightScopeApp extends Applet {

  Appearance createMaterialAppearance(Color3f color) {
    Appearance appear = new Appearance();
    Material material = new Material();
    material.setDiffuseColor(color);
    material.setShininess(50.0f);
    appear.setMaterial(material);

    return appear;
  }

  Shape3D createXZPlane(Point3f p0, Point3f p1, Point3f p2, Point3f p3) {
    Shape3D plane = new Shape3D();
    QuadArray planeGeom = new QuadArray(4, QuadArray.COORDINATES
        | QuadArray.NORMALS);
    planeGeom.setCoordinate(0, p0);
    planeGeom.setCoordinate(1, p1);
    planeGeom.setCoordinate(2, p2);
    planeGeom.setCoordinate(3, p3);
    Vector3f norm = new Vector3f(0.0f, 1.0f, 0.0f);
    planeGeom.setNormal(0, norm);
    planeGeom.setNormal(1, norm);
    planeGeom.setNormal(2, norm);
    planeGeom.setNormal(3, norm);
    plane.setGeometry(planeGeom);
    return plane;
  }

  Shape3D createLampShape() {
    Shape3D lamp = new Shape3D();
    int stripCounts[] = { 10, 10 };
    TriangleStripArray lampGeom = new TriangleStripArray(20,
        GeometryArray.COORDINATES | GeometryArray.NORMALS, stripCounts);
    lampGeom.setCoordinate(0, new Point3f(-0.01f, 0.9f, 0.01f));
    lampGeom.setCoordinate(1, new Point3f(-0.01f, 0.0f, 0.01f));
    lampGeom.setCoordinate(2, new Point3f(0.01f, 0.9f, 0.01f));
    lampGeom.setCoordinate(3, new Point3f(0.01f, 0.0f, 0.01f));
    lampGeom.setCoordinate(4, new Point3f(0.01f, 0.9f, -0.01f));
    lampGeom.setCoordinate(5, new Point3f(0.01f, 0.0f, -0.01f));
    lampGeom.setCoordinate(6, new Point3f(-0.01f, 0.9f, -0.01f));
    lampGeom.setCoordinate(7, new Point3f(-0.01f, 0.0f, -0.01f));
    lampGeom.setCoordinate(8, new Point3f(-0.01f, 0.9f, 0.01f));
    lampGeom.setCoordinate(9, new Point3f(-0.01f, 0.0f, 0.01f));
    lampGeom.setCoordinate(10, new Point3f(-0.1f, 0.9f, 0.1f));
    lampGeom.setCoordinate(11, new Point3f(-0.2f, 0.5f, 0.2f));
    lampGeom.setCoordinate(12, new Point3f(0.1f, 0.9f, 0.1f));
    lampGeom.setCoordinate(13, new Point3f(0.2f, 0.5f, 0.2f));
    lampGeom.setCoordinate(14, new Point3f(0.1f, 0.9f, -0.1f));
    lampGeom.setCoordinate(15, new Point3f(0.2f, 0.5f, -0.2f));
    lampGeom.setCoordinate(16, new Point3f(-0.1f, 0.9f, -0.1f));
    lampGeom.setCoordinate(17, new Point3f(-0.2f, 0.5f, -0.2f));
    lampGeom.setCoordinate(18, new Point3f(-0.1f, 0.9f, 0.1f));
    lampGeom.setCoordinate(19, new Point3f(-0.2f, 0.5f, 0.2f));

    Vector3f norm = new Vector3f(-0.7f, 0.0f, 0.7f);
    lampGeom.setNormal(0, norm);
    lampGeom.setNormal(1, norm);
    norm.set(0.7f, 0.0f, 0.7f);
    lampGeom.setNormal(2, norm);
    lampGeom.setNormal(3, norm);
    norm.set(0.7f, 0.0f, -0.7f);
    lampGeom.setNormal(4, norm);
    lampGeom.setNormal(5, norm);
    norm.set(-0.7f, 0.0f, -0.7f);
    lampGeom.setNormal(6, norm);
    lampGeom.setNormal(7, norm);
    norm.set(-0.7f, 0.0f, 0.7f);
    lampGeom.setNormal(8, norm);
    lampGeom.setNormal(9, norm);
    norm.set(-0.7f, 0.0f, 0.7f);
    lampGeom.setNormal(10, norm);
    lampGeom.setNormal(11, norm);
    norm.set(0.7f, 0.0f, 0.7f);
    lampGeom.setNormal(12, norm);
    lampGeom.setNormal(13, norm);
    norm.set(0.7f, 0.0f, -0.7f);
    lampGeom.setNormal(14, norm);
    lampGeom.setNormal(15, norm);
    norm.set(-0.7f, 0.0f, -0.7f);
    lampGeom.setNormal(16, norm);
    lampGeom.setNormal(17, norm);
    norm.set(-0.7f, 0.0f, 0.7f);
    lampGeom.setNormal(18, norm);
    lampGeom.setNormal(19, norm);

    lamp.setGeometry(lampGeom);
    return lamp;
  }

  BranchGroup createScene() {
    BranchGroup scene = new BranchGroup();
    TransformGroup tableTG = new TransformGroup();
    TransformGroup lampTG = new TransformGroup();
    TransformGroup litBoxTG = new TransformGroup();
    TransformGroup unLitBoxTG = new TransformGroup();

    scene.addChild(tableTG);
    tableTG.addChild(lampTG);
    tableTG.addChild(litBoxTG);
    tableTG.addChild(unLitBoxTG);

    Color3f white = new Color3f(1.0f, 1.0f, 1.0f);
    Color3f red = new Color3f(1.0f, 0.0f, 0.0f);
    Color3f blue = new Color3f(0.0f, 1.0f, 0.0f);
    Color3f green = new Color3f(0.0f, 0.0f, 1.0f);
    Color3f black = new Color3f(0.0f, 0.0f, 0.0f);

    Vector3f transVector = new Vector3f();
    Transform3D transTransform = new Transform3D();

    transVector.set(0.0f, -0.4f, 0.5f);
    transTransform.setTranslation(transVector);
    tableTG.setTransform(transTransform);

    transVector.set(-0.4f, 0.001f, 0.1f);
    transTransform.setTranslation(transVector);
    lampTG.setTransform(transTransform);

    transVector.set(-0.2f, 0.1f, 0.2f);
    transTransform.setTranslation(transVector);
    litBoxTG.setTransform(transTransform);

    transVector.set(0.3f, 0.1f, -0.4f);
    transTransform.setTranslation(transVector);
    unLitBoxTG.setTransform(transTransform);

    Shape3D tablePlane = createXZPlane(new Point3f(-1.0f, 0.0f, -1.0f),
        new Point3f(-1.0f, 0.0f, 1.0f), new Point3f(1.0f, 0.0f, 1.0f),
        new Point3f(1.0f, 0.0f, -1.0f));
    tablePlane.setAppearance(createMaterialAppearance(white));
    tableTG.addChild(tablePlane);
    litBoxTG.addChild(new Box(0.1f, 0.1f, 0.1f, Box.GENERATE_NORMALS,
        createMaterialAppearance(red)));
    Shape3D shadowPlane = createXZPlane(new Point3f(0.1f, -0.095f, -0.1f),
        new Point3f(0.1f, -0.095f, 0.1f), new Point3f(0.2f, -0.095f,
            0.15f), new Point3f(0.2f, -0.095f, -0.15f));
    shadowPlane.setAppearance(createMaterialAppearance(black));
    litBoxTG.addChild(shadowPlane);

    Appearance redGlowMat = createMaterialAppearance(red);
    //    redGlowMat.getMaterial().setEmissiveColor(0.5f, 0.5f, 0.5f);
    unLitBoxTG.addChild(new Box(0.1f, 0.1f, 0.1f, Box.GENERATE_NORMALS,
        redGlowMat));

    Shape3D lamp = createLampShape();
    Appearance lampAppearance = createMaterialAppearance(blue);
    PolygonAttributes polyAttrib = new PolygonAttributes();
    polyAttrib.setCullFace(PolygonAttributes.CULL_NONE);
    polyAttrib.setBackFaceNormalFlip(true);
    lampAppearance.setPolygonAttributes(polyAttrib);
    lamp.setAppearance(lampAppearance);
    lampTG.addChild(lamp);

    PointLight lampLight = new PointLight();
    lampLight.setPosition(0.1f, 0.5f, -0.1f);
    lampLight.setInfluencingBounds(new BoundingSphere());
    lampTG.addChild(lampLight);

    Shape3D litPlane = createXZPlane(new Point3f(-0.4f, 0.0f, -0.4f),
        new Point3f(-0.4f, 0.0f, 0.4f), new Point3f(0.4f, 0.0f, 0.4f),
        new Point3f(0.4f, 0.0f, -0.4f));
    litPlane.setAppearance(createMaterialAppearance(white));
    lampTG.addChild(litPlane);

    lampLight.addScope(lampTG);
    lampLight.addScope(litBoxTG);

    AmbientLight lightA = new AmbientLight();
    lightA.setInfluencingBounds(new BoundingSphere());
    scene.addChild(lightA);

    DirectionalLight lightD1 = new DirectionalLight();
    lightD1.setInfluencingBounds(new BoundingSphere());
    lightD1.setColor(new Color3f(0.4f, 0.4f, 0.4f));
    Vector3f lightDir = new Vector3f(-1.0f, -1.0f, -1.0f);
    lightDir.normalize();
    lightD1.setDirection(lightDir);
    scene.addChild(lightD1);

    DirectionalLight lightD2 = new DirectionalLight();
    lightD2.setInfluencingBounds(new BoundingSphere());
    lightD2.setColor(new Color3f(0.2f, 0.2f, 0.2f));
    lightDir.set(1.0f, -1.0f, -1.0f);
    lightDir.normalize();
    lightD2.setDirection(lightDir);
    scene.addChild(lightD2);

    Background bg = new Background();
    bg.setColor(1.0f, 1.0f, 1.0f);
    bg.setApplicationBounds(new BoundingSphere());
    scene.addChild(bg);

    return scene;
  }

  public LightScopeApp() {
    setLayout(new BorderLayout());
    Canvas3D c = new Canvas3D(null);
    add("Center", c);

    BranchGroup scene = createScene();
    scene.compile();

    SimpleUniverse u = new SimpleUniverse(c);

    // This will move the ViewPlatform back a bit so the
    // objects in the scene can be viewed.
    u.getViewingPlatform().setNominalViewingTransform();

    u.addBranchGraph(scene);
  }

  public static void main(String argv[]) {
    new MainFrame(new LightScopeApp(), 256, 256);
  }
}

 

分享到:
评论

相关推荐

    MFC ModifyStyle

    WS_DISABLED Creates a window that is initially disabled. WS_DLGFRAME Creates a window with a double border but no title. WS_GROUP Specifies the first control of a group of controls in which the user...

    apng2gif.sf.net.rar

    gif2apng is a simple program that converts animations from GIF to APNG format. It disproves the common misconception that Animated PNG files are always too big, as it actually creates APNG files ...

    A cross-platform command-line utility that creates project.zip

    A cross-platform command-line utility that creates project.zip

    obscene:最小场景图库

    可建设的//Creates a scene node at the origin with no rotationvar origin = obscene.origin();//Creates a scene node translated forward 1 unitvar forward = obscene.translate([1, 0, 0]);//Creates a scene ...

    NameService

    A naming context is an object that contains a set of name bindings in which each name is unique. Different names can be bound to an object in the same or different contexts at the same time. There is...

    an improved illumination model for shaded display

    A visible surface algorithm creates this tree for each pixel of the display and passes it to the shader. The shader then traverses the tree to determine the intensity of the light received by the ...

    This program takes customers orders by code then creates a b

    This program takes customers orders by code then creates a bill for the purchases. Also can print the bill. All totals are calculated automatically客户购物和打印程序

    Nature Shaders v1.0.4

    Nature Shaders is a collection of shaders for your vegetation that brings you player interaction, wind simulations, better quality shading, and more. You can easily convert and import models from any...

    Map.Framework.A.Formal.Model.of.Maps

    The map data type presented in this book creates new mechanisms for the storage, analysis, and computation of map data objects in any field that represents data in a map form. The authors develop a ...

    .Net下的文件监视系统

    The remoting system creates a proxy object that represents the class and returns to the client object a reference to the proxy. When the client calls a method, the remoting infrastructure handles the...

    Virtually Intelligent Product Systems: Digital and Physical Twins.pdf

    from the object itself and then mirror or twin that object is a concept referred to as the Digital Twin. The Digital Twin is receiving a great deal of interest from manufacturers who make advanced ...

    Android代码-simplegraph

    If you clone the whole project, main app is a sample app that you can run and see simplegraph functionality in action. Why this library is out there? Well, I've spent too much time looking for library...

    c或者c++: Structures 结构

    Now write a program that creates a single instance of a structure of type Struct2. Read its member variables from the console one at a time. Create a second instance of Struct2 and copy al the ...

    To add a new job login workflow

    Create Sample Node: creates a new sample with a specific entity template and lifecycle. Create Test Node: assigns a test to a sample. Create Tests Node: assigns a set of tests from a test schedule to ...

    A Fast Elitist Non-Dominated Sorting Genetic

    selection operator is presented which creates a mating pool by combining the parent and child populations and selecting the best (with respect to fitness and spread) solutions. Simulation results on ...

    A Neural Algorithm of Artistic Style(一个艺术风格化的神经网络算法)

    Here we introduce an artificial system based on a Deep Neural Network that creates artistic images of high perceptual quality. The system uses neural representations to sepa- rate and recombine ...

    史蒂芬·霍金的博士论文,Stephen William Hawking's doctoral thesis

    In Chapter 1 it is shown that this expansion creates grave difficulties for the Hoyle-Narlikar theory of gravitation. Chapter 2 deals with perturbations of an expanding homogeneous and isotropic ...

    once upon a time rule book

    create a story using cards that show important elements from fairy tales. One player is the storyteller and creates a story using the ingredients on her cards, trying to guide the plot toward her own ...

    WizFlow网页编辑

    users' freedom, it does ensure that the user of a program that is linked with the Library has the freedom and the wherewithal to run that program using a modified version of the Library. The precise...

    hibernate-shards.jar

    users' freedom, it does ensure that the user of a program that is linked with the Library has the freedom and the wherewithal to run that program using a modified version of the Library. The precise...

Global site tag (gtag.js) - Google Analytics