Java/3D/3D Surface

Материал из Java эксперт
Версия от 18:01, 31 мая 2010; (обсуждение)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Triangulate a planar surface

/**********************************************************
 Copyright (C) 2001   Daniel Selman
 First distributed with the book "Java 3D Programming"
 by Daniel Selman and published by Manning Publications.
 http://manning.ru/selman
 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation, version 2.
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 The license can be found on the WWW at:
 http://www.fsf.org/copyleft/gpl.html
 Or by writing to:
 Free Software Foundation, Inc.,
 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 Authors can be contacted at:
 Daniel Selman: daniel@selman.org
 If you make changes you think others would like, please 
 contact one of the authors or someone at the 
 www.j3d.org web site.
 **************************************************************/
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.GraphicsConfigTemplate;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.net.URL;
import javax.media.j3d.Alpha;
import javax.media.j3d.Appearance;
import javax.media.j3d.AudioDevice;
import javax.media.j3d.Background;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.Bounds;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.GraphicsConfigTemplate3D;
import javax.media.j3d.Group;
import javax.media.j3d.Locale;
import javax.media.j3d.PhysicalBody;
import javax.media.j3d.PhysicalEnvironment;
import javax.media.j3d.PolygonAttributes;
import javax.media.j3d.RotationInterpolator;
import javax.media.j3d.Shape3D;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.media.j3d.View;
import javax.media.j3d.ViewPlatform;
import javax.media.j3d.VirtualUniverse;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.Vector3d;
import com.sun.j3d.audioengines.javasound.JavaSoundMixer;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.geometry.GeometryInfo;
import com.sun.j3d.utils.geometry.NormalGenerator;
import com.sun.j3d.utils.geometry.Triangulator;
/**
 * This example illustrates using the Java 3D Triangulator and NormalGenerator
 * to triangulate a planar surface with a hole in it.
 */
public class TriangulatorTest extends Java3dApplet implements ActionListener {
  private static int m_kWidth = 400;
  private static int m_kHeight = 400;
  private double[] m_VertexArray = { 1, 1, 0, //0
      0, 3, 0, //1
      1, 5, 0, //2
      2, 4, 0, //3
      4, 5, 0, //4
      3, 3, 0, //5
      4, 2, 0, //6
      4, 0, 0, //7
      3, 0, 0, //8
      2, 1, 0, //9
      // these are vertices for the hole
      1, 3, 0, //10
      2, 3, 0, //11
      3, 2, 0, //12
      3, 1, 0, //13
      2, 2, 0 };//14
  public TriangulatorTest() {
    initJava3d();
  }
  public void actionPerformed(ActionEvent event) {
  }
  protected Background createBackground() {
    return null;
  }
  protected BranchGroup createSceneBranchGroup() {
    BranchGroup objRoot = super.createSceneBranchGroup();
    TransformGroup objTrans = new TransformGroup();
    objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    Transform3D yAxis = new Transform3D();
    Alpha rotationAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE, 0, 0,
        4000, 0, 0, 0, 0, 0);
    RotationInterpolator rotator = new RotationInterpolator(rotationAlpha,
        objTrans, yAxis, 0.0f, (float) Math.PI * 2.0f);
    BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0),
        100.0);
    rotator.setSchedulingBounds(bounds);
    objTrans.addChild(rotator);
    // triangulate the polygon
    GeometryInfo gi = new GeometryInfo(GeometryInfo.POLYGON_ARRAY);
    gi.setCoordinates(m_VertexArray);
    int[] stripCountArray = { 10, 5 };
    int[] countourCountArray = { stripCountArray.length };
    gi.setContourCounts(countourCountArray);
    gi.setStripCounts(stripCountArray);
    Triangulator triangulator = new Triangulator();
    triangulator.triangulate(gi);
    NormalGenerator normalGenerator = new NormalGenerator();
    normalGenerator.generateNormals(gi);
    // create an appearance
    Appearance ap = new Appearance();
    // render as a wireframe
    PolygonAttributes polyAttrbutes = new PolygonAttributes();
    polyAttrbutes.setPolygonMode(PolygonAttributes.POLYGON_LINE);
    polyAttrbutes.setCullFace(PolygonAttributes.CULL_NONE);
    ap.setPolygonAttributes(polyAttrbutes);
    // add both a wireframe and a solid version
    // of the triangulated surface
    Shape3D shape1 = new Shape3D(gi.getGeometryArray(), ap);
    Shape3D shape2 = new Shape3D(gi.getGeometryArray());
    objTrans.addChild(shape1);
    objTrans.addChild(shape2);
    objRoot.addChild(objTrans);
    return objRoot;
  }
  public static void main(String[] args) {
    TriangulatorTest triTest = new TriangulatorTest();
    triTest.saveCommandLineArguments(args);
    new MainFrame(triTest, m_kWidth, m_kHeight);
  }
}
/*******************************************************************************
 * Copyright (C) 2001 Daniel Selman
 * 
 * First distributed with the book "Java 3D Programming" by Daniel Selman and
 * published by Manning Publications. http://manning.ru/selman
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, version 2.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * The license can be found on the WWW at: http://www.fsf.org/copyleft/gpl.html
 * 
 * Or by writing to: Free Software Foundation, Inc., 59 Temple Place - Suite
 * 330, Boston, MA 02111-1307, USA.
 * 
 * Authors can be contacted at: Daniel Selman: daniel@selman.org
 * 
 * If you make changes you think others would like, please contact one of the
 * authors or someone at the www.j3d.org web site.
 ******************************************************************************/
