Unprojecting a 2D screen point to a 3D coordinate

Started by
2 comments, last by kenic 24 years, 1 month ago
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
Advertisement
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

http://members.xoom.com/mutex0

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
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;
D3DXVECTOR3 WorldPos;

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;

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

D3DXVECTOR3 Eye;
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.

Thankx.

This topic is closed to new replies.

Advertisement