• android学习13——android egl hello world


    通常情况下我们使用GLSurfaceview来实现opengl渲染.GLSurfaceview实现上是对SurfaceView和EGL的封装.为了从本质上理解渲染流程,使用EGL和SurfaceView来写一个Demo.
    首先要封装一个EGLHelper用来操作EGL.下面代码的详细解释请参考参考资料3. EGLHelper.java代码如下:

    public class EglHelper {
        EGL10 mEgl;
        EGLDisplay mEglDisplay;
        EGLSurface mEglSurface;
        EGLConfig mEglConfig;
        EGLContext mEglContext;
    
        public EglHelper(){}
    
        public void start(int[] configSpec)
        {
            mEgl = (EGL10) EGLContext.getEGL();
            mEglDisplay = mEgl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
            int[] version = new int[2];
    
            mEgl.eglInitialize(mEglDisplay, version);
            EGLConfig[] configs = new EGLConfig[1];
            int[] num_config = new int[1];
    
            mEgl.eglChooseConfig(mEglDisplay, configSpec, configs, 1,
                    num_config);
            mEglConfig = configs[0];
            mEglContext = mEgl.eglCreateContext(mEglDisplay, mEglConfig,
                    EGL10.EGL_NO_CONTEXT, null);
            mEglSurface = null;
        }
    
        public GL createSurface(SurfaceHolder holder) {
            if (mEglSurface != null) {
                mEgl.eglMakeCurrent(mEglDisplay, EGL10.EGL_NO_SURFACE,
                        EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_CONTEXT);
                mEgl.eglDestroySurface(mEglDisplay, mEglSurface);
            }
            mEglSurface = mEgl.eglCreateWindowSurface(mEglDisplay,
                    mEglConfig, holder, null);
            mEgl.eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
                    mEglContext);
            GL gl = mEglContext.getGL();
            return gl;
        }
    
        public boolean swap() {
            mEgl.eglSwapBuffers(mEglDisplay, mEglSurface);
            return mEgl.eglGetError() != EGL11.EGL_CONTEXT_LOST;
        }
    
        public void finish() {
            if (mEglSurface != null) {
                mEgl.eglMakeCurrent(mEglDisplay, EGL10.EGL_NO_SURFACE,
                        EGL10.EGL_NO_SURFACE,
                        EGL10.EGL_NO_CONTEXT);
                mEgl.eglDestroySurface(mEglDisplay, mEglSurface);
                mEglSurface = null;
            }
            if (mEglContext != null) {
                mEgl.eglDestroyContext(mEglDisplay, mEglContext);
                mEglContext = null;
            }
            if (mEglDisplay != null) {
                mEgl.eglTerminate(mEglDisplay);
                mEglDisplay = null;
            }
        }
    }
    

    需要写一个类继承SurfaceView.要在SurfaceView的surfaceCreated和surfaceChanged执行之后再进行opengl的相关操作.代码如下:

    public class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback {
        public SurfaceHolder mHolder;
        private int w, h;
        private Context context;
        public MySurfaceView(Context context) {
            super(context);
            this.context = context;
            init();
        }
    
        public MySurfaceView(Context context, AttributeSet attrs) {
            super(context, attrs);
            init();
        }
    
        private void init() {
            mHolder = getHolder();
            mHolder.addCallback(this);
        }
    
        @Override
        public void onWindowFocusChanged(boolean hasFocus) {
            super.onWindowFocusChanged(hasFocus);
            EglHelper eglHelper = new EglHelper();
            int[] configSpec = {
                    EGL10.EGL_DEPTH_SIZE, 0,
                    EGL10.EGL_NONE
            };
            eglHelper.start(configSpec);
            GL10 gl = (GL10) eglHelper.createSurface(mHolder);
            gl.glClearColor(1f, 0f, 0f, 0f);
            gl.glViewport(0, 0, w, h);
            gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
            eglHelper.swap();
        }
    
        public void surfaceCreated(SurfaceHolder holder) {
    
        }
    
        public void surfaceDestroyed(SurfaceHolder holder) {
        }
    
        public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
            this.w = w;
            this.h = h;
        }
    }
    

    整个程序源代码: https://github.com/zhouyang209117/AndroidTutorial/tree/master/OpenGL/EGLHelloWorld
    参考资料

    1. Sayed Y, Hashimi, Satya Komatineni. Pro Android. Apress. pp325-362. 2009.
    2. (美)Kevin Brothaler著, 刘力平,苏统华译. OpenGL ES应用开发实践指南 android卷. 机械工业出版社. pp1-26. 2014.
    3. Aaftab Munshi, Dan Ginsburg, Dave Shreiner. OpenGL ES 2.0 Programming Guide. addison wesley. pp35-56
  • 相关阅读:
    值得学习的东西
    【单调队列】转载
    dom基础3 — 简易版template.js
    js基础6 — 数组对象
    js基础6 — 字符串基本操作
    js基础5 — 将十六进制颜色转为rgba()
    js基础4 — 数组操作
    placeholer属性修改
    clipboard.js实现复制到剪切板
    JS 获取指定日期在当年的第几周
  • 原文地址:https://www.cnblogs.com/zhouyang209117/p/5415486.html
Copyright © 2020-2023  润新知