Results 1 to 2 of 2
  1. #1

    trouble with glReadPixels() in opengl

    Hi,

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

    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;
    	    }
    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 wrong, as I'm getting nothing usable. The code that calls this function does subtract the height from the window height so that the y value is increasing from below as opengl requires. 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'.

    Code:
    <uses-permission android:name="android.permission.ACCESS_SURFACE_FLINGER" />
    		<uses-permission android:name="android.permission.READ_FRAME_BUFFER" />
    Sorry if my post is a little disorganized. Any help would be appreciated.

    EDIT: OK, I'm still getting the same problem, but I realize that adjusting the y value by subtracting it from the screen height is wrong. I've remedied that.
    Last edited by radiodee1; 07-23-2011 at 07:06 AM. Reason: realized mistake

  2. #2
    As it turns out I do have to be concerned with the y values in glReadPixels. It was a major stumbling block. They do start at the bottom of the screen. I used a combination of techniques to get the y value I needed. I used 'getRawY()' from the MotionEvent and another method of the view I was in called 'getLocationInWindow()' which reports the location in the parent window of the current window. I also used 'getHeight()' on the view also. My code looks something like this:

    Code:
    int [] locationXY = new int[2];
    button_view.getLocationInWindow(locationXY);
    int mBot = button_view.getHeight();
    int newYValue = ( mBot + locationXY[1] ) - event.getRawY;
    The value 'newYValue' is fed to the glReadPixels routine. button_view is the button from the Activity that handles the touch event. This works for me because my layout is very simple. I also found the link below helpful:

    http://www.anddev.org/android-2d-3d-...ng-t11572.html



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •