LightScopeApp creates a scene that is paritally light : 光 « 三维图形动画 « Java

En
Java
1. 图形用户界面
2. 三维图形动画
3. 高级图形
4. 蚂蚁编译
5. Apache类库
6. 统计图
7. 
8. 集合数据结构
9. 数据类型
10. 数据库JDBC
11. 设计模式
12. 开发相关类
13. EJB3
14. 电子邮件
15. 事件
16. 文件输入输出
17. 游戏
18. 泛型
19. GWT
20. Hibernate
21. 本地化
22. J2EE平台
23. 基于J2ME
24. JDK-6
25. JNDI的LDAP
26. JPA
27. JSP技术
28. JSTL
29. 语言基础知识
30. 网络协议
31. PDF格式RTF格式
32. 映射
33. 常规表达式
34. 脚本
35. 安全
36. Servlets
37. Spring
38. Swing组件
39. 图形用户界面
40. SWT-JFace-Eclipse
41. 线程
42. 应用程序
43. Velocity
44. Web服务SOA
45. 可扩展标记语言
Java 教程
Java » 三维图形动画 » 屏幕截图 
LightScopeApp creates a scene that is paritally light
LightScopeApp creates a scene that is paritally light


/*
 *      LightScopeApp.java 1.0 99/04/12
 *
 * Copyright (c) 1999 Sun Microsystems, Inc. All Rights Reserved.
 *
 * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
 * modify and redistribute this software in source and binary code form,
 * provided that i) this copyright notice and license appear on all copies of
 * the software; and ii) Licensee does not utilize the software in a manner
 * which is disparaging to Sun.
 *
 * This software is provided "AS IS," without a warranty of any kind. ALL
 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
 * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
 * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
 * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
 * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
 * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
 * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
 * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
 * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES.
 *
 * This software is not designed or intended for use in on-line control of
 * aircraft, air traffic, aircraft navigation or aircraft communications; or in
 * the design, construction, operation or maintenance of any nuclear
 * facility. Licensee represents and warrants that it will not use or
 * redistribute the Software for such purposes.
 */

