오늘은 행렬 회전, 이동, 결합 에 대한 수업을 들었다.
행렬의 이동, 확대/축소, 회전
#include <windows.h>
#include <d3d11.h>
#include <d3dx11.h>
#include <d3dcompiler.h>
#include <xnamath.h>
#include <iostream>
void printVector(CXMVECTOR v);
int main()
{
XMMATRIX Mat{};
XMVECTOR V{};
XMVECTOR R{};
Mat = XMMatrixIdentity();
V.m128_f32[0] = 5.0f;
Mat = XMMatrixTranslation(1.0f, 1.0f, 1.0f);
R = XMVector3TransformCoord(V, Mat);
printVector(R);
Mat = XMMatrixScaling(2.0f, 1.0f, 1.0f);
R = XMVector3TransformCoord(V, Mat);
printVector(R);
Mat = XMMatrixRotationY(XMConvertToRadians(90.f));
R = XMVector3TransformCoord(V, Mat);
printVector(R);
}
void printVector(CXMVECTOR v)
{
printf("%f,%f,%f\n", v.m128_f32[0], v.m128_f32[1], v.m128_f32[2]);
}
코드 설명
- CXMVECTOR : const XMVECTOR&
- CXMMATRIX : const XMMATRIX&
행렬의 초기화 [암기 사항]
Mat = XMMatrixIdentity();
- DirectX에서는 행렬과 벡터를 초기화하지 않으면 더미 값이 들어가 문제가 발생할 수 있음
- 모든 행렬의 기본값은 단위 행렬이어야 함
- 단위 행렬: 주대각선의 원소가 모두 1이고 나머지 원소는 0인 정사각 행렬
이동(Translation)
Mat = XMMatrixTranslation(1.0f, 1.0f, 1.0f);
R = XMVector3TransformCoord(V, Mat);
- XMMatrixTranslation(1.0f, 1.0f, 1.0f) : 이동 좌표 설정
- XMVector3TransformCoord(V, Mat) :행렬을 변환 후 벡터에 적용
확대/축소(Scaling)
Mat = XMMatrixScaling(2.0f, 1.0f, 1.0f);
R = XMVector3TransformCoord(V, Mat);
- XMMatrixScaling(2.0f, 1.0f, 1.0f) :각 축의 배율 설정
- XMVector3TransformCoord(V, Mat) :변환된 행렬을 벡터에 적용
회전(Rotation) (Y축 기준)
Mat = XMMatrixRotationY(XMConvertToRadians(90.f));
R = XMVector3TransformCoord(V, Mat);
- XMConvertToRadians() :도(degree) 값을 라디안 값으로 변환
- XMMatrixRotationY(XMConvertToRadians(90.f)) :Y축을 기준으로 회전 ( 라디안 값을 넣어야 함 / 파이 == 180도)
- R = XMVector3TransformCoord(V, Mat) :변환된 행렬을 벡터에 적용
행렬의 결합 법칙 / 교환 법칙
#include <windows.h>
#include <d3d11.h>
#include <d3dx11.h>
#include <d3dcompiler.h>
#include <xnamath.h>
#include <iostream>
void printVector(CXMVECTOR v);
int main()
{
XMVECTOR V{};
XMVECTOR R{};
XMMATRIX matRotation{};
XMMATRIX matLocation{};
XMMATRIX matTranslation{};
V = XMVectorSet(5.0f,0.0f,0.0f,1.0f);
matRotation = XMMatrixRotationY(XM_PIDIV2);
matLocation = XMMatrixTranslation(5.0f, 0.0f, 0.0f);
matTranslation = matRotation * matLocation;
R = XMVector3TransformCoord(V, matTranslation);
printVector(R);
}
void printVector(CXMVECTOR v)
{
printf("%f,%f,%f\n", v.m128_f32[0], v.m128_f32[1], v.m128_f32[2]);
}
코드 설명
- XMMATRIX matRotation{} :회전 행렬
- XMMATRIX matLocation{} :이동 행렬
- XMMATRIX matTranslation{} :행렬 결합
V = XMVectorSet(5.0f,0.0f,0.0f,1.0f);
- 벡터의 x, y, z, w 값 설정
matRotation = XMMatrixRotationY(XM_PIDIV2);
matLocation = XMMatrixTranslation(5.0f, 0.0f, 0.0f);
matTranslation = matRotation * matLocation;
R = XMVector3TransformCoord(V, matTranslation);
- matRotation = XMMatrixRotationY(XM_PIDIV2) :Y축을 기준으로 90도 회전
( XM_PIDIV2 = 파이 / 2, XM_PI = 파이 ) - matLocation = XMMatrixTranslation(5.0f, 0.0f, 0.0f) :이동 행렬 설정
- matTranslation = matRotation * matLocation :회전과 이동을 결합
- R = XMVector3TransformCoord(V, matTranslation) : 변환된 행렬을 벡터에 적용
[중요] 행렬 연산 순서에 따른 차이
- 이동 후 회전: 결과값 (0, 0, -10)
- 회전 후 이동: 결과값 (5, 0, -5)
결론: 행렬의 교환 법칙이 성립하지 않음 ( 연산 순서가 다르면 결과도 다름. )
- 이동 후 회전 vs. 회전 후 이동은 다른 결과를 만듦.
최종적인 3D 변환 방식
- 3D 연산은 하나의 최종 변환 행렬을 구한 후 적용
- 월드 → 뷰 → 프로젝션 행렬을 따로 구하는 것이 아니라, 최종 행렬을 결합하여 적용
- 변환 순서: 스케일 → 회전 → 이동
- 이동이 마지막에 적용되어야 정확한 결과가 나옴
추가 개념
- 파이프라인 개념: 모든 변환을 순서대로 적용해야 올바른 결과를 얻을 수 있음
- XMVECTOR4 : 4원수 계산 (Quaternion), 회전에 대한 보다 정교한 방식이 필요할 때 사용
- XMVECTOR3 : 회전, 이동, 확대/축소에 대한 경우 사용
'C++' 카테고리의 다른 글
[강의] 2월 10일 수업정리 (0) | 2025.02.10 |
---|---|
[강의] 2월 7일 수업정리 (0) | 2025.02.07 |
[강의] 2월 4일 수업정리 (0) | 2025.02.04 |
[강의] 2월 3일 수업정리 (0) | 2025.02.03 |
[강의] 1월 31일 수업정리 (0) | 2025.01.31 |