# Still searching for something that makes sense (Animated Mesh)

## Recommended Posts

ktuluorion    393
Does anybody have a simple tutorial for loading and using animated meshes? It is driving me crazy trying to find something that just shows the bare bones code needed to do so, and explains it in a way that is understandable. If anyone can point me in a good direction, i'd appreciate it. Oh, i'm using DX9. Thanks!

##### Share on other sites
greytone    174
I don't have a nice tutorial for you, but if you are using DirectX meshes here is some simple code for Tweening between two meshes.

//this handy structure locks the vertex buffer and then makes it avalable for changing, //useing it for mesh tweening //borrowed from a directx sdk example.struct MESHTOOL{    struct VERTEX { D3DXVECTOR3 p, n; FLOAT tu, tv; }; // Same as a Vertex    LPDIRECT3DVERTEXBUFFER9 pVB;    DWORD                   dwNumVertices;    VERTEX*                 pVertices;    MESHTOOL( LPD3DXMESH pMesh )    {        pMesh->GetVertexBuffer( &pVB );		pVB->Lock( 0, 0, (VOID**)&pVertices, 0 );        dwNumVertices= pMesh->GetNumVertices();    }    ~MESHTOOL()    {        pVB->Unlock();        pVB->Release();    }};LPD3DXMESH tweenedMesh;LPD3DXMESH mesh_one;LPD3DXMESH mesh_two;void BlendMeshes(FLOAT fWeight){    // Build convenient objects to access the mesh vertices    MESHTOOL dest( tweenedMesh );    MESHTOOL src2( mesh_one );    MESHTOOL src1( mesh_two );	// The blending factors    FLOAT fWeight1 = fabsf(fWeight);    FLOAT fWeight2 = 1.0f - fWeight1;    // LERP positions and normals    for( DWORD i=0; i<dest.dwNumVertices; i++ )    {        dest.pVertices[i].p = fWeight1*src1.pVertices[i].p +                              fWeight2*src2.pVertices[i].p;        dest.pVertices[i].n = fWeight1*src1.pVertices[i].n +                              fWeight2*src2.pVertices[i].n;    }}

The basic idea is mesh_one and mesh_two are keyframe meshes, and the tweenedMesh is a calculated in-between mesh. The weight value determines how each key frame is weighted in calculating the tweened mesh. 0.0 being entirely mesh_one and 1.0 being entirely mesh_two.

I have found the tweening animation method is easyer to understand than the boned methods. Of course it does have its limitations but it should be an easy place to start.

-Greytone