Jump to content

  • Log In with Google      Sign In   
  • Create Account


Sesha

Member Since 13 Sep 2011
Offline Last Active Nov 24 2013 07:30 AM
-----

Topics I've Started

Shadow mapping problem

27 June 2013 - 09:48 PM

Hello people, I am tring shadow mapping in directx 9 with HLSL. My system config Dual core, 1.5 GB RAM, ATI RADON 6600.

My Problem is i am getting the shadow map but it fails to produce shadow. I am following Frank D Luna's book.

Attached my HLSL code.

float4x4 matWorldViewProjection;
float4x4 matWorldInverseTranspose;
float4x4 matWorld;
float4x4 matLWorld;
float3 EyePos;
texture gTex;
texture gShadowMap;

float3 LightDirection;
float4 Ambient;
float4 Diffuse;
float4 Specular;
float SpecularPower;

float4x4 MatrixPalette[35];
int numBoneInfluence = 2;
static const float SHADOW_EPSILON = 0.00005f;
static const float SMAP_SIZE = 512.0f;

sampler Texture = sampler_state
{
   Texture   = (gTex);
   ADDRESSU  = WRAP;
   ADDRESSV  = WRAP;
   MAGFILTER = Anisotropic;
   MINFILTER = Anisotropic;
   MIPFILTER = Anisotropic;
   MAXANISOTROPY = 2;
};

sampler ShadowMapS = sampler_state
{
	Texture   = (gShadowMap);
	MinFilter = POINT;
	MagFilter = POINT;
	MipFilter = POINT;
	AddressU  = CLAMP; 
    AddressV  = CLAMP;
};

struct VSHADOW_OUTPUT 
{
   float4 Position     : POSITION0;
   float2 Depth        : TEXCOORD0;
};

struct VSHADOW1_OUTPUT 
{
   float4 Position     : POSITION0;
   float3 Normal       : TEXCOORD0;
   float3 PosW         : TEXCOORD1;
   float2 TextureCoord : TEXCOORD2;
   float4 ProjTex      : TEXCOORD3;
};

VSHADOW_OUTPUT BuildShadowMapVS(float4 Position : POSITION0)
{
    VSHADOW_OUTPUT OutputSh;
	
    OutputSh.Position = mul( Position, matLWorld );
    
    OutputSh.Depth.x = Position.z;
    OutputSh.Depth.y = Position.w;
   
    return OutputSh;
};

float4 BuildShadowMapPS(float2 Depth : TEXCOORD0) : COLOR
{
	return (Depth.x/Depth.y);
}

technique BuildShadowTech
{
   pass P0
   {
      VertexShader = compile vs_2_0 BuildShadowMapVS();
      PixelShader  = compile ps_2_0 BuildShadowMapPS();
   }
}

VSHADOW1_OUTPUT LightShadowVS( float4 Position : POSITION0, float3 Normal : NORMAL0, float2 TextureCoord : TEXCOORD0 )
{
   VSHADOW1_OUTPUT Output;
   
   Output.Position = mul( Position, matWorldViewProjection );
   
   Output.Normal = mul( Normal, matWorldInverseTranspose);
   
   Output.PosW = mul( Position, matWorld);
   
   Output.TextureCoord = TextureCoord;
   
   Output.ProjTex = mul( Position, matLWorld );
   
   return Output; 
}

