I’ve tweaked the native-activity NDK sample to incorporate the OpenGL code from the hello-gl2 example. I handle the APP_CMD_INIT_WINDOW message and then attempt to create the shaders. The shader creation fails and I attempt to get information via getShaderInfoiv, but that silently fails as well.
So my question is – how can I create an OpenGL ES 2.0 shader in a pure native Android application?
P.S. I know shader creation can fail if you use the Java GLSurfaceView and don’t create them in the correct thread, but looking at the native-activity sample, it only appears to have one thread!
It certainly is possible to create an OpenGL ES 2.0 shader in a native Android application. The key thing is to use proper OpenGL ES 2.0 context. I did something similar in my app namely initialized EGL context in native part and then created (and used) shaders in native code only. Based on what I managed to do I assume that what you want to do is also perfectly possible.
Since I had an entry point in Java code (did not use NativeAcvity mechanism) I also had to pass native window handle (
EGLNativeWindowType) from java to C++ in order to create EGL surface in native code. However, since you want to simply modify NativeActivity example then you can useengine->app->windowfor creating EGL surface on as presented in NativeActivitymain.csample.OK, how to create proper OpenGL ES 2.0 context in native code? I have just made two changes to the
main.cfile in NativeActivity sample and checked it worked.Firstly, used the following EGL attributes
in
eglChooseConfig(display, attribs, &config, 1, &numConfigs);.Secondly, later in creating context used
in
context = eglCreateContext(display, config, NULL, attrib_list);I left the rest of the code unchanged. I printed some info to make sure that OpenGL ES 2.0 is being used:
Hope it helps!