Imode平台下使用原生数据创建3D模型_旋转

Imode平台下有两种方式创建3D模型:1,从外部的.mbac文件导入模型;2,使用原生数据创建3D模型。下面是一个使用原生数据创建一个三角锥体的程序,可以绕x,y轴旋转。
 
import com.nttdocomo.ui.*;
import com.nttdocomo.opt.ui.j3d.*;
//
/**
* @author ppby
 * Rotate 3D
 */
public class Rotate extends IApplication {
 private Canvas3D canv;
 public void start() {
  try {
   canv = new Canvas3D();
   Display.setCurrent(canv);
   Thread thread = new Thread(canv);
   thread.start();
  } catch (IllegalThreadStateException itse) {
   itse.printStackTrace();
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
}
/**
 * Canvas class
 */
class Canvas3D extends Canvas implements Runnable {
 // Command list array
 private int[]  cmdListFunc_;
 private int[]  cmdListPrim_;
 // 3D affine transformation matrix
 private AffineTrans viewT = new AffineTrans();
 private AffineTrans affineT  = new AffineTrans();
 private AffineTrans rotateT  = new AffineTrans();
 private int   rotX, rotY; // Variable for object rotation

 public Canvas3D() {
  // Set a camera:(eye position,look at position,up direction)
  viewT.lookAt(
    new Vector3D(0, 0, 800),
    new Vector3D(0, 0, 0),
    new Vector3D(0, 4096, 0)
    );
  // Set a command list
  setupCmdListFunc();
  setupCmdListPrim();
 }
 public void run() {
  while (true) {
   UpdateAffineTrans();
   repaint();
   try {
    Thread.sleep(100); // wait 100ms
   } catch (InterruptedException ie) {
   }
  }
 }
 public void UpdateAffineTrans()
 {
  affineT.setIdentity();
  
  //rotate y axis
  rotateT.setIdentity();
  rotateT.setRotateY(rotY);
  affineT.mul(rotateT);
  
  //rotate x axis
  rotateT.setIdentity();
  rotateT.setRotateX(rotX);
  affineT.mul(rotateT,affineT);
 }
 /**
  * Rendering method.
  */
 public void paint(Graphics g) {
  if (g == null)
   return;
  try {
   g.lock();
   // Background Clear
   g.setColor(Graphics.getColorOfRGB(0, 0, 0));
   g.fillRect(0, 0, getWidth(), getHeight());
   g.setColor(Graphics.getColorOfRGB(150, 150, 120));
   Graphics3D g3d = (Graphics3D) g;
   
   //Method 1
   g3d.setClipRect3D(0, 0, getWidth(),getHeight());
   g3d.setScreenCenter(getWidth()>>1, getHeight()>>1);
   g3d.setPerspective(200, 10000, 4096*50/360);
    
   //Method 2:The effect is the same s method 1. 
  // g3d.executeCommandList(cmdListFunc_); // Excute the command list
   
   //set viewport
   affineT.mul(viewT,affineT);
   g3d.setViewTrans(affineT);    // Set view coordinate matrix
   setupCmdListPrim();
   g3d.executeCommandList(cmdListPrim_); // Excute a command list
   //
   g.setColor(Graphics.getColorOfRGB(0, 255, 0xff));
   g.drawString("Use:up,down,left,right key.", 0, Font.getDefaultFont()
     .getHeight());
   g.unlock(true);
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
 /**
  * Key event process.
  */
 public void processEvent(int type, int param) {
  try {
   if (type == Display.KEY_PRESSED_EVENT) {
    switch (param) {
    case Display.KEY_UP:
     rotX -= 128;
     rotX %= 4096;
     break;
    case Display.KEY_DOWN:
     rotX += 128;
     rotX %= 4096;
     break;
    case Display.KEY_LEFT:
     rotY -= 128;
     rotY %= 4096;
     break;
    case Display.KEY_RIGHT:
     rotY += 128;
     rotY %= 4096;
     break;
    }
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
 /**
  * Set the command list for methods.
  * @return true - success. false - fail.
  */
 private boolean setupCmdListFunc() {
  try {
   int[] cmdListFunc = {
     Graphics3D.COMMAND_LIST_VERSION_1,
     Graphics3D.COMMAND_CLIP_RECT, 0, 0, getWidth(),
     getHeight(), Graphics3D.COMMAND_SCREEN_CENTER,
     (getWidth() >> 1), (getHeight() >> 1),
     Graphics3D.COMMAND_PERSPECTIVE1, 200, 10000,
     4096 * 50 / 360, Graphics3D.COMMAND_END };
   cmdListFunc_ = cmdListFunc;
   return true;
  } catch (Exception e) {
   e.printStackTrace();
   return false;
  }
 }
 /**
  * Set the command list for primitives.
  * @return true - Setting succeeded. false - Setting failed.
  */
 private boolean setupCmdListPrim() {
  try {
   //draw four trangles.
   int[] cmdListPrim = {
     Graphics3D.COMMAND_LIST_VERSION_1,
     // TRIANGLES
     (Graphics3D.COMMAND_RENDER_TRIANGLES
       | Graphics3D.COLOR_PER_FACE | (4 << 16)),//trangle NUM << 16
     //each trangle's three vertexes.vertex:(x,y,z)
     0, 120, 0, -120, -120, 120, 120, -120, 120,
     0, 120, 0, 120, -120, 120, 120, -120, -120,
     0, 120, 0, 120, -120, -120, -120, -120, -120,
     0, 120, 0, -120, -120, -120, -120, -120, 120,
     //each trangle's color
     0xff << 16, //red
     0xff << 8, //green
     0xff << 4, //blue
     0xff << 8 | 0xff << 16, //yellow
     // flush
     Graphics3D.COMMAND_FLUSH, Graphics3D.COMMAND_END };
   cmdListPrim_ = cmdListPrim;
   return true;
  } catch (Exception e) {
   e.printStackTrace();
   return false;
  }
 }
}

你可能感兴趣的:(exception,list,vector,command,平台,transformation)