Android 3 Dゲーム実現のFirst Step


この例では、従来の純粋なOpengles実装と比較して、このフレームワークが便利であると個人的に考えられているため、JPCT-AE実装を採用した立方体の具体的な実装プロセスを示し、今日からそのウェブサイト上のWikiを通じてJPCT-AE実装を紹介する.この例では、JPCT-AEのヘルプドキュメント、すなわち入門をすばやく理解できます.
 
(1)JPCTとは何か:OPENGL esを封入した3 Dゲームエンジンで、j 2 seとandroidの2バージョンがある.
(2)jarパッケージおよびヘルプドキュメントの入手方法:http://download.csdn.net/user/Simdanfegダウンロード
最初の例:同じ立方体、異なる実装
 
package com.threed.jpct.example;

import java.lang.reflect.Field;

import javax.microedition.khronos.egl.EGL10;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.egl.EGLDisplay;
import javax.microedition.khronos.opengles.GL10;

import android.app.Activity;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.view.MotionEvent;

import com.threed.jpct.Camera;
import com.threed.jpct.FrameBuffer;
import com.threed.jpct.Light;
import com.threed.jpct.Logger;
import com.threed.jpct.Object3D;
import com.threed.jpct.Primitives;
import com.threed.jpct.RGBColor;
import com.threed.jpct.SimpleVector;
import com.threed.jpct.Texture;
import com.threed.jpct.TextureManager;
import com.threed.jpct.World;
import com.threed.jpct.util.BitmapHelper;
import com.threed.jpct.util.MemoryHelper;

/**
 *        。            android             JPCT-AE  3D    。
 *     Activity    pause resume   
 * 
 * @author EgonOlsen
 * 
 */
public class HelloWorld extends Activity {

    // HelloWorld      Activity onPause onResume  
    private static HelloWorld master = null;

    // GLSurfaceView  
    private GLSurfaceView mGLView;

    //  MyRenderer  
    private MyRenderer renderer = null;

    //  JPCT     FrameBuffer        ,                              。
    private FrameBuffer fb = null;

    // World  JPCT        ,          " "  。            JPCT   
    private World world = null;

    //   java.awt.*  Color 
    private RGBColor back = new RGBColor(50, 50, 100);

    private float touchTurn = 0;
    private float touchTurnUp = 0;

    private float xpos = -1;
    private float ypos = -1;

    // Object3D        ,            java.lang.Object  。
    //   Object3D                World   。Object3D World
    //           ,            /             .
    //                  。         World   ,  
    //         (          )。                
    //      World   ( World.addObject()      )。
    private Object3D cube = null;

    //     
    private int fps = 0;

    //    
    private Light sun = null;

    protected void onCreate(Bundle savedInstanceState) {
        // Logger   jPCT               ,         。
        //    JPCT                 
        Logger.log("onCreate");
        //         NULL,  Object         
        if (master != null) {
            copy(master);
        }

        super.onCreate(savedInstanceState);

        //    GLSurfaceView
        mGLView = new GLSurfaceView(this);
        //         EGLConfigChooser,      setRenderer(renderer)  
        //     setEGLConfigChooser     ,      ,          android.view.Surface    16       EGLConfig。
        mGLView.setEGLConfigChooser(new GLSurfaceView.EGLConfigChooser() {
            public EGLConfig chooseConfig(EGL10 egl, EGLDisplay display) {
                // Ensure that we get a 16bit framebuffer. Otherwise, we'll fall
                // back to Pixelflinger on some device (read: Samsung I7500)
                int[] attributes = new int[] { EGL10.EGL_DEPTH_SIZE, 16,
                        EGL10.EGL_NONE };
                EGLConfig[] configs = new EGLConfig[1];
                int[] result = new int[1];
                egl.eglChooseConfig(display, attributes, configs, 1, result);
                return configs[0];
            }
        });
        //    MyRenderer
        renderer = new MyRenderer();
        //   View    ,          ,      
        mGLView.setRenderer(renderer);
        //          
        setContentView(mGLView);
    }

    //   onPause()
    @Override
    protected void onPause() {
        super.onPause();
        mGLView.onPause();
    }

    //   onResume()
    @Override
    protected void onResume() {
        super.onResume();
        mGLView.onResume();
    }

    //   onStop()
    @Override
    protected void onStop() {
        super.onStop();
    }

    private void copy(Object src) {
        try {
            //     
            Logger.log("Copying data from master Activity!");
            //       ,                Filed  
            Field[] fs = src.getClass().getDeclaredFields();
            //   fs  
            for (Field f : fs) {
                //            。     false      ,   true,    。
                f.setAccessible(true);
                //              
                f.set(this, f.get(src));
            }
        } catch (Exception e) {
            //        
            throw new RuntimeException(e);
        }
    }

