开发者

Problems with glReadPixels in android

I am trying to implement an opengl picking system I read about and have hit an issue with glReadPixels. Basically, every node in the scene gets a unique color and when a new touch happens, it renders the scene with nothing but the nodes painted with their uni开发者_开发技巧que color ids. I am trying to check the input coordinate with a list of stored color ids.

I can not get glReadPixels to work right. It always returns 0 0 0 for pixel values. i would really appreciate any help with getting the correct pixel values from it. thanks

here is the relevant code

private void handleEvent(MotionEvent event) {
    int x = (int) event.getX();
    int y = (int) event.getY();
    int action = event.getAction();
    int actionCode = action & MotionEvent.ACTION_MASK;
    if (actionCode == 0) {
        // Paint with colorID color
        mSettings.picking(true);
        dumpEvent(event);
        final GL10 gl = mSettings.getGL();

        ByteBuffer PixelBuffer = ByteBuffer.allocateDirect(4);
        PixelBuffer.order(ByteOrder.nativeOrder());
        gl. glPixelStorei(gl.GL_UNPACK_ALIGNMENT, 1);
        gl.glReadPixels(x, y, 1, 1, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, PixelBuffer);
        byte b[] = new byte[4];
        PixelBuffer.get(b);
        String key = "" + b[0] + b[1] + b[2];       
        // Check for selection
        mRenderer.processSelection(event, new SGColorI(pixel[0], pixel[1], pixel[2], pixel[3]));

        log.pl("GL on touchdown", key);
    } else if (actionCode == 2) {
        mSettings.picking(false);
    }
}


allocateDirect "just doesn't work" in this case. Use allocate.

It seemed very strange to me, but allocate vs allocateDirect was the only difference I came to.

Also this post in google groups helped me a lot.

btw, this discovery was made on emulator (several different versions), not the real device.


The use of glReadPixels for the classical pick problem is overkilled. Remember that glReadPixels is a blocking function, while openGL works mainly in an asynchronous way. Using glReadPixels means that all the commands in the driver queue have to be processed and this could be a huge waste of time.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