Jump to content
  • Advertisement
Sign in to follow this  
Sesha

Updateskinnedmesh returning D3DERR_INVALIDCALL

This topic is 2617 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

[color="#000000"]Hi guys i am new to this forum and this is my first post.

My aim of the program is to load simple animated mesh based on toymaker tutorial. Now the problem is updateskinnedmesh for the model is returning a D3DERR_INVALIDCALL. There is no problem when i render the source mesh and the originalmesh of meshcontainer without skinning, the problem comes as the updateskinnedmesh is used. Source is as given below.

XFileloader.cpp
#include<windows.h>
#include<assert.h>
#include"XFileLoader.h"

void Xloader::Load(LPDIRECT3DDEVICE9 &pDevice)
{
d3dDev=pDevice;
HierarchyLoader *Allocator = new HierarchyLoader;
D3DXLoadMeshHierarchyFromX("Bounce.x", D3DXMESH_MANAGED, d3dDev, Allocator, NULL, &RootFrame, &AnimeController);

if(AnimeController)
NumAnimation = AnimeController->GetMaxNumAnimationSets();

if(RootFrame)
{
SetUpBoneMatrices((Bone*)RootFrame);
CurrentBoneMatrix = new D3DXMATRIX[maxBone];
ZeroMemory(CurrentBoneMatrix, sizeof(D3DXMATRIX)*maxBone);
}

delete Allocator;
Allocator = 0;
}

void Xloader::SetUpBoneMatrices(Bone *RootBone)
{
BoneMesh *boneMesh = (BoneMesh*)RootBone->pMeshContainer;

while(boneMesh && boneMesh->pSkinInfo)
{
D3DVERTEXELEMENT9 Declaration[MAX_FVF_DECL_SIZE];
if (FAILED(boneMesh->MeshData.pMesh->GetDeclaration(Declaration)))
return;

boneMesh->MeshData.pMesh->CloneMesh(D3DXMESH_MANAGED,Declaration,d3dDev,&boneMesh->OriginalMesh);
maxBone = max(maxBone,(UINT)boneMesh->pSkinInfo->GetNumBones());
D3DXFRAME* TempFrame = NULL;

for(unsigned int i=0; i<boneMesh->pSkinInfo->GetNumBones(); i++)
{
const char* boneName = boneMesh->pSkinInfo->GetBoneName(i);
TempFrame = D3DXFrameFind(RootBone, boneName);
if(TempFrame)
{
Bone* tempFrame = (Bone*)TempFrame;
boneMesh->BoneMatrixPtrs = &tempFrame->CombinedTransformationMatrix;
}
}
boneMesh=(BoneMesh*)boneMesh->pNextMeshContainer;
}

if(RootBone->pFrameSibling!=NULL)
SetUpBoneMatrices((Bone*)RootBone->pFrameSibling);
if(RootBone->pFrameFirstChild!=NULL)
SetUpBoneMatrices((Bone*)RootBone->pFrameFirstChild);
}

void Xloader::Update(D3DXMATRIX *ParentMatrix,float &Radius,float &TimeElapsed)
{
if(AnimeController!=NULL)
{
AnimeController->AdvanceTime(TimeElapsed,NULL);
}
currentTime+=TimeElapsed;
UpdateBoneMatrices(RootFrame, ParentMatrix);
UpdateMesh(RootFrame, Radius);
}

void Xloader::UpdateBoneMatrices(D3DXFRAME *bone,D3DXMATRIX *ParentMatrix)
{
Bone *CurrentBone = (Bone*)bone;
if(ParentMatrix!=NULL)
{
D3DXMatrixMultiply(&CurrentBone->CombinedTransformationMatrix, &CurrentBone->TransformationMatrix, ParentMatrix);
}else{
CurrentBone->CombinedTransformationMatrix = CurrentBone->TransformationMatrix;
}

if(CurrentBone->pFrameSibling!=NULL)
UpdateBoneMatrices(CurrentBone->pFrameSibling, ParentMatrix);

if(CurrentBone->pFrameFirstChild!=NULL)
UpdateBoneMatrices(CurrentBone->pFrameFirstChild, &CurrentBone->CombinedTransformationMatrix);
}

