TwistStripApp demonstrates back face culling : 三维曲面 « 三维图形动画 « 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 » 三维图形动画 » 三维曲面屏幕截图 
TwistStripApp demonstrates back face culling
TwistStripApp demonstrates back face culling


/*
 * TwistStripApp.java 1.0 98/12/30
 
 * Copyright (c) 1998 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.
 */

/*
 * Getting Started with the Java 3D API written in Java 3D
 
 * TwistStripApp.java demonstrates back face culling. A 'twisted strip' is a
 * continuous surface with a two twists in it. (A Mobius strip has one twist)
 * The Class Strip creates a surface using a TriangleStripArray. A twist strip
 * is placed in a scene graph with a RotationInterpolator so the strip spins. As
 * the strip spins, when the back faces of the individual triangles face the
 * image plate, they disappear.
 
 * One twisted strip is created using the inner class (Twist). This visual
 * object is rendered as filled polygons. A second twist visual object is made
 * from the same Geometry with a different Appearance to render as lines (only).
 * The line-only strip helps to see where the filled polygon strip is.
 */

import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Frame;

import javax.media.j3d.Alpha;
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.Geometry;
import javax.media.j3d.PolygonAttributes;
import javax.media.j3d.RotationInterpolator;
import javax.media.j3d.Shape3D;
import javax.media.j3d.TransformGroup;
import javax.media.j3d.TriangleStripArray;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;

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

public class TwistStripApp extends Applet {

  /////////////////////////////////////////////////
  //
  // create Twist visual object
  //
  public class Twist extends Shape3D {

    ////////////////////////////////////////////
    //
    // create twist subgraph
    //
    public Twist() {

      this.setGeometry(createGeometry());
      this.setAppearance(createAppearance());
    // end of twist constructor

    Geometry createGeometry() {

      TriangleStripArray twistStrip;
      Color3f blue = new Color3f(0.0f0.0f1.0f);

      // create triangle strip for twist
      int N = 80;
      int stripCounts[] };
      twistStrip = new TriangleStripArray(
          N,
          TriangleStripArray.COORDINATES | TriangleStripArray.COLOR_3,
          stripCounts);

      double a;
      int v;
      for (v = 0, a = 0.0; v < N; v += 2, a = v * 2.0 * Math.PI / (N - 2)) {
        twistStrip.setCoordinate(v, new Point3d(0.7 * Math.sin(a0.2
            * Math.cos(a)0.3 * Math.sin(a)0.7 * Math.cos(a)
            0.2 * Math.cos(a)));
        twistStrip.setCoordinate(v + 1new Point3d(0.7 * Math.sin(a)
            0.2 * Math.cos(a), -0.3 * Math.sin(a)0.7
            * Math.cos(a0.2 * Math.cos(a)));
        twistStrip.setColor(v, blue);
        twistStrip.setColor(v + 1, blue);
      }

      return twistStrip;

    }

    // create Appearance for Twist Strip
    //
    // this method creates the default Appearance for the
    // twist strip. The commented line of code containting
    // the setCullFace will fix the problem of half of the
    // Twisted Strip disappearing.

    Appearance createAppearance() {

      Appearance twistAppear = new Appearance();
      PolygonAttributes polyAttrib = new PolygonAttributes();
      // polyAttrib.setCullFace(PolygonAttributes.CULL_NONE);
      twistAppear.setPolygonAttributes(polyAttrib);

      return twistAppear;
    }

  // end of class Twist

  /////////////////////////////////////////////////
  //
  // create scene graph branch group
  //
  public BranchGroup createSceneGraph() {

    BranchGroup contentRoot = new BranchGroup();

    // Create the transform group node and initialize it to the
    // identity. Add it to the root of the subgraph.
    TransformGroup objSpin = new TransformGroup();
    objSpin.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    contentRoot.addChild(objSpin);

    Shape3D twist = new Twist();
    objSpin.addChild(twist);

    // Duplicate the twist strip geometry and set the
    // appearance of the new Shape3D object to line mode
    // without culling.
    // Add the POLYGON_FILLED and POLYGON_LINE strips
    // in the scene graph at the same point.
    // This will show the triangles of the original Mobius strip that
    // are clipped. The PolygonOffset is set to prevent stitching.
    PolygonAttributes polyAttrib = new PolygonAttributes();
    polyAttrib.setCullFace(PolygonAttributes.CULL_NONE);
    polyAttrib.setPolygonMode(PolygonAttributes.POLYGON_LINE);
    polyAttrib.setPolygonOffset(0.001f);
    Appearance polyAppear = new Appearance();
    polyAppear.setPolygonAttributes(polyAttrib);
    objSpin.addChild(new Shape3D(twist.getGeometry(), polyAppear));

    Alpha rotationAlpha = new Alpha(-116000);

    RotationInterpolator rotator = new RotationInterpolator(rotationAlpha,
        objSpin);

    // a bounding sphere specifies a region a behavior is active
    // create a sphere centered at the origin with radius of 1
    BoundingSphere bounds = new BoundingSphere();
    rotator.setSchedulingBounds(bounds);
    objSpin.addChild(rotator);

    // make background white
    Background background = new Background(1.0f1.0f1.0f);
    background.setApplicationBounds(bounds);
    contentRoot.addChild(background);

    // Let Java 3D perform optimizations on this scene graph.
    contentRoot.compile();

    return contentRoot;
  // end of CreateSceneGraph method of TwistStripApp

  // Create a simple scene and attach it to the virtual universe

  public TwistStripApp() {
    setLayout(new BorderLayout());
    Canvas3D canvas3D = new Canvas3D(null);
    add("Center", canvas3D);

    BranchGroup scene = createSceneGraph();

    // SimpleUniverse is a Convenience Utility class
    SimpleUniverse simpleU = new SimpleUniverse(canvas3D);

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

    simpleU.addBranchGraph(scene);

  // end of TwistStripApp constructor

  //  The following method allows this to be run as an application

  public static void main(String[] args) {
    System.out.println("TwistStripApp - Java 3D API version 1.1");
    System.out.print("See \"Getting Started with the Java 3D API\"");
    System.out.println(" (section 2.6)");
    System.out.println("This program demonstrates back face culling.");
    System.out.print("In this program two visual objects rotate, ");
    System.out.println("one wireframe and one solid surface.");
    System.out.print("The wire frame is visible only when components");
    System.out.println(" of the surface are culled.");

    Frame frame = new MainFrame(new TwistStripApp()256256);
  // end of main method of TwistStripApp

// end of class TwistStripApp


           
       
Related examples in the same category
1. 三角平面三角平面
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.