302 lines
6.6 KiB
C++
302 lines
6.6 KiB
C++
#include "pch.h"
|
|
#include "Model.h"
|
|
#include "Mesh.h"
|
|
#include "Shader.h"
|
|
#include "Material.h"
|
|
#include "Bone.h"
|
|
#include "Animation.h"
|
|
|
|
CModel::CModel(ID3D11Device* pDevice, ID3D11DeviceContext* pContext)
|
|
: CComponent( pDevice, pContext )
|
|
{
|
|
}
|
|
|
|
CModel::CModel(const CModel& Prototype)
|
|
: CComponent( Prototype )
|
|
, m_eType{ Prototype.m_eType }
|
|
, m_iNumMeshes{ Prototype.m_iNumMeshes }
|
|
, m_Meshes{ Prototype.m_Meshes }
|
|
, m_iNumMaterials{ Prototype.m_iNumMaterials }
|
|
, m_Materials{ Prototype.m_Materials }
|
|
, m_PreTransformMatrix{ Prototype.m_PreTransformMatrix }
|
|
, m_iNumAnimations{ Prototype.m_iNumAnimations }
|
|
{
|
|
for (auto& pPrototypeBone : Prototype.m_Bones)
|
|
m_Bones.push_back(pPrototypeBone->Clone());
|
|
|
|
for (auto& pMesh : m_Meshes)
|
|
Safe_AddRef(pMesh);
|
|
|
|
for (auto& pMaterial : m_Materials)
|
|
Safe_AddRef(pMaterial);
|
|
|
|
for (auto& pPrototypeAnim : Prototype.m_Animations)
|
|
m_Animations.push_back(pPrototypeAnim->Clone());
|
|
}
|
|
|
|
_int CModel::Get_BoneIndex(const _char* pBoneName) const
|
|
{
|
|
_int iBoneIndex = {};
|
|
|
|
auto iter = find_if(m_Bones.begin(), m_Bones.end(), [&](CBone* pBone)->_bool
|
|
{
|
|
if (true == pBone->Compare_Name(pBoneName))
|
|
return true;
|
|
|
|
++iBoneIndex;
|
|
|
|
return false;
|
|
});
|
|
|
|
if (iter == m_Bones.end())
|
|
return -1;
|
|
|
|
return iBoneIndex;
|
|
}
|
|
|
|
const _float4x4* CModel::Get_BoneMatrixPtr(const _char* pBoneName) const
|
|
{
|
|
auto iter = find_if(m_Bones.begin(), m_Bones.end(), [&](CBone* pBone)->_bool
|
|
{
|
|
if (true == pBone->Compare_Name(pBoneName))
|
|
return true;
|
|
|
|
return false;
|
|
});
|
|
|
|
|
|
return (*iter)->Get_CombinedTransformationMatrixPtr();
|
|
}
|
|
|
|
HRESULT CModel::Initialize_Prototype(MODEL eType, const _char* pModelFilePath, _fmatrix PreTransformMatrix)
|
|
{
|
|
_uint iFlag = {};
|
|
|
|
iFlag = aiProcess_ConvertToLeftHanded | aiProcessPreset_TargetRealtime_Fast;
|
|
|
|
if (MODEL::NONANIM == eType)
|
|
iFlag |= aiProcess_PreTransformVertices;
|
|
|
|
m_pAIScene = m_Importer.ReadFile(pModelFilePath, iFlag);
|
|
if (nullptr == m_pAIScene)
|
|
return E_FAIL;
|
|
|
|
m_eType = eType;
|
|
XMStoreFloat4x4(&m_PreTransformMatrix, PreTransformMatrix);
|
|
|
|
Ready_Bones(m_pAIScene->mRootNode, -1);
|
|
|
|
if (FAILED(Ready_Meshes()))
|
|
return E_FAIL;
|
|
|
|
if (FAILED(Ready_Materials(pModelFilePath)))
|
|
return E_FAIL;
|
|
|
|
if (FAILED(Ready_Animations()))
|
|
return E_FAIL;
|
|
|
|
return S_OK;
|
|
}
|
|
|
|
HRESULT CModel::Initialize(void* pArg)
|
|
{
|
|
return S_OK;
|
|
}
|
|
|
|
HRESULT CModel::Bind_BoneMatrices(_uint iMeshIndex, CShader* pShader, const _char* pConstantName)
|
|
{
|
|
if (iMeshIndex >= m_iNumMeshes)
|
|
return E_FAIL;
|
|
|
|
return m_Meshes[iMeshIndex]->Bind_BoneMatrices(m_Bones, pShader, pConstantName);
|
|
}
|
|
|
|
HRESULT CModel::Bind_Material(_uint iMeshIndex, CShader* pShader, const _char* pConstantName, aiTextureType eType, _uint iTextureIndex)
|
|
{
|
|
if (iMeshIndex >= m_iNumMeshes)
|
|
return E_FAIL;
|
|
|
|
_uint iMaterialIndex = m_Meshes[iMeshIndex]->Get_MaterialIndex();
|
|
|
|
if (iMaterialIndex >= m_iNumMaterials)
|
|
return E_FAIL;
|
|
|
|
return m_Materials[iMaterialIndex]->Bind_SRV(pShader, pConstantName, eType, iTextureIndex);
|
|
|
|
}
|
|
|
|
_bool CModel::Play_Animation(_float fTimeDelta)
|
|
{
|
|
if (-1 == m_iCurrentAnimIndex ||
|
|
m_iCurrentAnimIndex >= m_iNumAnimations)
|
|
return false;
|
|
|
|
if (m_iPreAnimIndex != m_iCurrentAnimIndex)
|
|
{
|
|
m_fBlendTime += fTimeDelta;
|
|
_float fRatio = m_fBlendTime / m_fBlendDuration;
|
|
fRatio = min(fRatio, 1.f);
|
|
|
|
m_Animations[m_iPreAnimIndex]->Update_TransformationMatrices(fTimeDelta, m_isLoop, m_Bones);
|
|
m_isFinish = m_Animations[m_iCurrentAnimIndex]->Update_TransformationMatrices(fTimeDelta, m_isLoop, m_Bones);
|
|
m_Animations[m_iCurrentAnimIndex]->InterpAnim(m_Animations[m_iPreAnimIndex], m_Bones, fRatio);
|
|
|
|
if (fRatio >= 1.f)
|
|
{
|
|
m_iPreAnimIndex = m_iCurrentAnimIndex;
|
|
m_fBlendTime = 0.f;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
m_isFinish = m_Animations[m_iCurrentAnimIndex]->Update_TransformationMatrices(fTimeDelta, m_isLoop, m_Bones);
|
|
}
|
|
|
|
|
|
for (auto& pBone : m_Bones)
|
|
{
|
|
pBone->Update_CombinedTransformationMatrix(m_Bones, XMLoadFloat4x4(&m_PreTransformMatrix));
|
|
}
|
|
|
|
return m_isFinish;
|
|
}
|
|
|
|
void CModel::Set_AnimationIndex(_int iIndex, _bool isLoop)
|
|
{
|
|
if (iIndex >= 0 && iIndex < m_iNumAnimations)
|
|
{
|
|
m_iCurrentAnimIndex = iIndex;
|
|
m_isLoop = isLoop;
|
|
}
|
|
else
|
|
m_iCurrentAnimIndex = -1;
|
|
}
|
|
|
|
HRESULT CModel::Render(_uint iMeshIndex)
|
|
{
|
|
m_Meshes[iMeshIndex]->Bind_Resources();
|
|
m_Meshes[iMeshIndex]->Render();
|
|
|
|
return S_OK;
|
|
}
|
|
|
|
HRESULT CModel::Ready_Meshes()
|
|
{
|
|
m_iNumMeshes = m_pAIScene->mNumMeshes;
|
|
|
|
for (size_t i = 0; i < m_iNumMeshes; i++)
|
|
{
|
|
CMesh* pMesh = CMesh::Create(m_pDevice, m_pContext, m_eType, this, m_pAIScene->mMeshes[i], XMLoadFloat4x4(&m_PreTransformMatrix));
|
|
if (nullptr == pMesh)
|
|
return E_FAIL;
|
|
|
|
m_Meshes.push_back(pMesh);
|
|
}
|
|
|
|
return S_OK;
|
|
}
|
|
|
|
HRESULT CModel::Ready_Materials(const _char* pModelFilePath)
|
|
{
|
|
m_iNumMaterials = m_pAIScene->mNumMaterials;
|
|
|
|
for (size_t i = 0; i < m_iNumMaterials; i++)
|
|
{
|
|
CMaterial* pMaterial = CMaterial::Create(m_pDevice, m_pContext, pModelFilePath, m_pAIScene->mMaterials[i]);
|
|
if (nullptr == pMaterial)
|
|
return E_FAIL;
|
|
|
|
m_Materials.push_back(pMaterial);
|
|
}
|
|
|
|
return S_OK;
|
|
}
|
|
|
|
HRESULT CModel::Ready_Bones(const aiNode* pAINode, _int iParentIndex)
|
|
{
|
|
CBone* pBone = CBone::Create(pAINode, iParentIndex);
|
|
if (nullptr == pBone)
|
|
return E_FAIL;
|
|
|
|
m_Bones.push_back(pBone);
|
|
|
|
_int iParent = (_int)m_Bones.size() - 1;
|
|
|
|
for (_uint i = 0; i < pAINode->mNumChildren; i++)
|
|
{
|
|
Ready_Bones(pAINode->mChildren[i], iParent);
|
|
}
|
|
|
|
return S_OK;
|
|
}
|
|
|
|
HRESULT CModel::Ready_Animations()
|
|
{
|
|
m_iNumAnimations = m_pAIScene->mNumAnimations;
|
|
|
|
for (size_t i = 0; i < m_iNumAnimations; i++)
|
|
{
|
|
CAnimation* pAnimation = CAnimation::Create(this, m_pAIScene->mAnimations[i]);
|
|
if (nullptr == pAnimation)
|
|
return E_FAIL;
|
|
|
|
m_Animations.push_back(pAnimation);
|
|
}
|
|
|
|
return S_OK;
|
|
}
|
|
|
|
CModel* CModel::Create(ID3D11Device* pDevice, ID3D11DeviceContext* pContext, MODEL eType, const _char* pModelFilePath, _fmatrix PreTransformMatrix)
|
|
{
|
|
CModel* pInstance = new CModel(pDevice, pContext);
|
|
|
|
if (FAILED(pInstance->Initialize_Prototype(eType, pModelFilePath, PreTransformMatrix)))
|
|
{
|
|
MSG_BOX("Failed to Created : CModel");
|
|
Safe_Release(pInstance);
|
|
}
|
|
|
|
return pInstance;
|
|
}
|
|
|
|
CComponent* CModel::Clone(void* pArg, CGameObject* pOwner)
|
|
{
|
|
CModel* pInstance = new CModel(*this);
|
|
|
|
if (FAILED(pInstance->Initialize(pArg)))
|
|
{
|
|
MSG_BOX("Failed to Cloned : CModel");
|
|
Safe_Release(pInstance);
|
|
}
|
|
|
|
return pInstance;
|
|
}
|
|
|
|
|
|
void CModel::Free()
|
|
{
|
|
__super::Free();
|
|
|
|
for (auto& pMaterial : m_Materials)
|
|
Safe_Release(pMaterial);
|
|
m_Materials.clear();
|
|
|
|
for (auto& pMesh : m_Meshes)
|
|
Safe_Release(pMesh);
|
|
m_Meshes.clear();
|
|
|
|
for (auto& pBone : m_Bones)
|
|
Safe_Release(pBone);
|
|
m_Bones.clear();
|
|
|
|
for (auto& pAnim : m_Animations)
|
|
Safe_Release(pAnim);
|
|
m_Animations.clear();
|
|
|
|
m_Importer.FreeScene();
|
|
}
|
|
|
|
void CModel::Describe_Entity()
|
|
{
|
|
}
|