Way to gluUnProject to a specific z-plane?

Started by elias4444, December 20, 2005, 22:03:47

Previous topic - Next topic

elias4444

I'm trying to position a mouse cursor on a rotated and translated playing field. I know the exact z-plane that I want to position it on, but I can't seem to get gluUnProject to give me the right coordinates. It uses the depth value rather than a z value. So, how do you translate the z-plane coordinate into that 0.0 to 1.0 depth value? Is there an algorithm?

Also, I tried the glReadPixels thing, but that just seems to return the z-plane value of the first drawn pixel it runs into.

Any help would be appreciated.
=-=-=-=-=-======-=-=-=-=-=-
http://www.tommytwisters.com

darkprophet

from my experience, gluUnproject wont help you in your case...

gluProject/gluUnProject simply convert world coordinates to screencoordates and viceversa, but you lose the Z coordinate.

I would suggest you try picking by firing a ray and checking which triangle it hits and positioning the cursor on the middle of the triangle (or something similar)

DP

elias4444

Problem is, then there has to be an object that covers the whole board at that z-coordinate... which kinda messes up my rendering. :(

There's gotta be a way to just position the cursor in 3D space by specifying a z-coordinate and then projecting it. Or maybe I'm just missing something.
=-=-=-=-=-======-=-=-=-=-=-
http://www.tommytwisters.com

darkprophet

glReadPixel the depth component (its clamped between [0,1]) at the mouse location and multiply between the difference of near/far plane ?

DP

elias4444

Right, that was what I was doing before... but you have to have something drawn there, as glReadPixels takes the first pixel it runs into, or the farplane if it doesn't find anything. Problem there is, that your z-coordinate will change depending on what's drawn at that location (my game is a 3rd person view, and I want to draw the cursor on the "floor" all the time). If the mouse goes over the character or another sprite, the z-coordinate will change.

I just want to always have the z-coordinate at a specific number (let's say "0"), but set the x and y coordinates so that the mouse-sprite is always under the mouse cursor.

Just as an update - I'm able to use gluProject to obtain a winz value, which is then plugged into gluUnProject... but gluUnProject is returning sporadic z coordinates... I have no idea why it keeps changing on me. :(
=-=-=-=-=-======-=-=-=-=-=-
http://www.tommytwisters.com

napier

I had a similar problem when I first got into unProject().  

One problem was that I wasn't giving unProject the correct modelview, projection and viewport matrices.  

Another stranger problem, which might just be some sort of newbie error on my part, was here:

   public static float getZDepthAt(float worldx, float worldy, float worldz)
    {
        float[] resultf = new float[3];
        project( worldx, worldy, worldz, resultf);
        return ((int)(resultf[2] * 10000F)) / 10000f;  // truncate to 4 decimals
    }


I was doing the same thing as you, I projected a world point to get the equivalent Z depth value.  I found that whenever the depth value went to more than 4 decimal places my Z coords became erratic.    I tried truncating the Z depth to 4 decimals and got the results I wanted.

I hesitate to bring it up because it's a long shot, but maybe theres a precision issue at work.
penGL/Java/LWJGL demos and code: http://potatoland.org/code/gl