Android NDK OpenGLES不渲染三角形

时间:2018-03-10 18:24:33

标签: c++ opengl-es android-ndk

我一直在努力使用OpenGLES 1.0,2.0或3.0技术正确渲染任何几何体。我的测试设备是三星Galaxy S7 Edge(运行Android 7.0)。我已经为OpenGLES和EGL实现了许多指南,例如:

https://www.khronos.org/registry/EGL/sdk/docs/man/html/

https://www.khronos.org/assets/uploads/books/openglr_es_20_programming_guide_sample.pdf

http://www.ikerhurtado.com/egl-use-android-native-opengles-applications

该代码最初是根据“原生活动”示例改编的:

https://github.com/googlesamples/android-ndk/tree/master/native-activity

Android Manifest :(主要来自原生活动示例)

...
<!-- Tell the system this app requires OpenGL ES 3.0. -->
<uses-feature android:glEsVersion="0x00030000" android:required="true" />
...

CMAKE :(主要来自原生活动示例)

...

add_library(
    native-activity
    SHARED
    main.cpp)

target_include_directories(
    native-activity
    PRIVATE
    ${ANDROID_NDK}/sources/android/native_app_glue)

# add lib dependencies
target_link_libraries(
    native-activity
    android
    native_app_glue
    EGL
    GLESv3
    log)

...

main.cpp :(主要来自原生活动示例,因相关而裁剪)

...

#include <EGL/egl.h>
#include <EGL/eglext.h>
#include <GLES3/gl3.h>
#include <GLES3/gl3ext.h>
#include <android_native_app_glue.h>

...

/*
 *Test GLES 1.0, should draw a white triangle on red background
 */
class TestGLES10 {
public:
    GLuint program;

    void create() {

        char vssource[] =
                "attribute vec4 vertexPosition; \n"
                "void main(){                       \n"
                "   gl_Position = vertexPosition;   \n"
                "}                                  \n";

        char fssource[] =
                "precision mediump float; \n"
                "void main(){ \n"
                "   gl_FragColor = vec4(1.0); "
                "}                                  \n";

        auto compile = [](const char *source, GLenum type){
            GLuint shader = glCreateShader(type);
            glShaderSource(shader, 1, &source, nullptr);
            glCompileShader(shader);
            return shader;
        };

        GLuint vs = compile(string(vssource).c_str(), GL_VERTEX_SHADER);
        GLuint fs = compile(string(fssource).c_str(), GL_FRAGMENT_SHADER);

        program = glCreateProgram();
        glAttachShader(program, vs);
        glAttachShader(program, fs);

        glBindAttribLocation(program, 0, "vertexPosition");

        glLinkProgram(program);

        glDisable(GL_DEPTH_TEST);
    }

    void update() {

        glClearColor(1,0,0,1);
        glClear(GL_COLOR_BUFFER_BIT);

        glUseProgram(program);

        float positions[]{
                0.0f,  0.1f,
                -0.1f, -0.1f,
                0.1f, -0.1f
        };

        glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, positions);
        glEnableVertexAttribArray(0);

        glDrawArrays(GL_TRIANGLES, 0, 3);
    }
};

/*
 *Test GLES 3.0, should draw a white triangle on blue background
 */
class TestGLES30 {
public:
    GLuint vbo, vao, program;

    void create() {

        char vssource[] =
            "#version 300 es\n"
            "layout(location=0) in vec4 vertexPosition; \n"
            "void main(){                       \n"
            "   gl_Position = vertexPosition;   \n"
            "}                                  \n";

        char fssource[] =
            "#version 300 es\n"
            "precision mediump float; \n"
            "out vec4 fragment; \n"
            "void main(){ \n"
            "   fragment = vec4(1.0); "
            "}                                  \n";

        auto compile = [](const char *source, GLenum type){
            GLuint shader = glCreateShader(type);
            glShaderSource(shader, 1, &source, nullptr);
            glCompileShader(shader);
            return shader;
        };

        GLuint vs = compile(string(vssource).c_str(), GL_VERTEX_SHADER);
        GLuint fs = compile(string(fssource).c_str(), GL_FRAGMENT_SHADER);

        program = glCreateProgram();
        glAttachShader(program, vs);
        glAttachShader(program, fs);
        glLinkProgram(program);

        float positions[]{
             0.0f,  0.5f,
            -0.5f, -0.5f,
             0.5f, -0.5f
        };

        glGenBuffers(1, &vbo);
        glBindBuffer(GL_ARRAY_BUFFER, vbo);
        glBufferData(GL_ARRAY_BUFFER, 2*4, positions, GL_STATIC_DRAW);

        glGenVertexArrays(1, &vao);
        glBindVertexArray(vao);

        glEnableVertexAttribArray(0);
        glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2*4, 0);