/*
 * This application demonstrates the use of scoping for controlling
 * the influence of lights.
 *
 * This applicaion is not necessarilly a demonstration of good
 * program design or coding.
 *
 * This application (or a version of it) generated one or more
 * of the images in Chapter 6 of Getting Started with the Java 3D API.
 * The Java 3D Turtorial.
 *
 * See http://www.sun.com/desktop/java3d/collateral for more information.
 *
 */

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.0f1.0f0.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[] 1010 };
    TriangleStripArray lampGeom = new TriangleStripArray(20,
        GeometryArray.COORDINATES | GeometryArray.NORMALS, stripCounts);
    lampGeom.setCoordinate(0new Point3f(-0.01f0.9f0.01f));
    lampGeom.setCoordinate(1new Point3f(-0.01f0.0f0.01f));
    lampGeom.setCoordinate(2new Point3f(0.01f0.9f0.01f));
    lampGeom.setCoordinate(3new Point3f(0.01f0.0f0.01f));
    lampGeom.setCoordinate(4new Point3f(0.01f0.9f, -0.01f));
    lampGeom.setCoordinate(5new Point3f(0.01f0.0f, -0.01f));
    lampGeom.setCoordinate(6new Point3f(-0.01f0.9f, -0.01f));
    lampGeom.setCoordinate(7new Point3f(-0.01f0.0f, -0.01f));
    lampGeom.setCoordinate(8new Point3f(-0.01f0.9f0.01f));
    lampGeom.setCoordinate(9new Point3f(-0.01f0.0f0.01f));
    lampGeom.setCoordinate(10new Point3f(-0.1f0.9f0.1f));
    lampGeom.setCoordinate(11new Point3f(-0.2f0.5f0.2f));
    lampGeom.setCoordinate(12new Point3f(0.1f0.9f0.1f));
    lampGeom.setCoordinate(13new Point3f(0.2f0.5f0.2f));
    lampGeom.setCoordinate(14new Point3f(0.1f0.9f, -0.1f));
    lampGeom.setCoordinate(15new Point3f(0.2f0.5f, -0.2f));
    lampGeom.setCoordinate(16new Point3f(-0.1f0.9f, -0.1f));
    lampGeom.setCoordinate(17new Point3f(-0.2f0.5f, -0.2f));
    lampGeom.setCoordinate(18new Point3f(-0.1f0.9f0.1f));
    lampGeom.setCoordinate(19new Point3f(-0.2f0.5f0.2f));

    Vector3f norm = new Vector3f(-0.7f0.0f0.7f);
    lampGeom.setNormal(0, norm);
    lampGeom.setNormal(1, norm);
    norm.set(0.7f0.0f0.7f);
    lampGeom.setNormal(2, norm);
    lampGeom.setNormal(3, norm);
    norm.set(0.7f0.0f, -0.7f);
    lampGeom.setNormal(4, norm);
    lampGeom.setNormal(5, norm);
    norm.set(-0.7f0.0f, -0.7f);
    lampGeom.setNormal(6, norm);
    lampGeom.setNormal(7, norm);
    norm.set(-0.7f0.0f0.7f);
    lampGeom.setNormal(8, norm);
    lampGeom.setNormal(9, norm);
    norm.set(-0.7f0.0f0.7f);
    lampGeom.setNormal(10, norm);
    lampGeom.setNormal(11, norm);
    norm.set(0.7f0.0f0.7f);
    lampGeom.setNormal(12, norm);
    lampGeom.setNormal(13, norm);
    norm.set(0.7f0.0f, -0.7f);
    lampGeom.setNormal(14, norm);
    lampGeom.setNormal(15, norm);
    norm.set(-0.7f0.0f, -0.7f);
    lampGeom.setNormal(16, norm);
    lampGeom.setNormal(17, norm);
    norm.set(-0.7f0.0f0.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.0f1.0f1.0f);
    Color3f red = new Color3f(1.0f0.0f0.0f);
    Color3f blue = new Color3f(0.0f1.0f0.0f);
    Color3f green = new Color3f(0.0f0.0f1.0f);
    Color3f black = new Color3f(0.0f0.0f0.0f);

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

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

    transVector.set(-0.4f0.001f0.1f);
    transTransform.setTranslation(transVector);
    lampTG.setTransform(transTransform);

    transVector.set(-0.2f0.1f0.2f);
    transTransform.setTranslation(transVector);
    litBoxTG.setTransform(transTransform);

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

    Shape3D tablePlane = createXZPlane(new Point3f(-1.0f0.0f, -1.0f),
        new Point3f(-1.0f0.0f1.0f)new Point3f(1.0f0.0f1.0f),
        new Point3f(1.0f0.0f, -1.0f));
    tablePlane.setAppearance(createMaterialAppearance(white));
    tableTG.addChild(tablePlane);
    litBoxTG.addChild(new Box(0.1f0.1f0.1f, Box.GENERATE_NORMALS,
        createMaterialAppearance(red)));
    Shape3D shadowPlane = createXZPlane(new Point3f(0.1f, -0.095f, -0.1f),
        new Point3f(0.1f, -0.095f0.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.1f0.1f0.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.1f0.5f, -0.1f);
    lampLight.setInfluencingBounds(new BoundingSphere());
    lampTG.addChild(lampLight);

    Shape3D litPlane = createXZPlane(new Point3f(-0.4f0.0f, -0.4f),
        new Point3f(-0.4f0.0f0.4f)new Point3f(0.4f0.0f0.4f),
        new Point3f(0.4f0.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.4f0.4f0.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.2f0.2f0.2f));
    lightDir.set(1.0f, -1.0f, -1.0f);
    lightDir.normalize();
    lightD2.setDirection(lightDir);
    scene.addChild(lightD2);

    Background bg = new Background();
    bg.setColor(1.0f1.0f1.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()256256);
  }
}



           
       
Related examples in the same category
1. 光演示光演示
2. 创建一个环境光线和一个定向光创建一个环境光线和一个定向光
3. This builds a red sphere using the Sphere utility class and adds lightsThis builds a red sphere using the Sphere utility class and adds lights
4. ExDirectionalLight -说明使用方向灯ExDirectionalLight -说明使用方向灯
5. ExAmbientLight -说明使用环境灯ExAmbientLight -说明使用环境灯
6. ExPointLight -说明使用指向灯ExPointLight -说明使用指向灯
7. ExLightScope -说明使用光范围ExLightScope -说明使用光范围
8. Illustrate use of light influencing bounds, and bounding leaves Illustrate use of light influencing bounds, and bounding leaves
9. ExSpotLight -说明使用射灯ExSpotLight -说明使用射灯
10. AmbientLight, DirectionalLight, PointLight and SpotLightAmbientLight, DirectionalLight, PointLight and SpotLight
11. 照明平面照明平面
12. 聚光灯聚光灯
13. 光查看光查看
14. 轻缺陷轻缺陷
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.