dommstyle.txt

来自「Simple DOOM style navigation of a 3D sce」· 文本 代码 · 共 1,957 行 · 第 1/4 页

TXT
1,957
字号
    guard.createObject(m_GuardAppearance, new Vector3d(point.x,
        (m_kFloorLevel + m_kCeilingLevel) / 2, point.z), new Vector3d(
        1, 1, 1), null, null, null);
  }

  void createLight(Group mapGroup, int nPixelX, int nPixelY) {
    Point3d point = convertToWorldCoordinatesPixelCenter(nPixelX, nPixelY);

    // we do not share an Appearance for lights
    // or they all animate in synch...
    final double lightHeight = m_kCeilingHeight / 1.5;

    Light light = new Light(this, mapGroup, ComplexObject.GEOMETRY
        | ComplexObject.TEXTURE);
    light.createObject(new Appearance(), new Vector3d(point.x,
        m_kFloorLevel + lightHeight / 2, point.z), new Vector3d(5,
        lightHeight, 5), "light.gif", null, null);
  }

  void createWater(Group mapGroup, int nPixelX, int nPixelY) {
    Point3d point = convertToWorldCoordinatesPixelCenter(nPixelX, nPixelY);

    if (m_WaterAppearance == null) {
      m_WaterAppearance = new Appearance();
      m_WaterAppearance.setPolygonAttributes(new PolygonAttributes(
          PolygonAttributes.POLYGON_FILL,
          PolygonAttributes.CULL_NONE, 0, false));
      m_WaterAppearance
          .setTransparencyAttributes(new TransparencyAttributes(
              TransparencyAttributes.BLENDED, 1.0f));
      m_WaterAppearance.setTextureAttributes(new TextureAttributes(
          TextureAttributes.REPLACE, new Transform3D(), new Color4f(
              0, 0, 0, 1), TextureAttributes.FASTEST));

    }

    Land water = new Land(this, mapGroup, ComplexObject.GEOMETRY
        | ComplexObject.TEXTURE);
    water.createObject(m_WaterAppearance, new Vector3d(point.x,
        m_kFloorLevel + 0.1, point.z), new Vector3d(40, 1, 40),
        "water.gif", null, null);
  }

  void createBookcase(Group mapGroup, int nPixelX, int nPixelY) {
    Point3d point = convertToWorldCoordinatesPixelCenter(nPixelX, nPixelY);

    if (m_BookcaseAppearance == null)
      m_BookcaseAppearance = new Appearance();

    Vector3d squareSize = getMapSquareSize();

    Cuboid bookcase = new Cuboid(this, mapGroup, ComplexObject.GEOMETRY
        | ComplexObject.TEXTURE);
    bookcase.createObject(m_BookcaseAppearance, new Vector3d(point.x,
        m_kFloorLevel, point.z), new Vector3d(squareSize.x / 2,
        m_kCeilingHeight / 2.7, squareSize.z / 2), "bookcase.gif",
        null, null);

  }

  public static void main(String[] args) {
    KeyNavigateTest keyTest = new KeyNavigateTest();
    keyTest.saveCommandLineArguments(args);

    new MainFrame(keyTest, 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.com/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;
  }
}

class KeyCollisionBehavior extends KeyBehavior {
  private CollisionChecker m_CollisionChecker = null;

  public KeyCollisionBehavior(TransformGroup tg,
      CollisionDetector collisionDetector) {
    super(tg);

    m_CollisionChecker = new CollisionChecker(tg, collisionDetector, true);
  }

  protected void updateTransform() {
    if (m_CollisionChecker.isCollision(transform3D) == false)
      transformGroup.setTransform(transform3D);
  }

  // dissallow rotation up or down
  protected void altMove(int keycode) {
  }

  // dissallow moving up or down
  protected void controlMove(int keycode) {
  }
}

class Guard extends ComplexObject {
  private CollisionDetector m_CollisionDetector = null;

  public Guard(Component comp, Group g, int nFlags, CollisionDetector detector) {
    super(comp, g, nFlags);

    m_CollisionDetector = detector;
  }

  protected Group createGeometryGroup(Appearance app, Vector3d position,
      Vector3d scale, String szTextureFile, String szSoundFile) {
    TransformGroup tg = new TransformGroup();
    tg.addChild(new Cone(5, 30));

    attachBehavior(new RandomWalkBehavior(getBehaviorTransformGroup(),
        m_CollisionDetector));

    return tg;
  }
}

interface CollisionDetector {
  public boolean isCollision(Transform3D t3d, boolean bViewSide);
}

class CollisionChecker {
  CollisionDetector m_Detector = null;

  Transform3D m_ToWorld = null;

  Transform3D m_Transform3D = null;

  Node m_Node = null;

  boolean m_bViewSide = false;

  public CollisionChecker(Node node, CollisionDetector detector,
      boolean bViewSide) {
    m_Detector = detector;
    m_Node = node;
    m_bViewSide = bViewSide;
  }

  public boolean isCollision(Transform3D t3d) {
    return m_Detector.isCollision(t3d, m_bViewSide);
  }
}

class Light extends ComplexObject {
  private TextureAttributes m_TextureAttributes = null;

  public Light(Component comp, Group g, int nFlags) {
    super(comp, g, nFlags);
  }

  protected Group createGeometryGroup(Appearance app, Vector3d position,
      Vector3d scale, String szTextureFile, String szSoundFile) {
    Group g = new Group();

    app.setPolygonAttributes(new PolygonAttributes(
        PolygonAttributes.POLYGON_FILL, PolygonAttributes.CULL_NONE, 0,
        false));
    app.setTransparencyAttributes(new TransparencyAttributes(
        TransparencyAttributes.BLENDED, 1.0f));

    m_TextureAttributes = new TextureAttributes(TextureAttributes.REPLACE,
        new Transform3D(), new Color4f(0, 0, 0, 1),
        TextureAttributes.FASTEST);
    app.setTextureAttributes(m_TextureAttributes);

    if ((m_nFlags & ComplexObject.TEXTURE) == ComplexObject.TEXTURE)
      setTexture(app, szTextureFile);

    Cone cone = new Cone(1, 1, Primitive.GENERATE_TEXTURE_COORDS, app);

    g.addChild(cone);

    attachBehavior(new TextureAnimationBehavior(m_TextureAttributes));

    return g;
  }
}

class TextureAnimationBehavior extends Behavior {
  // the wake up condition for the behavior
  protected WakeupCondition m_WakeupCondition = null;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?