본문 바로가기

예전/3D

2D 좌표를 3D좌표로 바꾸는 함수

http://gamepro.tistory.com/m/post/view/id/481



// 3D 좌표를 2D좌표로 바꾸는 함수
POINT  PHYSICSCAL::CONVERT_3DTO2D_POINT ( D3DXVECTOR3& v )
 {
 D3DXMATRIX matProj;
 D3DXMATRIX matView;
 D3DVIEWPORT9 vp;

 g_GlobalData->g_pd3dDevice->GetViewport(&vp);
 g_GlobalData->g_pd3dDevice->GetTransform( D3DTS_PROJECTION, &matProj );
 g_GlobalData->g_pd3dDevice->GetTransform( D3DTS_VIEW, &matView );

 D3DXMATRIXA16 matCombine = matView * matProj;
 D3DXVec3TransformCoord ( &v, &v, &matCombine );

 v.x += 1.0f;
 v.y += 1.0f;

 v.x = ( vp.Width * (v.x  ) ) / 2.0f + vp.X;
 v.y = ( vp.Height * (2.0f-v.y) ) / 2.0f + vp.Y;

 POINT pt;

 pt.x = (long)v.x;
 pt.y = (long)v.y; 

 return pt;
 }


POINT ptCursor;
GetCursorPos( &ptCursor );
ScreenToClient( g_GlobalData->g_hWnd, &ptCursor );

// 2D 좌표를 3D좌표로 바꾸는 함수
D3DXVECTOR3  PHYSICSCAL::CONVERT_2DTO3D_POINT (POINT& ptCursor, float fDistance )
 {
 D3DXMATRIXA16 matProj;
 D3DXMATRIXA16 matView;
 D3DXMATRIXA16 matWorld;
 D3DVIEWPORT9 vp;
 D3DXVECTOR3  v;

 g_GlobalData->g_pd3dDevice->GetViewport(&vp);
 g_GlobalData->g_pd3dDevice->GetTransform( D3DTS_PROJECTION, &matProj );
 g_GlobalData->g_pd3dDevice->GetTransform( D3DTS_VIEW, &matView );

 v.x = ((  (((ptCursor.x-vp.X)*2.0f/vp.Width ) - 1.0f)) - matProj._31 ) / matProj._11;
 v.y = ((- (((ptCursor.y-vp.Y)*2.0f/vp.Height) - 1.0f)) - matProj._32 ) / matProj._22;
 v.z =  1.0f;

 D3DXMATRIXA16 m;
 D3DXVECTOR3 m_vPickRayDir, m_vPickRayOrig;

    D3DXMatrixInverse( &m, NULL, &matView );
    m_vPickRayDir.x  = v.x*m._11 + v.y*m._21 + v.z*m._31;
    m_vPickRayDir.y  = v.x*m._12 + v.y*m._22 + v.z*m._32;
    m_vPickRayDir.z  = v.x*m._13 + v.y*m._23 + v.z*m._33;
    m_vPickRayOrig.x = m._41;
    m_vPickRayOrig.y = m._42;
    m_vPickRayOrig.z = m._43;

 D3DXVECTOR3 pos;
 pos.x = ( m_vPickRayDir.x * fDistance ) + m_vPickRayOrig.x; 
 pos.y = ( m_vPickRayDir.y * fDistance ) + m_vPickRayOrig.y; 
 pos.z = ( m_vPickRayDir.z * fDistance ) + m_vPickRayOrig.z; 
 return pos;
 }

'예전 > 3D' 카테고리의 다른 글

[3D] 밉맵  (0) 2012.10.11
[3D] GetTransform과 GetViewport  (0) 2012.09.27
피킹참조자료 주움  (2) 2012.09.27
[UI엔진] 처음 내 프로젝트를 만들고서  (0) 2012.09.27
[3D] 김벌락 (Gimbal Lock)  (0) 2012.09.06