Trouble with gluUnproject

I am drawing a flat image in an XY plane. In the example that I posted z[0] is the z value of that plane. Here is my code:


projectionBuff.rewind();
modelBuff.rewind();
viewportBuff.rewind();

GL11.glGetFloat(GL11.GL_PROJECTION_MATRIX, projectionBuff);
GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, modelBuff);
GL11.glGetInteger(GL11.GL_VIEWPORT, viewportBuff);

modelBuff.get(modelArray[0]);
modelBuff.get(modelArray[1]);
modelBuff.get(modelArray[2]);
modelBuff.get(modelArray[3]);

projectionBuff.get(projectionArray[0]);
projectionBuff.get(projectionArray[1]);
projectionBuff.get(projectionArray[2]);
projectionBuff.get(projectionArray[3]);

viewportBuff.get(viewportArray);
GLU.gluUnProject((float)mouseX, (float)mouseY, z[0], modelArray, projectionArray, viewportArray, oldResult);
System.out.println("("+oldResult[0]+","+oldResult[1]+")");

The trouble is that I am always getting really small results usually between 1.001E-6 and 7.5E-5. What am I doing wrong?

I have tried using a z of zero, -300, +300 and always I get the same values!!! Does anyone have an example of this actually working that they could post?

How is your projection matrix setup? Is your nearplane or farplane zero? Can you paste the code where you setup your projection matrix?

Here is the code in my initGL() method:


private void initGL()
 {
     try
     {
          setVSyncEnabled(true);
     }
     catch (LWJGLException e1)
     {
          System.err.println("Unable to enable VSync: " + e1);
     }
     GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Black Background
     GL11.glClearDepth(1.0); // Depth Buffer Setup

     GL11.glEnable(GL11.GL_DEPTH_TEST); // Enables Depth Testing
     GL11.glDepthFunc(GL11.GL_LEQUAL); // The Type Of Depth Testing To Do

     GL11.glEnable(GL11.GL_CULL_FACE);

     // Really Nice Perspective Calculations
     GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_FASTEST);
     GL11.glEnable(GL11.GL_LINE_SMOOTH);
     GL11.glLineWidth(1.0f);

     GL11.glEnable(GL11.GL_BLEND);
     GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);

     viewportInit();
}

And here is where I initialize the viewport, there is a bit of weirdness due to te fact that I have multiple viewports to create a Picture-in-a-Picture effect.


private void viewportInit()
{
     GL11.glMatrixMode(GL11.GL_PROJECTION); // Select The Projection Matrix
     GL11.glLoadIdentity(); // Reset The Projection Matrix
     // Calculate The Aspect Ratio Of The Window
     width = getWidth();
     height = getHeight();

     viewMaxX = (int)(width*0.75f);
     viewMinX = (int)(width*0.05f);
     viewMaxY = (int)(height*0.75f);
     viewMinY = (int)(height*0.05f);

     for(int i = 0; i < 7; i++)
     {
          if(viewWidth[i] == 0) viewWidth[i] = width/5;
          if(viewHeight[i] == 0) viewHeight[i] = height/5;
     }

     GLU.gluPerspective(45.0f, (float)width/(float)height, 0.1f, 2000.0f);
     GL11.glViewport(0, 0, width, height);
     GL11.glMatrixMode(GL11.GL_MODELVIEW); // Select The Modelview Matrix
     GL11.glLoadIdentity();
}

Ok, I have found out that I am an idiot! I just read in the Red Book that the winZ value must be between 0 and 1, I was using the actual dpeth that I was drawing! So now I am trying to read the depth from the DepthBuffer using glReadPixels(), but I am not understanding it either! Here is what I am doing:


private IntBuffer winZ = ByteBuffer.allocateDirect(4).order(ByteOrder.nativeOrder()).asIntBuffer();
...
winZ.rewind();
GL11.glReadPixels(mouseX, mouseY, 1, 1, GL11.GL_DEPTH_COMPONENT, GL11.GL_INT, winZ);
GLU.gluUnProject((float)mouseX, (float)mouseY, winZ.get(), modelArray, projectionArray, viewportArray, oldResult);
 winZ.rewind();
System.out.println("Z="+winZ.get()+"\t("+oldResult[0]+","+oldResult[1]+")");

and it is returning 2147483647, which is most definately not between 0 and 1! ???

Depth values lie in the [0, 1] range, but are represented in fixed-point (see 2.11.1, DepthRange in the OpenGL spec for details). You may use GL_FLOAT in glReadPixels to get the [0, 1] value(s), the conversion will be done automatically.

Just to be sure that I understand, I should use GL_FLOAT and then pass it a ByteBuffer? Because according to the lwjgl javadoc, my only options are a ByteBuffer, and IntBuffer and a ShortBuffer.

-=EDIT=-
It can’t be ByteBuffer, it complied fine, but it always returns 0.

Yes, you could do that. In LWJGL cvs there’re also the Float & Double versions, so in the next release you will be able to use the Float one instead.

[quote=“cborders,post:7,topic:25197”]
It works for me here. Make sure you’re passing a direct ByteBuffer with native order and using buffer.getFloat().

Ahhh, I was just using get()! Thank you!