DirectX ビルボード[C++]

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
Indent
記事: 1
登録日時: 6年前

DirectX ビルボード[C++]

#1

投稿記事 by Indent » 6年前

現在DirectXで3Dゲームを作っています。

プレイヤーをビルボードで表現したいのですが、平行移動とスケールはうまくいくのに回転がうまくいきません。

カメラの角度を変えずにz回転すると問題ないのですが、カメラの移動をしてからプレイヤーを回転させるとz軸が補正(?)されていないせいか斜めに回転してしまいます。

ネットで検索して調べたのですがうまくいかなかったので、計算の仕方を教えてください。

pell

Re: DirectX ビルボード[C++]

#2

投稿記事 by pell » 6年前

LookAtとかでも調べてみたらいいかも。
DirectXMathとD3D11でやるならこうなる。
XMMatrixLookAtLH。

コード:

   D3D11_MAPPED_SUBRESOURCE MappedResource;
   CBChangesFrame* cb;
   XMMATRIX Rot = XMMatrixIdentity();
	XMFLOAT3 fl = XMFLOAT3(0.0f, 1.0f, 0.0f);
	FXMVECTOR EyePosition = XMLoadFloat3(&g_vmove);//カメラ座標
	FXMVECTOR FocusPosition = XMLoadFloat3(&fl);
	FXMVECTOR UpDirection = XMVectorSet(0.0f, 1.0f, 0.0f, 1.0f);

	XMVECTOR EyeDirection = XMVectorSubtract(FocusPosition, EyePosition);

	XMVECTOR R2 = XMVector3Normalize(EyeDirection);

	XMVECTOR R0 = XMVector3Cross(UpDirection, R2);
	R0 = XMVector3Normalize(R0);

	XMVECTOR R1 = XMVector3Cross(R2, R0);

	XMVECTOR NegEyePosition = XMVectorNegate(EyePosition);

	XMVECTOR D0 = XMVector3Dot(R0, NegEyePosition);
	XMVECTOR D1 = XMVector3Dot(R1, NegEyePosition);
	XMVECTOR D2 = XMVector3Dot(R2, NegEyePosition);

	Rot.r[0] = XMVectorSelect(D0, R0, g_XMSelect1110.v);
	Rot.r[1] = XMVectorSelect(D1, R1, g_XMSelect1110.v);
	Rot.r[2] = XMVectorSelect(D2, R2, g_XMSelect1110.v);
	Rot.r[3] = g_XMIdentityR3.v;

	Rot = XMMatrixTranspose(Rot);
	Rot = XMMatrixInverse(nullptr, Rot);
// オフセットを切る
	Rot.r[3].m128_f32[0] = 0.0f;   
	Rot.r[3].m128_f32[1] = 0.0f; 
	Rot.r[3].m128_f32[2] = 0.0f; 

	pd3dImmediateContext->Map(g_pCBChangesFrame, 0, D3D11_MAP_WRITE_DISCARD, 0, &MappedResource);
	cb = (CBChangesFrame*)MappedResource.pData;
	cb->matWLP = XMMatrixTranspose(Rot/* *g_matLightVP*/);
	pd3dImmediateContext->Unmap(g_pCBChangesFrame, 0);

	DrawMesh(g_pMesh);

返信

“C言語何でも質問掲示板” へ戻る