Thread: Skinned Meshes

  1. #1
    Darkness Prevails Dark_Phoenix's Avatar
    Join Date
    Oct 2006
    Location
    Houston, Texas
    Posts
    174

    Skinned Meshes

    I am trying to load and render a skinned mesh with animation from a .X file. That part was pretty simple. But what I want to do now is create an mesh instance to do all my rendering and animation from. I want this instance to refer back to the original mesh so that I can have only one skinned mesh in memory and have multiple instaces doing different animations.

    Does anyone know of any good resources or tutorials that explains how to do this? I have a pretty good start on it but I keep getting an access violation that I can't figure out.
    Using Code::Blocks and Windows XP

    In every hero, there COULD be a villain!

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    You need a mesh resource manager.

    Store it once, use it multiple times.

  3. #3
    Darkness Prevails Dark_Phoenix's Avatar
    Join Date
    Oct 2006
    Location
    Houston, Texas
    Posts
    174
    Quote Originally Posted by Bubba
    You need a mesh resource manager.
    That is where I am running into problems. While I have not created a manager class yet, I do have it set up to render multiple instances of the same mesh. Using a simple mesh was no problem, but I am running into problems using skinned meshes.

    I can load and render a skinned mesh just fine, but when I try to do multiple instances I keep getting access violation errors at run time.

    From what I understand I have to keep a seperate ID3DXMesh and Combined Transform Matrix for each instance but I am having problems making this work. I think I am actually making this harder than what it really is

    Can anyone give me a web site or point me to a tutorial or even just explain how this works?
    Using Code::Blocks and Windows XP

    In every hero, there COULD be a villain!

  4. #4
    Darkness Prevails Dark_Phoenix's Avatar
    Join Date
    Oct 2006
    Location
    Houston, Texas
    Posts
    174
    Well, I think I finally have it figured out. After much research I have figured that I have to load the mesh Hierarchy and for each instance of it I have to clone the frame hierarchy and the animation controller, then set the frame hierarchy to use the new animation controller. Everything seems to be working OK except that the program is hanging up in my FrameMove function at the call to ID3DxAnimationController::AdvanceTime(). Here is what I have so far
    Code:
    void CMeshInstance::SetMesh(LPDIRECT3DDEVICE9 pDevice, CSkinnedMesh *pMesh, bool MakeSphere, bool MakeBox) 
    {
        Release();
        m_pSkinnedMesh     = pMesh;
        D3DXFRAME *pOriginalFrameRoot = m_pSkinnedMesh->GetFrameRoot();
        ID3DXAnimationController *pAC = m_pSkinnedMesh->GetAnimController();
        if (pAC && m_pFrameRootEx)
        {
            // Clone the mesh hierarchy
            CloneFrameHierarchy(pOriginalFrameRoot, &m_pFrameRootEx);
            // Clone the animation controller
            if(FAILED(pAC->CloneAnimationController(pAC->GetMaxNumAnimationOutputs(),
                                                    pAC->GetMaxNumAnimationSets(),
                                                    pAC->GetMaxNumTracks(),
                                                    pAC->GetMaxNumEvents(),
                                                    &m_pAnimControllerInstance) ) )
            {
                SHOW_ERROR("Cannot clone animation controller");
            }
            // Set our cloned animation controller to work with our cloned frame hierarchy
            if(FAILED(D3DXFrameRegisterNamedMatrices(m_pFrameRootEx, m_pAnimControllerInstance) ) )
            {
                SHOW_ERROR("Cannot register frame hierarchy with animation controller");
            }
      // Set the bones up
      SetupBoneMatrices(pDevice,(D3DXFrameEx*)m_pFrameRootEx, NULL);
      // Create the bone matrices array for use during FrameMove to hold the final transform
      m_pBoneMatrices  = new D3DXMATRIX[m_NumBones];
      ZeroMemory(m_pBoneMatrices, sizeof(D3DXMATRIX) * m_NumBones);
     }
        return;
    }
    and my FrameMove function
    Code:
    void CMeshInstance::FrameMove(float ElapsedTime)
    {
        // Adjust animation speed
        ElapsedTime /= m_SpeedAdjust;
        // Advance the time and set in the controller
        if (m_pAnimControllerInstance)
        {
            m_pAnimControllerInstance->AdvanceTime(ElapsedTime, NULL);
        } else {
            SHOW_MESSAGE("Anim Controller set to NULL");
        }
        m_CurrentTime += ElapsedTime;
        // Now update the model's world matrices in the hierarchy
        UpdateFrameMatrices(m_pFrameRootEx, GetTransform() );
        // If the model contains a skinned mesh update the vertices
        LPD3DXMeshContainerEx pMesh = m_pFirstMeshEx;
        if(pMesh && pMesh->pSkinInfo)
        {
            UINT Bones = pMesh->pSkinInfo->GetNumBones();
            // Create the bone matrices that transform each bone from bone space into
            // character space, and also wraps the
            // mesh around the bones using the bone offsets
            for (UINT i = 0; i < Bones; ++i)
                D3DXMatrixMultiply(&m_pBoneMatrices[i],
                                   &pMesh->pBoneOffsetsEx[i],
                                   pMesh->ppCombinedMatrixEx[i] );
            // Modify the vertex positions based on the new bone matrices.
            void *srcPtr;
            pMesh->MeshData.pMesh->LockVertexBuffer(D3DLOCK_READONLY, (void**)&srcPtr);
            void *destPtr;
            pMesh->pSkinMeshEx->LockVertexBuffer(0, (void**)&destPtr);
            // Update the skinned mesh
            pMesh->pSkinInfo->UpdateSkinnedMesh(m_pBoneMatrices, NULL, srcPtr, destPtr);
            // Unlock the meshes vertex buffers
            pMesh->pSkinMeshEx->UnlockVertexBuffer();
            pMesh->MeshData.pMesh->UnlockVertexBuffer();
        }
        return;
    }
    in RED is where it hangs up. If I comment out that part then it runs just fine, just without any animation.

    Can anyone tell me what I am doing wrong here?
    Using Code::Blocks and Windows XP

    In every hero, there COULD be a villain!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Meshes and Movement
    By Dark_Phoenix in forum Game Programming
    Replies: 1
    Last Post: 09-16-2007, 06:26 PM
  2. Skinned Program
    By obduk in forum C++ Programming
    Replies: 7
    Last Post: 05-11-2007, 06:07 AM
  3. Reloading Meshes on Device Reset
    By Epo in forum Game Programming
    Replies: 6
    Last Post: 05-30-2005, 04:21 PM