Example8.java
上传用户:gyyuli
上传日期:2013-07-09
资源大小:3050k
文件大小:4k
源码类别:

J2ME

开发平台:

Java

  1. import javax.microedition.m3g.*;
  2. /**
  3.  * Morphing test.
  4.  */
  5. public class Example8 extends ExampleBase
  6. {
  7.     private MorphingMesh iMorphingMesh;
  8.     private Light iLight;
  9.     private Camera iCamera;
  10.     private int iCurrentFrame;
  11.     private Transform trans;
  12.     private Transform iLightTrans;
  13.     private float[] iWeights;
  14.     public Example8()
  15.     {
  16.         super(SHOW_RENDER_TIME);
  17.         iCurrentFrame = 0;
  18.         trans = new Transform();
  19.         iWeights = new float[2];
  20.     }
  21.     protected void render(int time)
  22.     {
  23.         Background back = new Background();
  24.         back.setColor(0xffFFFFFF);
  25.         back.setDepthClearEnable(true);
  26.         Graphics3D.getInstance().clear(back);
  27.         Graphics3D.getInstance().resetLights();
  28.         Graphics3D.getInstance().addLight(iLight, iLightTrans);
  29.         for (int i = 0; i < iWeights.length; i++)
  30.         {
  31.             iWeights[i] = (float) Math.sin(((float) (iCurrentFrame % (60*(i+1)))) / ((float) (60*(i+1))) * Math.PI);
  32.         }
  33.         iCurrentFrame++;
  34.         iMorphingMesh.setWeights(iWeights);
  35.         Graphics3D.getInstance().render(iMorphingMesh, trans);
  36.     }
  37.     protected void initialize()
  38.     {
  39.         VertexArray normals = new VertexArray(6, 3, 1);
  40.         normals.set(0,6,new byte[] {0,0,-127,
  41.                                     0,0,-127,
  42.                                     0,0,-127,
  43.                                     0,0,-127,
  44.                                     0,0,-127,
  45.                                     0,0,-127});
  46.         VertexArray basePositions = new VertexArray(6, 3, 2);
  47.         basePositions.set(0,6,new short[] {-100, -100, 0,
  48.                                           100,  -100, 0,
  49.                                           100,   0, 0,
  50.                                           100,   100, 0,
  51.                                           -100,  100, 0,
  52.                                           -100,  0, 0});
  53.        
  54.         VertexArray[] targetPos = new VertexArray[2];
  55.         for (int i = 0; i < targetPos.length; i++)
  56.         {
  57.             targetPos[i] = new VertexArray(6,3,2);
  58.             targetPos[i].set(0,6,new short[] {-100,              -100, 0,
  59.                                              100,               -100, 0,
  60.                                              (short)(50*(1-i)+100),   0, 0,
  61.                                              100,                100, 0,
  62.                                              -100,               100, 0,
  63.                                              (short)(-50*i-100), 0, 0});
  64.         }
  65.         TriangleStripArray strip = new TriangleStripArray(new int[] {1, 0, 2, 5, 3, 4},
  66.                                                           new int[] {6});
  67.         VertexBuffer base = new VertexBuffer();
  68.         base.setPositions(basePositions, .1f, null);
  69.         base.setNormals(normals);
  70.         base.setDefaultColor(0xFF0B7FCC);
  71.         VertexBuffer[] targets = new VertexBuffer[2];
  72.         for (int i = 0; i < targets.length; i++)
  73.         {
  74.             targets[i] = new VertexBuffer();
  75.             targets[i].setPositions(targetPos[i], 1.0f, null);
  76.             if (i == 0)
  77.             {
  78.                 targets[i].setDefaultColor(0xFFFF0000);
  79.             }
  80.             else
  81.             {
  82.                 targets[i].setDefaultColor(0xFF0B7FCC);
  83.             }
  84.         }
  85.         Material material = new Material();
  86.         material.setVertexColorTrackingEnable(true);
  87.         PolygonMode polyMode = new PolygonMode();
  88.         polyMode.setCulling(PolygonMode.CULL_NONE);
  89.         polyMode.setTwoSidedLightingEnable(true);
  90.         Appearance appearance = new Appearance();
  91.         appearance.setMaterial(material);
  92.         appearance.setPolygonMode(polyMode);
  93.         iMorphingMesh = new MorphingMesh(base, targets, strip, appearance);
  94.         iLight = new Light();
  95.         iLight.setMode(Light.DIRECTIONAL);
  96.         iLight.setIntensity(0.7f);
  97.         iLight.setColor(0xFFFFFF);
  98.         iLightTrans = new Transform();
  99.         iLightTrans.postTranslate(-10.f, -10.f, 6.f);
  100.         iCamera = new Camera();
  101.         iCamera.setPerspective(90.f, 1.f, 1.f, 150.f);
  102.         Transform cameraTrans = new Transform();
  103.         cameraTrans.postTranslate(0.f, 0.f, 26.f);
  104.         Graphics3D.getInstance().setCamera(iCamera, cameraTrans);
  105.     }
  106. }