glGetString(GL_VERSION)返回“OpenGL ES-CM 1.1”但我的手机支持OpenGL 2

时间:2014-09-15 08:05:47

标签: android opengl-es android-ndk

我正在尝试制作基于NDK的OpenGL应用程序。在我的代码中的某个时刻,我想检查设备上可用的OpenGL版本。

我使用以下代码:

const char *version = (const char *) glGetString(GL_VERSION);
if (strstr(version, "OpenGL ES 2.")) {
    // do something 
} else {
    __android_log_print(ANDROID_LOG_ERROR, "NativeGL", "Open GL 2 not available (%s)", version=;
}

问题是版本字符串总是等于"OpenGL ES-CM 1.1"

我在Moto G(Android 4.4.4)和三星Galaxy Nexus(Android 4.3)上进行测试,两者都符合OpenGL ES 2.0(moto G也符合OpenGL ES 3.0标准)。

我在初始化显示时尝试强制EGL_CONTEXT_CLIENT_VERSION,但是然后eglChooseConfig返回0配置。当我在默认配置中测试上下文客户端版本值时,它始终为0:

const EGLint attrib_list[] = {
        EGL_BLUE_SIZE, 8,
        EGL_GREEN_SIZE, 8,
        EGL_RED_SIZE, 8,
        EGL_NONE
};

// get the number of configs matching the attrib_list
EGLint num_configs;
eglChooseConfig(display, attrib_list, NULL, 0, &num_configs);
LOG_D(TAG, "   • %d EGL configurations found", num_configs);

// find matching configurations
EGLConfig configs[num_configs];
EGLint client_version = 0, depth_size = 0, stencil_size = 0, surface_type = 0;
eglChooseConfig(display, requirements, configs, num_configs, &num_configs);
for(int i = 0; i < num_configs; ++i){

    eglGetConfigAttrib(display, configs[i], EGL_CONTEXT_CLIENT_VERSION, &client_version);


    LOG_D(TAG, " client version %d = 0x%08x", i, client_version);

}

// Update the window format from the configuration
EGLint format;
eglGetConfigAttrib(display, config, EGL_NATIVE_VISUAL_ID, &format);
ANativeWindow_setBuffersGeometry(window, 0, 0, format);

// create the surface and context
EGLSurface surface = eglCreateWindowSurface(display, config, window, NULL);
EGLContext context = eglCreateContext(display, config, NULL, NULL);

我链接到Open GL ES 2.0库:这里是我Android.mk

的摘录
LOCAL_LDLIBS    := -landroid -llog -lEGL -lGLESv2

2 个答案:

答案 0 :(得分:4)

感谢mstorsjo提供的提示,我设法得到了正确的初始化代码,如果其他人在努力解决这个问题,请在此处显示。

const EGLint attrib_list[] = {
        // this specifically requests an Open GL ES 2 renderer
        EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, 
        // (ommiting other configs regarding the color channels etc...
        EGL_NONE
};

EGLConfig config;
EGLint num_configs;
eglChooseConfig(display, attrib_list, &config, 1, &num_configs);

// ommiting other codes 

const EGLint context_attrib_list[] = { 
        // request a context using Open GL ES 2.0
        EGL_CONTEXT_CLIENT_VERSION, 2, 
        EGL_NONE 
};
EGLContext context = eglCreateContext(display, config, NULL, context_attrib_list);

答案 1 :(得分:2)

您从glGetString(GL_VERSION)获得的版本取决于您将代码与哪个库相关联,libGLESv1_CM.solibGLESv2.so。类似地,对于所有其他常见的GL功能。这意味着在实践中,您需要为GL ES 1和2版本的渲染构建两个单独的.so文件,并且只有在您知道可以使用哪一个(或动态加载函数指针)时才加载正确的文件。 )。 (当在GL ES 2和3之间具有兼容性时,这显然是不同的,您可以使用glGetString(GL_VERSION)进行检查。)

您没有说出您尝试使用EGL_CONTEXT_CLIENT_VERSION的位置 - 它应该在参数数组中用于eglCreateContext(您只能在实际选择配置后调用它)。赋予eglChooseConfig的属性数组应该具有EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT对以获得合适的配置。

相关问题