0

I am looking for a solution to intersection point of a cube and a line. So i used

GLES20.glReadPixels(touchX, touchY, 1, 1, GLES20.GL_DEPTH_COMPONENT, GLES20.GL_FLOAT, zz);

and i showed the zz , but result was 0. so how could i get the depth buffer value of a Cube when i touched on the cube(actually on the 2d screen). I use GLES20 and Android API level15.And My code is below.

        ByteBuffer PixelBuffer = ByteBuffer.allocateDirect(4);
    ByteBuffer zBuffer = ByteBuffer.allocateDirect(4);

    PixelBuffer.order(ByteOrder.nativeOrder());
    PixelBuffer.position(0);
    zBuffer.order(ByteOrder.nativeOrder());
    zBuffer.position(0);
    FloatBuffer zz;
    zz = zBuffer.asFloatBuffer();
    GLES20.glReadPixels(touchX, touchY, 1, 1, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, PixelBuffer);
    GLES20.glReadPixels(touchX, touchY, 1, 1, GLES20.GL_DEPTH_COMPONENT, GLES20.GL_FLOAT, zz);

by the way picking color works fine.

Thanks!

1 Answers1

0

You forget to prepare target framebuffer to read... Try like this:

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glPixelStorei(GL_PACK_ALIGNMENT, 1);
glReadPixels(0, 0, deviceWidth, deviceHeight, GL_RGBA, GL_UNSIGNED_BYTE, buffer);

Or just write simple shader and render your zbuffer data into your FBO, simething like

gl_FragColor = vec4(vec3(gl_FragCoord.z), 1.0);

and then read color information form this FBO...

Tutankhamen
  • 3,532
  • 1
  • 30
  • 38
  • thanks Tutakhame. However according to the GLES sources, you can`t use glReadPixels function to get depth values. It only provides color buffer infos. so I couldn`t get a depth infos out of it. – Mungunbat Enkhbayar Sep 06 '12 at 06:28