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

📄 m3gcanvas.java

📁 宝剑模型
💻 JAVA
📖 第 1 页 / 共 2 页
字号:

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

public class M3GCanvas extends GameCanvas implements Runnable 
{


  private static final float X_CAMERA = 0.0f; 
  private static final float Y_CAMERA = 0.0f; 
  private static final float Z_CAMERA = 3.0f; 
    // camera coordinates; change as required
  private static final float MODEL_MOVE = 0.2f; 
    // distance to move the model up/down/left/right/fwd/back for a key press
  private static final float MODEL_ROT = 5.0f;     // 5 degrees
    // angle to rotate the model left/right for a key press
  private Graphics3D g3d;
  private Camera camera;
  private Light light;
  private Background bg;
  private VertexBuffer vertexBuffer; 
  private IndexBuffer indexBuffer;   
  private Appearance appearance;

  private Transform camTrans, modelTrans;
    // transforms used by the camera and the model

  // model position and angle information
  private float xTrans, yTrans, zTrans;
  private int totalDegrees;




  public M3GCanvas()
  {
    super(true);


    try {
      createScene();
    }
    catch (Exception e) 
    {  e.printStackTrace(); }
    Thread t = new Thread(this);
    t.start();
  } // end of SwordCanvas()





  private void createScene() throws Exception
  {
    g3d = Graphics3D.getInstance();
    // create a camera
    camera = new Camera();
    float aspectRatio = ((float) getWidth()) / ((float) getHeight());
    camera.setPerspective(45.0f, aspectRatio, 0.1f, 50.0f);
    // was 60, aspectRatio, 1, 1000
    // set up the camera's position
    camTrans = new Transform();
    camTrans.postTranslate(X_CAMERA, Y_CAMERA, Z_CAMERA);
    // create a light
    light = new Light();
    light.setColor(0xffffff);  // white light
    light.setIntensity(1.25f); // over bright
    // initialise the model's transform vars
    modelTrans = new Transform();
    xTrans = 0.0f; yTrans = 0.0f; zTrans = 0.0f;
    totalDegrees = 0;
    makeGeometry();
    makeAppearance();
    bg = new Background();
    bg.setColor(0x9EFEFE); // light blue 
  } // end of createScene()
  // ------------------- model creation -----------------------------
  private void makeGeometry()
  /* Read in the values for the vertices, normals, texture coords,
     and colour coords, and create a VertexBuffer object. Also
     read in the strip lengths array to create the index for the
     triangle strips used in the vertex buffer.

     Depending on the model, there may not be any texture or colour
     coordinates, and so the calls related to them should be commented
     out.

     The texture coordinates can use 2, 3, or 4 components, and the
     colour coordinates 3 or 4, which will require changes to the array
     creation code. The code here assumes (s,t) format for texture coords
     (2 components) and RGB for the colours (3 components), the most common 
     formats.
  */
  {
    // create vertices
    short[] POINTS = getVerts();
    VertexArray POSITION_ARRAY  = new VertexArray(POINTS.length/3, 3, 2);
    POSITION_ARRAY.set(0, POINTS.length/3,POINTS);

    // create normals
    byte[] NORMALS = getNormals();
    VertexArray NORMAL_ARRAY = new VertexArray(NORMALS.length/3, 3, 1);
    NORMAL_ARRAY.set(0, NORMALS.length/3, NORMALS);

    // create texture coordinates
    short[] TEXCOORDS = getTexCoords();
    VertexArray TEXCOORD_ARRAY = new VertexArray(TEXCOORDS.length/2, 2, 2);
                          // this assumes (s,t) texture coordinates
    TEXCOORD_ARRAY.set(0, TEXCOORDS.length/2, TEXCOORDS);
    vertexBuffer = new VertexBuffer();
    float[] pbias = {(1.0f/255.0f), (1.0f/255.0f), (1.0f/255.0f)};
    vertexBuffer.setPositions(POSITION_ARRAY, (2.0f/255.0f), pbias); // scale, bias
       // fix the scale and bias to create points in range [-1 to 1]

    vertexBuffer.setNormals(NORMAL_ARRAY);
    vertexBuffer.setTexCoords(0, TEXCOORD_ARRAY, (1.0f/255.0f), null);
    int[] LENGTHS = {82, 33, 6, 24, 60, 6, 50, 16, 182, 47};
     indexBuffer = new TriangleStripArray(0, LENGTHS);
  }  // end of makeGeometry()

