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

📄 tree.java

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


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


public class Tree 
{
  private Mesh tree;
  private Group cameraGroup;   
     // the billboard aligns itself with this camera position


  public Tree(Image2D image2D, Group camGroup, float x, float z, float size) 
  { 
    cameraGroup = camGroup;

    // build bbMesh
    VertexBuffer vertexBuffer = makeGeometry();

    int[] indicies = {1,2,0,3};  // the billboard is one quad
    int[] stripLens = {4};
    IndexBuffer indexBuffer = new TriangleStripArray(indicies, stripLens);

    Appearance appearance = makeAppearance(image2D);

    tree = new Mesh(vertexBuffer,indexBuffer, appearance);

    float size2 = size * 0.5f;   
       /* The mesh is 2-by-2 in size, and so the extra 0.5 factor
          in the scaling reduces it to 1-by-1. */
    tree.scale(size2, size2, size2);
    tree.setTranslation(x, size2, z);

    tree.setAlignment(cameraGroup, Node.Z_AXIS, null, Node.NONE);  
         /* The billboard alignment will be along its z-axis only,
            no y-axis alignment is employed. */
  }  // end of Billboard()


  private VertexBuffer makeGeometry()
  /*  The geometry defines a square resting on top of the XZ plane,
      centered at (0,0), with sides of length 2.
      There are no normals, but there are texture coords.
  */
  {
    /* Create vertices, starting at the bottom left and going 
       counter-clockwise. */
    short[] POINTS = {-1,-1,0,  1,-1,0,  1,1,0,  -1,1,0};
    VertexArray POSITION_ARRAY = new VertexArray(POINTS.length/3, 3, 2);
    POSITION_ARRAY.set(0, POINTS.length/3, POINTS);

    // create texture coordinates using the same order as the vertices
    short[] TEXCOORDS = {0,1,  1,1,  1,0,  0,0};
    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 size, bias
    vertexBuffer.setTexCoords(0, TEXCOORD_ARRAY, 1.0f, null);

    return vertexBuffer;
  }  // end of makeGeometry()


  private Appearance makeAppearance(Image2D image2D)
  /* The image's alpha component will cause the square's
     surface to be invisible at those locations.
  */
  { Appearance appearance = new Appearance();

    // The alpha component of the texture will be used in the rendering
    CompositingMode compositingMode = new CompositingMode();
    compositingMode.setBlending(CompositingMode.ALPHA);
    appearance.setCompositingMode(compositingMode);

    if (image2D != null) {
      Texture2D texture2D = new Texture2D(image2D);
      texture2D.setFiltering(Texture2D.FILTER_NEAREST, Texture2D.FILTER_NEAREST);
      texture2D.setWrapping(Texture2D.WRAP_CLAMP, Texture2D.WRAP_CLAMP);
      texture2D.setBlending(Texture2D.FUNC_REPLACE); 
                    // the texture with replace the surface

      appearance.setTexture(0, texture2D);
    }
    return appearance;
  }  // end of makeAppearance()


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

  public Mesh getMesh()
  {  return tree;  }


  public void align()
  // align the board's z-axis with the current position of the cameraGroup node
  { tree.align(cameraGroup);  }

} // end of Billboard Class

⌨️ 快捷键说明

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