microsoft / DirectXTK12

The DirectX Tool Kit (aka DirectXTK12) is a collection of helper classes for writing DirectX 12 code in C++
https://walbourn.github.io/directx-tool-kit-for-directx-12/
MIT License
1.44k stars 369 forks source link

Wiki Issue for Getting Started -> Animating using model bones #218

Open focusright opened 3 months ago

focusright commented 3 months ago

In this wiki documentation

https://github.com/microsoft/DirectXTK12/wiki/Animating-using-model-bones#modifying-model-bones

The result does not match the screenshot provided. The problem can be addressed either in the model or the CreateLookAt() function in Game::CreateWindowSizeDependentResources(). Since I don't have the original source file of the model I fixed it in the code with

void Game::CreateWindowSizeDependentResources()
{
    // TODO: Initialize windows-size dependent objects here.

    auto size = m_deviceResources->GetOutputSize();
    m_view = Matrix::CreateLookAt(Vector3(10, 2, 0),
        Vector3(0, 2, 0), Vector3::UnitY);
    m_proj = Matrix::CreatePerspectiveFieldOfView(XM_PI / 4.f,
        float(size.right) / float(size.bottom), 0.1f, 10000.f);
}

The main fix being in changing the Vector3 parameters in the CreateLookAt() function.

focusright commented 3 months ago

The repo where I am working on this is located here

https://github.com/focusright/graphics/tree/main/codebase/directx12/practice/03-directxtk12-wiki/07-animating-using-model-bones

walbourn commented 3 months ago

FWIW, all the tutorials source can be found https://github.com/walbourn/directxtk-tutorials

focusright commented 3 months ago

I looked at the tutorial source and found the culprit, the line

m_model->CopyBoneTransformsTo(nbones, m_animBones.get());

needs to remain right after the lines where the bone arrays are made:

m_drawBones = ModelBone::MakeArray(nbones);
m_animBones = ModelBone::MakeArray(nbones);

m_model->CopyBoneTransformsTo(nbones, m_animBones.get());

uint32_t index = 0;
for (const auto& it : m_model->bones)
{
    if (_wcsicmp(it.name.c_str(), L"tank_geo") == 0)
    {
        // Need to recenter the model.
        m_animBones[index] = XMMatrixIdentity();
    }

    ++index;  
}

In this section of the wiki

https://github.com/microsoft/DirectXTK12/wiki/Animating-using-model-bones#modifying-model-bones

the last sentence is "(after you have loaded the model and created the bone arrays):"

which made my code like this:

m_drawBones = ModelBone::MakeArray(nbones);
m_animBones = ModelBone::MakeArray(nbones);

uint32_t index = 0;
for (const auto& it : m_model->bones)
{
    if (_wcsicmp(it.name.c_str(), L"tank_geo") == 0)
    {
        // Need to recenter the model.
        m_animBones[index] = XMMatrixIdentity();
    }

    ++index;  
}

m_model->CopyBoneTransformsTo(nbones, m_animBones.get());

Hopefully this will help someone else who tries to follow the tutorial like I did.