float4 LightShadowPS(float3 normal : TEXCOORD0, float3 posW : TEXCOORD1, float2 TextureCoord : TEXCOORD2, float4 ProjTex : TEXCOORD3) : COLOR0
{
   normal = normalize(normal);
   posW   = normalize(posW);
   float3 light = normalize(LightDirection);
   float3 toEye = normalize(EyePos - posW);
   
   float3 r = reflect(-light, normal);
   float  t = pow(max(dot(r, toEye), 0.0f), SpecularPower);
   float  diffuseContribution = max(dot(normal, light), 0.0f);
   
   float4 tex      = tex2D(Texture, TextureCoord);
   float4 ambient  = Ambient * tex;
   float4 diffuse  = Diffuse * tex * diffuseContribution;
   float4 specular = Specular * tex * t;
   
   ProjTex.xy /= ProjTex.w;
   ProjTex.x =  0.5f*ProjTex.x + 0.5f;
   ProjTex.y = -0.5f*ProjTex.y + 0.5f;
   
   float depth = ProjTex.z / ProjTex.w;
   
   float2 texelpos = SMAP_SIZE * ProjTex.xy;
   float2 lerps = frac( texelpos );
   float dx = 1.0f / SMAP_SIZE;
   
   float s0 = (tex2D(ShadowMapS, ProjTex.xy).r + SHADOW_EPSILON < depth) ? 0.0f : 1.0f;
   float s1 = (tex2D(ShadowMapS, ProjTex.xy + float2(dx, 0.0f)).r + SHADOW_EPSILON < depth) ? 0.0f : 1.0f;
   float s2 = (tex2D(ShadowMapS, ProjTex.xy + float2(0.0f, dx)).r + SHADOW_EPSILON < depth) ? 0.0f : 1.0f;
   float s3 = (tex2D(ShadowMapS, ProjTex.xy + float2(dx, dx)).r   + SHADOW_EPSILON < depth) ? 0.0f : 1.0f;
   
   float shadowCoeff = lerp( lerp( s0, s1, lerps.x ), lerp( s2, s3, lerps.x ), lerps.y );
   
   return float4(ambient + diffuse*shadowCoeff + specular*shadowCoeff);
}

technique DrawShadowTech
{
   pass P0
   {
      VertexShader = compile vs_2_0 LightShadowVS();
      PixelShader  = compile ps_2_0 LightShadowPS();
   }
}

Please help me i am stuck with this.

Thanks in advance.


Problem with drawindexedprimitive

14 May 2012 - 08:53 AM

Hai people,

After long practice now i am stuck with a simple problem. The thing is i want to render the second triangle of a square but it draws the next adjacent triangle.

void D3DApp::Init_Vertexbuffer()
{
HRESULT hr;
float PosPtrX = -width*0.5f;
float PosPtrZ = depth*0.5f;
int i = 0;
hr=d3ddev->CreateVertexBuffer(numVertices*sizeof(Vertices),D3DUSAGE_WRITEONLY,D3D9T_CUSTOMVERTEX,D3DPOOL_MANAGED,&vb,0);
std::vector<D3DXVECTOR3>vert;
vert.resize(numVertices);
for(int j=0; j<numVertCols; j++)
{
  for(int k=0; k<numVertRows; k++)
  {
   vert[i].x = PosPtrX;
   vert[i].y = 0.0f;
   vert[i].z = PosPtrZ;
   PosPtrZ += -dz;
   ++i;
  }
  PosPtrX += +dx;
  PosPtrZ = depth*0.5f;
}
hr=vb->Lock(0,0,(void**)&v,0);
for(int i = 0; i<numVertices; ++i)
{
  v[i].pos = vert[i];
  v[i].col = D3DXCOLOR(0.0f, 0.0f, 0.0f, 1.0f);
}
vb->Unlock();
}

void D3DApp::Init_Indexbuffer()
{
HRESULT hr;
int numIndices = 3 * numTris;
hr=d3ddev->CreateIndexBuffer(numIndices*sizeof(Vertices),D3DUSAGE_WRITEONLY,D3DFMT_INDEX16,D3DPOOL_MANAGED,&ib,0);
std::vector<DWORD>index;
index.resize(numIndices);
int c = 0;
for(int a=0; a<numCellCols; a++)
{
  for(int b=0; b<numCellRows; b++)
  {
   index[c++]   =  a    * numVertRows + b;
   index[c++] = (a + 1) * numVertRows + b;
   index[c++] =    a    * numVertRows + b + 1;
   index[c++] =    a    * numVertRows + b + 1;
   index[c++] = (a + 1) * numVertRows + b;
   index[c++] = (a + 1) * numVertRows + b + 1;
   //c += 6;
  }
}
WORD *k = 0;
hr=ib->Lock(0,0,(void**)&k,0);
for(int i = 0; i<numIndices; ++i)
  k[i] = (WORD)index[i];
ib->Unlock();
}

