좌표변환 (로컬 <-> 화면)

반응형

 // 스크린좌표로부터 로컬좌표를 계산함. 변환된 후의 로컬좌표의 z축좌표는 입력인자로 전달됨.
void ScreenToLocal( D3DXMATRIX* matWorld, D3DXMATRIX* matView, D3DXMATRIX* matProjection, D3D10_VIEWPORT* viewport,
       D3DXVECTOR3* localCoord, D3DXVECTOR3* screenCoord, float localZ )
{
 float px = ((2.0f*screenCoord->x/viewport->Width) - 1.0f)/(*matProjection)(0, 0);
 float py = ((-2.0f*screenCoord->y/viewport->Height) + 1.0f)/(*matProjection)(1, 1);

 Ray ray;
 ray.origin = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
 ray.direction = D3DXVECTOR3(px, py, 1.0f);

 D3DXMATRIX ViewInverse;
 D3DXMatrixInverse(&ViewInverse, 0, matView);

 D3DXVec3TransformCoord(&ray.origin, &ray.origin, &ViewInverse);
 D3DXVec3TransformNormal(&ray.direction, &ray.direction, &ViewInverse);
 D3DXVec3Normalize(&ray.direction, &ray.direction);

 float t = (localZ-ray.origin.z)/ray.direction.z;
 D3DXVECTOR3 World_Coord2 = ray.origin + ray.direction*t;

 D3DXMATRIX WorldMatInv;
 D3DXMatrixInverse(&WorldMatInv, 0, matWorld);
 D3DXVec3TransformCoord(localCoord, &World_Coord2, &WorldMatInv);
}

 // 로컬좌표로부터 스크린 좌표를 계산
void LocalToScreen(D3DXMATRIX* matWorld, D3DXMATRIX* matView, D3DXMATRIX* matProjection, D3D10_VIEWPORT vp,
       D3DXVECTOR3* screenCoord, D3DXVECTOR3* localCoord)
{
// 이 함수 하나로 아래 연산을 모두 수행함.
 // D3DXVec3Project(screenCoord, localCoord, &vp, &matProjection, &matView, &matWorld);

 D3DXMATRIX vw = (*matView)*(*matWorld);
 D3DXVECTOR3 cameraCoord;
 D3DXVec3TransformCoord(&cameraCoord, localCoord, &vw);
 D3DXVec3Normalize(&cameraCoord, &cameraCoord);
 screenCoord->x = (cameraCoord.x * (*matProjection)(0,0) * 1.0f) * vp.Width/2.0f;
 screenCoord->y = -(cameraCoord.y * (*matProjection)(1,1) - 1.0f) * vp.Height/2.0f;
}


'Study > Directx 10' 카테고리의 다른 글

Texture Mapping  (0) 2010.05.23
TextureAddressMode  (0) 2010.05.23
텍스쳐 매핑.  (0) 2010.05.16
Texture load  (0) 2010.05.15
방향성광원, 점광원  (0) 2010.05.15
TAGS.

Comments