Hello Guest

How do I get depth values from glReadPixels?

  • 2 Replies
  • 8831 Views
*

Offline napier

  • ***
  • 104
    • http://potatoland.org
How do I get depth values from glReadPixels?
« on: March 15, 2004, 03:30:37 »
I want to get world coordinates at a screen x,y position.  I call glReadPixels (with the GL.GL_DEPTH_COMPONENT param) to get the depth value at a mouse click position, but I don't know how to pass for the "pixels" param.  

I'm following an example at NeHe: http://nehe.gamedev.net/data/articles/article.asp?article=13 .  I'm expecting to get a depth value between 0 and 1 but no matter how I call glReadPixels I always get back a depth value of 1.06469197E9.

Code: [Select]
   static private void checkPoint(int x, int y) {
       DoubleBuffer modelview = ByteBuffer.allocateDirect(16*8).order(ByteOrder.nativeOrder()).asDoubleBuffer();
       DoubleBuffer projection = ByteBuffer.allocateDirect(16*8).order(ByteOrder.nativeOrder()).asDoubleBuffer();
       DoubleBuffer result = ByteBuffer.allocateDirect(3*8).order(ByteOrder.nativeOrder()).asDoubleBuffer();
       IntBuffer viewport = ByteBuffer.allocateDirect(4*4).order(ByteOrder.nativeOrder()).asIntBuffer();
       IntBuffer zdepth = ByteBuffer.allocateDirect(1*4).order(ByteOrder.nativeOrder()).asIntBuffer();
       float z;

       GL.glGetDouble(GL.GL_MODELVIEW_MATRIX, modelview);
       GL.glGetDouble(GL.GL_PROJECTION_MATRIX, projection);
       GL.glGetInteger(GL.GL_VIEWPORT, viewport);

       // get the depth at given x,y
       GL.glReadPixels(x, y, 1, 1, GL.GL_DEPTH_COMPONENT, GL.GL_FLOAT, zdepth);
       z = ((float)(zdepth.get(0)));

       System.out.println("Zdepth=" + (float)z);
       System.out.println("V0=" + viewport.get(0) + " V1=" + viewport.get(1)
                          + " V2=" + viewport.get(2) + " V3=" + viewport.get(3) );
       System.out.println("Screen Coords at cursor are " + x + "," + y);

       GLU.gluUnProject((double)x, (double)y, (double)0,  modelview, projection, viewport, result);
       float ox = (float)result.get(0);
       float oy = (float)result.get(1);
       float oz = (float)result.get(2);
       System.out.println("World Coords at near clip plane are: "+ox+", "+oy+", "+oz);

       GLU.gluUnProject((double)x, (double)y, (double)1,  modelview, projection, viewport, result);
       ox = (float)result.get(0);
       oy = (float)result.get(1);
       oz = (float)result.get(2);
       System.out.println("World Coords at far clip plane are: "+ox+", "+oy+", "+oz);
    }


The function output looks right for the world coords at the near and far clip planes:

      Zdepth=1.06469197E9
      V0=0 V1=0 V2=1024 V3=768
      Screen Coords at cursor are 534,370
      World Coords at near clip plane are: 0.024808, -0.021049, 14.0
      World Coords at far clip plane are: 0.74424, -0.63147, -14.999
penGL/Java/LWJGL demos and code: http://potatoland.org/code/gl

*

Offline spasi

  • *****
  • 2261
    • WebHotelier
How do I get depth values from glReadPixels?
« Reply #1 on: March 15, 2004, 08:59:51 »
Try making the zdepth buffer a ByteBuffer. Then z = zdepth.getFloat();

*

Offline napier

  • ***
  • 104
    • http://potatoland.org
How do I get depth values from glReadPixels?
« Reply #2 on: March 15, 2004, 14:45:04 »
thanks spasi,

The ByteBuffer/getFloat combo works.
penGL/Java/LWJGL demos and code: http://potatoland.org/code/gl