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

📄 tiledfloor.java

📁 3D手机游戏开发实例源代码 3D迷宫游戏的演示程序
💻 JAVA
字号:


import javax.microedition.lcdui.*;
import javax.microedition.m3g.*;


public class TiledFloor 
{
  private Mesh floor;


  public TiledFloor(Image2D image2D, int sz) 
  { 
    /* Make sure the size value is divisible by 2, since
       we'll use size/2 later. */
    int size = (sz/2)*2;
    if (size != sz)
      System.out.println("Size set to multiple of 2: " + size);

    int numTiles = size*size;  
      /* Each tile is a 1-by-1 quadrilateral (quad), so numTiles
         of them will cover a size-by-size floor. */

    // build the floor mesh
    VertexBuffer vertexBuffer = makeGeometry(size, numTiles);

    int pos1 = 1; int pos2 = 2;
    int pos3 = 0; int pos4 = 3;

    int[] index = new int[4*numTiles];   // 4 points for each tile
    for(int i = 0; i < 4*numTiles; i += 4) {
      index[i] = pos1; pos1 += 4;   // increment the positions by 4
      index[i+1] = pos2; pos2 += 4;
      index[i+2] = pos3; pos3 +=4;
      index[i+3] = pos4; pos4 += 4;
    }
    int[] lens = new int[numTiles]; 
    for(int i = 0; i < numTiles; i++) lens[i] = 4;
    IndexBuffer indexBuffer = 
         new TriangleStripArray(index,lens);

    Appearance appearance = makeAppearance(image2D);

    floor = new Mesh(vertexBuffer, indexBuffer, appearance);
  }  // end of TiledFloor()


  private VertexBuffer makeGeometry(int size, int numTiles)
  /*  numTiles squares centered at the origin on the XZ plane.
      Normals aren't required since the floor doesn't reflect
      light. */
  {
    // create vertices
        short[] POINTS = new short[12*numTiles];   // 3 * 4 points for each tile
    int i=0;
    for(int z = (-size/2)+1; z <= size/2; z++)
      for(int x = -size/2; x <= (size/2)-1; x++) {
        POINTS[i] = (short) x; POINTS[i+1]=0; POINTS[i+2] = (short) z;
        POINTS[i+3] = (short)(x+1); POINTS[i+4]=0; POINTS[i+5] = (short) z;
        POINTS[i+6] = (short)(x+1); POINTS[i+7]=0; POINTS[i+8] = (short)(z-1);
        POINTS[i+9] = (short) x; POINTS[i+10]=0; POINTS[i+11] = (short)(z-1);
        i += 12;
      }
      
   // short[] POINTS = makeVerts(size, numTiles);
    VertexArray POSITION_ARRAY = new VertexArray(POINTS.length/3, 3, 2);
    POSITION_ARRAY.set(0, POINTS.length/3, POINTS);
    short[] TEXCOORDS = new short[8*numTiles];   // 2 * 4 points for each tile
    for(i = 0; i < 8*numTiles; i += 8) {  // {0,1,  1,1,  1,0,  0,0}
      TEXCOORDS[i] = 0; TEXCOORDS[i+1] = 1;               // for each tile
      TEXCOORDS[i+2] = 1; TEXCOORDS[i+3] = 1;
      TEXCOORDS[i+4] = 1; TEXCOORDS[i+5] = 0;
      TEXCOORDS[i+6] = 0; TEXCOORDS[i+7] = 0;
    }
    // create texture coordinates
   // short[] TEXCOORDS = makeTexCoords(numTiles);
    VertexArray TEXCOORD_ARRAY = new VertexArray(TEXCOORDS.length/2, 2, 2);
    TEXCOORD_ARRAY.set(0, TEXCOORDS.length/2, TEXCOORDS);

    VertexBuffer vertexBuffer = new VertexBuffer(); 
    vertexBuffer.setPositions(POSITION_ARRAY, 1.0f, null); // no scale, bias
    vertexBuffer.setTexCoords(0, TEXCOORD_ARRAY, 1.0f, null);

    return vertexBuffer;
  }  // end of makeGeometry()


 




  private Appearance makeAppearance(Image2D image2D)
  // No Material, with the texture clamped to each tile
  {
    Appearance app = new Appearance();

    if (image2D != null) {
      Texture2D tex = new Texture2D(image2D);
      tex.setFiltering(Texture2D.FILTER_NEAREST, Texture2D.FILTER_NEAREST);
      tex.setWrapping(Texture2D.WRAP_CLAMP, Texture2D.WRAP_CLAMP);
      app.setTexture(0, tex);
    }

    // add perspective correction, and switch off all culling
    PolygonMode floorPolygonMode = new PolygonMode();
    floorPolygonMode.setPerspectiveCorrectionEnable(true);
    floorPolygonMode.setCulling(PolygonMode.CULL_NONE);

    app.setPolygonMode(floorPolygonMode);

    return app;
  }  // end of makeAppearance()


  // -------------------- access method ----------------------

  public Mesh getFloorMesh()
  {  return floor;  }

} // end of TiledFloor Class

⌨️ 快捷键说明

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