简体   繁体   中英

Android NDK Native Activity OpenGL ES 1.1 triangle won't show up

I'm new to Android NDK and Native Activity, I liked to create a triangle in the middle of the screen but no matter how I tried I wouldn't show up!

Here is my initialize method:

void Engine::initialize() {
    LOGI("Engine::initialize fired!");

    const EGLint attribs[] = {
            EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
            EGL_BLUE_SIZE, 8,
            EGL_GREEN_SIZE, 8,
            EGL_RED_SIZE, 8,
            EGL_NONE
    };
    EGLint w, h, dummy, format;
    EGLint numConfigs;
    EGLConfig config;
    EGLSurface surface;
    EGLContext context;

    EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
    eglInitialize(display, 0, 0);
    eglChooseConfig(display, attribs, &config, 1, &numConfigs);
    eglGetConfigAttrib(display, config, EGL_NATIVE_VISUAL_ID, &format);
    ANativeWindow_setBuffersGeometry(this->app->window, 0, 0, format);

    surface = eglCreateWindowSurface(display, config, this->app->window, NULL);
    context = eglCreateContext(display, config, NULL, NULL);

    if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE) {
        LOGW("Unable to eglMakeCurrent");
        return;
    }

    eglQuerySurface(display, surface, EGL_WIDTH, &w);
    eglQuerySurface(display, surface, EGL_HEIGHT, &h);

    this->display = display;
    this->context = context;
    this->surface = surface;
    this->width = w;
    this->height = h;

    // Initialize GL state.
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
    glEnable(GL_CULL_FACE);
    glShadeModel(GL_SMOOTH);
    glDisable(GL_DEPTH_TEST);

    this->animating = true;
}

And here is my render method:

  void Engine::onRender() {
    glClearColor(0.7, 0.1, 0.5, 1);
    glClear(GL_COLOR_BUFFER_BIT);

    glViewport(0, 0, this->width, this->height);

    //glMatrixMode(GL_PROJECTION);
    //glLoadIdentity();
    //glFrustumf(-this->width / 2, this->width / 2, -this->height / 2, this->height / 2, 1, 3);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glTranslatef(0, 0, 0);

    GLfloat triangle[] = {
        0, 0, 0,
        0, 100, 0, 
        100, -100, 0
    };

    glPushMatrix();
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glTranslatef(0, 0, 0);
    glColor4f(1.0f, 0.3f, 0.0f, .5f);
    glEnableClientState(GL_VERTEX_ARRAY);
    glVertexPointer(3, GL_FLOAT, 0, triangle);
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 3);
    glDisableClientState(GL_VERTEX_ARRAY);
    glPopMatrix();

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glTranslatef(0, 0, -10);

    eglSwapBuffers(this->display, this->surface);
}

Anyone can help?

All I can see the pink/purple background but know any other pixel :| No errors in console.

You have probably OpenGL errors, but they are not logged automatically. You can use this function for that:

static void checkGlError(const char* op) {
    for (GLint error = glGetError(); error; error = glGetError()) {
        LOGI("after %s() glError (0x%x)\n", op, error);
    }
}

and call it after each OpenGL call. Then you will see exactly where it crashes.

A part from that, I would recommend you using OpenGL ES 2.0. I'm not sure right now if all the calls you are using work with ES 1.1 (maybe someone else can confirm). In addition, there is an NDK sample implementing exactly the same than you, but using ES 2.0 instead. You can find it here:

http://code.google.com/p/android-cmake/source/browse/samples/hello-gl2/jni/gl_code.cpp?r=787b14cf9ed13299cb4c729d9a67d06e300fd52e

It uses a simple shader to paint the triangle and renders it using a VBO.

I was having the same problem for many hours today, and finally found an answer. It is not an error on your code, but most likely on the testing device.

First: Are you working with an Android Virtual Device or with a physical mobile?

With the first case, you need to use an ADV min API-15, and add gpu-emulation set to yes. Or from the command line, you can use this line when you run your ADV:

emulator -avd <avd_name> -gpu on

If it is ok, you will find these lines in the logcat:

D/libEGL  (  595): loaded /system/lib/egl/libGLES_android.so
D/libEGL  (  595): loaded /system/lib/egl/libEGL_emulation.so
D/libEGL  (  595): loaded /system/lib/egl/libGLESv1_CM_emulation.so
D/libEGL  (  595): loaded /system/lib/egl/libGLESv2_emulation.so

Else, you might find only the first, and an error like "egl.cnf not found, falling back to default" (Found the helping at: https://developer.amazon.com/sdk/fire/enable-features.html#GPU ).

Now, if you are using a physical mobile, I just read that some mobiles seem to not support egl, mainly some with CyanogenMod (They display a similar error in the logcat). In this case, you should test it on other phone, or an AVD with the specifications above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM