XNA Creators Club Online
Page 1 of 1 (4 items)
Sort Posts: Previous Next

2d to 3d space without using Unproject

Last post 7/19/2009 6:04 AM by legalize. 3 replies.
  • 7/17/2009 10:50 PM

    2d to 3d space without using Unproject

    Hi, just wanted to check if this can be done - if I use the inverse matrices of the view/proj - I can create near and far point from the mouse pos?

    e.g. Code Snippet:

            // Pixel Space - ScreenSpace -1 to 1
            Vector2 screenSpace;
            screenSpace.X =  ( ( ( 2.0f * mousePos.X ) / screenSize.X  ) - 1 );
            screenSpace.Y = -( ( ( 2.0f * mousePos.Y ) / screenSize.Y  ) - 1 );
            
            // Inverse View/Proj Matrix
            Matrix invProj = Matrix.Invert(proj);
            Matrix invView = Matrix.Invert(view);
            
            // Near/Far Point
            Vector3 nearPos = new Vector3(screenSpace.X, screenSpace.Y, 0);
            Vector3 farPos  = new Vector3(screenSpace.X, screenSpace.Y, 1);
            
            nearPos = Matrix.Transform(nearPos, invProj * invView);
            farPos  = Matrix.Transform(farPos,  invProj * invView);

    Thanx,

    Ben
  • 7/18/2009 10:05 AM In reply to

    Re: 2d to 3d space without using Unproject

    Yes, you can do this. Of course, you're just re-implementing D3DXVec3Unproject...

  • 7/18/2009 11:32 PM In reply to

    Re: 2d to 3d space without using Unproject

    Not sure if this is right, but upon checking:

                // Pixel Space - ScreenSpace -1 to 1
                Vector2 screenSpace;
                screenSpace.X =  ( ( ( 2.0f * mousePos.X ) / screenSize.X  ) - 1 );
                screenSpace.Y = -( ( ( 2.0f * mousePos.Y ) / screenSize.Y  ) - 1 );
                
                // Inverse View/Proj Matrix
                Matrix invMProj = Matrix.Invert(projection);
                Matrix invMView = Matrix.Invert(view);
                
                // Near/Far Point
                Vector3 nearPos = new Vector3(screenSpace.X, screenSpace.Y, 0);
                Vector3 farPos  = new Vector3(screenSpace.X, screenSpace.Y, 1);
                
                nearPos = Vector3.Transform(nearPos, invMProj * invMView);  /**** My Vals *****/
                farPos  = Vector3.Transform(farPos,  invMProj * invMView);    /**** My Vals *****/
                
                // Check some values - compare
                Vector3 nearScreenPoint = new Vector3(mousePos.X, mousePos.Y, 0);
                Vector3 farScreenPoint  = new Vector3(mousePos.X, mousePos.Y, 1);
                Vector3 nearWorldPoint = m_device.Viewport.Unproject(nearScreenPoint, projection, view, Matrix.Identity); /*** Check Vals ****/
                Vector3 farWorldPoint  = m_device.Viewport.Unproject(farScreenPoint,  projection, view, Matrix.Identity);   /*** Check Vals ****/

    If I use the ViewPort unproject - my nearPos I calculate using the inverse matrices and the nearWorldPoint that the Unproject calculates are more or less the same.

    But the farWorldPoint and my farPos seem to be out by a factor proportional to the farclip plane...for example, I've got the farview set to 1000...so if I multiply the my farview by a 1000, I get the same as the viewport Unproject.

    Anyone give me some light on this?

    Thanx,

    Ben





  • 7/19/2009 6:04 AM In reply to

    Re: 2d to 3d space without using Unproject

    With a perspective projection, objects far away appear smaller because they are scaled by their distance from the camera. I haven't worked through your math, but I'm guessing that's why you need to do that scaling. This guess would be confirmed by using an orthographic projection and seeing if you still need the scale.

Page 1 of 1 (4 items) Previous Next