void Xloader::UpdateMesh(D3DXFRAME *currentBone, float &Radius)
{
HRESULT hr;
Bone *bone = (Bone*)currentBone;

BoneMesh *bMesh = (BoneMesh*)bone->pMeshContainer;

while(bMesh && bMesh->pSkinInfo)
{
unsigned int numBones = bMesh->pSkinInfo->GetNumBones();
assert(numBones<=maxBone);
for(unsigned int i=0; i < numBones; ++i)
{
assert(i<maxBone);
D3DXMatrixMultiply(&CurrentBoneMatrix, &bMesh->BoneOffsetMatrices, bMesh->BoneMatrixPtrs);
}
void *src = 0;
void *dest = 0;

bMesh->MeshData.pMesh->LockVertexBuffer(D3DLOCK_READONLY, (void**)&src);
bMesh->OriginalMesh->LockVertexBuffer(0, (void**)&dest);

hr=bMesh->pSkinInfo->UpdateSkinnedMesh(CurrentBoneMatrix, NULL, src, dest);
if(hr=D3DERR_INVALIDCALL)
MessageBox(NULL,"D3DERR_INVALIDCALL","Skinning",MB_OK);

//D3DXComputeBoundingSphere((D3DXVECTOR3*)dest,bMesh->OriginalMesh->GetNumVertices(),D3DXGetFVFVertexSize(bMesh->pSkinInfo->GetFVF()),&center,&rad);
Radius=rad;

bMesh->OriginalMesh->UnlockVertexBuffer();
bMesh->MeshData.pMesh->UnlockVertexBuffer();

bMesh = (BoneMesh*)bMesh->pNextMeshContainer;
}

if(bone->pFrameSibling!=NULL)
UpdateMesh((Bone*)bone->pFrameSibling, Radius);

if(bone->pFrameFirstChild!=NULL)
UpdateMesh((Bone*)bone->pFrameFirstChild, Radius);
}

void Xloader::RenderMesh(void)
{
if (RootFrame)
DrawFrame(RootFrame);
}

void Xloader::DrawFrame(LPD3DXFRAME MainFrame)
{
LPD3DXMESHCONTAINER Mesh = MainFrame->pMeshContainer;
while(Mesh)
{
DrawMesh(Mesh, MainFrame);
Mesh = Mesh->pNextMeshContainer;
}
if (MainFrame->pFrameSibling!=NULL)
DrawFrame(MainFrame->pFrameSibling);

if (MainFrame->pFrameFirstChild!=NULL)
DrawFrame(MainFrame->pFrameFirstChild);
}

void Xloader::DrawMesh(LPD3DXMESHCONTAINER RMesh, LPD3DXFRAME RFrame)
{
Bone *RBone = (Bone*)RFrame;
BoneMesh *RMeshContainer = (BoneMesh*)RMesh;

if(RMeshContainer->pSkinInfo!=NULL)
{
D3DXMATRIX mat,Trans;
D3DXMatrixIdentity(&mat);
D3DXMatrixIdentity(&Trans);
D3DXMatrixTranslation(&Trans, 0.0f, 0.0f, 0.0f);
D3DXMatrixMultiply(&mat, &mat, &Trans);
d3dDev->SetTransform(D3DTS_WORLD, &mat);
}else{
D3DXMATRIX mat;
D3DXMatrixIdentity(&mat);
D3DXMatrixTranslation(&mat, 0.0f, 0.0f, 0.0f);
D3DXMatrixMultiply(&mat, &mat, &RBone->CombinedTransformationMatrix);
d3dDev->SetTransform(D3DTS_WORLD, &RBone->CombinedTransformationMatrix);
}

for(unsigned int iMat=0; iMat<RMeshContainer->NumMaterials; iMat++)
{
d3dDev->SetMaterial(&RMeshContainer->MaterialList[iMat]);
d3dDev->SetTexture(0, RMeshContainer->TextureList[iMat]);
//LPD3DXMESH pDrawMesh = (RMeshContainer->pSkinInfo) ? RMeshContainer->OriginalMesh: RMeshContainer->MeshData.pMesh;
//pDrawMesh->DrawSubset(iMat);
RMeshContainer->OriginalMesh->DrawSubset(iMat);
}
}

