Hi Folks,
I don't get any animations out of this code, the model is confirmed to be animated in mesh viewer.
shader
////////////////////////////////////////////////////////////////////////////
extern float4x4 FinalTransforms[100];
extern int NumVertInfluences = 2; // <--- Normally set dynamically.
//Vertex Input
struct VS_INPUT_SKIN
{
float4 position : POSITION0;
float3 normal : NORMAL;
float2 tex0 : TEXCOORD0;
float4 weights : BLENDWEIGHT0;
int4 boneIndices : BLENDINDICES0;
};
VS_OUTPUT vs_Skinning(VS_INPUT_SKIN IN)
{
VS_OUTPUT OUT = (VS_OUTPUT)0;
float4 p = float4(0.0f, 0.0f, 0.0f, 1.0f);
float3 norm = float3(0.0f, 0.0f, 0.0f);
float lastWeight = 0.0f;
int n = NumVertInfluences-1;
norm = normalize(IN.normal);
for(int i = 0; i < n; ++i)
{
lastWeight += IN.weights;
p += IN.weights * mul(IN.position, FinalTransforms[IN.boneIndices]);
norm += IN.weights * mul(IN.normal, FinalTransforms[IN.boneIndices]);
}
lastWeight = 1.0f - lastWeight;
p += lastWeight * mul(IN.position, FinalTransforms[IN.boneIndices[n]]);
norm += lastWeight * mul(IN.normal, FinalTransforms[IN.boneIndices[n]]);
p.w = 1.0f;
float4 posWorld = mul(p, matW);
OUT.position = mul(posWorld, matVP);
OUT.tex0 = IN.tex0;
//Calculate Lighting
norm = normalize(norm);
norm = mul(norm, matW);
OUT.shade = max(dot(norm, normalize(lightPos - posWorld)), 0.2f);
return OUT;
}
main logics
if( SUCCEEDED(beginScene()) )
{
D3DXMATRIXA16 mViewProj = D3DXMATRIXA16((float*)&ViewMatrix()) * D3DXMATRIXA16((float*)&ProjMatrix());
// DX10 spec only guarantees Sincos function from -100 * Pi to 100 * Pi
float fBoundedTime = (float) m_fTime - (floor( (float) m_fTime / (2.0f * D3DX_PI)) * 2.0f * D3DX_PI);
//m_pConstantTable->SetMatrix( g_pDevice, "mWorldViewProj", &mWorldViewProj );
//m_pConstantTable->SetFloat( g_pDevice, "fTime", fBoundedTime );
g_pEffect->SetMatrix("matVP", &mViewProj);
g_pEffect->SetVector("lightPos", &D3DXVECTOR4(0.0f, -1.0f, 0.0f, 0.0f));
//g_pEffect->SetFloat("fTime", fBoundedTime);
for (int i = 0; i < m_vRenderObjects.size(); i++)
{
D3DXMATRIX mat = m_vRenderObjects->GetWorldMatrix();
m_vRenderObjects->GetMesh()->Update(mat, fBoundedTime);
m_vRenderObjects->GetMesh()->Draw();
}
endScene();
}
SkinnedMesh.cpp
void SkinnedMesh::Update(const D3DXMATRIX& mat, float dt)
{
D3DXMATRIX m(mat);
if (m_pAnimCtrl)
{
m_pAnimCtrl->AdvanceTime(dt, NULL);
}
this->UpdateMatrices((D3DXFRAME_DERIVED*)m_pRoot, &m);
}
void SkinnedMesh::Draw()
{
Render(NULL);
}
void SkinnedMesh::UpdateMatrices(D3DXFRAME_DERIVED* f, D3DXMATRIX* parentMatrix)
{
if(f == NULL)return;
D3DXMatrixMultiply(&f->CombinedTransformationMatrix,
&f->TransformationMatrix,
parentMatrix);
if(f->pFrameSibling != NULL)
{
UpdateMatrices((D3DXFRAME_DERIVED*)f->pFrameSibling, parentMatrix);
}
if(f->pFrameFirstChild != NULL)
{
UpdateMatrices((D3DXFRAME_DERIVED*)f->pFrameFirstChild, &f->CombinedTransformationMatrix);
}
}
void SkinnedMesh::SetupBoneMatrixPointers(D3DXFRAME_DERIVED *bone)
{
if(bone->pMeshContainer != NULL)
{
D3DXMESHCONTAINER_DERIVED *boneMesh = (D3DXMESHCONTAINER_DERIVED*)bone->pMeshContainer;
if(boneMesh->pSkinInfo != NULL)
{
int NumBones = boneMesh->pSkinInfo->GetNumBones();
boneMesh->ppBoneMatrixPtrs = new D3DXMATRIX*[NumBones];
for(int i=0;i < NumBones;i++)
{
D3DXFRAME_DERIVED *b = (D3DXFRAME_DERIVED*)D3DXFrameFind(m_pRoot, boneMesh->pSkinInfo->GetBoneName(i));
if(b != NULL)boneMesh->ppBoneMatrixPtrs = &b->CombinedTransformationMatrix;
else boneMesh->ppBoneMatrixPtrs = NULL;
}
}
}
if(bone->pFrameSibling != NULL)SetupBoneMatrixPointers((D3DXFRAME_DERIVED*)bone->pFrameSibling);
if(bone->pFrameFirstChild != NULL)SetupBoneMatrixPointers((D3DXFRAME_DERIVED*)bone->pFrameFirstChild);
}
void SkinnedMesh::Render(D3DXFRAME_DERIVED *f)
{
if(f == NULL)f = (D3DXFRAME_DERIVED*)m_pRoot;
//If there is a mesh to render...
if(f->pMeshContainer != NULL)
{
D3DXMESHCONTAINER_DERIVED *boneMesh = (D3DXMESHCONTAINER_DERIVED*)f->pMeshContainer;
if (boneMesh->pSkinInfo != NULL)
{
// set up bone transforms
int numBones = boneMesh->pSkinInfo->GetNumBones();
for(int i=0;i < numBones;i++)
{
D3DXMatrixMultiply(&m_pBoneMatrices,
&boneMesh->pBoneOffsetMatrices,
boneMesh->ppBoneMatrixPtrs);
}
D3DXMATRIX view, proj, identity;
g_pEffect->SetMatrixArray("FinalTransforms", m_pBoneMatrices, boneMesh->pSkinInfo->GetNumBones());
//Render the mesh
for(int i=0;i < (int)boneMesh->NumMaterials; i++)//>NumAttributeGroups;i++)
{
//int mtrlIndex = boneMesh->attributeTable.AttribId;
//g_pDevice->SetMaterial(&(boneMesh->materials[mtrlIndex]));
//g_pDevice->SetTexture(0, boneMesh->textures[mtrlIndex]);
g_pDevice->SetMaterial(&(boneMesh->pMaterials).MatD3D);
g_pDevice->SetTexture(0, boneMesh->ppTextures);
g_pEffect->SetMatrix("matW", &f->CombinedTransformationMatrix);
g_pEffect->SetVector( "materialColor",
( D3DXVECTOR4* )&(
boneMesh->pMaterials.MatD3D.Diffuse ) );
if (boneMesh->ppTextures != NULL)
g_pEffect->SetTexture("texDiffuse", boneMesh->ppTextures);
D3DXHANDLE hTech = g_pEffect->GetTechniqueByName("Skinning");
g_pEffect->SetTechnique(hTech);
g_pEffect->Begin(NULL, NULL);
g_pEffect->BeginPass(0);
boneMesh->MeshData.pMesh->DrawSubset(i);
g_pEffect->EndPass();
g_pEffect->End();
}
}
else
{
//Normal Static Mesh
g_pEffect->SetMatrix("matW", &f->CombinedTransformationMatrix);
D3DXHANDLE hTech = g_pEffect->GetTechniqueByName("Lighting");
g_pEffect->SetTechnique(hTech);
//Render the mesh
for(int i=0;i < boneMesh->NumMaterials ;i++)
{
g_pDevice->SetMaterial(&boneMesh->pMaterials.MatD3D);
g_pEffect->SetVector( "materialColor",
( D3DXVECTOR4* )&(
boneMesh->pMaterials.MatD3D.Diffuse ) );
if (boneMesh->ppTextures != NULL)
g_pEffect->SetTexture("texDiffuse", boneMesh->ppTextures);
g_pEffect->Begin(NULL, NULL);
g_pEffect->BeginPass(0);
boneMesh->MeshData.pMesh->DrawSubset(i);
g_pEffect->EndPass();
g_pEffect->End();
}
}
}
if(f->pFrameSibling != NULL)
{
Render((D3DXFRAME_DERIVED*)f->pFrameSibling);
}
if(f->pFrameFirstChild != NULL)
{
Render((D3DXFRAME_DERIVED*)f->pFrameFirstChild);
}
}
void SkinnedMesh::SetAnimation(std::string name)
{
ID3DXAnimationSet *anim = NULL;
int numAnims = (int)m_pAnimCtrl->GetMaxNumAnimationSets();
for(int i=0;i<numAnims;i++)
{
anim = NULL;
m_pAnimCtrl->GetAnimationSet(i, &anim);
if(anim != NULL)
{
if(strcmp(name.c_str(), anim->GetName()) == 0)
m_pAnimCtrl->SetTrackAnimationSet(0, anim);
anim->Release();
}
}
}
void SkinnedMesh::GetAnimations(std::vector<std::string> &animations)
{
ID3DXAnimationSet *anim = NULL;
if (m_pAnimCtrl)
{
for(int i=0;i<(int)m_pAnimCtrl->GetMaxNumAnimationSets();i++)
{
anim = NULL;
m_pAnimCtrl->GetAnimationSet(i, &anim);
if(anim != NULL)
{
animations.push_back(anim->GetName());
anim->Release();
}
}
}
else
{
animations.clear();
}
}
Please help
Thanks
Jack