    public boolean onTouchEvent(MotionEvent me) {

        //     
        if (me.getAction() == MotionEvent.ACTION_DOWN) {
            //        x,y   xpos,ypos 
            xpos = me.getX();
            ypos = me.getY();
            return true;
        }
        //     
        if (me.getAction() == MotionEvent.ACTION_UP) {
            //   x,y         
            xpos = -1;
            ypos = -1;
            touchTurn = 0;
            touchTurnUp = 0;
            return true;
        }

        if (me.getAction() == MotionEvent.ACTION_MOVE) {
            //   x,y     x,y       
            float xd = me.getX() - xpos;
            float yd = me.getY() - ypos;
            // Logger.log("me.getX() - xpos----------->>"
            // + (me.getX() - xpos));
            xpos = me.getX();
            ypos = me.getY();
            Logger.log("xpos------------>>" + xpos);
            // Logger.log("ypos------------>>" + ypos);
            //  x   ,         ,       
            touchTurn = xd / -100f;
            touchTurnUp = yd / -100f;
            Logger.log("touchTurn------------>>" + touchTurn);
            // Logger.log("touchTurnUp------------>>" + touchTurnUp);
            return true;
        }

        //  Move      
        try {
            Thread.sleep(15);
        } catch (Exception e) {
            // No need for this...
        }

        return super.onTouchEvent(me);
    }

    // MyRenderer   GLSurfaceView.Renderer  
    class MyRenderer implements GLSurfaceView.Renderer {
        //         
        private long time = System.currentTimeMillis();
        //     
        private boolean stop = false;

        //   
        public void stop() {
            stop = true;
        }

        //       
        public void onSurfaceChanged(GL10 gl, int w, int h) {
            //   FrameBuffer  NULL,  fb    
            if (fb != null) {
                fb.dispose();
            }
            //        w,  h FrameBuffer
            fb = new FrameBuffer(gl, w, h);
            Logger.log(master + "");
            //   master  
            if (master == null) {

                //    World  
                world = new World();

                //          。              ,         。
                world.setAmbientLight(20, 20, 20);

                //  World         
                sun = new Light(world);

                //       
                sun.setIntensity(250, 250, 250);

                //       
                //     Texture(Bitmap image)
                // static Bitmap rescale(Bitmap bitmap, int width, int height)
                // static Bitmap convert(Drawable drawable)
                Texture texture = new Texture(BitmapHelper.rescale(
                        BitmapHelper.convert(getResources().getDrawable(
                                R.drawable.glass)), 64, 64));

                // TextureManager.getInstance()    Texturemanager  
                // addTexture("texture",texture)      
                TextureManager.getInstance().addTexture("texture", texture);

                // Object3D     :-)

                // Primitives            ,                
                //              ,         ,        。
                //   public static Object3D getCube(float scale) scale:  
                //        
                cube = Primitives.getCube(10);

                //             "  "   
                cube.calcTextureWrapSpherical();

                //        
                cube.setTexture("texture");

                //          PolygonManager  ,               
                cube.strip();

                //                          。
                //      "    "(  ,    ,  ,      ,
                //           ),  build()     ,
                cube.build();

                //  Object3D     world  
                world.addObject(cube);

                //  Camera   Camera/viewer           ,              
                //      Camera            World           。
                //       ,    Camera     ,  Camera(  )  w       World  w  、
                //         ,  ,       ,World  camera ,camera       。      
                //     ,     rotateCamera()  
                Camera cam = world.getCamera();

                //  50       Camera(        )
                cam.moveCamera(Camera.CAMERA_MOVEOUT, 50);

                // cub.getTransformedCenter()       
                // cam.lookAt(SimpleVector lookAt))
                //     camera            world-space    
                cam.lookAt(cube.getTransformedCenter());

                // SimpleVector             ,        
                //   SimpleVector       SimpleVector     (    
                //             (float x,float y,float z)  )。
                SimpleVector sv = new SimpleVector();

                //    SimpleVector x,y,z      SimpleVector(cube.getTransformedCenter())  
                sv.set(cube.getTransformedCenter());

                // Y     100
                sv.y -= 100;

                // Z     100
                sv.z -= 100;

                //       
                sun.setPosition(sv);

                //   GC finalization            ,            ,
                //               ,  ,               
                MemoryHelper.compact();

                //   master  ,        master HelloWorld  
                if (master == null) {
                    Logger.log("Saving master Activity!");
                    master = HelloWorld.this;
                }
            }
        }

        //     onSurfaceCreated(GL10 gl, EGLConfig config)
        public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        }

        //         :-D
        public void onDrawFrame(GL10 gl) {

            try {
                //   stop true
                if (!stop) {
                    //   touchTurn  0, Y   touchTure  
                    if (touchTurn != 0) {
                        //         Y     W  (          ),           。
                        cube.rotateY(touchTurn);
                        //  touchTurn 0
                        touchTurn = 0;
                    }

                    if (touchTurnUp != 0) {
                        //          x      (  ,      )   ,           。
                        cube.rotateX(touchTurnUp);
                        //  touchTureUp 0
                        touchTurnUp = 0;
                    }

                    //       (back)  FrameBuffer
                    fb.clear(back);
                    //           
                    world.renderScene(fb);
                    //   
                    world.draw(fb);
                    //       
                    fb.display();

                    //   FPS
                    if (System.currentTimeMillis() - time >= 1000) {
                        // Logger.log(fps + "fps");
                        fps = 0;
                        time = System.currentTimeMillis();
                    }
                    fps++;

                    //   stop false,  FrameBuffer
                } else {
                    if (fb != null) {
                        fb.dispose();
                        fb = null;
                    }
                }
                //      ,      
            } catch (Exception e) {
                Logger.log(e, Logger.MESSAGE);
            }
        }
    }
}