void Xloader::LoaderRelease()
{
AnimeController->Release();
}

void Xloader::SetAnimation()
{
CurrentAnimation = 1;
LPD3DXANIMATIONSET AnimeSet = NULL;
AnimeController->GetAnimationSet(CurrentAnimation, &AnimeSet);
AnimeController->SetTrackAnimationSet(CurrentAnimation, AnimeSet);
AnimeSet->Release();
}


Hierarchy.cpp
#include"Hierarchy.h"

HRESULT HierarchyLoader::CreateFrame(LPCSTR Name, LPD3DXFRAME *ppNewFrame)
{
*ppNewFrame = 0;

Bone *newBone=new Bone;
ZeroMemory(newBone,sizeof(Bone));

D3DXMatrixIdentity(&newBone->TransformationMatrix);
D3DXMatrixIdentity(&newBone->CombinedTransformationMatrix);

newBone->pMeshContainer=0;
newBone->pFrameSibling=0;
newBone->pFrameFirstChild=0;

*ppNewFrame=newBone;

if(Name)
{
int nNameSize = strlen(Name)+1;
newBone->Name = new char[nNameSize];
memcpy(newBone->Name, Name, nNameSize*sizeof(char));
}else{
newBone->Name = NULL;
}

return S_OK;
}

HRESULT HierarchyLoader::DestroyFrame(LPD3DXFRAME pFrameToFree)
{
if(pFrameToFree)
{
if(pFrameToFree->Name!=NULL)
delete[]pFrameToFree->Name;
delete pFrameToFree;
}
pFrameToFree=NULL;
return S_OK;
}

