简体   繁体   English

Android NDK Native Activity OpenGL ES 1.1三角形不会显示

[英]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! 我是Android NDK和Native Activity的新手,我喜欢在屏幕中间创建一个三角形,但是无论如何尝试,我都不会出现!

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. 您可能有OpenGL错误,但不会自动记录它们。 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. 并在每次OpenGL调用后调用它。 Then you will see exactly where it crashes. 然后,您将确切看到崩溃的位置。

A part from that, I would recommend you using OpenGL ES 2.0. 除此之外,我建议您使用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). 我现在不确定您使用的所有呼叫是否都适用于ES 1.1(也许其他人可以确认)。 In addition, there is an NDK sample implementing exactly the same than you, but using ES 2.0 instead. 另外,还有一个NDK示例实现与您完全相同,但是使用ES 2.0。 You can find it here: 你可以在这里找到它:

http://code.google.com/p/android-cmake/source/browse/samples/hello-gl2/jni/gl_code.cpp?r=787b14cf9ed13299cb4c729d9a67d06e300fd52e 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. 它使用简单的着色器绘制三角形,并使用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? 第一:您使用的是Android虚拟设备还是物理移动设备?

With the first case, you need to use an ADV min API-15, and add gpu-emulation set to yes. 对于第一种情况,您需要使用ADV min API-15,并将gpu仿真设置添加为yes。 Or from the command line, you can use this line when you run your ADV: 或者在命令行中,当您运行ADV时可以使用以下行:

emulator -avd <avd_name> -gpu on

If it is ok, you will find these lines in the logcat: 如果可以的话,您会在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 ). 否则,您可能只找到第一个,并且出现类似“找不到egl.cnf,恢复为默认值”之类的错误(在以下网址找到了帮助: 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). 现在,如果您使用的是物理移动设备,我只是读到一些移动设备似乎不支持egl,主要是某些具有CyanogenMod的移动设备(它们在logcat中显示类似的错误)。 In this case, you should test it on other phone, or an AVD with the specifications above. 在这种情况下,您应该在其他手机或具有上述规格的AVD上进行测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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