问题
I'm writing a game using android ndk. I'm using picking to select objects in opengl. For that i have an offscreen framebuffer object where i render into and i use glReadPixels() to get the color from the FBO. The color then encodes the id of the clicked object.
The problem is glReadPixels() does not work. It just does not read any color values. Is there anything I have to do, to make it work?
GLubyte pixel[4] = {0,0,0,0};
glReadPixels(x, y , 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, (void *)pixel);
This is how i try to use readpixels. But the result is always (0,0,0,0). I checked the coordinates (x,y). They are fine. The combination GL_RGBA, GL_UNSIGNED_BYTE should also be supported by any opengl es 2.0 implementation, according to the opengl docs.
I've read something about permissions that I have to set in the android manifest. I tried to add a permission like this:
<uses-permission android:name="android.permission.READ_FRAME_BUFFER"/>
but it didn't work either.
Has anyone an idea what the solution might be? or where I can look at?
回答1:
the problem was just a threading issue. all openGL calls have to be done in the same thread. Otherwise this can result in weird behavoir like this and worse.
回答2:
Dirk's suggestion fixed my issue. It was thread context.
来源:https://stackoverflow.com/questions/8761231/android-ndk-glreadpixels-from-offscreen-buffer