        glBindBuffer(GL_ARRAY_BUFFER, 0);
        glBindVertexArray(0);
    }

    void update() {

        glClearColor(0,0,1,1);
        glClear(GL_COLOR_BUFFER_BIT);

        glUseProgram(program);

        glBindVertexArray(vao);
        glDrawArrays(GL_TRIANGLES, 0, 3);
        glBindVertexArray(0);
    }
};

...

void android_main(android_app* appInterface) {

    appInterface->onAppCmd = commandRelay;
    appInterface->onInputEvent = inputRelay;

    while(appInterface->window == nullptr){
        pollEvents(appInterface);
    }

    EGLDisplay display;
    EGLSurface surface;
    EGLContext context;
    EGLint majorVersion, minorVersion;
    EGLConfig config;
    EGLint width, height;
    EGLint nativeVisualID;
    EGLint configCount;

    display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
    assert(display != EGL_NO_DISPLAY);

    eglInitialize(display, &majorVersion, &minorVersion);

    //https://www.khronos.org/registry/EGL/sdk/docs/man/html/eglChooseConfig.xhtml
    const EGLint displayAttrib[] = {
        EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,//egl 1.3 req
        EGL_NATIVE_RENDERABLE, EGL_TRUE,
        EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
        EGL_BLUE_SIZE, 8,
        EGL_GREEN_SIZE, 8,
        EGL_RED_SIZE, 8,
        EGL_NONE
    };

    //retreive configs
    eglChooseConfig(display, displayAttrib, 0, 0, &configCount);
    std::unique_ptr<EGLConfig[]> supported(new EGLConfig[configCount]);
    eglChooseConfig(display, displayAttrib, supported.get(), configCount, &configCount);

    EGLint i = 0, v;
    for (; i < configCount; i++) {

        auto get = [&](EGLint attrib){
            eglGetConfigAttrib(display, supported[i], attrib, &v);
            return v;
        };
        if (Math2::equal(8,
            get(EGL_RED_SIZE),
            get(EGL_GREEN_SIZE),
            get(EGL_BLUE_SIZE),
            get(EGL_DEPTH_SIZE))) {//expands to 8 == get(EGL_RED_SIZE) == ...

            config = supported[i];
            break;
        }
    }
    if (i == configCount) {//default to first
        config = supported[0];
    }

    const EGLint surfaceAttrib[] = {
        EGL_RENDER_BUFFER, EGL_BACK_BUFFER,// render to the back buffer, egl 1.2 req
        EGL_NONE
    };

    eglGetConfigAttrib(display, config, EGL_NATIVE_VISUAL_ID, &nativeVisualID);
    surface = eglCreateWindowSurface(display, config, appInterface->window, surfaceAttrib);

    const EGLint contextAttrib[] = {
        EGL_CONTEXT_CLIENT_VERSION, 2,// request a context using Open GL ES 2.0
        EGL_NONE
    };

    context = eglCreateContext(display, config, EGL_NO_CONTEXT, contextAttrib);
    assert(context != EGL_NO_CONTEXT);

    //sets initial viewport and scissor dimensions to draw surface size
    eglMakeCurrent(display, surface, surface, context);

    eglQuerySurface(display, surface, EGL_WIDTH, &width);
    eglQuerySurface(display, surface, EGL_HEIGHT, &height);

    //this call was mentioned, but missing from the example (removing has same effect)
    ANativeWindow_setBuffersGeometry(appInterface->window, width, height, nativeVisualID);


    TestGLES10 test;
    test.create();

    while (true){
        test.update();
        eglSwapBuffers(display, surface);
    }
}

结果:

记录信息:

Vendor : Qualcomm
Renderer : Adreno (TM) 530
Version : OpenGL ES 3.2 V@145.0 (GIT@I86b60582e4)
EGL version : 1.4
Window dimensions : 1080, 1920

此外,该设备的分辨率缩放设置为1080x1920。

测试1.0:https://i.imgur.com/dmofGYw.png 测试3.0:https://i.imgur.com/uoghOZc.png

根据: https://www.khronos.org/registry/EGL/sdk/docs/man/html/eglMakeCurrent.xhtml glViewPort应该初始化为表面尺寸(1080x1920),所以我对白色矩形的发生方式感到茫然。

两个测试的着色器代码在没有Adreno错误的情况下编译。

为什么不正确渲染几何体?

根据: https://www.khronos.org/registry/EGL/sdk/docs/man/html/eglCreateContext.xhtml 没有办法专门请求3.0或更高版本的上下文,当我只能请求2.0时,为什么创建的上下文有版本3.2?

1 个答案:

答案 0 :(得分:0)

  

为什么不正确渲染几何体?

你的位置数组是两个组合的三个顶点(x,y),但是你告诉GL他们每个都有三个组件:

glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, positions);
  

当我只能请求2.0时,为什么创建的上下文有版本3.2?

版本3.x完全向前兼容为2.0版编写的代码,因此驱动程序只需将上下文转换为它们支持的最新版本即可。