  private void makeAppearance() throws Exception
  {
    // load the image for the texture
    Image im = Image.createImage("/swordTextures.png");

    // create an Image2D for the Texture2D
    Image2D image2D = new Image2D(Image2D.RGB, im);

    // create the Texture2D and enable mip mapping
    // the texture color is modulated with the lit material color
    Texture2D texture = new Texture2D(image2D);
    texture.setFiltering(Texture2D.FILTER_NEAREST, Texture2D.FILTER_NEAREST);
    texture.setWrapping(Texture2D.WRAP_CLAMP, Texture2D.WRAP_CLAMP);
    // texture.setWrapping(Texture2D.WRAP_REPEAT, Texture2D.WRAP_REPEAT);
    texture.setBlending(Texture2D.FUNC_MODULATE);
    // create the appearance
    appearance = new Appearance();
    appearance.setTexture(0, texture);
    Material material = new Material();

    material.setColor(Material.AMBIENT, 0x00000000);
    material.setColor(Material.EMISSIVE, 0x00000000);
    material.setColor(Material.DIFFUSE, 0xFFFFFFFF);
    material.setColor(Material.SPECULAR, 0x00000000);
    material.setShininess(1.0f);
    appearance.setMaterial(material);
  }  // end of makeAppearance()


  private void updateModelTrans()
  // the model's transform = a translation * a rotation
  {
    modelTrans.setIdentity();   // reset
    modelTrans.postTranslate(xTrans, yTrans, zTrans);
    modelTrans.postRotate(totalDegrees, 0, 1, 0);  // around y-axis
   }  // end of updateModelTrans()



  // -----------------key operation methods ---------------------


  public void keyRepeated(int keyCode)
  {
    int gameAction = getGameAction(keyCode);
    if (hasRepeatEvents())
      performAction( gameAction );
  }


  protected void keyPressed(int keyCode)
  {
    int gameAction = getGameAction(keyCode);
    if (!hasRepeatEvents())
      performAction( gameAction );
  } // end of keyPressed()




  public void performAction(int gameAction) 
  // called by KeyRepeat object or the keyRepeated() method
  {
    if (gameAction == UP) {
      moveModel(0,MODEL_MOVE,0);  // y up
    }
    else if (gameAction == DOWN) {
      moveModel(0,-MODEL_MOVE,0);  // y down
    }
    else if (gameAction == LEFT) {
      moveModel(-MODEL_MOVE,0,0);   // x left
    }
    else if (gameAction == RIGHT) {
      moveModel(MODEL_MOVE,0,0);   // x right
    }
    else if (gameAction == GAME_A) {
      // System.out.println("fwd");
      moveModel(0,0,MODEL_MOVE);   // z fwd
    }
    else if (gameAction == GAME_B) {
      // System.out.println("back");
      moveModel(0,0,-MODEL_MOVE);   // z back
    }
    else if (gameAction == GAME_C) {
      // System.out.println("rot left");
      rotYModel(-MODEL_ROT);   // rotate left
    }
    else if (gameAction == GAME_D) {
      // System.out.println("rot right");
      rotYModel(MODEL_ROT);   // rotate right
    }
  } // end of keyPressed()


  private void moveModel(float x, float y, float z)
  // update the model's translation values
  { xTrans += x; yTrans += y; zTrans += z;
    repaint();
  }


  private void rotYModel(float degrees)
  // update the model's y-axis rotation value
  { totalDegrees += degrees;
    repaint();
  }  // end of rotYModel()