void D3DApp::Scene_Update(HWND &hWnd)
{
d3ddev->Clear(0,0,D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,0xff888888,1.0f,0);
d3ddev->BeginScene();
cInput.Press();
if(cInput.KeyDown(DIK_LCONTROL))
{
  thetaY += cInput.MouseX() / 250.0f;
}
z -= cInput.MouseZ() / 3000.0f;
camx = (z * 1.5f * width * sin(thetaY));
camy = (z * 1.5f * width * sin(thetaZ));
camz = (z * 1.5f * depth * cos(thetaY));
eyevector=D3DXVECTOR3(camx, camy, -camz);
D3DXMatrixLookAtLH(&viewmatrix,&eyevector,&lookatvector,&upvector);
d3ddev->SetFVF(D3D9T_CUSTOMVERTEX);
d3ddev->SetStreamSource(0,vb,0,sizeof(Vertices));
d3ddev->SetIndices(ib);
mfx->SetMatrix(mMatrix,&(viewmatrix*projectionmatrix));
mfx->SetTechnique(mTech);
d3ddev->SetRenderState(D3DRS_FILLMODE,D3DFILL_WIREFRAME);
d3ddev->SetRenderState(D3DRS_LIGHTING,FALSE);
d3ddev->SetRenderState(D3DRS_ZENABLE, TRUE );
UINT numpass=0;
mfx->Begin(&numpass,0);
for(unsigned int a=0; a < numpass; ++a)
{
  mfx->BeginPass(a);
  d3ddev->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,0,0,numVertices,0,numTris);
  mfx->EndPass();
}
mfx->End();
//d3ddev->SetRenderState(D3DRS_FILLMODE,D3DFILL_WIREFRAME);
if(cInput.KeyDown(DIK_LSHIFT))
  CheckHit(hWnd);
d3ddev->EndScene();
Time.End_Timer();
d3ddev->Present(0,0,0,0);
}

