⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cartest.java

📁 Java 3D API, 一套完整的3d引擎
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
import java.io.*;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;

import com.threed.jpct.*;
import com.threed.jpct.util.*;


class CarTest {

   /**
    * A flag that signals a change of the renderer (don't ask about the 35 here....)
    */
   private final static int SWITCH_RENDERER=35;

   /**
    * Should we try to do fullscreen?
    */
   private boolean fullscreen=false;

   /**
    * Are we using OpenGL?
    */
   private boolean openGL=false;

   /**
    * Are we rendering in wireframe mode?
    */
   private boolean wireframe=false;

   /**
    * Some jPCT related stuff
    */
   private FrameBuffer buffer=null;
   private World theWorld=null;
   private TextureManager texMan=null;
   private Camera camera=null;

   /**
    * Some things for the "game logic"
    */
   private Object3D terrain=null;
   private Car car=null;
   private ProjectileManager bulMan=null;
   private PlantManager plantMan=null;
   private SkidMarkManager skidMan=null;

   /**
    * The texture used for blitting the framerate
    */
   private Texture numbers=null;

   /**
    * Default size of the framebuffer
    */
   private int width=640;
   private int height=480;

   /**
    * Some AWT related stuff
    */
   private Frame frame=null;
   private Graphics gFrame=null;
   private BufferStrategy bufferStrategy=null;
   private GraphicsDevice device=null;
   private int titleBarHeight=0;
   private int leftBorderWidth=0;

   private int switchMode=0;

   private int fps;
   private int lastFps;
   private long totalFps;

   private int pps;
   private int lastPps;

   private boolean isIdle=false;
   private boolean exit=false;

   /**
    * Flags for the keys
    */
   private boolean left=false;
   private boolean right=false;
   private boolean forward=false;
   private boolean back=false;
   private boolean fire=false;
   private int fireCount=3;

   private float speed=0;

   private KeyMapper keyMapper=null;

   /**
    * Very complex stuff...impossible to explain...
    */
   public static void main(String[] args) {
      new CarTest(args);
   }

   /**
    * The constructor. Here we are initializing things...
    */
   private CarTest(String[] args) {

     Config.maxPolysVisible=10000;

      /**
       * Evaluate the commandline parameters
       */
      for (int i=0; i<args.length; i++) {
         if (args[i].equals("fullscreen")) {
            fullscreen=true;
            Config.glFullscreen=true;
         }
         if (args[i].equals("mipmap")) {
            Config.glMipmap=true;
         }
         if (args[i].equals("trilinear")) {
            Config.glTrilinear=true;
         }

         if (args[i].equals("16bit")) {
            Config.glColorDepth=16;
         }
         try {
            if (args[i].startsWith("width=")) {
               width=Integer.parseInt(args[i].substring(6));
            }
            if (args[i].startsWith("height=")) {
               height=Integer.parseInt(args[i].substring(7));
            }
            if (args[i].startsWith("refresh=")) {
               Config.glRefresh=Integer.parseInt(args[i].substring(8));
            }
            if (args[i].startsWith("zbuffer=")) {
               Config.glZBufferDepth=Integer.parseInt(args[i].substring(8));
               if (Config.glZBufferDepth==16) {
                  Config.glFixedBlitting=true;
               }
            }

         } catch (Exception e) {
            // We don't care
         }
      }

      isIdle=false;
      switchMode=0;
      totalFps=0;
      fps=0;
      lastFps=0;

      /**
       * Initialize the World instance and get the TextureManager (a singleton)
       */
      theWorld=new World();
      texMan=TextureManager.getInstance();

      /**
       * Setup the lighting. We are not using overbright lighting because the OpenGL
       * renderer can't do it, but we are using RGB-scaling. Some hardware/drivers
       * for OpenGL don't support this (but most do).
       */
      Config.fadeoutLight=false;
      theWorld.getLights().setOverbrightLighting(Lights.OVERBRIGHT_LIGHTING_DISABLED);
      theWorld.getLights().setRGBScale(Lights.RGB_SCALE_2X);
      theWorld.setAmbientLight(25, 30, 30);

      /**
       * Place the lightsources...
       */
      theWorld.addLight(new SimpleVector(0, -150, 0), 25, 22, 19);
      theWorld.addLight(new SimpleVector(-1000, -150, 1000), 22, 5, 4);
      theWorld.addLight(new SimpleVector(1000, -150, -1000), 4, 2, 22);

      /**
       * We are using fog. Please note that the fog implementation is not very well suited for
       * any other fog color than black when using OpenGL's lighting model.
       */
      theWorld.setFogging(World.FOGGING_ENABLED);
      theWorld.setFogParameters(1200, 0, 0, 0);
      Config.farPlane=1200;

      /**
       * Load the textures needed and add them to the TextureManager. We are loading the "numbers"
       * texture for blitting the framerate and the texture for the ground. Textures used by the game
       * entities are loaded by them.
       */
      char c=File.separatorChar;

      numbers=new Texture("textures"+c+"other"+c+"numbers.jpg");
      texMan.addTexture("numbers", numbers);

      Texture rocks=new Texture("textures"+c+"rocks.jpg");
      texMan.addTexture("rocks", rocks);

      /**
       * Load the map (i.e. the terrain or "ground")
       */
      Object3D[] objs=Loader.load3DS("models"+c+"terascene.3ds", 400);
      if (objs.length>0) {
         terrain=objs[0];
         terrain.setTexture("rocks");
      }

      terrain.enableLazyTransformations();
      theWorld.addObject(terrain);

      /**
       * We want to drive around...a car may help
       */
      car=new Car();
      car.addToWorld(theWorld);

      /**
       * The game entities are building themselves, so we only have to build
       * the terrain here
       */
      terrain.build();

      /**
       * The terrain isn't located where we want it to, so we take
       * care of this here:
       */
      SimpleVector pos=terrain.getCenter();
      pos.scalarMul(-1f);
      terrain.translate(pos);
      terrain.rotateX((float)-Math.PI/2f);
      terrain.translateMesh();
      terrain.rotateMesh();
      terrain.setTranslationMatrix(new Matrix());
      terrain.setRotationMatrix(new Matrix());

      /**
       * That won't hurt...
       */
      terrain.createTriangleStrips(2);

      /**
       * The game logic relies on "Managers" in this example. We are
       * instantiating them here.
       */
      bulMan=new ProjectileManager(theWorld);
      plantMan=new PlantManager(theWorld, terrain, 1800);
      skidMan=new SkidMarkManager(theWorld);


      /**
       * Setup the octree and the collision mode/listener for the terrain
       */
      OcTree oc=new OcTree(terrain,50,OcTree.MODE_OPTIMIZED);
      terrain.setOcTree(oc);
      oc.setCollisionUse(OcTree.COLLISION_USE);

      Config.collideOffset=250;

      terrain.setCollisionMode(Object3D.COLLISION_CHECK_OTHERS);
      terrain.setCollisionOptimization(Object3D.COLLISION_DETECTION_OPTIMIZED);
      terrain.addCollisionListener(new BulletTerrainListener(bulMan));

      /**
       * Place the camera at the starting position.
       */
      camera=theWorld.getCamera();
      camera.setPosition(0,-2500,-1500);
      camera.lookAt(car.getTransformedCenter());

      /**
       * Setup some optimizations for outdoor rendering
       */
      Config.tuneForOutdoor();

      /**
       * Do some AWT setup work
       */
      initializeFrame();

      /**
       * Here we go...!
       */
      gameLoop();
   }

