android 人物行走动画,android 3D 游戏实现之人物行走(MD2)
如果充分理解了HelloWorld(見android 3D 游戲實現之First step),了解了一些JPCT-AE游戲框架的機制,
那么,這個示例可以是進階篇了,其實用JPCT-AE加載3DS,MD2文件都很簡單了,主要是掌握一些基本的操作,
今天我先把怎么實現加載MD2文件的代碼附上,明天我再傳上如果實現載入3DS及3DS KeyFrame動畫的代碼附上。
此程序所需的紋理貼圖及MD2文件請從http://download.csdn.net/user/Simdanfeg處下載
下面附上源程序(呵呵,很Cool^_^)
(1)Activity源碼
package sim.feel;
import java.io.IOException;
import java.io.InputStream;
import android.app.Activity;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
public class LoadM extends Activity {
private GLSurfaceView glView;
private MyRenderer mr;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 加載圖片
LoadImage.loadi(getResources());
// 加載文件
new LoadAssets(getResources());
glView = new GLSurfaceView(this);
mr = new MyRenderer();
glView.setRenderer(mr);
setContentView(glView);
}
}
// 載入紋理圖片
class LoadImage {
public static Bitmap bitmap;
public static void loadi(Resources res) {
bitmap = BitmapFactory.decodeResource(res, R.drawable.disco);
}
}
// 載入Assets文件夾下的文件
class LoadAssets {
public static Resources res;
public LoadAssets(Resources resources) {
res = resources;
}
public static InputStream loadf(String fileName) {
AssetManager am = LoadAssets.res.getAssets();
try {
return am.open(fileName, AssetManager.ACCESS_UNKNOWN);
} catch (IOException e) {
return null;
}
}
}
(2)MyRenderer類代碼
package sim.feel;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.opengl.GLSurfaceView.Renderer;
import com.threed.jpct.FrameBuffer;
import com.threed.jpct.Loader;
import com.threed.jpct.Object3D;
import com.threed.jpct.RGBColor;
import com.threed.jpct.Texture;
import com.threed.jpct.TextureManager;
import com.threed.jpct.World;
public class MyRenderer implements Renderer {
// 紋理數組
private String[] textures = { "disco" };
// scale
private float thingScale = 0.8f;
// world對象
private World world;
// FrameBuffer對象
private FrameBuffer fb;
// Object3D
private Object3D snork;
// 行走動畫
private int an = 2;
private float ind = 0;
/**
* 繪制到屏幕上
*/
public void onDrawFrame(GL10 gl) {
doAnim();
// 以藍色清除整個屏幕
fb.clear(RGBColor.BLACK);
world.renderScene(fb);
world.draw(fb);
fb.display();
}
public void onSurfaceChanged(GL10 gl, int width, int height) {
if (fb != null) {
fb = null;
}
fb = new FrameBuffer(gl, width, height);
}
public void onSurfaceCreated(GL10 gl, EGLConfig arg1) {
world = new World();
world.setAmbientLight(150, 150, 150);
// 將所有紋理添加到所定義的紋理數組中,此程序只有一個,所以為txtures[0]
for (int i = 0; i < textures.length; i++) {
TextureManager.getInstance().addTexture(textures[i],
new Texture(LoadImage.bitmap));
}
// 從assets文件夾中讀取snork.md2文件
snork = loadModel("snork.md2", thingScale);
// 旋轉snork對象到"適當位置"
snork.translate(0, -25, -50);
// 給snork對象添加名為disco的紋理貼圖
snork.setTexture(textures[0]);
// 釋放部分資源
snork.strip();
// 編譯
snork.build();
// 將snork添加到World對象中
world.addObject(snork);
world.getCamera().setPosition(0, 0, -100);
world.getCamera().lookAt(snork.getTransformedCenter());
}
// 載入模型
private Object3D loadModel(String filename, float scale) {
Object3D model = Loader.loadMD2(LoadAssets.loadf(filename), scale);
return model;
}
/**
* 實現MD2人物行走的關鍵代碼
*/
public void doAnim() {
//每一幀加0.018f
ind += 0.018f;
if (ind > 1f) {
ind -= 1f;
}
// 關于此處的兩個變量,ind的值為0-1(jpct-ae規定),0表示第一幀,1為最后一幀;
//至于an這個變量,它的意思是sub-sequence如果在keyframe(3ds中),因為在一個
//完整的動畫包含了seq和sub-sequence,所以設置為2表示執行sub-sequence的動畫,
//但這里設置為2我就不太明白了,不過如果不填,效果會不自然,所以我就先暫時把它
//設置為2
snork.animate(ind, an);
}
}
運行效果:
總結
以上是生活随笔為你收集整理的android 人物行走动画,android 3D 游戏实现之人物行走(MD2)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 配置node服务器并且链接微信公众号接口
- 下一篇: Oracle调优总结