`
我爱3G
  • 浏览: 29533 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
社区版块
存档分类
最新评论

Android OpenGL

阅读更多
1 什么是 OpenGL?
  OpenGL 是个专业的3D程序接口,是一个功能强大,调用方便的底层3D图形库。OpenGL  的前身是 SGI 公司为其图形工作站开的 IRIS GL。IRIS GL 是一个工业标准的3D图形软件接口,功能虽然强大但是移植性不好,于是 SGI 公司便在 IRIS GL 的基础上开发 OpenGL  。

2 OpenGL 的发展历程
  1992年7月 发布了 OpenGL 1.0 版本,并与微软共同推出 Windows NT 版本的 OpenGL 。
  1995年 OpenGL 1.1 版本面市,加入了新功能,并引入了纹理特性等等。
  一直到 2009年8月Khronos小组发布了OpenGL 3.2,这是一年以来OpenGL进行的第三次重要升级。

3 OpenGL  ES 简介
      Android 3D 引擎采用的是OpenGL ES。OpenGL ES是一套为手持和嵌入式系统设计的3D引擎API,由Khronos公司维护。在PC领域,一直有两种标准的3D API进行竞争,OpenGL 和 DirectX。一般主流的游戏和显卡都支持这两种渲染方式,DirectX在Windows平台上有很大的优势,但是 OpenGL 具有更好的跨平台性。
由于嵌入式系统和PC相比,一般说来,CPU、内存等都比PC差很多,而且对能耗有着特殊的要求,许多嵌入式设备并没有浮点运算协处理器,针对嵌入式系统的以上特点,Khronos对标准的 OpenGL 系统进行了维护和改动,以期望满足嵌入式设备对3D绘图的要求。


4  Android OpenGL ES 简介
      Android系统使用 OpenGL 的标准接口来支持3D图形功能,android 3D 图形系统也分为 java 框架和本地代码两部分。本地代码主要实现的 OpenGL 接口的库,在 Java 框架层,javax.microedition.khronos.opengles 是 java 标准的 OpenGL 包,android.opengl 包提供了 OpenGL 系统和 Android GUI 系统之间的联系。

5 Android 支持 OpenGL 列表

    * GL
    * GL 10
    * GL 10 EXT
    * GL 11
    * GL 11 EXT
    * GL 11 ExtensionPack

6 GLSurfaceView
    Android中提供了GLSurfaceView用于显示OpenGL渲染,GLSurfaceView包含了以下功能:

    * 在 OpenGL ES 和 View 系统之间建立联系;
    * 使得 OpenGL ES 可以工作在 Activity 生命周期中;
    * 可选择合适的 frame buffer 像素格式;
    * 创建并管理一个单独的渲染线程,可以实现平滑的动画;
    * 提供debugging 工具和 API。

    public void setRenderer(GLSurfaceView.Renderer renderer) 提供了渲染方法

7  GLSurfaceView.Renderer

    普通的渲染接口,当你创建自己的render时需要实现这个接口同时重写一些方法。然后通过 GLSurfaceView的setRenderer(GLSurfaceView.Renderer)将自己的render渲染到 GLSurfaceView中,实现GLSurfaceView.Renderer时,你需要实现以下三个方法:

    * public void onSurfaceCreated(GL10 gl, EGLConfig config)

             该方法在渲染开始前调用,OpenGL ES 的绘制上下文被重建 时也会被调用。当 activity 暂停时绘制上下文会丢失,当 activity 继续 时,绘制上下文会被重建。通俗的来说在渲染的过程中一些不经常变换的,可以在这个方法里面设置,比方说:创建长期存在的 OpenGL 资源(如 texture)往往也在这里进行。

    * public void onDrawFrame(GL10 gl)

             通常这个方法中就是实际绘画的地方,每帧都通过该方法进行绘制。绘制时通常先调用 glClear  函数来清空 framebuffer,然后在调用 OpenGL ES 的起它的接口进行绘制。

    * public void onSurfaceChanged(GL10 gl, int width, int height)

              surface 的尺寸发生改变时该方法被调用(横竖屏切换时)。往往在 这里设置 viewport。若你的 camera 是固定的,也可以在这里设置 camera。

8 应用示例

    了解完GLSurfaceView和GLSurfaceView.Render之后,接下来我们开始实际操刀:

   1. 创建Activity:

view plaincopy to clipboardprint?

   1. package se.jayway.opengl.tutorial; 
   2.  
   3. import android.app.Activity; 
   4. import android.opengl.GLSurfaceView; 
   5. import android.os.Bundle; 
   6.  
   7. public class TutorialPartI extends Activity { 
   8.     /** Called when the activity is first created. */ 
   9.     @Override 
  10.     public void onCreate(Bundle savedInstanceState) { 
  11.         super.onCreate(savedInstanceState); 
  12.         GLSurfaceView view = new GLSurfaceView(this); 
  13.         view.setRenderer(new OpenGLRenderer()); 
  14.         setContentView(view); 
  15.     } 
  16. } 

package se.jayway.opengl.tutorial;

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

public class TutorialPartI extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
GLSurfaceView view = new GLSurfaceView(this);
   view.setRenderer(new OpenGLRenderer());
   setContentView(view);
    }
}

   1. 创建Render:package se.jayway.opengl.tutorial;



view plaincopy to clipboardprint?
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150

   1. package se.jayway.opengl.tutorial; 
   2. import javax.microedition.khronos.egl.EGLConfig; 
   3. import javax.microedition.khronos.opengles.GL10; 
   4. import android.opengl.GLU; 
   5. import android.opengl.GLSurfaceView.Renderer; 
   6. public class OpenGLRenderer implements Renderer { 
   7.     /*
   8.      * @see
   9.      * android.opengl.GLSurfaceView.Renderer#onSurfaceCreated(javax.
  10.          * microedition.khronos.opengles.GL10, javax.microedition.khronos.
  11.          * egl.EGLConfig)
  12.      */ 
  13.     public void onSurfaceCreated(GL10 gl, EGLConfig config) { 
  14.         // Set the background color to black ( rgba ). 
  15.         gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);  // OpenGL docs. 
  16.         // Enable Smooth Shading, default not really needed. 
  17.         gl.glShadeModel(GL10.GL_SMOOTH);// OpenGL docs. 
  18.         // Depth buffer setup. 
  19.         gl.glClearDepthf(1.0f);// OpenGL docs. 
  20.         // Enables depth testing. 
  21.         gl.glEnable(GL10.GL_DEPTH_TEST);// OpenGL docs. 
  22.         // The type of depth testing to do. 
  23.         gl.glDepthFunc(GL10.GL_LEQUAL);// OpenGL docs. 
  24.         // Really nice perspective calculations. 
  25.         gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, // OpenGL docs. 
  26.                           GL10.GL_NICEST); 
  27.     } 
  28.     /*
  29.      * @see
  30.      * android.opengl.GLSurfaceView.Renderer#onDrawFrame(javax.
  31.          * microedition.khronos.opengles.GL10)
  32.      */ 
  33.     public void onDrawFrame(GL10 gl) { 
  34.         // Clears the screen and depth buffer. 
  35.         gl.glClear(GL10.GL_COLOR_BUFFER_BIT | // OpenGL docs. 
  36.                            GL10.GL_DEPTH_BUFFER_BIT); 
  37.     } 
  38.     /*
  39.      * @see
  40.      * android.opengl.GLSurfaceView.Renderer#onSurfaceChanged(javax.
  41.          * microedition.khronos.opengles.GL10, int, int)
  42.      */ 
  43.     public void onSurfaceChanged(GL10 gl, int width, int height) { 
  44.         // Sets the current view port to the new size. 
  45.         gl.glViewport(0, 0, width, height);// OpenGL docs. 
  46.         // Select the projection matrix 
  47.         gl.glMatrixMode(GL10.GL_PROJECTION);// OpenGL docs. 
  48.         // Reset the projection matrix 
  49.         gl.glLoadIdentity();// OpenGL docs. 
  50.         // Calculate the aspect ratio of the window 
  51.         GLU.gluPerspective(gl, 45.0f, 
  52.                                    (float) width / (float) height, 
  53.                                    0.1f, 100.0f); 
  54.         // Select the modelview matrix 
  55.         gl.glMatrixMode(GL10.GL_MODELVIEW);// OpenGL docs. 
  56.         // Reset the modelview matrix 
  57.         gl.glLoadIdentity();// OpenGL docs. 
  58.     } 
  59. } 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics