I pick up some objects coordinates with gluUnproject I have some trouble to get some points depending on the Z position…

I defined my gluPerspective like that:
glu.gluPerspective(Angle_View_Degree, width/height, 10.0f, 500.0f);
Near plane = 10;
Far plane = 500;

I move in my plane using glTranslate(X,Y,Z)…
If my Z is -10 I will put the value 0 for my gluUnproject z value like this:
glu.gluUnProject((double)X,(double)Y,0,mvmatrix,projmatrix,viewport,x,y,z)
and if my Z is -500 I will give the value 1 to gluUnproject z value.

When I use it like that gluUnproject gives me the correct values …

Now I want to use it for a Z value between 10 and 500. We can take the middle value 255… For this Z value I gave to gluUnproject z the value 0.5 but here it does not work … It does not give the correct object coordinates…

Is there anybody who knows why it does not work ?
Thanks a lot

The distribution of z values is not linear when using a perspective projection. Close to the near plane the z values are more tightly packed than towards the far plane.

Therefor, a screen z of 0.5 will not map to (near+far)/2 but much closer to near.

Try it yourself by using the gluProject function and checking the resulting z value…

Is there a function that gives you the possibility to determine the z gluUnproject value just by giving the point in function of Z value of the plan ?

Thanks
Mat

Originally posted by JML:
[b]The distribution of z values is not linear when using a perspective projection. Close to the near plane the z values are more tightly packed than towards the far plane.

Therefor, a screen z of 0.5 will not map to (near+far)/2 but much closer to near.

Try it yourself by using the gluProject function and checking the resulting z value…

Originally posted by Mat:
[b]Is there a function that gives you the possibility to determine the z gluUnproject value just by giving the point in function of Z value of the plan ?

Thanks
Mat

[/b]

Yes, gluProject will transform world <xyz> to screen <xyz>.

You could call it with <0, 0, requestedWorldZ> and use the resulting screenZ for subsequent gluUnproject calls.