Sign in to follow this  
Crazy Chicken

2D Screen to 3D world projection. I am confused !!!

Recommended Posts

I have noticed that there are two possible ways to convert 2D screen coordinates into 3D world coordinates. The problem is that each approach gives me a different result for my ray origin and direction. Here are the two aprroaches as implemented in my code. Function 1:
void ScreenToWorld1( iX, int iY )
{
  D3DVIEWPORT9 viewport;
  D3DXMATRIXA16 matProj, matView, matWorld;
  D3DXVECTOR3 vRayOrigin, vRayDir;
	
  D3DXVECTOR3 vMouseNear, vMouseFar;
  vMouseNear = D3DXVECTOR3((float)iX, (float)iY, 0.0f);
  vMouseFar = D3DXVECTOR3((float)iX, (float)iY, 1.0f);

  m_pD3DDevice->GetViewport(&viewport);

  m_pD3DDevice->GetTransform( D3DTS_PROJECTION, &matProj );
  m_pD3DDevice->GetTransform( D3DTS_VIEW, &matView );
  D3DXMatrixIdentity(&matWorld);

  D3DXVec3Unproject( &vRayOrigin, &vMouseNear, &viewport, &matProj, &matView, &matWorld );
  D3DXVec3Unproject( &vRayDir, &vMouseFar, &viewport, &matProj, &matView, &matWorld );
}

Function2:
void ScreenToWorld2( int iX, int iY )
{
  D3DVIEWPORT9 view;
  D3DXMATRIXA16 matProj, matView, matWorld, m;
  D3DXVECTOR3 vRayOrigin, vRayDir;

  m_pD3DDevice->GetViewport(&view);
  m_pD3DDevice->GetTransform(D3DTS_PROJECTION, &matProj);	

  // Compute the vector of the pick ray in screen space
  D3DXVECTOR3 v;
  v.x =  ( ( ( 2.0f * iX ) / view.Width  ) - 1 ) / matProj._11;
  v.y = -( ( ( 2.0f * iY ) / view.Height ) - 1 ) / matProj._22;
  v.z =  1.0f;

  D3DXMatrixIdentity(&matWorld);
  m_pD3DDevice->GetTransform(D3DTS_VIEW, &matView);

  m = matWorld * matView;
  D3DXMatrixInverse( &m, NULL, &m );

  // Transform the screen space pick ray into 3D space
  vRayDir.x  = v.x*m._11 + v.y*m._21 + v.z*m._31;
  vRayDir.y  = v.x*m._12 + v.y*m._22 + v.z*m._32;
  vRayDir.z  = v.x*m._13 + v.y*m._23 + v.z*m._33;
  vRayOrigin.x = m._41;
  vRayOrigin.y = m._42;
  vRayOrigin.z = m._43;	
}

What is really confusing is that I need to use Function 1 when checking for ray-3D plane intersection and Function 2 for ray-mesh intersection. If some could please clear this up for me I would be very grateful. Thanks, Lukas

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