DirectX Graphics 프로그래밍 가이드 고정 기능 파이프라인 변환 월드 변환 [목차열람] [주소복사] [슬롯비우기] |
Microsoft DirectX 9.0 |
여기에서는, 월드 변환에 대한 기본적인 개념을 소개해, Microsoft® Direct3D® 애플리케이션으로 월드 변환 행렬을 설정하는 방법에 대해 자세하게 설명한다.
월드 변환에서는, 정점이 모델의 로컬 원점에 대해서 정의된 모델 공간으로부터, 정점이 장면(scene)내의 모든 개체에 공통되는 원점에 대해서 정의된월드 공간에 좌표를 변환 한다. 기본적으로는, 월드 변환에 의해 모델이 월드에 배치되는 것으로부터, 이러한 이름이 붙어 있다. 다음 그림은 월드 좌표계와 모델의 로컬 좌표계의 관계를 나타내고 있다.
월드 변환에서는, 평행이동, 회전, 및 스케일링을 어떻게 조합해도 사용할 수 있다. 변환의 수학 위에의 설명에 대해서는, 「3D 변환」을 참조할것.
다른 모든 변환과 같이, 일련의 변환 행렬을, 각각의 효과를 모두 포함한 단일의 행렬에 연결하는 것으로 월드 변환을 생성 한다. 간단한 예에서는, 어느 모델이 월드 원점에 있어, 그 로컬 좌표축이 월드 공간과 같은 방향으로 정의되고 있는 경우, 월드 행렬은 단위행렬이다. 일반적인 월드 행렬은, 월드 공간에의 평행이동에, 필요에 따라서 모델의 회전을 몇개인가 조합한 것이다.
다음 예는 C++ 로 기술된 가공의 3D 모델 클래스에 관한 것이다. 여기에서는, Direct3D extension (D3DX) 유틸리티 라이브러리에 포함되는 헬퍼-함수를 사용해, 모델의 자세를 결정하는 회전을 3 회 실시해, 한층 더 월드 공간에서의 위치를 기준에 그 모델을 재배치하는 평행이동을 실시하는 월드 행렬을 생성 한다.
/* * For the purposes of this example, the following variables * are assumed to be valid and initialized. * * The m_xPos, m_yPos, m_zPos variables contain the model's * location in world coordinates. * * The m_fPitch, m_fYaw, and m_fRoll variables are floats that * contain the model's orientation in terms of pitch, yaw, and roll * angles, in radians. */ void C3DModel::MakeWorldMatrix( D3DXMATRIX* pMatWorld ) { D3DXMATRIX MatTemp; // Temp matrix for rotations. D3DXMATRIX MatRot; // Final rotation matrix, applied to // pMatWorld. // Using the left-to-right order of matrix concatenation, // apply the translation to the object's world position // before applying the rotations. D3DXMatrixTranslation(pMatWorld, m_xPos, m_yPos, m_zPos); D3DXMatrixIdentity(&MatRot); // Now, apply the orientation variables to the world matrix if(m_fPitch || m_fYaw || m_fRoll) { // Produce and combine the rotation matrices. D3DXMatrixRotationX(&MatTemp, m_fPitch); // Pitch D3DXMatrixMultiply(&MatRot, &MatRot, &MatTemp); D3DXMatrixRotationY(&MatTemp, m_fYaw); // Yaw D3DXMatrixMultiply(&MatRot, &MatRot, &MatTemp); D3DXMatrixRotationZ(&MatTemp, m_fRoll); // Roll D3DXMatrixMultiply(&MatRot, &MatRot, &MatTemp); // Apply the rotation matrices to complete the world matrix. D3DXMatrixMultiply(pMatWorld, &MatRot, pMatWorld); } }
월드 변환 행렬을 준비할 수 있으면 이 행렬을 설정하기 위해서 IDirect3DDevice9::SetTransform 메서드를 호출해, 제 1 인수에 D3DTS_WORLD 매크로를 지정한다.