zoukankan      html  css  js  c++  java
  • 【转载】Android 第一个OpenGL ES程序

    原帖地址:http://www.cnblogs.com/mengdd/archive/2013/05/02/3055649.html

    Android 第一个OpenGL ES程序

      在你的Android应用中用OpenGL ES绘制图形,首先需要有一个容器,最直接的方法是实现GLSurfaceView 和  GLSurfaceView.Renderer

      前者是一个放置图形的View容器,后者用来控制在这个View中如何进行绘制。

      GLSurfaceView只是一种选择,比较适合于全屏绘制图形或者近似全屏绘制,其他可以选择的还有 TextureViewSurfaceView

      

      本文展示一个最基本的Android OpenGL ES绘制Demo。

     

    1.在Manifest中添加声明

      为了使用OpenGL ES 2.0 API,需要添加如下声明:

    <uses-feature android:glEsVersion="0x00020000" android:required="true" />

      OpenGL ES 2.0 requires Android 2.2 (API Level 8) or higher,所以需要确认系统版本。

    2.创建Activity

      在Activity的布局中,需要加入GLSurfaceView来放置绘制的图形。

      一个最简单的版本如下:

    复制代码
    public class OpenGLES20 extends Activity {
    
        private GLSurfaceView mGLView;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            // Create a GLSurfaceView instance and set it
            // as the ContentView for this Activity.
            mGLView = new MyGLSurfaceView(this);
            setContentView(mGLView);
        }
    }
    复制代码

     

    3.创建GLSurfaceView

      GLSurfaceView是一个特殊的组件,你可以在其中绘制OpenGL ES图形。

      你需要扩展这个类,在它的构造方法中设置渲染器:

    复制代码
    class MyGLSurfaceView extends GLSurfaceView {
    
        public MyGLSurfaceView(Context context){
            super(context);
    
            // Set the Renderer for drawing on the GLSurfaceView
            setRenderer(new MyRenderer());
        }
    }
    复制代码

      如果使用OpenGL ES 2.0,还需要加一句声明:

    // Create an OpenGL ES 2.0 context
    setEGLContextClientVersion(2);

      还有一个可选的设置是,把渲染模式改为 GLSurfaceView.RENDERMODE_WHEN_DIRTY ,这样仅在你的数据有变化时重新进行渲染。

    // Render the view only when there is a change in the drawing data
    setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);

      除非你调用requestRender(),这个设置会阻止帧被重画,有些情况下这样效率更高。

    4.建立一个Renderer类

      Renderer类(渲染器类),即 GLSurfaceView.Renderer的实现类,它控制了与它相关联的 GLSurfaceView 上绘制什么。

      其中有三个主要的回调方法:

    • onSurfaceCreated() - Called once to set up the view's OpenGL ES environment.
    • onDrawFrame() - Called for each redraw of the view.
    • onSurfaceChanged() - Called if the geometry of the view changes, for example when the device's screen orientation changes.

      

      一个简单的实现例子:

    复制代码
    public class MyGL20Renderer implements GLSurfaceView.Renderer {
    
        public void onSurfaceCreated(GL10 unused, EGLConfig config) {
            // Set the background frame color
            GLES20.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
        }
    
        public void onDrawFrame(GL10 unused) {
            // Redraw background color
            GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
        }
    
        public void onSurfaceChanged(GL10 unused, int width, int height) {
            GLES20.glViewport(0, 0, width, height);
        }
    }
    复制代码

     

    程序例子

      一个简单的程序例子,并没有绘制什么,只是设置了背景色,为了展示方便,GLSurfaceView类和渲染器类都作为Acitivity的内部类写出。

      首先在Manifest中加上声明:

    Manifest
    Activity

    参考资料

      Training: Building an OpenGL ES Environment

      http://developer.android.com/training/graphics/opengl/environment.html

      OpenGL ES Developer Guide:

      http://developer.android.com/guide/topics/graphics/opengl.html

  • 相关阅读:
    UIImageView动画 UISlider控制速度
    python学习笔记——装饰器
    python学习笔记——继承
    python学习笔记——异步IO
    思路清楚的最大堆实现
    推荐几个IDEA必备的好用插件
    记工作中遇到的Date类相关的坑
    navicat数据库表数据复制到excel产生的列错位问题
    submit提交时进行页面验证
    SublimeText3常用快捷键和优秀插件(转载)
  • 原文地址:https://www.cnblogs.com/LiesSu/p/3831137.html
Copyright © 2011-2022 走看看