<meter id="pryje"><nav id="pryje"><delect id="pryje"></delect></nav></meter>
          <label id="pryje"></label>

          新聞中心

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

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

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

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

          本文引用地址:http://www.ex-cimer.com/article/149549.htm

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

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


          第一個(gè)示例:同樣的立方體,不同的實(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;

          /**
          * 一個(gè)簡(jiǎn)單的例子。比起展示如何寫(xiě)一個(gè)正確的android應(yīng)用它更著重于展示如何使用JPCT-AE這個(gè)框架。
          * 它包含了Activity類去處理pause和resume等方法
          *
          * @author EgonOlsen
          *
          */
          public class HelloWorld extends Activity {

          // HelloWorld對(duì)象用來(lái)處理Activity的onPause和onResume方法
          private static HelloWorld master = null;

          // GLSurfaceView對(duì)象
          private GLSurfaceView mGLView;

          // 類MyRenderer對(duì)象
          private MyRenderer renderer = null;

          // 當(dāng)JPCT渲染背景時(shí)FrameBuffer類提供了一個(gè)緩沖,它的結(jié)果本質(zhì)上是一個(gè)能顯示或者修改甚至能進(jìn)行更多后處理的圖片。
          private FrameBuffer fb = null;

          // World類是JPCT時(shí)最重要的一個(gè)類,它好像膠水一樣把事物粘起來(lái)。它包含的對(duì)象和光線定義了JPCT的場(chǎng)景
          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類是一個(gè)三維對(duì)象,千萬(wàn)不要傻呼呼的認(rèn)為它與java.lang.Object類似。
          // 一個(gè)Object3D對(duì)象作為一個(gè)實(shí)例被添加到在渲染的World對(duì)象中。Object3D在World
          // 中一次添加一個(gè)實(shí)例 ,他們可能被聯(lián)系起作為孩子/父母來(lái)在他們中建立一個(gè)制度.
          // 人體模型當(dāng)然也能應(yīng)用在以上的規(guī)則中。他們常常不加到一個(gè)World實(shí)例中,而是
          // 綁定到其它對(duì)象中(人體模型或非人體模型)。有些方法 在這個(gè)類中需要一個(gè)實(shí)例
          // 添加到一個(gè)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中一個(gè)普通的用于打印和存儲(chǔ)消息,錯(cuò)誤和警告的日志類。
          // 每一個(gè)JPCT生成的消息將被加入到這個(gè)類的隊(duì)列中
          Logger.log(onCreate);
          // 如果本類對(duì)象不為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)之前
          // 如果沒(méi)有setEGLConfigChooser方法被調(diào)用,則默認(rèn)情況下,視圖將選擇一個(gè)與當(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的渲染器,同時(shí)啟動(dòng)線程調(diào)用渲染,以至啟動(dòng)渲染
          mGLView.setRenderer(renderer);
          // 設(shè)置一個(gè)明確的視圖
          setContentView(mGLView);
          }

          // 重寫(xiě)onPause()
          @Override
          protected void onPause() {
          super.onPause();
          mGLView.onPause();
          }

          // 重寫(xiě)onResume()
          @Override
          protected void onResume() {
          super.onResume();
          mGLView.onResume();
          }

          // 重寫(xiě)onStop()
          @Override
          protected void onStop() {
          super.onStop();
          }


          private void copy(Object src) {
          try {
          // 打印日志
          Logger.log(Copying data from master Activity!);
          // 返回一個(gè)數(shù)組,其中包含目前這個(gè)類的的所有字段的Filed對(duì)象
          Field[] fs = src.getClass().getDeclaredFields();
          // 遍歷fs數(shù)組
          for (Field f : fs) {
          // 嘗試設(shè)置無(wú)障礙標(biāo)志的值。標(biāo)志設(shè)置為false將使訪問(wèn)檢查,設(shè)置為true,將其禁用。
          f.setAccessible(true);
          // 將取到的值全部裝入當(dāng)前類中
          f.set(this, f.get(src));
          }
          } catch (Exception e) {
          // 拋出運(yùn)行時(shí)異常
          throw new RuntimeException(e);
          }
          }



          public boolean onTouchEvent(MotionEvent me) {

          // 按鍵開(kāi)始
          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)屏幕改變時(shí)
          public void onSurfaceChanged(GL10 gl, int w, int h) {
          // 如果FrameBuffer不為NULL,釋放fb所占資源
          if (fb != null) {
          fb.dispose();
          }
          // 創(chuàng)建一個(gè)寬度為w,高為h的FrameBuffer
          fb = new FrameBuffer(gl, w, h);
          Logger.log(master + );
          // 如果master為空
          if (master == null) {

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

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

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

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

          // 創(chuàng)建一個(gè)紋理
          // 構(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()取得一個(gè)Texturemanager對(duì)象
          // addTexture(texture,texture)添加一個(gè)紋理
          TextureManager.getInstance().addTexture(texture, texture);

          // Object3D對(duì)象開(kāi)始了:-)

          // Primitives提供了一些基本的三維物體,假如你為了測(cè)試而生成一些對(duì)象或?yàn)?br /> // 其它目的使用這些類將很明智,因?yàn)樗纯焖儆趾?jiǎn)單,不需要載入和編輯。
          // 調(diào)用public static Object3D getCube(float scale) scale:角度
          // 返回一個(gè)立方體
          cube = Primitives.getCube(10);

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

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

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

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

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

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

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

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

          // SimpleVector是一個(gè)代表三維矢量的基礎(chǔ)類,幾乎每一個(gè)矢量都
          // 是用SimpleVector或者至少是一個(gè)SimpleVector變體構(gòu)成的(有時(shí)由于
          // 某些原因比如性能可能會(huì)用(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工作來(lái)試圖去釋放一些內(nèi)存,同時(shí)將當(dāng)時(shí)的內(nèi)存寫(xiě)入日志,
          // 這樣可以避免動(dòng)畫(huà)不連貫的情況,然而,它僅僅是減少這種情況發(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軸角(弧度順時(shí)針?lè)较驗(yàn)檎担?應(yīng)用到對(duì)象下一次渲染時(shí)。
          cube.rotateY(touchTurn);
          // 將touchTurn置0
          touchTurn = 0;
          }

          if (touchTurnUp != 0) {
          // 旋轉(zhuǎn)物體的旋轉(zhuǎn)圍繞x由給定角度寬(弧度,逆時(shí)針為正值)軸矩陣,應(yīng)用到對(duì)象下一次渲染時(shí)。
          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);
          }
          }
          }
          }



          評(píng)論


          相關(guān)推薦

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

          關(guān)閉
          看屁屁www成人影院,亚洲人妻成人图片,亚洲精品成人午夜在线,日韩在线 欧美成人 (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();