Sign in to follow this  
Raloth

Project/Unproject discrepancies

Recommended Posts

I'm trying to get D3DXVec3Unproject to work, but it seems to use different input than D3DXVec3Project. Project works fine. I can take any object in my world, project its coordinates onto the screen, put a dot there, and they will appear in the same place on the screen. Unproject is working a little differently, and I can't figure out what it's doing. I know the viewport and the three matrices are right because Project works fine, so it must be something to do with the coordinates I pass. Checking the values shows that the depth value works fine, but the other two are off. I've tried adjusting the values so that (0,0) is the center of the client area but still no luck. The only examples I can find with source calculate the position by hand, which I'm about to do if I can't get this to work. How am I supposed to set up the (x,y) coordinates to be passed to Unproject?

Share this post


Link to post
Share on other sites
Quote:
Original post by Raloth
but the other two are off. I've tried adjusting the values so that (0,0) is the center of the client area but still no luck.


Try adjusting the values so that they map between -1 and 1 (for both X and Y)

So that if your output from Ve3Project is (.5, .5), then they should go into Vec3Unproject as (0,0) (the center of -1, 1)

output (x,y)
input ( 2 * x / screenWidth - 1, 2 * y / screenHeight - 1 )

Also, I have no idea if this is really the problem or not, because I haven't used Vec3Unproject but I'd assume that the output of Vec3Project can obviously be used as the input for Vec3Unproject, but apparently it dosen't work that way.

Share this post


Link to post
Share on other sites
Tried it, no go.

I set up a camera view facing top-down so that I could just click around and spit out the coordinates of the near point, and they hardly ever change. I don't think the range has anying to do with scaling, or else why would the viewport be necessary?

[edit] I'd be glad to go through the DX picking sample if I could, but I don't have it. They got rid of it for some reason in the latest SDK release. If someone could post the source online for download (or pm me with the link in private if it's not exactly legal) that would be just as good. [smile]

[Edited by - Raloth on September 2, 2004 9:25:53 PM]

Share this post


Link to post
Share on other sites
Raloth,

Heres my C# code to do this.

Vector3 vNear = new Vector3(mouseX,mouseY,0f );
vNear.Unproject(device.Viewport, device.Transform.Projection, device.Transform.View, device.Transform.World);
Vector3 vFar = new Vector3(mouseX,mouseY,1f );
vFar.Unproject(device.Viewport, device.Transform.Projection, device.Transform.View, device.Transform.World);

vFar.Subtract(vNear);

if(Mesh.Intersect(vNear,vFar))
{
...
}

You don't need to normalise the mouse X and Y as the managed layer does it for you. However ... mine (and I pinched it from a couple of dozen examples on the web) isn't accurate either. I've had the following response to a query on this but haven't had time to try it yet

" couldn't get Unproject to work reasonably (... well coordinates that I got out were really small too small) when my projection-matrix was PerspectiveFovLH. When I changed it to PerspectiveLH, I started to get reasonable outcome. I don't need to scale screen coordinates between -1 and 1. Now I can send just send plain pixel coordinates to Vector3.Unproject and
get back co-ordinates in world-system. "

Hope this helps.

Phillip

Share this post


Link to post
Share on other sites
After projecting to screenspace you end up with x,y,z. z DOES matter. It will be between 0 and 1.

You ought to pass the same z back for unprojecting as this will affect how far into the screen that point is supposed to be. If you don't have a z value (mouse pos) unprojecting is a bit different. It'll be easier to unproject two points (same x,y different z). This will give you a ray in world space which you can use for intersecting calculation.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this