J2ME的3D手机开发必看例

加载World

  首先,要从M3D文件中加载World。在pogoroo.m3g文件中,你会看到一只袋鼠在一根弹簧单高跷杆上跳跃,其身边是一片绿茵。下面的列表1调用了加载器类的方法load()。

  列表1. 加载

try {
 //从M3D文件中加载World
 myWorld = (World)Loader.load("/pogoroo.m3g")[0];
 getObjects();
 setupAspectRatio();
}
catch(Exception e) {
 e.printStackTrace();
}

  从3D世界中取得对象

  3D世界已经被加载,现在你必须从中取得各个对象(见列表2)。这里,3D世界中有四个对象,其中之一是有关动画(袋鼠在单脚跳)的信息。你可以使用World的find()方法来取得这些对象。

  列表2. 从3D World中取得对象

try {
 tRoo = (Group) myWorld.find(POGOROO);
 tCams = (Group) myWorld.find(CAMERA);
 acRoo = (Group) myWorld.find(TRANSFORM);
 animRoo = (AnimationController) myWorld.find(ROO);
 //取得动画的长度
 AnimationTrack track = acRoo.getAnimationTrack(0);
 animLength = 1000; // 缺省长度为1秒
 if (track != null) {
  KeyframeSequence ks = track.getKeyframeSequence();
  if (ks != null) animLength = ks.getDuration();
 }

}
catch(Exception e) {
 e.printStackTrace();
}

  设置窗口宽高比例

  你必须设置窗口的宽高比例以使对象能够正确着色。列表3中的代码是未改动的-基本上同Sun的例子一样。首先,检查画布的宽度和高度,然后根据相机的类型来计算宽高比例。

  列表3. 设置宽高比例

void setupAspectRatio() {
 viewport_x = 0;
 viewport_y = 0;
 viewport_width = myCanvas.getWidth();
 viewport_height = myCanvas.getHeight();
 Camera cam = myWorld.getActiveCamera();
 float[] params = new float[4];
 int type = cam.getProjection(params);
 if(type != Camera.GENERIC) {
  //计算窗口的宽高比
  float waspect=viewport_width/viewport_height;
  if (waspect<params[1]) {
   float height = viewport_width/params[1];
   viewport_height=(int)height;
   viewport_y=(myCanvas.getHeight()-viewport_height)/2;
  }
  else {
   float width = viewport_height*params[1];
   viewport_width=(int)width;
   viewport_x=(myCanvas.getWidth()-viewport_width)/2;
  }
 }
}

  刷新视图

  为了刷新视图,你可以用TimerTask来调用画布的repaint()方法。另一种方法是直接使用线程,然后创建ExampleCanvas(画布类的名字)来实现Runnable接口。

  列表4. 刷新视图

private class RefreshTask extends TimerTask
{
 public void run(){
  if(myCanvas != null && myGraphics3D != null && myWorld != null) {
   int startTime = (int)System.currentTimeMillis();
   int validity = myWorld.animate(startTime);
   myCanvas.repaint(viewport_x, viewport_y, viewport_width, viewport_height);
  }
 }
}

完整的例程代码分析

  在列表5中,你会看到应用程序的完整代码。虽然长些,但是比Sun的例子要简单许多。你可以通过给应用程序添加上一些动作和逻辑来练习你的MIDP技能。

  列表5. 完整的例程代码

package com.kontio;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.lang.IllegalArgumentException;
import java.io.*;
import java.util.*;
import javax.microedition.m3g.*;

public class Example3D extends MIDlet implements CommandListener{
 //我们在场景中使用的对象的UserID
 static final int POGOROO = 554921620;
 static final int CAMERA = 769302310;
 static final int TRANSFORM = 347178853;
 static final int ROO = 418071423;
 
 private Display myDisplay = null;
 private ExampleCanvas myCanvas = null;

 private Timer myRefreshTimer = new Timer();
 private TimerTask myRefreshTask = null;

 private Command exitCommand = new Command("Exit", Command.ITEM, 1);

 Graphics3D myGraphics3D = Graphics3D.getInstance();
 World myWorld = null;

 private AnimationController animRoo = null;
 private Group tRoo = null;
 private Group tCams = null;
 private Group acRoo = null;

 private int animLength = 0;

 int viewport_x;
 int viewport_y;
 int viewport_width;
 int viewport_height;

 public Example3D(){
  super();
  myDisplay = Display.getDisplay(this);
  myCanvas = new ExampleCanvas(this);
  myCanvas.setCommandListener(this);
  myCanvas.addCommand(exitCommand);
 }

