2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

DirectX11のワールド変換行列から各成分を抽出

Last updated at Posted at 2018-03-27

DirectX::XMMATRIXで定義されたワールド変換行列から、スケーリング、回転、オフセットの各成分を取り出します。
(参考 → 知っていると便利?ワールド変換行列から情報を抜き出そうhttp://marupeke296.com/DXG_No39_WorldMatrixInformation.html)

ワールド変換行列が Scaling×Rotation×Offset の順になっていることが前提ですが、それは標準なのかな

using namespace DirectX;
XMMATRIX ExtractOffset(const XMMATRIX& mWorld) {
	return XMMatrixTranslation(mWorld.r[3].m128_f32[0], mWorld.r[3].m128_f32[1], mWorld.r[3].m128_f32[2]);
}
XMMATRIX ExtractScaling(const XMMATRIX& mWorld) {
	return XMMatrixScaling(
		XMVector3Length(XMVECTOR{ mWorld.r[0].m128_f32[0],mWorld.r[0].m128_f32[1],mWorld.r[0].m128_f32[2] }).m128_f32[0],
		XMVector3Length(XMVECTOR{ mWorld.r[1].m128_f32[0],mWorld.r[1].m128_f32[1],mWorld.r[1].m128_f32[2] }).m128_f32[0],
		XMVector3Length(XMVECTOR{ mWorld.r[2].m128_f32[0],mWorld.r[2].m128_f32[1],mWorld.r[2].m128_f32[2] }).m128_f32[0]
	);
}
// ワールド行列から回転成分のみを抽出する
XMMATRIX ExtractRotation(const XMMATRIX&  mWorld) {
	XMMATRIX mOffset = ExtractOffset(mWorld);
	XMMATRIX mScaling = ExtractScaling(mWorld);

	XMVECTOR det;
	// 左からScaling、右からOffsetの逆行列をそれぞれかける。
	return XMMatrixInverse(&det, mScaling) * mWorld * XMMatrixInverse(&det, mOffset);
}
2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?