当前位置: 动力学知识库 > 问答 > 编程问答 >

OpenGL ES 3.0 in Android NDK r9

问题描述:

The recent Android NDK r9 unveils support of OpenGL ES 3.0. There is an example samples/gles3jni which demonstrates how to use OpenGL ES 3.0 from JNI/native code. The sample can be built two different ways:

  • Compatible with API level 11 and later
  • Require API level 18 or later.

Both versions include an OpenGL ES 2.0 fallback path for devices that don't support OpenGL ES 3.0. However, the in the first case example is statically linked against OpenGL ES 2 using LOCAL_LDLIBS option -lGLESv2. In the second case it is statically linked with GLES 3 the same way.

The initialization goes like this:

const char* versionStr = (const char*)glGetString(GL_VERSION);

if (strstr(versionStr, "OpenGL ES 3.") && gl3stubInit()) {

g_renderer = createES3Renderer();

} else if (strstr(versionStr, "OpenGL ES 2.")) {

g_renderer = createES2Renderer();

}

How can I omit the static linking at all and load GLES 2 or 3 dynamically from .so?

网友答案:

I didn't have a 4.3 device to test it, but my understanding is that the 1st method actually uses GLES 3 if available, so it is equivalent to dynamically linking libGLESv3.

Dynamic linking with libglesxx.so is also possible, but then you don't have shortcuts, and have to dlsym all functions that you use. It's not worth it, IMHO.

网友答案:

On API 18 and later, you can use eglGetProcAddress to dynamically query ES 2.0 functions, just like gl3stub.c in the sample does for ES 3.0 functions. Before API 18, you need to do something like this:

// global scope, probably a header file
extern GL_APICALL const GLubyte* (* GL_APIENTRY glGetString) (GLenum name);
extern GL_APICALL GLenum (* GL_APIENTRY glGetError) (void);
...

// initialization code
void* libGLESv2 = dlopen("libGLESv2.so", RTLD_GLOBAL | RTLD_NOW);
glGetString = dlsym(libGLESv2, "glGetString");
glGetError = dlsym(libGLESv2, "glGetError");
...

Add error-checking on the dlopen and dlsym calls, of course.

I'm not sure why you'd do this, though. libGLESv2.so is present on any version of Android you're likely to want to target, there shouldn't be any downside to linking against it.

分享给朋友:
您可能感兴趣的文章:
随机阅读: