I need a bit of a hand reading the buffer that is spat out by the glReadPixels feature in android’s opengl-es api. Here is my code so far…
public static void pick(GL11 gl){
int[] viewport = new int[4];
IntBuffer pixel = IntBuffer.allocate(384000);
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,viewport[3]-(int)y, 1, 1, GL11.GL_RGB, GL11.GL_UNSIGNED_BYTE, pixel);
}
the name of the output buffer in this code is “pixel” what do I need to add to this code to get the colour values back from the “pixel” buffer.
You can use one of the get() methods of the
IntBufferto access individual values.RGB color values are usually stored in that very order, so calling
pixel.get(0)will get you the red value of the first pixel,pixel.get(1)will get you the green channel and so on. Usually, the values are stored line-wise.So, if you need a value for a particular pixel, (x,y) you will have to call
get(screenWidth*3*y + x)By the way, you can retrieve the raw int array from your
IntBufferby callingpixels.array()