android 3D 游戏实现之人物行走(MD2)
如果充分理解了HelloWorld,了解了一些JPCT-AE游戏框架的机制,
那么,这个示例可以是进阶篇了,其实用JPCT-AE加载
3DS,MD2文件都很简单了,主要是掌握一些基本的操作,
今天先把怎么实现加载MD2文件的代码附上,明天我再传上如果实现载入3DS及3DS KeyFrame动画的代码附上。
此程序所需的纹理贴图及MD2文件请从
http://www.doczj.com/doc/ca589a0179563c1ec5da71eb.html/user/Simdanfeg处下载
下面附上源程序(呵呵,很Cool^_^)
(1)Activity源码
Java代码
1.packagesim.feel;
2.
3.importjava.io.IOException;
4.
5.importjava.io.InputStream;
6.
7.importandroid.app.Activity;
8.
9.importandroid.content.res.AssetManager;
10.
11.importandroid.content.res.Resources;
12.
13.importandroid.graphics.Bitmap;
14.
15.importandroid.graphics.BitmapFactory;
16.
17.importandroid.opengl.GLSurfaceView;
18.
19.importandroid.os.Bundle;
20.
21.publicclassLoadMextendsActivity{
22.
23.privateGLSurfaceViewglView;
24.
25.privateMyRenderermr;
26.
27.@Override
28.
29.publicvoidonCreate(BundlesavedInstanceState){
30.
31.super.onCreate(savedInstanceState);
32.
33.//加载图片
34.
35.LoadImage.loadi(getResources());
36.
37.//加载文件
38.
39.newLoadAssets(getResources());
40.
41.glView=newGLSurfaceView(this);
42.
43.mr=newMyRenderer();
44.
45.glView.setRenderer(mr);
46.
47.setContentView(glView);
48.
49.}
50.
51.}
52.
53.//载入纹理图片
54.
55.classLoadImage{
56.
57.publicstaticBitmapbitmap;
58.
59.publicstaticvoidloadi(Resourcesres){
60.
61.bitmap=BitmapFactory.decodeResource
(res,R.drawable.disco);
62.
63.}
64.
65.}
66.
67.//载入Assets文件夹下的文件
68.
69.classLoadAssets{
70.
71.publicstaticResourcesres;
72.
73.publicLoadAssets(Resourcesresources){
74.
75.res=resources;
76.
77.}
78.
79.publicstaticInputStreamloadf(StringfileName){
80.
81.AssetManageram=LoadAssets.res.getAssets();
82.
83.try{
84.
85.returnam.open
(fileName,AssetManager.ACCESS_UNKNOWN);
86.
87.}catch(IOExceptione){
88.
89.returnnull;
90.
91.}
92.
93.}
94.
95.}
(2)MyRenderer类代码
Java代码
1.packagesim.feel;
2.importjavax.microedition.khronos.egl.EGLConfig;
3.importjavax.microedition.khronos.opengles.GL10;
4.importandroid.opengl.GLSurfaceView.Renderer;
5.importcom.threed.jpct.FrameBuffer;
6.importcom.threed.jpct.Loader;
7.importcom.threed.jpct.Object3D;
8.importcom.threed.jpct.RGBColor;
9.importcom.threed.jpct.Texture;
10.importcom.threed.jpct.TextureManager;
11.importcom.threed.jpct.World;
12.publicclassMyRendererimplementsRenderer{
13.//纹理数组
14.privateString[]textures={"disco"};
15.//scale
16.privatefloatthingScale=0.8f;
17.//world对象
18.privateWorldworld;
19.//FrameBuffer对象
20.privateFrameBufferfb;
21.//Object3D
22.privateObject3Dsnork;
23.//行走动画
24.privateintan=2;
25.privatefloatind=0;
26./**
27.*绘制到屏幕上
28.*/
29.publicvoidonDrawFrame(GL10gl){
30.doAnim();
31.//以蓝色清除整个屏幕
32.fb.clear(RGBColor.BLACK);
33.world.renderScene(fb);
34.world.draw(fb);
35.fb.display();
36.}
37.publicvoidonSurfaceChanged(GL10gl,intwidth,intheight){
38.if(fb!=null){
39.fb=null;
40.}
41.fb=newFrameBuffer(gl,width,height);
42.}
43.publicvoidonSurfaceCreated(GL10gl,EGLConfigarg1){
44.world=newWorld();
45.world.setAmbientLight(150,150,150);
46.//将所有纹理添加到所定义的纹理数组中,此程序只有一个,所以为txtures[0]
47.for(inti=0;i48.TextureManager.getInstance().addTexture(textures[i],
49.newTexture(LoadImage.bitmap));
50.}
51.//从assets文件夹中读取snork.md2文件
52.snork=loadModel("snork.md2",thingScale);
53.//旋转snork对象到"适当位置"
54.snork.translate(0,-25,-50);
55.//给snork对象添加名为disco的纹理贴图
56.snork.setTexture(textures[0]);
57.//释放部分资源
58.snork.strip();
59.//编译
60.snork.build();
61.//将snork添加到World对象中
62.world.addObject(snork);
63.world.getCamera().setPosition(0,0,-100);
64.world.getCamera().lookAt(snork.getTransformedCenter());
65.}
66.//载入模型
67.privateObject3DloadModel(Stringfilename,floatscale){
68.Object3Dmodel=Loader.loadMD2(LoadAssets.loadf(filename),scale);
69.returnmodel;
70.}
71./**
72.*实现MD2人物行走的关键代码
73.*/
74.publicvoiddoAnim(){
75.//每一帧加0.018f
76.ind+=0.018f;
77.if(ind>1f){
78.ind-=1f;
79.}
80.//关于此处的两个变量,ind的值为0-1(jpct-ae规定),0表示第一帧,1为最后一帧;
81.//至于an这个变量,它的意思是sub-sequence如果在keyframe(3ds中),因为在一个
82.//完整的动画包含了seq和sub-sequence,所以设置为2表示执行sub-sequence的动画,
83.//但这里设置为2我就不太明白了,不过如果不填,效果会不自然,所以我就先暂时把它
84.//设置为2
85.snork.animate(ind,an);
86.}
87.}
运行效果: