中文字幕 另类精品,亚洲欧美一区二区蜜桃,日本在线精品视频免费,孩交精品乱子片免费

<sup id="3hn2b"></sup>

    1. <sub id="3hn2b"><ol id="3hn2b"></ol></sub><legend id="3hn2b"></legend>

      1. <xmp id="3hn2b"></xmp>

      2. 新聞中心

        EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > Android 3D游戲?qū)崿F(xiàn)入門

        Android 3D游戲?qū)崿F(xiàn)入門

        作者: 時間:2012-02-28 來源:網(wǎng)絡(luò) 收藏

        此示例展示了一個立方體的具體過程,與之前的純Opengl es相比,它采用了JPCT-AE來,因?yàn)閭€人認(rèn)為這個框架很方便,于是從今天開始通過其網(wǎng)站上的Wiki來介紹JPCT-AE的實(shí)現(xiàn)。通過這個示例能讓你快速了解JPCT-AE的幫助文檔,也就是。

        本文引用地址:http://www.antipu.com.cn/article/149549.htm

        (1)什么是JPCT:一種封裝了OPENGL es的引擎,有j2se與android兩個版本。

        (2)如何獲得其jar包及幫助文檔:http://download.csdn.net/user/Simdanfeg處下載


        第一個示例:同樣的立方體,不同的實(shí)現(xiàn)

        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.Object;
        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應(yīng)用它更著重于展示如何使用JPCT-AE這個框架。
        * 它包含了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;

        // 當(dāng)JPCT渲染背景時FrameBuffer類提供了一個緩沖,它的結(jié)果本質(zhì)上是一個能顯示或者修改甚至能進(jìn)行更多后處理的圖片。
        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類是一個三維對象,千萬不要傻呼呼的認(rèn)為它與java.lang.Object類似。
        // 一個Object3D對象作為一個實(shí)例被添加到在渲染的World對象中。Object3D在World
        // 中一次添加一個實(shí)例 ,他們可能被聯(lián)系起作為孩子/父母來在他們中建立一個制度.
        // 人體模型當(dāng)然也能應(yīng)用在以上的規(guī)則中。他們常常不加到一個World實(shí)例中,而是
        // 綁定到其它對象中(人體模型或非人體模型)。有些方法 在這個類中需要一個實(shí)例
        // 添加到一個World實(shí)例中(用World.addObject()方法可以實(shí)現(xiàn))。
        private Object3D cube = null;

        // 每秒幀數(shù)
        private int fps = 0;

        // 光照類
        private Light sun = null;

        protected void onCreate(Bundle savedInstanceState) {
        // Logger類中 jPCT中一個普通的用于打印和存儲消息,錯誤和警告的日志類。
        // 每一個JPCT生成的消息將被加入到這個類的隊(duì)列中
        Logger.log(onCreate);
        // 如果本類對象不為NULL,將從Object中所有屬性裝入該類
        if (master != null) {
        copy(master);
        }

        super.onCreate(savedInstanceState);

        // 實(shí)例化GLSurfaceView
        mGLView = new GLSurfaceView(this);
        // 使用自己實(shí)現(xiàn)的 EGLConfigChooser,該實(shí)現(xiàn)必須在setRenderer(renderer)之前
        // 如果沒有setEGLConfigChooser方法被調(diào)用,則默認(rèn)情況下,視圖將選擇一個與當(dāng)前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];
        }
        });
        // 實(shí)例化MyRenderer
        renderer = new MyRenderer();
        // 設(shè)置View的渲染器,同時啟動線程調(diào)用渲染,以至啟動渲染
        mGLView.setRenderer(renderer);
        // 設(shè)置一個明確的視圖
        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!);
        // 返回一個數(shù)組,其中包含目前這個類的的所有字段的Filed對象
        Field[] fs = src.getClass().getDeclaredFields();
        // 遍歷fs數(shù)組
        for (Field f : fs) {
        // 嘗試設(shè)置無障礙標(biāo)志的值。標(biāo)志設(shè)置為false將使訪問檢查,設(shè)置為true,將其禁用。
        f.setAccessible(true);
        // 將取到的值全部裝入當(dāng)前類中
        f.set(this, f.get(src));
        }
        } catch (Exception e) {
        // 拋出運(yùn)行時異常
        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;
        }
        // 按鍵結(jié)束
        if (me.getAction() == MotionEvent.ACTION_UP) {
        // 設(shè)置x,y及旋轉(zhuǎn)角度為初始值
        xpos = -1;
        ypos = -1;
        touchTurn = 0;
        touchTurnUp = 0;
        return true;
        }

        if (me.getAction() == MotionEvent.ACTION_MOVE) {
        // 計(jì)算x,y偏移位置及x,y軸上的旋轉(zhuǎn)角度
        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軸為例,鼠標(biāo)從左向右拉為正,從右向左拉為負(fù)
        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類實(shí)現(xiàn)GLSurfaceView.Renderer接口
        class MyRenderer implements GLSurfaceView.Renderer {
        // 當(dāng)前系統(tǒng)的毫秒數(shù)
        private long time = System.currentTimeMillis();
        // 是否停止
        private boolean stop = false;

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

        // 當(dāng)屏幕改變時
        public void onSurfaceChanged(GL10 gl, int w, int h) {
        // 如果FrameBuffer不為NULL,釋放fb所占資源
        if (fb != null) {
        fb.dispose();
        }
        // 創(chuàng)建一個寬度為w,高為h的FrameBuffer
        fb = new FrameBuffer(gl, w, h);
        Logger.log(master + );
        // 如果master為空
        if (master == null) {

        // 實(shí)例化World對象
        world = new World();

        // 設(shè)置了環(huán)境光源強(qiáng)度。設(shè)置此值是負(fù)的整個場景會變暗,而為正將照亮了一切。
        world.setAmbientLight(20, 20, 20);

        // 在World中創(chuàng)建一個新的光源
        sun = new Light(world);

        // 設(shè)置光照強(qiáng)度
        sun.setIntensity(250, 250, 250);

        // 創(chuàng)建一個紋理
        // 構(gòu)造方法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提供了一些基本的三維物體,假如你為了測試而生成一些對象或?yàn)?br /> // 其它目的使用這些類將很明智,因?yàn)樗纯焖儆趾唵危恍枰d入和編輯。
        // 調(diào)用public static Object3D getCube(float scale) scale:角度
        // 返回一個立方體
        cube = Primitives.getCube(10);

        // 以紋理的方式給對象所有面包裝上紋理
        cube.calcTextureWrapSpherical();

        // 給對象設(shè)置紋理
        cube.setTexture(texture);

        // 除非你想在事后再用PolygonManager修改,否則釋放那些不再需要數(shù)據(jù)的內(nèi)存
        cube.strip();

        // 初始化一些基本的對象是幾乎所有進(jìn)一步處理所需的過程。
        // 如果對象是準(zhǔn)備渲染(裝載,紋理分配,安置,渲染模式設(shè)置,
        // 動畫和頂點(diǎn)控制器分配),那么build()必須被調(diào)用,
        cube.build();

        // 將Object3D對象添加到world集合
        world.addObject(cube);

        // 該Camera代表了Camera/viewer在當(dāng)前場景的位置和方向,它也包含了當(dāng)前視野的有關(guān)信息
        // 你應(yīng)該記住Camera的旋轉(zhuǎn)矩陣實(shí)際上是應(yīng)用在World中的對象的一個旋轉(zhuǎn)矩陣。
        // 這一點(diǎn)很重要,當(dāng)選擇了Camera的旋轉(zhuǎn)角度,一個Camera(虛擬)圍繞w旋轉(zhuǎn)和通過圍繞World圍繞w旋轉(zhuǎn)、
        // 將起到相同的效果,因此,考慮到旋轉(zhuǎn)角度,World圍繞camera時,camera的視角是靜態(tài)的。假如你不喜歡
        // 這種習(xí)慣,你可以使用rotateCamera()方法
        Camera cam = world.getCamera();

        // 以50有速度向后移動Camera(相對于目前的方向)
        cam.moveCamera(Camera.CAMERA_MOVEOUT, 50);

        // cub.getTransformedCenter()返回對象的中心
        // cam.lookAt(SimpleVector lookAt))
        // 旋轉(zhuǎn)這樣camera以至于它看起來是在給定的world-space 的位置
        cam.lookAt(cube.getTransformedCenter());

        // SimpleVector是一個代表三維矢量的基礎(chǔ)類,幾乎每一個矢量都
        // 是用SimpleVector或者至少是一個SimpleVector變體構(gòu)成的(有時由于
        // 某些原因比如性能可能會用(float x,float y,float z)之類)。
        SimpleVector sv = new SimpleVector();

        // 將當(dāng)前SimpleVector的x,y,z值設(shè)為給定的SimpleVector(cube.getTransformedCenter())的值
        sv.set(cube.getTransformedCenter());

        // Y方向上減去100
        sv.y -= 100;

        // Z方向上減去100
        sv.z -= 100;

        // 設(shè)置光源位置
        sun.setPosition(sv);

        // 強(qiáng)制GC和finalization工作來試圖去釋放一些內(nèi)存,同時將當(dāng)時的內(nèi)存寫入日志,
        // 這樣可以避免動畫不連貫的情況,然而,它僅僅是減少這種情況發(fā)生的機(jī)率
        MemoryHelper.compact();

        // 如果master為空,使用日志記錄且設(shè)master為HelloWorld本身
        if (master == null) {
        Logger.log(Saving master Activity!);
        master = HelloWorld.this;
        }
        }
        }

        // 需實(shí)現(xiàn)的onSurfaceCreated(GL10 gl, EGLConfig config)
        public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        }

        // 繪制到當(dāng)前屏幕哦:-D
        public void onDrawFrame(GL10 gl) {

        try {
        // 如果stop為true
        if (!stop) {
        // 如果touchTurn不為0,向Y軸旋轉(zhuǎn)touchTure角度
        if (touchTurn != 0) {
        // 旋轉(zhuǎn)物體的旋轉(zhuǎn)繞Y由給定矩陣W軸角(弧度順時針方向?yàn)檎担?應(yīng)用到對象下一次渲染時。
        cube.rotateY(touchTurn);
        // 將touchTurn置0
        touchTurn = 0;
        }

        if (touchTurnUp != 0) {
        // 旋轉(zhuǎn)物體的旋轉(zhuǎn)圍繞x由給定角度寬(弧度,逆時針為正值)軸矩陣,應(yīng)用到對象下一次渲染時。
        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;
        }
        }
        // 當(dāng)出現(xiàn)異常,打印異常信息
        } catch (Exception e) {
        Logger.log(e, Logger.MESSAGE);
        }
        }
        }
        }



        評論


        相關(guān)推薦

        技術(shù)專區(qū)

        關(guān)閉