 public void startApp() throws MIDletStateChangeException{
  myDisplay.setCurrent(myCanvas);

  try{
   // 从文件中加载World
   myWorld = (World)Loader.load("/pogoroo.m3g")[0];
   getObjects();
   setupAspectRatio();
  }
  catch(Exception e){
   e.printStackTrace();
  }

  myRefreshTask = new RefreshTask();

  // 调度一个重要执行的计时器以显示出帧速率20fps.
  myRefreshTimer.schedule(myRefreshTask, 0, 50);
 }

 void setupAspectRatio(){
  viewport_x = 0;
  viewport_y = 0;
  viewport_width = myCanvas.getWidth();
  viewport_height = myCanvas.getHeight();

  Camera cam = myWorld.getActiveCamera();

  float[] params = new float[4];
  int type = cam.getProjection(params);
  if(type != Camera.GENERIC){
   //计算窗口的宽高比例
   float waspect=viewport_width/viewport_height;

   if (waspect<params[1]){
    float height = viewport_width/params[1];
    viewport_height=(int)height;
    viewport_y=(myCanvas.getHeight()-viewport_height)/2;
   }
   else{
    float width = viewport_height*params[1];
    viewport_width=(int)width;
    viewport_x=(myCanvas.getWidth()-viewport_width)/2;
   }
  }
 }

 public void getObjects(){
  try{
   tRoo = (Group) myWorld.find(POGOROO);
   tCams = (Group) myWorld.find(CAMERA);
   acRoo = (Group) myWorld.find(TRANSFORM);
   animRoo = (AnimationController) myWorld.find(ROO);

   //取得动画的长度
   AnimationTrack track = acRoo.getAnimationTrack(0);
   animLength = 1000; // 缺省的长度,1秒
   if (track != null){
    KeyframeSequence ks = track.getKeyframeSequence();
    if (ks != null)
     animLength = ks.getDuration();
   }

  }
  catch(Exception e){
   e.printStackTrace();
  }
 }

 public void pauseApp(){}

 public void destroyApp(boolean unconditional) throws MIDletStateChangeException{
  myRefreshTimer.cancel();
  myRefreshTimer = null;
  myRefreshTask = null;
 }

 public void paint(Graphics g){
  if(g.getClipWidth() != viewport_width ||
   g.getClipHeight() != viewport_height ||
   g.getClipX() != viewport_x ||
   g.getClipY() != viewport_y){
  g.setColor(0x00);
  g.fillRect(0, 0, myCanvas.getWidth(), myCanvas.getHeight());
 }

 if ((myGraphics3D != null) && (myWorld != null)){
  myGraphics3D.bindTarget(g);
  myGraphics3D.setViewport(viewport_x, viewport_y,
  viewport_width, viewport_height);
  myGraphics3D.render(myWorld);
  myGraphics3D.releaseTarget();
 }
}

public void commandAction(Command cmd, Displayable disp)
{
 if (cmd == exitCommand){
  try{
   destroyApp(false);
   notifyDestroyed();
  }
  catch(Exception e){
   e.printStackTrace();
  }
 }
}

private class RefreshTask extends TimerTask{
 public void run(){
  if(myCanvas !=null && myGraphics3D != null && myWorld != null{
   int startTime = (int)System.currentTimeMillis();
   int validity = myWorld.animate(startTime);
   myCanvas.repaint(viewport_x, viewport_y, viewport_width, viewport_height);
  }
 }
}

class ExampleCanvas extends Canvas{
 Example3D myRooMIDlet;
 int i = 0;

 ExampleCanvas(Example3D Testlet) { myRooMIDlet = Testlet; }
 void init() { }

 void destroy() { }

 protected void paint(Graphics g) { myRooMIDlet.paint(g); }
 protected void keyPressed(int i) { }
 protected void keyReleased(int i) { }
 protected void keyRepeated(int i) { }
 protected void pointerDragged(int x, int y) { }
 protected void pointerPressed(int x, int y) { }
 protected void pointerReleased(int x, int y) { }
}
}

  运行在模拟器中的例程

  展示了例程在WTK模拟器中运行的结果。图中的袋鼠和田地看上去棒极了。如果设计者选择对其中任何对象改变一下的话,可以用the3D工具来完成,而在例程MIDlet中不需要作任何变化。

 

 例程在模拟器中运行的结果

 结论

  现在,你又看到一种使用JSR 184(也称移动3D API)的更高级的方式来创建3D应用程序。在保留模式下,设计者可以使用现有的3D建模工具来创建3D世界和其中的对象,然后把这些模型输出到M3G文件中。之后,应用程序只需装入该模型并在屏幕上绘制3D世界的视图即可