void D3DApp::CheckHit(HWND &hWnd)
{
POINT s;
float w, h;
D3DXVECTOR3 Origin;
D3DXVECTOR3 Dir;
D3DXMATRIX invView;
GetCursorPos(&s);
ScreenToClient(hWnd, &s);
w = (float)d3dpp.BackBufferWidth;
h = (float)d3dpp.BackBufferHeight;
float x =  ( (2.0f * s.x / w) - 1.0f) / viewmatrix._11;
float y = -( (2.0f * s.y / h) - 1.0f) / viewmatrix._22;
Origin = D3DXVECTOR3(x, y, 1.0f);
Dir    = D3DXVECTOR3(x, y, 100.0f);
D3DXMatrixInverse(&invView, 0, &viewmatrix);
D3DXVec3TransformCoord(&Origin, &Origin, &invView);
D3DXVec3TransformNormal(&Dir, &Dir, &invView);
D3DXVec3Normalize(&Dir, &Dir);
float bu = 0.0f;
float bv = 0.0f;
float Dist = 0.0f;
D3DXVECTOR3 vector0;
D3DXVECTOR3 vector1;
D3DXVECTOR3 vector2;
int c = 0;
for(int a=0; a<numCellCols; a++)
{
  for(int b=0; b<numCellRows; b++)
  {
   vector0 = v[   a    * numVertRows + b].pos;
   vector1 = v[(a + 1) * numVertRows + b].pos;
   vector2 = v[   a    * numVertRows + b + 1].pos;
   if(D3DXIntersectTri(&vector0, &vector1, &vector2, &Origin, &Dir, &bu, &bv, &Dist))
   {
    vb->Lock(0,0,(void**)&v,0);
    v[   a    * numVertRows + b].col	 = D3DXCOLOR(1.0f, 0.0f, 0.0f, 1.0f);
    v[(a + 1) * numVertRows + b].col	 = D3DXCOLOR(1.0f, 0.0f, 0.0f, 1.0f);
    v[   a    * numVertRows + b + 1].col = D3DXCOLOR(1.0f, 0.0f, 0.0f, 1.0f);
    vb->Unlock();
    UINT numpass=0;
    mfx->Begin(&numpass,0);
    for(unsigned int d=0; d < numpass; ++d)
    {
	 mfx->BeginPass(d);
	 d3ddev->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,(a * numVertRows + b),0,3,0,1);
	 mfx->EndPass();
    }
    mfx->End();
    vb->Lock(0,0,(void**)&v,0);
    v[   a    * numVertRows + b].col	 = D3DXCOLOR(0.0f, 0.0f, 0.0f, 1.0f);
    v[(a + 1) * numVertRows + b].col	 = D3DXCOLOR(0.0f, 0.0f, 0.0f, 1.0f);
    v[   a    * numVertRows + b + 1].col = D3DXCOLOR(0.0f, 0.0f, 0.0f, 1.0f);
    vb->Unlock();
   }
   vector0 = v[   a    * numVertRows + b + 1].pos;
   vector1 = v[(a + 1) * numVertRows + b].pos;
   vector2 = v[(a + 1) * numVertRows + b + 1].pos;
   if(D3DXIntersectTri(&vector0, &vector1, &vector2, &Origin, &Dir, &bu, &bv, &Dist))
   {
    vb->Lock(0,0,(void**)&v,0);
    v[   a    * numVertRows + b + 1].col = D3DXCOLOR(1.0f, 0.0f, 0.0f, 1.0f);
    v[(a + 1) * numVertRows + b].col	 = D3DXCOLOR(1.0f, 0.0f, 0.0f, 1.0f);
    v[(a + 1) * numVertRows + b + 1].col = D3DXCOLOR(1.0f, 0.0f, 0.0f, 1.0f);
    vb->Unlock();
    UINT numpass=0;
    mfx->Begin(&numpass,0);
    for(unsigned int d=0; d < numpass; ++d)
    {
	 mfx->BeginPass(d);
	 d3ddev->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,(a * numVertRows + b + 1),0,3,0,1);
	 mfx->EndPass();
    }
    mfx->End();
    vb->Lock(0,0,(void**)&v,0);
    v[   a    * numVertRows + b + 1].col = D3DXCOLOR(0.0f, 0.0f, 0.0f, 1.0f);
    v[(a + 1) * numVertRows + b].col	 = D3DXCOLOR(0.0f, 0.0f, 0.0f, 1.0f);
    v[(a + 1) * numVertRows + b + 1].col = D3DXCOLOR(0.0f, 0.0f, 0.0f, 1.0f);
    vb->Unlock();
   }//end if
  }//end for b
}//end for a
/*Vertices *vertex = 0;
hr=vb->Lock(0,0,(void**)&v,0);
for(int i = 0; i<numVertices; ++i)
{
  v[i].pos = vert[i];
  v[i].col = D3DXCOLOR(1.0f, 0.0f, 0.0f, 1.0f);
}
vb->Unlock();*/
}

Drawing the second drawindexprimitive of checkhit() is the focus it draws the wrong second vertex.

Thanks in advance.

Updateskinnedmesh returning D3DERR_INVALIDCALL

13 September 2011 - 10:06 AM

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[i] = &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[i], &bMesh->BoneOffsetMatrices[i], bMesh->BoneMatrixPtrs[i]);
		}
		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[i]=0;
			newBoneMesh->MaterialList[i]=pMaterials[i].MatD3D;
			newBoneMesh->MaterialList[i].Ambient=newBoneMesh->MaterialList[i].Diffuse;
			if(FAILED(D3DXCreateTextureFromFileA(pDevice,pMaterials[i].pTextureFilename,&newBoneMesh->TextureList[i])))
					newBoneMesh->TextureList[i]=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[i]=*(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[i]->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.

PARTNERS