Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


Unprojecting a 2D screen point to a 3D coordinate

This topic is 6820 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''m porting a game from OpenGL to Direct3D. In OpenGL I use gluUnproject function, that maps my mouse window coordinates to object coordinates in my projection plane. The question is: How can I make the same as gluUnproject with Direct3D? Can I make the same with Direct3D, or I must implement it? In this case how can I implement it? I hope this is enough information for someone to help me with...or maybe a few of u have had this same problem thanx

Share this post

Link to post
Share on other sites
you have to write your own code. there was a post way back about this exact same subject and it helped me a lot. sorry i don''t have the time to explain now (science project due tomorrow), but my defyengine modeller at my web site does the unprojecting. the code is in CVw somewhere, probably OnLButtonDown


Share this post

Link to post
Share on other sites
Use an inverse for each matrix you used in the final scene.

For instance

world matrix
view matrix
projection matrix

inverse world matrix
inverse view matrix
inverse projection matrix

=Original vertex locations

You can stor the inverse of all the matrix operations into one set and call it when you need to get coordinates.

Hope that helps

Share this post

Link to post
Share on other sites
My final code to handle a WM_MOUSEMOVE message is something like this:

D3DXMATRIX WorldMatrix, WorldMatrixInv;
D3DXMATRIX ViewMatrix, ViewMatrixInv;
D3DXMATRIX ProjectionMatrix, ProjectionMatrixInv;
float det;

D3DXMATRIX Matrix, PreMatrix, MatrixInv;

m_pd3dDevice->GetTransform (D3DTRANSFORMSTATE_WORLD, (D3DMATRIX*)&WorldMatrix);
m_pd3dDevice->GetTransform (D3DTRANSFORMSTATE_VIEW, (D3DMATRIX*)&ViewMatrix);
m_pd3dDevice->GetTransform (D3DTRANSFORMSTATE_PROJECTION, (D3DMATRIX*)&ProjectionMatrix);

D3DXMatrixInverse (&ProjectionMatrixInv, &det, &ProjectionMatrix);
D3DXMatrixInverse (&WorldMatrixInv, &det, &WorldMatrix);
D3DXMatrixInverse (&ViewMatrixInv, &det, &ViewMatrix);
D3DXMatrixMultiply (&PreMatrix, &ProjectionMatrixInv, &ViewMatrixInv);
D3DXMatrixMultiply (&Matrix, &PreMatrix, &WorldMatrixInv);

RECT ViewportRect;
GetClientRect (hWnd, &ViewportRect);
POINT MidSize;
MidSize.x = ViewportRect.right / 2;
MidSize.y = ViewportRect.bottom / 2;

D3DXVECTOR3 ScreenPos (LOWORD (lParam), HIWORD (lParam), 0.0);
D3DXVECTOR4 PreWorldPos;

ScreenPos.x = (ScreenPos.x - MidSize.x) / MidSize.x;
ScreenPos.y = - ((ScreenPos.y - MidSize.y) / MidSize.y);

D3DXVec3Transform (&PreWorldPos, &ScreenPos, &Matrix);

WorldPos.x = PreWorldPos.x;
WorldPos.y = PreWorldPos.y;
WorldPos.z = PreWorldPos.z;

D3DXVECTOR3 Org (-5.0, -5.0, 0.0);
D3DXVECTOR3 Normal (0.0f, 0.0f, 1.0f);
CreatePlane (&Plane, &Org, &Normal);

Eye.x = vEyePt.x;
Eye.y = vEyePt.y;
Eye.z = vEyePt.z;
D3DXPlaneIntersectLine (&Point, &Plane, &WorldPos, &Eye);

Point += Org;

Pos.x = - Point.x * 25.6f;
Pos.y = - Point.y * 25.6f;
Pos.z = - Point.z * 25.6f;

char Buff[256];
sprintf (Buff, "(%5.2f,%5.2f,%5.2f)", Pos.x, Pos.y, Pos.z);
SetWindowText (hWnd, Buff);

lpCursorPos->x = (long)Pos.x;
lpCursorPos->y = (long)Pos.y;

It works fine.


Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!