  // ----------------- data-specific methods added here -------
/* 
  The following methods can be pasted in from the ObjView application
  output stored in examObj.txt:

  private short[] getVerts() {...}
  private byte[] getNormals() {...}
  private short[] getTexCoords() (...}
  private short[] getColourCoords() (...}
  private int[] getStripLengths() {...}
  private Material setMatColours() {...}
*/
    public void run() {
        Graphics g = getGraphics();
        while(true) {

        g.setColor( 255, 255, 255 );
       g.fillRect( 0, 0, getWidth(), getHeight() ); 
       g3d = Graphics3D.getInstance();
  
    g3d.bindTarget(g, true, Graphics3D.DITHER | Graphics3D.TRUE_COLOR);

    g3d.clear(bg);    // clear the color and depth buffers

    g3d.setCamera(camera, camTrans);   // position the camera

    // set up a "headlight": a directional light shining
    // from the direction of the camera
    g3d.resetLights();
    g3d.addLight(light, camTrans);

    updateModelTrans();

    // Render the model. We provide the vertex and index buffers
    // to specify the geometry; the appearance so we know what
    // material and texture to use; and a tranform to position
    // the model
    g3d.render(vertexBuffer, indexBuffer, appearance, modelTrans);

    g3d.releaseTarget();      // flush
    flushGraphics();
     try {
                Thread.sleep(200); 
            }
            catch (InterruptedException e) {
                
    }
  }
        
           
    
    }

// Methods for model in sword.obj

