try {
//从M3D文件中加载World
myWorld = (World)Loader.load("/pogoroo.m3g")[0];
getObjects();
setupaspectRatio();
}
catch(Exception e) {
e.PRintStackTrace();
}
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();
}
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 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;
}
}
}
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);
}
}
}
八、完整的例程代码分析package com.kontio;
新闻热点
疑难解答