简体   繁体   中英

OpenGL ES glReadPixels returns wrong values

First of all first time here so hello everyone. After searching the net for days including this site I failed to overcome this problem:

public void onDrawFrame(GL10 gl) {

gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

        gl.glMatrixMode(GL10.GL_MODELVIEW);
        gl.glLoadIdentity(); //load identity
        GLU.gluLookAt(gl, 0, -5, -25, 0, 0, 0, 0, 2, 0); //set camera

        if (fingerInput.isClicking()){
            /* Color Picking 4 START */
            gl.glDisable(GL10.GL_TEXTURE_2D); //turn off texturing, lighting and fog
            gl.glDisable(GL10.GL_FOG);
            gl.glDisable(GL10.GL_LIGHTING);

            while (i<squares.size()){ //draw picking colors
                squares.get(i).pickingDraw(gl); //note: picking is the same as draw() only with id colors and not textures
                i++;
            }
            i=0;

            gl.glReadPixels(fingerInput.getStart().x, screen_height-fingerInput.getStart().y, 1, 1, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, pixels); //read what was the color id pressed, store it in 'pixels' (a 4 slots array buffer)

            Log.d("tlog","at coords: ("+(screen_height-fingerInput.getStart().x)+", "+(screen_height-fingerInput.getStart().y)+")");

            for (j=0; j<4; j++){
                RGBA[j] = (int)(pixels.get(j) & 0xff);
                if (RGBA[j] < 0) RGBA[j]+=256; //correcting error caused by java using unsigned bytes and opengl singed bytes 
            }

anyway, for picking purposes, the squares are drawn each with a unique color,(currently drawing 3 squares with colors 99,96 and 93 red, and 0s at blue green alpha) glReadPixels returns on clicking either (99,0,0) or (91,0,0).

if the box is colored (x,0,0,255) it returns a value as if it had a list of possible values with spaces of 8 between them. (91,99,107..) sort of "rounding" each read color value to nearest "possible" value.

I came across a similar problem a couple of months ago, but I was using the Bitmap class with the getPixel method to read the colors of the individual pixels. As a test I used a black image, completely black so all the pixels should have the value 0 , but the getPixel method was returning the values 0,6,0,6,0,6...etc.

If you take a look at this link on the google code page of the Android SDK, someone else has got the same issue as me: Bitmap.getPixel error Unfortunately not fixed yet.

I am not sure if it has anything to do with OpenGL on the Android, but based on my experience, later SDK versions sometimes tend to mess up the color values somehow. The only thing that worked that day was using a lower SDK version.

Try your code on 1.4 or 1.5, if the problem disappears then wait for the fix on the later SDK. If it persists then it is another issue.

PS: If you find a solution on later SDKs for this, please let me know.

As i stated in here:

Java has no signed bytes and this implementation calls for a "GL10.GL_UNSIGNED_BYTE" answer from glReadPixels. the conversion hurts the data returned. (eg a 95 red would be returned as 91 or 99).

Since Java is incapable of dealing with this conversion issue there are two solutions i see for the problem: 1) Making a conversion map between each two possible values (for one color) and a new absolute value.

2) Using NDK would probably give the precise color value immediately. Bear in mind i haven't tested it and deciding on using NDK with OpenGL doesn't always give better performance than Java.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM