3ddirectx-11directxtk

How to use DirectX::Model with a custom DirectX::Effect


I'm using the DirectXToolKit. I created a model like this

m_Model = Model::CreateFromCMO(device, L"Content/Meshes/cube.cmo", *m_EffectFactory);

And I want to use a custom effect I created that inherited from DirectX::IEffect.

    m_ModelEffect = std::make_shared<UnlitEffect>(device);

However, DirectX::Model by default creates its own effects, meaning in order to use my own shaders, I have to replace them using

MeshPart::ModifyEffect(ID3D11Device* d3dDevice, std::shared_ptr<IEffect>& ieffect, bool isalpha);

I'm confused on how to send my effect to that function.

Here is the code I'm using now:

for(const auto& mesh : m_Model->meshes)
{
    for(const auto& part : mesh->meshParts)
    {
        part->ModifyEffect(device, &m_ModelEffect);
    }
}

However this causes this error initial value of reference to non-const must be an lvalue Searching for that error gives me many solutions that involve changing the function signature to use a const reference but how the heck am I supposed to use the function as is without changing it? What's the proper way to pass an address of a shared_ptr?

Edit: I got it to work using this code

m_ModelEffect = std::make_shared<UnlitEffect>(device);

m_Model = Model::CreateFromCMO(device, L"Content/Meshes/cube.cmo", *m_EffectFactory);
for(auto& mesh : m_Model->meshes)
{
    for(auto& part : mesh->meshParts)
    {
        part->ModifyEffect(device, m_ModelEffect);
    }
}

Solution

  • You can provide your own IEffectFactory class instance to the loader, and use that to directly create your custom effects.

    You can also replace them as you are trying to do. The reason you are getting initial value of reference to non-const must be an lvalue is because you declared the part pointer to be const which means it cannot be modified.

    Instead try:

    for(auto& mesh : m_Model->meshes)
    {
        for(auto& part : mesh->meshParts)
        {
            part->ModifyEffect(device, &m_ModelEffect);
        }
    }
    m_Model->Modified();
    

    See the wiki