//*****************************************************************************
/**
 * Java3dApplet
 * 
 * Base class for defining a Java 3D applet. Contains some useful methods for
 * defining views and scenegraphs etc.
 * 
 * @author Daniel Selman
 * @version 1.0
 */
//*****************************************************************************
abstract class Java3dApplet extends Applet {
  public static int m_kWidth = 300;
  public static int m_kHeight = 300;
  protected String[] m_szCommandLineArray = null;
  protected VirtualUniverse m_Universe = null;
  protected BranchGroup m_SceneBranchGroup = null;
  protected Bounds m_ApplicationBounds = null;
  //  protected com.tornadolabs.j3dtree.Java3dTree m_Java3dTree = null;
  public Java3dApplet() {
  }
  public boolean isApplet() {
    try {
      System.getProperty("user.dir");
      System.out.println("Running as Application.");
      return false;
    } catch (Exception e) {
    }
    System.out.println("Running as Applet.");
    return true;
  }
  public URL getWorkingDirectory() throws java.net.MalformedURLException {
    URL url = null;
    try {
      File file = new File(System.getProperty("user.dir"));
      System.out.println("Running as Application:");
      System.out.println("   " + file.toURL());
      return file.toURL();
    } catch (Exception e) {
    }
    System.out.println("Running as Applet:");
    System.out.println("   " + getCodeBase());
    return getCodeBase();
  }
  public VirtualUniverse getVirtualUniverse() {
    return m_Universe;
  }
  //public com.tornadolabs.j3dtree.Java3dTree getJ3dTree() {
  //return m_Java3dTree;
  //  }
  public Locale getFirstLocale() {
    java.util.Enumeration e = m_Universe.getAllLocales();
    if (e.hasMoreElements() != false)
      return (Locale) e.nextElement();
    return null;
  }
  protected Bounds getApplicationBounds() {
    if (m_ApplicationBounds == null)
      m_ApplicationBounds = createApplicationBounds();
    return m_ApplicationBounds;
  }
  protected Bounds createApplicationBounds() {
    m_ApplicationBounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0),
        100.0);
    return m_ApplicationBounds;
  }
  protected Background createBackground() {
    Background back = new Background(new Color3f(0.9f, 0.9f, 0.9f));
    back.setApplicationBounds(createApplicationBounds());
    return back;
  }
  public void initJava3d() {
    //  m_Java3dTree = new com.tornadolabs.j3dtree.Java3dTree();
    m_Universe = createVirtualUniverse();
    Locale locale = createLocale(m_Universe);
    BranchGroup sceneBranchGroup = createSceneBranchGroup();
    ViewPlatform vp = createViewPlatform();
    BranchGroup viewBranchGroup = createViewBranchGroup(
        getViewTransformGroupArray(), vp);
    createView(vp);
    Background background = createBackground();
    if (background != null)
      sceneBranchGroup.addChild(background);
    //    m_Java3dTree.recursiveApplyCapability(sceneBranchGroup);
    //  m_Java3dTree.recursiveApplyCapability(viewBranchGroup);
    locale.addBranchGraph(sceneBranchGroup);
    addViewBranchGroup(locale, viewBranchGroup);
    onDoneInit();
  }
  protected void onDoneInit() {
    //  m_Java3dTree.updateNodes(m_Universe);
  }
  protected double getScale() {
    return 1.0;
  }
  public TransformGroup[] getViewTransformGroupArray() {
    TransformGroup[] tgArray = new TransformGroup[1];
    tgArray[0] = new TransformGroup();
    // move the camera BACK a little...
    // note that we have to invert the matrix as
    // we are moving the viewer
    Transform3D t3d = new Transform3D();
    t3d.setScale(getScale());
    t3d.setTranslation(new Vector3d(0.0, 0.0, -20.0));
    t3d.invert();
    tgArray[0].setTransform(t3d);
    return tgArray;
  }
  protected void addViewBranchGroup(Locale locale, BranchGroup bg) {
    locale.addBranchGraph(bg);
  }
  protected Locale createLocale(VirtualUniverse u) {
    return new Locale(u);
  }
  protected BranchGroup createSceneBranchGroup() {
    m_SceneBranchGroup = new BranchGroup();
    return m_SceneBranchGroup;
  }
  protected View createView(ViewPlatform vp) {
    View view = new View();
    PhysicalBody pb = createPhysicalBody();
    PhysicalEnvironment pe = createPhysicalEnvironment();
    AudioDevice audioDevice = createAudioDevice(pe);
    if (audioDevice != null) {
      pe.setAudioDevice(audioDevice);
      audioDevice.initialize();
    }
    view.setPhysicalEnvironment(pe);
    view.setPhysicalBody(pb);
    if (vp != null)
      view.attachViewPlatform(vp);
    view.setBackClipDistance(getBackClipDistance());
    view.setFrontClipDistance(getFrontClipDistance());
    Canvas3D c3d = createCanvas3D();
    view.addCanvas3D(c3d);
    addCanvas3D(c3d);
    return view;
  }
  protected PhysicalBody createPhysicalBody() {
    return new PhysicalBody();
  }
  protected AudioDevice createAudioDevice(PhysicalEnvironment pe) {
    JavaSoundMixer javaSoundMixer = new JavaSoundMixer(pe);
    if (javaSoundMixer == null)
      System.out.println("create of audiodevice failed");
    return javaSoundMixer;
  }
  protected PhysicalEnvironment createPhysicalEnvironment() {
    return new PhysicalEnvironment();
  }
  protected float getViewPlatformActivationRadius() {
    return 100;
  }
  protected ViewPlatform createViewPlatform() {
    ViewPlatform vp = new ViewPlatform();
    vp.setViewAttachPolicy(View.RELATIVE_TO_FIELD_OF_VIEW);
    vp.setActivationRadius(getViewPlatformActivationRadius());
    return vp;
  }
  protected Canvas3D createCanvas3D() {
    GraphicsConfigTemplate3D gc3D = new GraphicsConfigTemplate3D();
    gc3D.setSceneAntialiasing(GraphicsConfigTemplate.PREFERRED);
    GraphicsDevice gd[] = GraphicsEnvironment.getLocalGraphicsEnvironment()
        .getScreenDevices();
    Canvas3D c3d = new Canvas3D(gd[0].getBestConfiguration(gc3D));
    c3d.setSize(getCanvas3dWidth(c3d), getCanvas3dHeight(c3d));
    return c3d;
  }
  protected int getCanvas3dWidth(Canvas3D c3d) {
    return m_kWidth;
  }
  protected int getCanvas3dHeight(Canvas3D c3d) {
    return m_kHeight;
  }
  protected double getBackClipDistance() {
    return 100.0;
  }
  protected double getFrontClipDistance() {
    return 1.0;
  }
  protected BranchGroup createViewBranchGroup(TransformGroup[] tgArray,
      ViewPlatform vp) {
    BranchGroup vpBranchGroup = new BranchGroup();
    if (tgArray != null && tgArray.length > 0) {
      Group parentGroup = vpBranchGroup;
      TransformGroup curTg = null;
      for (int n = 0; n < tgArray.length; n++) {
        curTg = tgArray[n];
        parentGroup.addChild(curTg);
        parentGroup = curTg;
      }
      tgArray[tgArray.length - 1].addChild(vp);
    } else
      vpBranchGroup.addChild(vp);
    return vpBranchGroup;
  }
  protected void addCanvas3D(Canvas3D c3d) {
    setLayout(new BorderLayout());
    add(c3d, BorderLayout.CENTER);
    doLayout();
  }
  protected VirtualUniverse createVirtualUniverse() {
    return new VirtualUniverse();
  }
  protected void saveCommandLineArguments(String[] szArgs) {
    m_szCommandLineArray = szArgs;
  }
  protected String[] getCommandLineArguments() {
    return m_szCommandLineArray;
  }
}





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.0f, 0.0f, 1.0f);
      // create triangle strip for twist
      int N = 80;
      int stripCounts[] = { N };
      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(a) + 0.2
            * Math.cos(a), 0.3 * Math.sin(a), 0.7 * Math.cos(a)
            + 0.2 * Math.cos(a)));
        twistStrip.setCoordinate(v + 1, new Point3d(0.7 * Math.sin(a)
            - 0.2 * Math.cos(a), -0.3 * Math.sin(a), 0.7
            * Math.cos(a) - 0.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(-1, 16000);
    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.0f, 1.0f, 1.0f);
    background.setApplicationBounds(bounds);
    contentRoot.addChild(background);
    // Let Java 3D perform optimizations on this scene graph.
    contentRoot.rupile();
    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(), 256, 256);
  } // end of main method of TwistStripApp
} // end of class TwistStripApp