2

I'm trying to detect the opengl object under the cursor... I have read it referred to as picking. Here is my code:

public int makeBuffer(GL10 gl, int x, int y) {

    ByteBuffer PixelBuffer = ByteBuffer.allocateDirect(4);
    PixelBuffer.order(ByteOrder.nativeOrder());
    PixelBuffer.position(0);
    int mTemp = 0;
    gl.glReadPixels((int)x, (int) y, 1, 1, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, PixelBuffer);
    Log.e("Picking", " xy: x" + x + " y"+ y);
    byte b [] = new byte[4];
    PixelBuffer.get(b);
    Log.e("Picking", " rgba: r"+ PixelBuffer.get(0) + " g" + PixelBuffer.get(1) + " b" +
            PixelBuffer.get(2) + " a" + PixelBuffer.get(3));
    Log.e("Picking", " rgba: r"+ b[0] + " g" + b[1] + " b" +
            b[2] + " a" + b[3]);

    //mTemp = PixelBuffer.get(0);
    mTemp = b[0];

    Log.e("Picking", "result:" + mTemp );

    return mTemp;
}

See that most of the code above is logcat statements. My code prints zeros to the screen for r,g, and b. For alpha it prints '-1' which is translatable to 255 (unsigned) as 'full alpha'. I'm trying to detect a color on the screen at the given x/y position. I would be happy with a red value that's somewhere between 1 and 15, as that's the color that should be below the touch. I would expect that if I was doing it entirely wrong I would get all zeroes, but I must be doing it at least partially right, as I'm getting alpha. I have also included lines in my manifest that tell the phone that I use the permissions for the 'surface flinger' and the 'read frame buffer'. I don't know if these lines are working.

<uses-permission
android:name="android.permission.ACCESS_SURFACE_FLINGER" />
<uses-permission android:name="android.permission.READ_FRAME_BUFFER" /

any help would be appreciated.

D Liebman
  • 337
  • 1
  • 8
  • 17
  • I believe my trouble was that I was passing the wrong y value to the function. It turns out that opengl expects values increasing from the bottom and android provides values increasing from the top. – D Liebman Jul 24 '11 at 17:00
  • 1
    So...this actually works? It was just Y-inverted and you are getting pixel colors? – goger Aug 02 '11 at 14:57
  • Hi! I have the same problem here: http://stackoverflow.com/questions/7488197/picking-objects-in-android-opengl-es-using-colors-does-not-retrieve-correct-color. Seems I'm doing the same as you are, but I keep getting -1 values for red (instead of a number between 128-130) I would appreciate a lot if you could come by that thread and take a look at my code. I don't see any difference and you said that now you got it working. Thanks in advance! – Pedriyoo Sep 22 '11 at 09:22

2 Answers2

2

These permissions are only available for signature apps. Apps that were signed when the Android ROM was compiled. So they do nothing for regular apps.

android:name="android.permission.ACCESS_SURFACE_FLINGER"
android:name="android.permission.READ_FRAME_BUFFER" 
Rui Marques
  • 8,567
  • 3
  • 60
  • 91
2

I also faced similar issues when creating an openGL ES app on android.

My workaround so far was to change the app from RGB565 to RGBA8888. Insert these lines before setting the renderer:

getHolder().setFormat(PixelFormat.RGBA_8888);
setEGLConfigChooser(8, 8, 8, 8, 0, 0);

You must be aware that this solution will only work on devices that support RGBA8888.

  • I'm not sure what the first line is for. But setEGLConfigChooser(8, 8, 8, 8, 0, 0); was enough to solve this problem for me. – Michel Jun 12 '12 at 21:40
  • `setEGLConfigChooser(8, 8, 8, 8, 16, 0); getHolder().setFormat(PixelFormat.RGBA_8888);` – Juliano Bittencourt Aug 09 '12 at 01:54
  • I used both lines in the constructor of my GLSurfaceView . As I understand, this makes sure you GL Context and View surface use the same representation for pixels (RGB8888 instead of RGB565, which is the default). If both aren't the same, you will notice small differences in the values you draw and you read from glreadpixels. It's also importance to notice that setEGLConfigChoose tries to choose the most approximate configuration based on your request. The actual config will depend on you hardware. – Juliano Bittencourt Aug 09 '12 at 02:00