   /**
    * This initializes the AWT frame either in fullscreen or in windowed mode.
    * This is not a waterproof intialization, but i didn't want to do a AWT
    * tutorial here (and i would be the wrong person to do this anyway...:-)).
    * Change whatever you think that needs change here...
    */
   private void initializeFrame() {
      if (fullscreen) {
         GraphicsEnvironment env=GraphicsEnvironment.getLocalGraphicsEnvironment();
         device=env.getDefaultScreenDevice();
         GraphicsConfiguration gc=device.getDefaultConfiguration();
         frame=new Frame(gc);
         frame.setUndecorated(true);
         frame.setIgnoreRepaint(true);
         device.setFullScreenWindow(frame);
         if (device.isDisplayChangeSupported()) {
            device.setDisplayMode(new DisplayMode(width, height, 32, 0));
         }
         frame.createBufferStrategy(2);
         bufferStrategy=frame.getBufferStrategy();
         Graphics g=bufferStrategy.getDrawGraphics();
         bufferStrategy.show();
         g.dispose();
      } else {
         frame=new Frame();
         frame.setTitle("jPCT "+Config.getVersion());
         frame.pack();
         Insets insets = frame.getInsets();
         titleBarHeight=insets.top;
         leftBorderWidth=insets.left;
         frame.setSize(width+leftBorderWidth+insets.right, height+titleBarHeight+insets.bottom);
         frame.setResizable(false);
         frame.show();
         gFrame=frame.getGraphics();
      }

      /**
       * The listeners are bound to the AWT frame...they are useless in OpenGL mode.
       */
      frame.addWindowListener(new WindowEvents());
      keyMapper=new KeyMapper(frame);
   }

   /**
    * The fps counter is blitted into the rendered framebuffer here and the rendering results
    * will be displayed (hence the name of the method...:-))
    */
   private void display() {

      blitNumber((int) totalFps, 5, 2);
      blitNumber((int) lastPps, 5, 12);

      plantMan.drawRadar(buffer, car);

      if (!openGL) {
         if (!fullscreen) {
            buffer.display(gFrame, leftBorderWidth, titleBarHeight);
         } else {
            Graphics g=bufferStrategy.getDrawGraphics();
            g.drawImage(buffer.getOutputBuffer(), 0, 0, null);
            bufferStrategy.show();
            g.dispose();
         }
      } else {
         buffer.displayGLOnly();

⌨️ 快捷键说明

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