你好,游客 登录 注册 搜索
背景:
阅读新闻

Android开发之3D物理引擎

[日期:2012-06-04] 来源:Linux社区  作者:htttw [字体:大 中 小]

第三个实验

我们写一个Android下跑的3D程序:屏幕中央有一个正方体,用手拖动到不同的地方会朝那个地方旋转,这里这样参考了官网的Demo:http://www.jpct.net/wiki/index.php/Hello_World_for_Android

1.

新建一个Demo的Android Project,如下:


2.

将jpct-ae.jar加入到Project中(注意,不是jpct.jar)

3.

修改DemoActivity.java如下:

  1. package com.test.demo;  
  2.   
  3. import java.lang.reflect.Field;  
  4.   
  5. import javax.microedition.khronos.egl.EGL10;  
  6. import javax.microedition.khronos.egl.EGLConfig;  
  7. import javax.microedition.khronos.egl.EGLDisplay;  
  8. import javax.microedition.khronos.opengles.GL10;  
  9.   
  10. import android.app.Activity;  
  11. import android.opengl.GLSurfaceView;  
  12. import android.os.Bundle;  
  13. import android.view.MotionEvent;  
  14.   
  15. import com.threed.jpct.Camera;  
  16. import com.threed.jpct.FrameBuffer;  
  17. import com.threed.jpct.Light;  
  18. import com.threed.jpct.Logger;  
  19. import com.threed.jpct.Object3D;  
  20. import com.threed.jpct.Primitives;  
  21. import com.threed.jpct.RGBColor;  
  22. import com.threed.jpct.SimpleVector;  
  23. import com.threed.jpct.Texture;  
  24. import com.threed.jpct.TextureManager;  
  25. import com.threed.jpct.World;  
  26. import com.threed.jpct.util.BitmapHelper;  
  27. import com.threed.jpct.util.MemoryHelper;  
  28.   
  29.   
  30. public class DemoActivity extends Activity {  
  31.   
  32.     // Used to handle pause and resume...   
  33.     private static DemoActivity master = null;  
  34.   
  35.     private GLSurfaceView mGLView;  
  36.     private MyRenderer renderer = null;  
  37.     private FrameBuffer fb = null;  
  38.     private World world = null;  
  39.     private RGBColor back = new RGBColor(50, 50, 100);  
  40.   
  41.     private float touchTurn = 0;  
  42.     private float touchTurnUp = 0;  
  43.   
  44.     private float xpos = -1;  
  45.     private float ypos = -1;  
  46.   
  47.     private Object3D cube = null;  
  48.     private int fps = 0;  
  49.   
  50.     private Light sun = null;  
  51.   
  52.     protected void onCreate(Bundle savedInstanceState) {  
  53.   
  54.         Logger.log("onCreate");  
  55.   
  56.         if (master != null) {  
  57.             copy(master);  
  58.         }  
  59.   
  60.         super.onCreate(savedInstanceState);  
  61.         mGLView = new GLSurfaceView(getApplication());  
  62.   
  63.         mGLView.setEGLConfigChooser(new GLSurfaceView.EGLConfigChooser() {  
  64.             public EGLConfig chooseConfig(EGL10 egl, EGLDisplay display) {  
  65.                 // Ensure that we get a 16bit framebuffer. Otherwise, we'll fall   
  66.                 // back to Pixelflinger on some device (read: Samsung I7500)   
  67.                 int[] attributes = new int[] { EGL10.EGL_DEPTH_SIZE, 16, EGL10.EGL_NONE };  
  68.                 EGLConfig[] configs = new EGLConfig[1];  
  69.                 int[] result = new int[1];  
  70.                 egl.eglChooseConfig(display, attributes, configs, 1, result);  
  71.                 return configs[0];  
  72.             }  
  73.         });  
  74.   
  75.         renderer = new MyRenderer();  
  76.         mGLView.setRenderer(renderer);  
  77.         setContentView(mGLView);  
  78.     }  
  79.   
  80.     @Override  
  81.     protected void onPause() {  
  82.         super.onPause();  
  83.         mGLView.onPause();  
  84.     }  
  85.   
  86.     @Override  
  87.     protected void onResume() {  
  88.         super.onResume();  
  89.         mGLView.onResume();  
  90.     }  
  91.   
  92.     @Override  
  93.     protected void onStop() {  
  94.         super.onStop();  
  95.     }  
  96.   
  97.     private void copy(Object src) {  
  98.         try {  
  99.             Logger.log("Copying data from master Activity!");  
  100.             Field[] fs = src.getClass().getDeclaredFields();  
  101.             for (Field f : fs) {  
  102.                 f.setAccessible(true);  
  103.                 f.set(this, f.get(src));  
  104.             }  
  105.         } catch (Exception e) {  
  106.             throw new RuntimeException(e);  
  107.         }  
  108.     }  
  109.   
  110.     public boolean onTouchEvent(MotionEvent me) {  
  111.   
  112.         if (me.getAction() == MotionEvent.ACTION_DOWN) {  
  113.             xpos = me.getX();  
  114.             ypos = me.getY();  
  115.             return true;  
  116.         }  
  117.   
  118.         if (me.getAction() == MotionEvent.ACTION_UP) {  
  119.             xpos = -1;  
  120.             ypos = -1;  
  121.             touchTurn = 0;  
  122.             touchTurnUp = 0;  
  123.             return true;  
  124.         }  
  125.   
  126.         if (me.getAction() == MotionEvent.ACTION_MOVE) {  
  127.             float xd = me.getX() - xpos;  
  128.             float yd = me.getY() - ypos;  
  129.   
  130.             xpos = me.getX();  
  131.             ypos = me.getY();  
  132.   
  133.             touchTurn = xd / -100f;  
  134.             touchTurnUp = yd / -100f;  
  135.             return true;  
  136.         }  
  137.   
  138.         try {  
  139.             Thread.sleep(15);  
  140.         } catch (Exception e) {  
  141.             // No need for this...   
  142.         }  
  143.   
  144.         return super.onTouchEvent(me);  
  145.     }  
  146.   
  147.     protected boolean isFullscreenOpaque() {  
  148.         return true;  
  149.     }  
  150.   
  151.     class MyRenderer implements GLSurfaceView.Renderer {  
  152.   
  153.         private long time = System.currentTimeMillis();  
  154.   
  155.         public MyRenderer() {  
  156.         }  
  157.   
  158.         public void onSurfaceChanged(GL10 gl, int w, int h) {  
  159.             if (fb != null) {  
  160.                 fb.dispose();  
  161.             }  
  162.             fb = new FrameBuffer(gl, w, h);  
  163.   
  164.             if (master == null) {  
  165.   
  166.                 world = new World();  
  167.                 world.setAmbientLight(20, 20, 20);  
  168.   
  169.                 sun = new Light(world);  
  170.                 sun.setIntensity(250, 250, 250);  
  171.   
  172.                 // Create a texture out of the icon...:-)   
  173.                 Texture texture = new Texture(BitmapHelper.rescale(BitmapHelper.convert(getResources().getDrawable(R.drawable.ic_launcher)), 64, 64));  
  174.                 TextureManager.getInstance().addTexture("texture", texture);  
  175.   
  176.                 cube = Primitives.getCube(10);  
  177.                 cube.calcTextureWrapSpherical();  
  178.                 cube.setTexture("texture");  
  179.                 cube.strip();  
  180.                 cube.build();  
  181.   
  182.                 world.addObject(cube);  
  183.   
  184.                 Camera cam = world.getCamera();  
  185.                 cam.moveCamera(Camera.CAMERA_MOVEOUT, 50);  
  186.                 cam.lookAt(cube.getTransformedCenter());  
  187.   
  188.                 SimpleVector sv = new SimpleVector();  
  189.                 sv.set(cube.getTransformedCenter());  
  190.                 sv.y -= 100;  
  191.                 sv.z -= 100;  
  192.                 sun.setPosition(sv);  
  193.                 MemoryHelper.compact();  
  194.   
  195.                 if (master == null) {  
  196.                     Logger.log("Saving master Activity!");  
  197.                     master = DemoActivity.this;  
  198.                 }  
  199.             }  
  200.         }  
  201.   
  202.         public void onSurfaceCreated(GL10 gl, EGLConfig config) {  
  203.         }  
  204.   
  205.         public void onDrawFrame(GL10 gl) {  
  206.             if (touchTurn != 0) {  
  207.                 cube.rotateY(touchTurn);  
  208.                 touchTurn = 0;  
  209.             }  
  210.   
  211.             if (touchTurnUp != 0) {  
  212.                 cube.rotateX(touchTurnUp);  
  213.                 touchTurnUp = 0;  
  214.             }  
  215.   
  216.             fb.clear(back);  
  217.             world.renderScene(fb);  
  218.             world.draw(fb);  
  219.             fb.display();  
  220.   
  221.             if (System.currentTimeMillis() - time >= 1000) {  
  222.                 Logger.log(fps + "fps");  
  223.                 fps = 0;  
  224.                 time = System.currentTimeMillis();  
  225.             }  
  226.             fps++;  
  227.         }  
  228.     }  
  229. }  

4.

点击Run As Android Application,打开模拟器,效果如下:


拖动中央的正方体,会朝不同的方向旋转

至此,第三个实验完成,快下载到手机里玩玩吧~~

这三个实验只是jpct的使用而已,更复杂的东西需要看官网的Wiki和相关文档。

linux
相关资讯       Android开发基础 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款