  private short[] getVerts()
  // return an array holding Verts [1518 values / 3 = 506 points] 
  {
    short[] POINTS = {
      	-3,60,-6,  	-1,60,-7,  	-1,60,-2,  	-1,68,-2,  
	-15,67,-2,  	-1,68,-2,  	-1,68,-7,  	-1,60,-7,  
	-1,68,-7,  	-15,67,-7,  	-15,67,-2,  	-15,60,-2,  
	-1,60,-2,  	-15,60,-2,  	-14,60,-4,  	-15,60,-2,  
	-15,60,-7,  	-15,67,-7,  	-15,60,-7,  	-1,60,-7,  
	-15,60,-7,  	-3,60,-6,  	-14,60,-4,  	-13,32,-4,  
	-14,60,-4,  	-3,60,-4,  	-1,60,-2,  	-3,60,-4,  
	-3,60,-6,  	-2,33,-6,  	-13,32,-4,  	-2,33,-6,  
	0,5,-6,  	-2,33,-6,  	-2,33,-4,  	-3,60,-4,  
	-2,33,-4,  	-13,32,-4,  	-2,33,-4,  	0,5,-4,  
	0,5,-6,  	2,-21,-6,  	0,5,-6,  	-11,3,-4,  
	-13,32,-4,  	-11,3,-4,  	0,5,-4,  	2,-21,-4,  
	2,-21,-6,  	5,-48,-6,  	2,-21,-6,  	-8,-26,-4,  
	-11,3,-4,  	-8,-26,-4,  	2,-21,-4,  	5,-48,-4,  
	5,-48,-6,  	10,-74,-6,  	5,-48,-6,  	-3,-54,-4,  
	-8,-26,-4,  	-3,-54,-4,  	5,-48,-4,  	10,-74,-4,  
	10,-74,-6,  	14,-101,-6,  	10,-74,-6,  	1,-80,-4,  
	-3,-54,-4,  	1,-80,-4,  	10,-74,-4,  	14,-101,-4,  
	14,-101,-6,  	21,-128,-4,  	11,-122,-4,  	14,-101,-4,  
	6,-103,-4,  	1,-80,-4,  	6,-103,-4,  	14,-101,-6,  
	6,-103,-4,  	11,-122,-4,  	-14,68,3,  	-13,68,4,  
	-13,67,4,  	-13,68,4,  	-13,67,5,  	-13,68,5,  
	-13,67,5,  	-12,67,5,  	-12,67,6,  	-12,67,5,  
	-11,67,6,  	-12,67,5,  	-10,67,6,  	-10,67,5,  
	-10,68,5,  	-10,68,4,  	-12,68,5,  	-10,68,4,  
	-11,68,4,  	-10,68,4,  	-10,67,4,  	-10,67,5,  
	-10,67,4,  	-12,67,5,  	-10,67,4,  	-11,67,4,  
	-11,68,4,  	-11,67,4,  	-12,68,5,  	-12,67,5,  
	-12,68,5,  	-12,67,5,  	-13,68,5,  	7,68,-5,  
	9,68,-5,  	6,68,-13,  	9,67,-5,  	6,67,-13,  
	7,67,-5,  	6,67,-13,  	0,68,-19,  	6,68,-13,  
	4,68,-13,  	6,68,-13,  	6,68,-9,  	7,68,-5,  
	6,67,-9,  	7,67,-5,  	6,67,-9,  	6,67,-13,  
	6,67,-9,  	4,67,-13,  	6,68,-9,  	4,67,-13,  
	4,68,-13,  	0,67,-17,  	4,68,-13,  	0,68,-17,  
	0,68,-19,  	0,68,-17,  	-2,68,-18,  	-2,67,-18,  
	-6,67,-19,  	0,68,-17,  	-2,67,-18,  	0,67,-17,  
	0,67,-19,  	4,67,-13,  	0,67,-19,  	6,67,-13,  
	0,67,-19,  	0,68,-19,  	-8,68,-21,  	-2,68,-18,  
	-6,68,-19,  	-6,67,-19,  	-10,67,-18,  	-6,67,-19,  
	-8,67,-21,  	-2,67,-18,  	-8,67,-21,  	0,67,-19,  
	-8,67,-21,  	-8,68,-21,  	-16,68,-19,  	-8,68,-21,  
	-10,68,-18,  	-6,68,-19,  	-10,68,-18,  	-10,67,-18,  
	-12,67,-15,  	-10,67,-18,  	-16,67,-19,  	-8,67,-21,  
	-16,67,-19,  	-16,68,-19,  	-22,68,-13,  	-16,68,-19,  
	-12,68,-15,  	-10,68,-18,  	-12,68,-15,  	-12,67,-15,  
	-12,67,-13,  	-12,67,-15,  	-22,67,-13,  	-16,67,-19,  
	-22,67,-13,  	-22,68,-13,  	-24,68,-5,  	-22,68,-13,  
	-22,68,-5,  	-22,68,-13,  	-12,68,-13,  	-12,68,-15,  
	-12,68,-13,  	-12,67,-13,  	-11,67,-11,  	-12,67,-13,  
	-22,67,-5,  	-22,67,-13,  	-22,67,-5,  	-24,67,-5,  
	-22,67,-1,  	-22,67,-13,  	-24,67,-5,  	-24,68,-5,  
	-22,68,3,  	-22,68,-1,  	-21,68,4,  	-24,68,-5,  
	-22,68,-1,  	-22,68,-5,  	-22,67,-1,  	-22,68,-5,  
	-22,67,-5,  	-21,68,0,  	-22,67,-5,  	-21,67,0,  
	-11,67,-11,  	-21,67,0,  	-17,67,4,  	-17,68,4,  
	-17,67,4,  	-12,68,6,  	-12,67,6,  	-11,68,6,  
	-11,67,6,  	-10,68,6,  	-10,67,6,  	-10,68,6,  
	-10,68,5,  	-10,68,6,  	-12,68,5,  	-11,68,6,  
	-12,68,5,  	-12,68,6,  	-13,68,5,  	-12,68,6,  
	-13,68,4,  	-17,68,4,  	-14,68,3,  	-17,68,4,  
	-13,68,2,  	-12,68,1,  	-13,67,2,  	-12,67,1,  
	-13,67,2,  	-17,67,4,  	-13,67,2,  	-14,67,3,  
	-13,68,2,  	-14,67,3,  	-14,68,3,  	-14,67,3,  
	-13,67,4,  	-17,67,4,  	-13,67,4,  	-12,67,6,  
	-13,67,5,  	-21,67,0,  	-17,68,4,  	-21,68,0,  
	-11,68,-11,  	-22,68,-5,  	-11,68,-11,  	-12,68,-13,  
	-11,68,-11,  	-11,67,-11,  	-9,67,-10,  	-17,67,4,  
	-9,67,-10,  	-12,67,1,  	-10,67,1,  	-10,68,1,  
	-8,68,1,  	-10,68,10,  	-6,68,9,  	-6,67,9,  
	-4,67,6,  	-6,67,9,  	0,67,9,  	-8,67,11,  
	0,68,9,  	-8,67,11,  	-8,68,11,  	-8,67,11,  
	-16,67,9,  	-8,67,11,  	-10,67,10,  	-6,67,9,  
	-10,67,10,  	-10,68,10,  	-14,68,9,  	-16,68,9,  
	-14,68,9,  	-17,68,7,  	-14,68,9,  	-14,67,9,  
	-10,67,10,  	-14,67,9,  	-16,67,9,  	-14,67,9,  
	-17,67,7,  	-17,68,7,  	-21,68,4,  	-17,68,7,  
	-22,68,3,  	-16,68,9,  	-22,68,3,  	-22,67,3,  
	-24,67,-5,  	-22,67,3,  	-22,67,-1,  	-21,67,4,  
	-22,68,-1,  	-21,67,4,  	-21,68,4,  	-21,67,4,  
	-17,67,7,  	-22,67,3,  	-16,67,9,  	-16,68,9,  
	-8,68,11,  	-10,68,10,  	-8,68,11,  	-6,68,9,  
	0,68,9,  	-6,68,9,  	-4,68,6,  	-4,67,6,  
	-4,67,3,  	-4,67,6,  	6,67,3,  	0,67,9,  
	6,68,3,  	0,68,9,  	6,68,3,  	-4,68,6,  
	-4,68,3,  	-4,67,3,  	-5,67,2,  	-4,67,3,  
	7,67,-5,  	6,67,3,  	9,67,-5,  	6,67,3,  
	9,68,-5,  	6,68,3,  	7,68,-5,  	-4,68,3,  
	-5,68,2,  	-5,67,2,  	-8,67,1,  	-5,67,2,  
	0,67,-14,  	-5,67,2,  	4,67,-9,  	7,67,-5,  
	4,68,-9,  	7,68,-5,  	4,68,-9,  	-5,68,2,  
	4,68,-9,  	0,68,-14,  	4,67,-9,  	0,68,-14,  
	0,67,-14,  	-5,68,-16,  	-5,67,-16,  	-6,68,-16,  
	-6,67,-16,  	-7,68,-15,  	-7,67,-15,  	-7,68,-15,  
	-7,68,-14,  	-7,68,-15,  	-6,68,-13,  	-6,68,-16,  
	-5,68,-14,  	-6,68,-16,  	-5,68,-15,  	-5,68,-16,  
	-5,68,-15,  	0,68,-14,  	-4,68,-15,  	-3,68,-14,  
	-4,68,-15,  	-4,67,-15,  	-4,68,-15,  	-5,67,-15,  
	-5,68,-15,  	-5,67,-15,  	-5,68,-15,  	-5,67,-14,  
	-5,68,-14,  	-6,67,-13,  	-6,68,-13,  	-7,67,-14,  
	-7,68,-14,  	-7,67,-14,  	-7,67,-15,  	-6,67,-13,  
	-6,67,-16,  	-5,67,-14,  	-6,67,-16,  	-5,67,-15,  
	-5,67,-16,  	-5,67,-15,  	0,67,-14,  	-4,67,-15,  
	-3,67,-14,  	-3,68,-14,  	-3,68,-13,  	0,68,-14,  
	-3,68,-13,  	-3,68,-11,  	-3,68,-13,  	-3,67,-13,  
	-3,67,-14,  	-3,67,-13,  	0,67,-14,  	-3,67,-13,  
	-3,67,-11,  	-3,68,-11,  	-4,68,-11,  	-3,68,-11,  
	-10,68,1,  	0,68,-14,  	-8,68,1,  	-5,68,2,  
	-8,68,1,  	-8,67,1,  	-10,67,1,  	0,67,-14,  
	-10,67,1,  	-3,67,-11,  	-4,67,-11,  	-4,68,-11,  
	-7,68,-10,  	-10,68,1,  	-7,68,-10,  	-9,68,-10,  
	-7,68,-10,  	-7,67,-10,  	-4,67,-11,  	-7,67,-10,  
	-10,67,1,  	-7,67,-10,  	-9,67,-10,  	-9,68,-10,  
	-11,68,-11,  	-9,68,-10,  	-17,68,4,  	-9,68,-10,  
	-12,68,1,  	-10,68,1,  	-12,67,1,  	-8,127,-1,  
	-8,127,-7,  	-12,127,-1,  	-12,127,-7,  	-12,127,-1,  
	-15,125,-1,  	-8,127,-1,  	-3,126,-1,  	-8,127,-1,  
	-3,126,-7,  	-8,127,-7,  	-15,125,-7,  	-12,127,-7,  
	-15,125,-7,  	-15,125,-1,  	-15,107,-1,  	-15,125,-1,  
	-3,107,-1,  	-3,126,-1,  	-3,107,-7,  	-3,126,-7,  

⌨️ 快捷键说明

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