void		CFVec4::SetXY( CFVec2Arg fv2Source )
{
	const XMVECTOR& v2V = *reinterpret_cast<const XMVECTOR*>( &fv2Source );
	XMVECTOR& v4V = *reinterpret_cast<XMVECTOR*>(this);

	v4V = XMVectorSetX( v4V, XMVectorGetX( v2V ) );
	v4V = XMVectorSetY( v4V, XMVectorGetY( v2V ) );
}
void		CFVec4::SetXYZ( CFVec3Arg fv3Source )
{
	const XMVECTOR& v3V = *reinterpret_cast<const XMVECTOR*>( &fv3Source );
	XMVECTOR& v4V = *reinterpret_cast<XMVECTOR*>(this);

	v4V = XMVectorSetX( v4V, XMVectorGetX( v3V ) );
	v4V = XMVectorSetY( v4V, XMVectorGetY( v3V ) );
	v4V = XMVectorSetZ( v4V, XMVectorGetZ( v3V ) );
}
Beispiel #3
0
//-------------------------------------------
// とりあえずIK
void BoneModel::VMDIkAnimation()
{

	//XMStoreFloat4()
	//XMLoadFloat4()
	if (mBone.empty())return;
	if (mMotion.empty())return;

	DWORD mBoneNum = mBone.size();
	DWORD mIkNum = mIk.size();
	// IK計算
	for (DWORD i = 0; i < mIkNum; i++){
		//{
		//	int i = 0;
		Ik& ik = mIk[i];
		UINT tg_idx = ik.target_bone_index;
		UINT ik_idx = ik.bone_index;

		for (UINT ite = 0; ite<ik.iterations; ++ite){
			for (UINT chn = 0; chn<ik.chain_length; ++chn){
				UINT link_idx = ik.child_bone_index[chn];//
				if (link_idx >= mBoneNum)continue;
				Bone& link_bone = mBone[link_idx];

				//UINT link_pidx = link_bone.mIkBoneIdx;
				UINT link_pidx = link_bone.mHierarchy.mIdxParent;

				//if (link_bone.mIkBoneIdx != 0){
				//	continue;
				//}

				if (link_pidx >= mBoneNum)continue;
				Bone& link_parent = mBone[link_pidx];

				Bone& tg_bone = mBone[tg_idx];
				(void)tg_bone;
				Bone& ik_bone = mBone[ik_idx];
				(void)ik_bone;

				XMVECTOR target_wpos = mBone[tg_idx].mMtxPose.r[3];
				XMVECTOR ik_wpos = mBone[ik_idx].mMtxPose.r[3];
				XMVECTOR lp_wpos = link_parent.mMtxPose.r[3];

				//Linkボーンのローカル空間に変換
				XMVECTOR Determinant;
				XMMATRIX inv_mtx = XMMatrixInverse(&Determinant, link_bone.mMtxPose);
				XMVECTOR tg_pos = XMVector4Transform(target_wpos, inv_mtx);
				XMVECTOR ik_pos = XMVector4Transform(ik_wpos, inv_mtx);
				XMVECTOR lp_pos = XMVector4Transform(lp_wpos, inv_mtx);


				// 回転軸と角度 
				XMVECTOR rot_axis = XMVectorSet(1, 0, 0, 0);
				float ang = 0.0f;
				bool same_dir = false;
				if (!RotDir(tg_pos, ik_pos, ik.control_weight, &rot_axis, &ang)){
					same_dir = true;
				}

				if (!same_dir){

					//tg_dirをik_dirに一致させるための回転
					XMVECTOR rot = XMQuaternionRotationAxis(rot_axis, ang);

					XMVECTOR lrot = FloatToVector(link_bone.mRot);
					XMVECTOR bone_rot_before = lrot;
					link_bone.mRot = VectorToFloat(XMQuaternionMultiply(rot, lrot));

					float dist_tg = XMVectorGetX(XMVector3Length(tg_pos));
					float dist_ik = XMVectorGetX(XMVector3Length(ik_pos));
					(void)dist_ik;
					float dist_lp = XMVectorGetX(XMVector3Length(lp_pos));
					(void)dist_lp;
					float dist_pltg = XMVectorGetX(XMVector3Length(lp_pos - tg_pos));
					float dist_plik = XMVectorGetX(XMVector3Length(lp_pos - ik_pos));
					float dot_tgik = XMVectorGetX(XMVector3Dot(XMVector3Normalize(tg_pos), XMVector3Normalize(ik_pos)));
					(void)dot_tgik;

					// 回転制限
					if (/*link.bLimit*/ 1){
						XMVECTOR rotmax, rotmin;
						//114.5916 = 2
						float a = 2;// XM_PI / 180.0f * 57.25f;
						rotmax = XMVectorSet(a, a, a, 0);//link.vMax;
						rotmin = XMVectorSet(-a, -a, -a, 0);//link.vMin;

						//名前に"ひざ"があったら回転制限
						if (std::string::npos != link_bone.mStrName.find("ひざ")){
							rotmax = XMVectorSet(-XM_PI / 180.0f*0.5f, 0, 0, 0);
							rotmin = XMVectorSet(-XM_PI, 0, 0, 0);
						}
						struct IkLink{
							XMFLOAT4 mMax;
							XMFLOAT4 mMin;
						};
						IkLink link = { VectorToFloat(rotmax), VectorToFloat(rotmin) };
						//Bone& link = link_bone;
						link_bone.mRot = VectorToFloat(LimitAngle(FloatToVector(link_bone.mRot), rotmin, rotmax));

						XMVECTOR angxyz = GetAngle(rot);
						//膝を曲げるための仮処理 かなりてきとう
						if (XMVectorGetX(angxyz) >= 0 &&
							//0.9f < dot_tgik &&
							//dist_tg > dist_ik &&
							dist_pltg > dist_plik &&
							link.mMax.x < 0 && link.mMax.y == link.mMin.y && link.mMax.z == link.mMin.z){
							//親リンクの回転接平面(できるだけこの平面に近づけたほうがよりIK目標に近づける)
							XMVECTOR lp_nor = XMVector3Normalize(-lp_pos);//平面の法線
							//lp_norとの内積が0になる位置を目標にする
							//2つあるので回転制限後の|内積|が小さいほう
							XMVECTOR tng = XMVector3Cross(XMVectorSet(1, 0, 0, 0), lp_nor);
							//+tngと-tngの2つ
							XMVECTOR rot_axis0, rot_axis1;
							float ang0 = 0, ang1 = 0;

							// 回転軸をXに限定
							rot_axis1 = rot_axis0 = XMVectorSet(1, 0, 0, 0);
							XMVECTOR tdir = XMVector3Normalize(XMVectorSetX(tg_pos, 0));
							tng = XMVector3Normalize(XMVectorSetX(tng, 0));
							RotDir(tdir, tng, ik.control_weight, &rot_axis0, &ang0);
							RotDir(tdir, -tng, ik.control_weight, &rot_axis1, &ang1);
							if (XMVectorGetX(rot_axis0) < 0.0f)ang0 = -ang0;
							if (XMVectorGetX(rot_axis1) < 0.0f)ang1 = -ang1;

							//これは絶対違う ぴくぴく対策
							float coef = (dist_pltg - dist_plik) / dist_tg;
							if (coef > 1)coef = 1;
							ang0 *= coef;
							ang1 *= coef;


							//ang0,1は現在の位置からの相対角度 
							// 回転制限を考慮した相対角度に
							float angx_b = XMVectorGetX(GetAngle(bone_rot_before));
							float angx_a0 = angx_b + ang0;
							float angx_a1 = angx_b + ang1;
							if (angx_a0 < link.mMin.x) angx_a0 = link.mMin.x;
							if (angx_a0 > link.mMax.x) angx_a0 = link.mMax.x;
							if (angx_a1 < link.mMin.x) angx_a1 = link.mMin.x;
							if (angx_a1 > link.mMax.x) angx_a1 = link.mMax.x;
							ang0 = angx_a0 - angx_b;
							ang1 = angx_a1 - angx_b;


							XMVECTOR rot0 = XMQuaternionRotationRollPitchYaw(ang0, 0, 0);
							XMVECTOR rot1 = XMQuaternionRotationRollPitchYaw(ang1, 0, 0);

							XMVECTOR tdir0 = XMVector3TransformCoord(tdir, XMMatrixRotationQuaternion(rot0));
							XMVECTOR tdir1 = XMVector3TransformCoord(tdir, XMMatrixRotationQuaternion(rot1));
							float d0 = XMVectorGetX(XMVectorAbs(XMVector3Dot(tdir0, lp_nor)));
							float d1 = XMVectorGetX(XMVectorAbs(XMVector3Dot(tdir1, lp_nor)));
							if (d0 < d1){
								link_bone.mRot = VectorToFloat(XMQuaternionMultiply(rot0, bone_rot_before));
							}
							else{
								link_bone.mRot = VectorToFloat(XMQuaternionMultiply(rot1, bone_rot_before));
							}
						}
					}

				}




				//ワールド行列更新
				link_bone.mMtxPose = SQTMatrix(FloatToVector(link_bone.mScale), FloatToVector(link_bone.mRot), FloatToVector(link_bone.mPos));
				if (link_bone.mHierarchy.mIdxParent < mBoneNum){
					link_bone.mMtxPose = XMMatrixMultiply(link_bone.mMtxPose, mBone[link_bone.mHierarchy.mIdxParent].mMtxPose);
				}

				// 子階層のリンク再計算
				for (int lidown = chn - 1; lidown >= 0; --lidown){
					UINT idx = ik.child_bone_index[lidown];
					if (idx >= mBoneNum)continue;
					Bone& linkb = mBone[idx];
					linkb.mMtxPose = SQTMatrix(FloatToVector(linkb.mScale), FloatToVector(linkb.mRot), FloatToVector(linkb.mPos));
					if (linkb.mHierarchy.mIdxParent < mBoneNum){
						linkb.mMtxPose = XMMatrixMultiply(linkb.mMtxPose, mBone[linkb.mHierarchy.mIdxParent].mMtxPose);
					}
				}

				mBone[tg_idx].mMtxPose = SQTMatrix(FloatToVector(mBone[tg_idx].mScale), FloatToVector(mBone[tg_idx].mRot), FloatToVector(mBone[tg_idx].mPos));
				if (mBone[tg_idx].mHierarchy.mIdxParent < mBoneNum){
					mBone[tg_idx].mMtxPose = XMMatrixMultiply(mBone[tg_idx].mMtxPose, mBone[mBone[tg_idx].mHierarchy.mIdxParent].mMtxPose);
				}
			}
		}


		//Bone& b = mBone[tg_idx];
		//Bone& b2 = mBone[mBone[tg_idx].mHierarchy.mIdxParent];
		//Bone& b3 = mBone[b2.mHierarchy.mIdxParent];
		//int sa = 1;

		//IKの計算結果を子階層に反映
		//UpdatePose();
	}
	UpdatePose();
}
void		CFVec4::X( FLOAT32 fVal )
{
	XMVECTOR& V4 = *reinterpret_cast<XMVECTOR*>(this);
	V4 = XMVectorSetX( V4, fVal );
}
void		CFVec4::XAdd( FLOAT32 fVal )
{
	XMVECTOR& v4V = *reinterpret_cast<XMVECTOR*>(this);
	
	v4V = XMVectorSetX( v4V, XMVectorGetX( v4V ) + fVal );
}
Beispiel #6
0
void Frustum::CreateFrustum(float screenDepth, XMMATRIX viewMatrix, XMMATRIX projMatrix)
{
	float zMinimum, r;
	XMFLOAT4X4 projMatrixFloat;
	XMFLOAT4X4 frustumMatrix;
	XMStoreFloat4x4(&projMatrixFloat, projMatrix);

	// Calculate the minimum Z distance in the frustum.
	zMinimum = -projMatrixFloat._43 / projMatrixFloat._33;
	r = screenDepth / (screenDepth - zMinimum);
	projMatrixFloat._33 = r;
	projMatrixFloat._43 = -r * zMinimum;

	XMMATRIX updatedProjMatrix = XMLoadFloat4x4(&projMatrixFloat);

	//Create the frustum from the viewMatrix and updated projectionMatrix
	XMStoreFloat4x4(&frustumMatrix, XMMatrixMultiply(viewMatrix, updatedProjMatrix)); ;

	//Calculate near plane of frustum
	XMVectorSetX(this->planes[0], frustumMatrix._14 + frustumMatrix._13);
	XMVectorSetY(this->planes[0], frustumMatrix._24 + frustumMatrix._23);
	XMVectorSetZ(this->planes[0], frustumMatrix._34 + frustumMatrix._33);
	XMVectorSetW(this->planes[0], frustumMatrix._44 + frustumMatrix._43);
	this->planes[0] = XMPlaneNormalize(this->planes[0]);
	//this->planes[0] = XMVector3Normalize(this->planes[0]);

	//Calculate far plane of frustum
	XMVectorSetX(this->planes[1], frustumMatrix._14 - frustumMatrix._13);
	XMVectorSetY(this->planes[1], frustumMatrix._24 - frustumMatrix._23);
	XMVectorSetZ(this->planes[1], frustumMatrix._34 - frustumMatrix._33);
	XMVectorSetW(this->planes[1], frustumMatrix._44 - frustumMatrix._43);
	this->planes[1] = XMPlaneNormalize(this->planes[1]);
	//this->planes[1] = XMVector3Normalize(this->planes[1]);

	//Calculate left plane of frustum
	XMVectorSetX(this->planes[2], frustumMatrix._14 + frustumMatrix._11);
	XMVectorSetY(this->planes[2], frustumMatrix._24 + frustumMatrix._21);
	XMVectorSetZ(this->planes[2], frustumMatrix._34 + frustumMatrix._31);
	XMVectorSetW(this->planes[2], frustumMatrix._44 + frustumMatrix._41);
	this->planes[2] = XMPlaneNormalize(this->planes[2]);
	//this->planes[2] = XMVector3Normalize(this->planes[2]);

	//Calculate right plane of frustum
	XMVectorSetX(this->planes[3], frustumMatrix._14 - frustumMatrix._11);
	XMVectorSetY(this->planes[3], frustumMatrix._24 - frustumMatrix._21);
	XMVectorSetZ(this->planes[3], frustumMatrix._34 - frustumMatrix._31);
	XMVectorSetW(this->planes[3], frustumMatrix._44 - frustumMatrix._41);
	this->planes[3] = XMPlaneNormalize(this->planes[3]);
	//this->planes[3] = XMVector3Normalize(this->planes[3]);

	//Calculate top plane of frustum
	XMVectorSetX(this->planes[4], frustumMatrix._14 - frustumMatrix._12);
	XMVectorSetY(this->planes[4], frustumMatrix._24 - frustumMatrix._22);
	XMVectorSetZ(this->planes[4], frustumMatrix._34 - frustumMatrix._32);
	XMVectorSetW(this->planes[4], frustumMatrix._44 - frustumMatrix._42);
	this->planes[4] = XMPlaneNormalize(this->planes[4]);
	//this->planes[4] = XMVector3Normalize(this->planes[4]);

	//Calculate bottom plane of frustum
	XMVectorSetX(this->planes[5], frustumMatrix._14 + frustumMatrix._12);
	XMVectorSetY(this->planes[5], frustumMatrix._24 + frustumMatrix._22);
	XMVectorSetZ(this->planes[5], frustumMatrix._34 + frustumMatrix._32);
	XMVectorSetW(this->planes[5], frustumMatrix._44 + frustumMatrix._42);
	this->planes[5] = XMPlaneNormalize(this->planes[5]);
	//this->planes[5] = XMVector3Normalize(this->planes[5]);

	/*for (int i = 0; i < 6; i++) {
		float denom = 1.0f / XMVectorGetX(XMVector3Length(this->planes[i]));
		XMVectorSetX(this->planes[i], XMVectorGetX(this->planes[i]) * denom);
		XMVectorSetY(this->planes[i], XMVectorGetY(this->planes[i]) * denom);
		XMVectorSetZ(this->planes[i], XMVectorGetZ(this->planes[i]) * denom);
		XMVectorSetW(this->planes[i], XMVectorGetW(this->planes[i]) * denom);
	}*/

	return;
}