Search code examples
c++opengl-esandroid-ndkegl

How do I create an OpenGL ES 2 context in a native activity?


For the life of me, I cannot find any good pure Android NDK examples for OpenGL ES 2. The one included native-activity sample project builds an ES 1 context. Are there any sample programs demonstrating the creation of an ES 2 context in pure C++?


Solution

  • Creating an OpenGL ES 2 context should be about the same than creating an OpenGL ES 1. Based on the "native-activity" sample from the NDK, you just need to add this to the attribute list passed to eglChooseConfig:

    const EGLint attribs[] =
    {
        EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
        ...
        EGL_NONE
    };
    

    This should ensure your config is ES2-compatible.

    Then pass this attribute list to eglCreateContext:

    EGLint AttribList[] = 
    {
        EGL_CONTEXT_CLIENT_VERSION, 2,
        EGL_NONE
    };
    

    with a call like this:

    context = eglCreateContext(display, config, NULL, AttribList);