Search code examples
javaandroidopengl-esglreadpixelserror-code

glReadPixel returns zeros and error 1282 (Android)


Here is my picking code

public static void pick(GL11 gl){
        int[] viewport = new int[4];
        ByteBuffer pixel = ByteBuffer.allocateDirect(3).order(ByteOrder.nativeOrder());

        mColourR = BaseObject.getColourR();
        mColourG = BaseObject.getColourG();
        mColourB = BaseObject.getColourB();

        x = MGLSurfaceView.X();
        y = MGLSurfaceView.Y();

        gl.glGetIntegerv(GL11.GL_VIEWPORT,viewport,0);

        gl.glReadPixels((int)x, (int)(viewport[3] - y), 1, 1, GL11.GL_RGB, GL11.GL_UNSIGNED_BYTE, pixel);
        Log.d(TAG, String.valueOf(gl.glGetError()));
        R = pixel.get(0);
        G = pixel.get(1);
        B = pixel.get(2);
        Log.d(TAG, "Colour:" + pixel.get(0) + " " + pixel.get(1) + " " + pixel.get(2));
        if (R == mColourR && G == mColourG && B == mColourB){
            match = true;

        }else{
            match = false;
        }
        Log.d(TAG, String.valueOf(match));
        Log.d(TAG, String.valueOf(viewport[0]) + "," + String.valueOf(viewport[1]) + "," + String.valueOf(viewport[2]) + "," + String.valueOf(viewport[3]));

} 

Sorry about the mess. Anyway this code always seems to return (0,0,0) regardless of where I click. I have a red square, a green, square and a blue background for testing purposes and this method gets called every time one of the squares is drawn and the screen has been pressed like so

BaseObject.newColour();
    mColourR = BaseObject.getColourR();
    mColourG = BaseObject.getColourG();
    mColourB = BaseObject.getColourB();
    gl.glPushMatrix();
    gl.glColor4f(mColourR, mColourG, mColourB, 0.0f);
    gl.glTranslatef(x1, y1, 0);
    square.draw(gl);
    gl.glPopMatrix();
     ColourPicking.pick((GL11)gl);
    match = ColourPicking.getMatch();
    if (match == true){
        x1 = (float) Math.floor(Math.random()*9);
            y1 = (float) Math.floor(Math.random()*7);
        match = false;
    }

Even though there is no black space on screen I always get black values so I'm pretty sure its not just a precision problem. I have a log tag in there that returns the viewport values and they're fine, I've also checked my x y coordinates and they seem legit the only thing I have to go on is the 1282 error code.

Does anyone have any idea what this could be?

EDIT:

Right we know that the problem is coming from the read pixel function because I put glGetError() before and after the function and it came back

Before: 0
After: 1282

Getting rid of scaling didn't help but that was a long shot

I've changed the Buffer to a FloatBuffer and now I'm getting back (0.0, 0.0, 0.0) instead of (0, 0, 0) not much of a victory I know. I also noticed that the error is now 1280 (GL_INVALID_ENUM). I don't know if this is a step foreward. Does anybody know how to fix this error if not error 1282.


Solution

  • When you're getting GL_INVALID_ENUM, that's probably because you're passing GL_RGB as format parameter to glReadPixels. According to the Khronos documentation for glReadPixels, there are only 2 possible pairs of values for format and type:

    • GL_RGBA (format) and GL_UNSIGNED_BYTE (type)
    • The values of GL_IMPLEMENTATION_COLOR_READ_FORMAT_OES and GL_IMPLEMENTATION_COLOR_READ_TYPE_OES. You can query these using glGetIntegerv.

    I ran into the same problem recently, and using GL_RGBA fixed it for me.