HRESULT HierarchyLoader::CreateMeshContainer(LPCSTR Name, const D3DXMESHDATA *pMeshData, const D3DXMATERIAL *pMaterials, const D3DXEFFECTINSTANCE *pEffectInstances, DWORD NumMaterials, const DWORD *pAdjacency, LPD3DXSKININFO pSkinInfo, LPD3DXMESHCONTAINER *ppNewMeshContainer)
{
BoneMesh *newBoneMesh=new BoneMesh;
ZeroMemory(newBoneMesh, sizeof(BoneMesh));

*ppNewMeshContainer = 0;

if(Name)
{
// Put in the name
int nNameSize = strlen(Name)+1;
newBoneMesh->Name = new char[nNameSize];
memcpy(newBoneMesh->Name, Name, nNameSize*sizeof(char));
}else{
newBoneMesh->Name = NULL;
}

if(pMeshData->Type!=D3DXMESHTYPE_MESH)
{
DestroyMeshContainer(newBoneMesh);
return E_FAIL;
}
newBoneMesh->MeshData.Type=D3DXMESHTYPE_MESH;

DWORD dwFaces = pMeshData->pMesh->GetNumFaces();
newBoneMesh->pAdjacency = new DWORD[dwFaces*3];
if(pAdjacency)
{
memcpy(newBoneMesh->pAdjacency, pAdjacency, sizeof(DWORD)*dwFaces*3);
}else{
if(FAILED( newBoneMesh->MeshData.pMesh->GenerateAdjacency( 1e-6f, (DWORD*)(void*)newBoneMesh->pAdjacency ) ) )
FillMemory((void*)newBoneMesh->pAdjacency, dwFaces*3, 0xFF );
}

IDirect3DDevice9 *pDevice=0;
pMeshData->pMesh->GetDevice(&pDevice);

newBoneMesh->MeshData.pMesh=pMeshData->pMesh;
pMeshData->pMesh->AddRef();

newBoneMesh->NumMaterials=max(NumMaterials,1);
newBoneMesh->MaterialList=new D3DMATERIAL9[newBoneMesh->NumMaterials];
newBoneMesh->TextureList=new LPDIRECT3DTEXTURE9[newBoneMesh->NumMaterials];

if(newBoneMesh->NumMaterials>0)
{
for(DWORD i=0; i<NumMaterials; i++)
{
newBoneMesh->TextureList=0;
newBoneMesh->MaterialList=pMaterials.MatD3D;
newBoneMesh->MaterialList.Ambient=newBoneMesh->MaterialList.Diffuse;
if(FAILED(D3DXCreateTextureFromFileA(pDevice,pMaterials.pTextureFilename,&newBoneMesh->TextureList)))
newBoneMesh->TextureList=NULL;
}
}else{
newBoneMesh->MaterialList=new D3DMATERIAL9[0];
newBoneMesh->MaterialList[0].Diffuse.r=1.0f;
newBoneMesh->MaterialList[0].Diffuse.g=1.0f;
newBoneMesh->MaterialList[0].Diffuse.b=1.0f;
newBoneMesh->MaterialList[0].Diffuse.a=1.0f;
newBoneMesh->TextureList=new LPDIRECT3DTEXTURE9[0];
newBoneMesh->TextureList[0]=0;
}

if(pSkinInfo!=NULL)
{
newBoneMesh->pSkinInfo=pSkinInfo;
pSkinInfo->AddRef();
//pMeshData->pMesh->CloneMeshFVF(D3DXMESH_MANAGED, pMeshData->pMesh->GetFVF(), pDevice, &newBoneMesh->MeshData.pMesh);

int NumBones=pSkinInfo->GetNumBones();
newBoneMesh->BoneOffsetMatrices=new D3DXMATRIX[NumBones];
newBoneMesh->BoneMatrixPtrs = new D3DXMATRIX*[NumBones];

for(int i=0; i<NumBones; i++)
newBoneMesh->BoneOffsetMatrices=*(newBoneMesh->pSkinInfo->GetBoneOffsetMatrix(i));
}else{
newBoneMesh->pSkinInfo = 0;
newBoneMesh->OriginalMesh = 0;
newBoneMesh->BoneOffsetMatrices = 0;
newBoneMesh->BoneMatrixPtrs = 0;
}
pDevice->Release();

*ppNewMeshContainer = newBoneMesh;
return S_OK;
}

HRESULT HierarchyLoader::DestroyMeshContainer(LPD3DXMESHCONTAINER pMeshContainerToFree)
{
BoneMesh* meshContainer = (BoneMesh*)pMeshContainerToFree;
if (!meshContainer)
return S_OK;

delete []meshContainer->Name;
meshContainer->Name=0;

delete []meshContainer->MaterialList;
meshContainer->MaterialList=0;

if(meshContainer->TextureList)
{
for(UINT i = 0; i < meshContainer->NumMaterials; ++i)
{
if (meshContainer->TextureList)
meshContainer->TextureList->Release();
}
}

delete []meshContainer->TextureList;

delete []meshContainer->pAdjacency;

delete []meshContainer->BoneOffsetMatrices;

if (meshContainer->OriginalMesh)
meshContainer->OriginalMesh->Release();

if (meshContainer->MeshData.pMesh)
meshContainer->MeshData.pMesh->Release();

if (meshContainer->pSkinInfo)
meshContainer->pSkinInfo->Release();

delete meshContainer;
meshContainer=0;

return S_OK;
}


Thanks in advance for your help.

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!