void BoneModel::createBone(){ auto& bones = mBoneAssetDataPtr->GetFileData().GetBoneData().mBoneBuffer; auto& boneName = mBoneAssetDataPtr->GetFileData().GetBoneData().mBoneName; DWORD mBoneNum = bones.size(); mBone.clear(); mIk.clear(); mBone.resize(mBoneNum); DWORD ikCount = 0; for (DWORD i = 0; i < mBoneNum; i++){ auto& bone = bones[i]; mBone[i].mStrName = boneName[i]; mBone[i].mHierarchy.mIdxSelf = i; mBone[i].mHierarchy.mIdxParent = bone.parent_bidx; if (bone.parent_bidx >= (int)mBoneNum) mBone[i].mHierarchy.mIdxParent = UINT(-1); XMVECTOR head_pos = XMVectorSet(bone.bone_head_pos[0], bone.bone_head_pos[1], bone.bone_head_pos[2], 0.0f); XMVECTOR parent_pos = { 0, 0, 0, 1 }; if (mBone[i].mHierarchy.mIdxParent < (int)mBoneNum){ UINT p = mBone[i].mHierarchy.mIdxParent; parent_pos = XMVectorSet(bones[p].bone_head_pos[0], bones[p].bone_head_pos[1], bones[p].bone_head_pos[2], 0.0f); } XMVECTOR local_pos = XMVectorSubtract(head_pos, parent_pos); mBone[i].mPos = XMFLOAT3(XMVectorGetX(local_pos), XMVectorGetY(local_pos), XMVectorGetZ(local_pos)); mBone[i].mScale = XMFLOAT3(1.0f, 1.0f, 1.0f); XMVECTOR q = XMQuaternionIdentity(); mBone[i].mRot = XMFLOAT4(XMVectorGetX(q), XMVectorGetY(q), XMVectorGetZ(q), XMVectorGetW(q)); //ワールド行列計算 XMVECTOR scale = { 1, 1, 1, 1 }; mBone[i].mMtxPose = SRTMatrix(scale, q, local_pos); if (mBone[i].mHierarchy.mIdxParent < (int)mBoneNum){ mBone[i].mMtxPose = XMMatrixMultiply(mBone[i].mMtxPose, mBone[mBone[i].mHierarchy.mIdxParent].mMtxPose); } if (bone.bone_flag & pmx::t_bone::BIT_IK){ mBone[i].mIkBoneIdx = (WORD)bone.t_ik_data_idx; createIk(ikCount, i); ikCount++; } else{ mBone[i].mIkBoneIdx = 0; } mBone[i].mMtxPoseInit = mBone[i].mMtxPose; } }
//This is the sphere test from the DX SDK "Touch" sample. Pretty elegant imo. bool CPUTModel_CPRT::IntersectionTest(const XMVECTOR &rayOrigin, const XMVECTOR &rayDirection, XMVECTOR &modelPosition, BoundingBox &box) { modelPosition = XMVectorSet ( m_pTransform.m_pData[0][3] , m_pTransform.m_pData[1][3] , m_pTransform.m_pData[2][3] , m_pTransform.m_pData[3][3] ); //TODO: may have to change this for(unsigned int i = 0; i < boxes.size(); ++i) { box = boxes.at(i); XMVECTOR rayToSphere = modelPosition - rayOrigin; //Basic idea is to compare the projection of the casted ray on the ray from the camera position to the sphere position. //If the the casted ray is sufficiently close to the rayToSphere then box.GetSphereRadiusSquared() - lengthOfDisplacementSquared will be > 0 float pointingTheRightWayMeasure = XMVectorGetW( XMVector3Dot( rayToSphere , rayDirection ) ); float lengthOfDisplacementSquared = XMVectorGetW( XMVector3Dot( rayToSphere , rayToSphere ) ); float magic = pointingTheRightWayMeasure*pointingTheRightWayMeasure - lengthOfDisplacementSquared + box.GetSphereRadiusSquared(); selected = ( magic > 0 ) ? true : false ; if(selected) return selected; } return selected; }
bool Frustum::IntersectBB(XMVECTOR* boundingBox) { XMVECTOR* p1; XMVECTOR* p2; bool pointInside = false; int insideAll = 0; for (int i = 0; i < 8; i++) { //Loop through all the corner points for bounding box for (int j = 0; j < 6; j++) { //Check if any point is inside all frustum planes if (XMVectorGetX(XMPlaneDotCoord(this->planes[i], boundingBox[j])) + XMVectorGetW(this->planes[i]) >= 0.0f) { insideAll++; } /*if (XMVectorGetX(XMVector3Dot(this->planes[i], boundingBox[j])) + XMVectorGetW(this->planes[i]) >= 0.0f) { insideAll++; }*/ } if (insideAll == 6) { //If any point is inside frustum -> return intersection return true; } insideAll = 0; } return false; }
static inline float DistToPlane(_In_ const XMVECTOR& plane, _In_ const XMVECTOR& position) { // dot replicates answer to all 4 components. Subtract plane means that the D value (in w) will be // subtracted from the w of the dot. Finally, we then extract the w. return XMVectorGetW(XMVectorSubtract(XMVector3Dot(plane, position), plane)); }
XMFLOAT4 VectorToFloat(const XMVECTOR& vec){ return XMFLOAT4(XMVectorGetX(vec), XMVectorGetY(vec), XMVectorGetZ(vec), XMVectorGetW(vec)); }
// とりあえずアニメーション void BoneModel::VMDAnimation(float key_time) { if (!mBoneAssetDataPtr)return; if (mMotion.empty())return; auto& bones = mBoneAssetDataPtr->GetFileData().GetBoneData().mBoneBuffer; if (bones.empty())return; for (DWORD mid = 0; mid < bones.size(); mid++){ Motion& mot = mMotion[mid]; if (mot.mKeyFrame.size() == 0)continue; UINT idx = 0; for (auto key : mot.mKeyFrame){ if (key.mKeyFrame.FrameNo >= key_time){ break; } ++idx; } if (idx >= mot.mKeyFrame.size())idx = mot.mKeyFrame.size() - 1; UINT prev = idx; if (idx>0)prev = idx - 1; vmd::VMDKeyFrame& key0 = mot.mKeyFrame[prev].mKeyFrame; vmd::VMDKeyFrame& key1 = mot.mKeyFrame[idx].mKeyFrame; float t = 1.0f; if (idx != prev){ t = float(key_time - key0.FrameNo) / float(key1.FrameNo - key0.FrameNo); } if (t < 0.0f)t = 0.0f; if (t > 1.0f)t = 1.0f; { XMVECTOR pos0 = XMVectorSet(key0.Location[0], key0.Location[1], key0.Location[2], 0.0f); XMVECTOR pos1 = XMVectorSet(key1.Location[0], key1.Location[1], key1.Location[2], 0.0f); XMVECTOR rot0 = XMVectorSet(key0.Rotatation[0], key0.Rotatation[1], key0.Rotatation[2], key0.Rotatation[3]); XMVECTOR rot1 = XMVectorSet(key1.Rotatation[0], key1.Rotatation[1], key1.Rotatation[2], key1.Rotatation[3]); float tx = Bezier(key0.Interpolation[0], key0.Interpolation[8], key0.Interpolation[4], key0.Interpolation[12], t); float ty = Bezier(key0.Interpolation[1], key0.Interpolation[9], key0.Interpolation[5], key0.Interpolation[13], t); float tz = Bezier(key0.Interpolation[2], key0.Interpolation[10], key0.Interpolation[6], key0.Interpolation[14], t); float tr = Bezier(key0.Interpolation[3], key0.Interpolation[11], key0.Interpolation[7], key0.Interpolation[15], t); XMVECTOR v = XMVectorLerpV(pos0, pos1, XMVectorSet(tx, ty, tz, 0.0f)); mBone[mid].mPos = XMFLOAT3(XMVectorGetX(v), XMVectorGetY(v), XMVectorGetZ(v)); XMVECTOR q = XMQuaternionSlerp(rot0, rot1, tr); mBone[mid].mRot = XMFLOAT4(XMVectorGetX(q), XMVectorGetY(q), XMVectorGetZ(q), XMVectorGetW(q)); ////ワールド行列計算 //XMVECTOR scale = { 1, 1, 1, 1 }; //mBone[mid].mMtxPose = SRTMatrix(scale, q, v); //if (mBone[mid].mHierarchy.mIdxParent < mBoneNum){ // mBone[mid].mMtxPose = XMMatrixMultiply(mBone[mid].mMtxPose, mBone[mBone[mid].mHierarchy.mIdxParent].mMtxPose); //} } } UpdatePose(); VMDIkAnimation(); }
void CFVec4::WAdd( FLOAT32 fVal ) { XMVECTOR& v4V = *reinterpret_cast<XMVECTOR*>(this); v4V = XMVectorSetW( v4V, XMVectorGetW( v4V ) + fVal ); }
FLOAT32 CFVec4::W( void ) const { const XMVECTOR& V4 = *reinterpret_cast<const XMVECTOR*>(this); return XMVectorGetW( V4 ); }
IActor *CreateObject(XMVECTOR const &xvPos, XMCOLOR const &Color, E_ACTOR_TYPE const eActorType, E_RIGID_BODY_FLAG const eBodyFlag) { const float fBoxSize = 0.4f; if (!g_pWireBoxModelRenderer) { g_pWireBoxModelRenderer = CDistributedObjectCreator::GetInstance()->CreateModelRenderer(); g_pWireBoxModelRenderer->SetRenderMethod(E_RENDERMETHOD::WIREFRAMED); g_pWireBoxModelRenderer->SetRigidBodyFlag(E_RIGID_BODY_FLAG::DYNAMIC); CRawModel RawModel; RawModel.m_eRidigBodyFlag = E_RIGID_BODY_FLAG::DYNAMIC; RawModel.m_vScale = XMFLOAT3(fBoxSize, fBoxSize, fBoxSize); ContructBoxVertexBuffer(&RawModel); for (auto iMesh : RawModel.m_RawMeshes) g_pWireBoxModelRenderer->VAddMesh(iMesh, iMesh); } if (!g_pSolidBoxModelRenderer) { g_pSolidBoxModelRenderer = CDistributedObjectCreator::GetInstance()->CreateModelRenderer(); g_pSolidBoxModelRenderer->SetRenderMethod(E_RENDERMETHOD::SOLID); g_pSolidBoxModelRenderer->SetRigidBodyFlag(E_RIGID_BODY_FLAG::DYNAMIC); CRawModel RawModel; RawModel.m_eRidigBodyFlag = E_RIGID_BODY_FLAG::DYNAMIC; RawModel.m_vScale = XMFLOAT3(fBoxSize, fBoxSize, fBoxSize); ContructBoxVertexBuffer(&RawModel); for (auto iMesh : RawModel.m_RawMeshes) g_pSolidBoxModelRenderer->VAddMesh(iMesh, iMesh); } if (!g_pWireSphereModelRenderer) { g_pWireSphereModelRenderer = CDistributedObjectCreator::GetInstance()->CreateModelRenderer(); g_pWireSphereModelRenderer->SetRenderMethod(E_RENDERMETHOD::WIREFRAMED); g_pWireSphereModelRenderer->SetRigidBodyFlag(E_RIGID_BODY_FLAG::DYNAMIC); CRawModel RawModel; RawModel.m_eRidigBodyFlag = E_RIGID_BODY_FLAG::DYNAMIC; RawModel.m_vScale = XMFLOAT3(fBoxSize, fBoxSize, fBoxSize); ContructSphereVertexBuffer(&RawModel); for (auto iMesh : RawModel.m_RawMeshes) g_pWireSphereModelRenderer->VAddMesh(iMesh, iMesh); } if (!g_pSolidSphereModelRenderer) { g_pSolidSphereModelRenderer = CDistributedObjectCreator::GetInstance()->CreateModelRenderer(); g_pSolidSphereModelRenderer->SetRenderMethod(E_RENDERMETHOD::SOLID); g_pSolidSphereModelRenderer->SetRigidBodyFlag(E_RIGID_BODY_FLAG::DYNAMIC); CRawModel RawModel; RawModel.m_eRidigBodyFlag = E_RIGID_BODY_FLAG::DYNAMIC; RawModel.m_vScale = XMFLOAT3(fBoxSize, fBoxSize, fBoxSize); ContructSphereVertexBuffer(&RawModel); for (auto iMesh : RawModel.m_RawMeshes) g_pSolidSphereModelRenderer->VAddMesh(iMesh, iMesh); } if (!g_pWirePlaneModelRenderer) { g_pWirePlaneModelRenderer = CDistributedObjectCreator::GetInstance()->CreateModelRenderer(); g_pWirePlaneModelRenderer->SetRenderMethod(E_RENDERMETHOD::WIREFRAMED); CRawModel RawModel; RawModel.m_vScale = XMFLOAT3(500.0f, 500.0f, 500.0f); ContructRegularGrid(XMINT2(5, 5), &RawModel); for (auto iMesh : RawModel.m_RawMeshes) g_pWirePlaneModelRenderer->VAddMesh(iMesh, iMesh); } if (!g_pSolidPlaneModelRenderer) { g_pSolidPlaneModelRenderer = CDistributedObjectCreator::GetInstance()->CreateModelRenderer(); g_pSolidPlaneModelRenderer->SetRenderMethod(E_RENDERMETHOD::SOLID); CRawModel RawModel; RawModel.m_vScale = XMFLOAT3(500.0f, 500.0f, 500.0f); ContructRegularGrid(XMINT2(5, 5), &RawModel); for (auto iMesh : RawModel.m_RawMeshes) g_pSolidPlaneModelRenderer->VAddMesh(iMesh, iMesh); } IActor *pActor = nullptr; //CObject *pObject; switch (eActorType) { case E_ACTOR_TYPE::CUBE: { // g_pWireBoxModelRenderer g_pSolidBoxModelRenderer pActor = new CBox(xvPos - XMVectorReplicate(fBoxSize / 2), xvPos + XMVectorReplicate(fBoxSize / 2), g_pWireBoxModelRenderer); } break; case E_ACTOR_TYPE::SPHERE: { XMFLOAT3 vPos; XMStoreFloat3(&vPos, xvPos); pActor = new CSphere(vPos, 1.0f, g_pWireSphereModelRenderer); } break; case E_ACTOR_TYPE::PLANE: pActor = new CPlane(g_pSolidPlaneModelRenderer); ((CPlane *)pActor)->GetPlane().x = XMVectorGetX(xvPos); ((CPlane *)pActor)->GetPlane().y = XMVectorGetY(xvPos); ((CPlane *)pActor)->GetPlane().z = XMVectorGetZ(xvPos); ((CPlane *)pActor)->GetPlane().w = XMVectorGetW(xvPos); break; } pActor->VInit(); for (auto iObject : pActor->GetObjects()) { iObject->SetColor(Color); } return pActor; }
FLOAT Light::intensity() const { XMVECTOR v = colorv(); return XMVectorGetW(v); }