gluUnproject problems

i have gone through all the topics posted related to gluUnproject, they did help me a lot but i am still having some problems. the x and y values i get are close, but not whatz desired. the z value is totally off. whenever i click at a different position the difference in the x and y values is very small(10-100) whereas the difference has to be in hunderds to thousands. herez some of my code (VB6):

Private Sub picOpengl_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim tempz As GLfloat
Dim temp As Boolean
If Button = vbLeftButton And Check1.Value = Checked Then
intXPosition = (intWidth / 2) + X
intYPosition = (intHeight / 2) + Y
OpenGLRender
temp = gluUnProject(X, Y, -CDbl(sngZatXY), mvmatrix(0), projmatrix(0), viewport(0), wx, wy, wz)
MsgBox intXPosition & " " & intYPosition & " " & wx & " " & wy & " " & wz
End If
End Sub

the intXPosition and intYPosition are right, cause the way i have set my viewport in VB is that the mouse event returns values between -250 to 250 for X and - 300 to 300 for Y. passing a negative value for the ‘z’ parameter in gluUnproject gets the values closer to whats desired, if i send a +ve value, the values returned are totally off. another thing, im not sure if readpixels returns a values between 0-1 or the actual z value. if it returns between 0-1 then do i have to convert it to the actual value when i pass it thru gluunproject?

display routine :

glpushmatrix
translate…
rotate…
rotate…
translate…
glGetIntegerv glgViewport, viewport(0)
glGetDoublev glgModelViewMatrix, mvmatrix(0)
glGetDoublev glgProjectionMatrix, projmatrix(0)
glReadBuffer rbmBack
glReadPixels intXPosition, intYPosition, 1, 1, rpDepthComponent, pxlFloat, sngZatXY

most of the variables are global.

also my readpixels always returns 1.

[This message has been edited by mithun_daa (edited 12-29-2003).]

anyone?

I don’t know Visual Basic, but I’m assuming it can do magic to read a value into sngZatXY. If you’re saying this always comes back as “1” then that might be your problem; make it come back as the appropriate depth value in the buffer. (This is assuming you haven’t already cleared the buffer – that would make it not work :slight_smile:

Also, you’re reading from the back buffer. You should probably be reading from the Z buffer.

Here’s my own code for doing it in a project i’m working on. It works perfectly

GLint viewport[4];
GLdouble modelview[16];
GLdouble projection[16];
GLfloat winX, winY, winZ;

glGetDoublev( GL_MODELVIEW_MATRIX, modelview );
glGetDoublev( GL_PROJECTION_MATRIX, projection );
glGetIntegerv( GL_VIEWPORT, viewport );

winX = (float)mouse_x;					// Holds The Mouse X Coordinate
winY = (float)viewport[3] - (float)mouse_y;

glReadPixels(int(winX), int(winY), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ );

gluUnProject( winX, winY, winZ, modelview, projection, viewport, &posX, &posY, &posZ);

Hope this helps.

no, the z value is still 1 or veryyyy close to 1. and oconnellseanm, are u passing the +ve value of the z value returned by the readpixels?

[This message has been edited by mithun_daa (edited 12-30-2003).]

Yes I am passing the z value returned form ReadPixels to gluUnProject. I got this code from NeHe’s website. He’s got a tutorial in the Articles section on it. It works just fine for me. Another thing you might need to consider is that in OpenGL the bottom of the window is considered y=0, instead the top, like in Visual Basic. Maybe this might be your problem?

Originally posted by oconnellseanm:
Another thing you might need to consider is that in OpenGL the bottom of the window is considered y=0, instead the top, like in Visual Basic. Maybe this might be your problem?

i have taken care of that and the origin is at the bottom left corner.

here are the matrices that are returned. could anyone help me with the projection matrix, cause i find it fishy and not conforming to the one shown on pg 674 (red book).

projection matrix :
0.968 0 0 0
0 1 0 0
0 0 -1.0004 -1
0 0 -200 0

modelview :
1 0 0 0
0 .995 .444 0
0 -.444 .895 0
0 -3123.82 -9282.531 1

viewport :
0 0
501 485

my near plane is at 100, and i am using glperspective.
thank u

[This message has been edited by mithun_daa (edited 12-30-2003).]

one thing that i came across is that “glReadPixels value reaches numbers very near to 1.0 if the zNear plane is too far away from the front of your drawing and might impact precision” (http://www.opengl.org/discussion_boards/ubb/Forum3/HTML/000613.html)

and this is exactly whatz happening. whenever i put my near plane very close to the scene, i get the right values. but the problem now is that, i have a very huge scene to be displayed and it has to be translated a long way so that the whole scene is visible, this in the bargain moves the near plane further away from the drawings. is there any way out?
can i use the gldepthrange function to clamp the values between 0 -10 so that gives me more precision than 0-1? is this how it works? cause when i use it, readpixels still returns a value close to 1 (near plane away from the drawing).
thank u

[This message has been edited by mithun_daa (edited 12-30-2003).]

Now I don’t know derpy diddle about programming OGL in VB, but where I live, earth, you can set the size (in bits) of the depth buffer. 16 is common, 24 is better, and 32 is best.

Originally posted by comatoast:
… but where I live, earth, you can set the size (in bits) of the depth buffer. 16 is common, 24 is better, and 32 is best.

How can you get a 32 bits depth ? Even if I do so, I get 24 bits at most.

Originally posted by comatoast:
Now I don’t know derpy diddle about programming OGL in VB, but where I live, earth, you can set the size (in bits) of the depth buffer. 16 is common, 24 is better, and 32 is best.

how can i set the size of the depth buffer? what do i use to set it?