Android Dev Intro - Opengl ES and EGL




Opengl ES define platform independent GL draw graphics instructions
EGL define display, context and surface.
Display (EGLDisplay) is the abstract of display device
Surface (EGLSurface) is the abstract of memory area used to store graphics
Context (EGLContext) is used to save state information of Opengl ES


General Steps.
1. acquire EGLDisplay object
   EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
   
2. init EGLDisplay object
   eglInitialize(display, 0, 0);
   
3. acquire EGLConfig object
   eglChooseConfig(display, attribs, nullptr,0, &numConfigs);
   auto supportedConfigs = new EGLConfig[numConfigs];
   assert(supportedConfigs);
   eglChooseConfig(display, attribs, supportedConfigs, numConfigs, &numConfigs);
   
   auto i = 0;
   for (; i < numConfigs; i++) {
       auto& cfg = supportedConfigs[i];
       EGLint r, g, b, d;
       if (eglGetConfigAttrib(display, cfg, EGL_RED_SIZE, &r)   &&
           eglGetConfigAttrib(display, cfg, EGL_GREEN_SIZE, &g) &&
           eglGetConfigAttrib(display, cfg, EGL_BLUE_SIZE, &b)  &&
           eglGetConfigAttrib(display, cfg, EGL_DEPTH_SIZE, &d) &&
           r == 8 && g == 8 && b == 8 && d == 0 ) {
           config = supportedConfigs[i];
           break;
       }
   }
   if (i == numConfigs) {
       config = supportedConfigs[0];
   }
   
   eglGetConfigAttrib(display, config, EGL_NATIVE_VISUAL_ID, &format);
   
4. Create EGLContext object
   context = eglCreateContext(display, config, NULL, NULL);
   
5. Create EGLSurface object
   surface = eglCreateWindowSurface(display, config, engine->app->window, NULL);


6. bind EGLSurface with EGLContext
   eglMakeCurrent(display, surface, surface, context);


7. draw graphics
    glClearColor(((float)engine->state.x)/engine->width, engine->state.angle,
                 ((float)engine->state.y)/engine->height, 1);
    glClear(GL_COLOR_BUFFER_BIT);


    eglSwapBuffers(engine->display, engine->surface);

8. unbind EGLSurface with EGLContext
   eglMakeCurrent(engine->display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
   
9. releas EGLContext object
   eglDestroyContext(engine->display, engine->context);
   
10. release EGLSurface object
   eglDestroySurface(engine->display, engine->surface);
   
11. Destory EGLDisplay object
   eglTerminate(engine->display);
   engine->display = EGL_NO_DISPLAY;
   engine->context = EGL_NO_CONTEXT;
   engine->surface = EGL_NO_SURFACE;

阅读更多
个人分类: OS/Android
上一篇Android Dev Intro - Android NDK with Android Studio
下一篇Android Dev Intro - ANativeWindow
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