3
\$\begingroup\$

I've been trying to implement shadows in my game and I've been following quite a few tutorials online, mainly Riemers, but I'm always getting the same 2 errors when I'm drawing my models and setting the parameters from the effect file. The errors are:

This method does not accept null for this parameter. Parameter name: value

and

Object reference not set to an instance of an object.

So I've then downloaded a sample and just replaced my model with the one found in the sample and the same errors occur. I this find very strange as it works with his model. I'm wondering if the problem is with my models (I made them myself). The errors start to occur after the second foreach loop.

void DrawModel(Model model, bool createShadowMap)
{
    string techniqueName = createShadowMap ? "CreateShadowMap" : "DrawWithShadowMap";

    Matrix[] transforms = new Matrix[model.Bones.Count];
    model.CopyAbsoluteBoneTransformsTo(transforms);

    // Loop over meshs in the model
    foreach (ModelMesh mesh in model.Meshes)
    {
        // Loop over effects in the mesh
        foreach (Effect effect in mesh.Effects)
        {
            // Set the currest values for the effect
            effect.CurrentTechnique = effect.Techniques[techniqueName];
            effect.Parameters["World"].SetValue(world);
            effect.Parameters["View"].SetValue(view);
            effect.Parameters["Projection"].SetValue(projection);
            effect.Parameters["LightDirection"].SetValue(lightDir);
            effect.Parameters["LightViewProj"].SetValue(lightViewProjection);

            if (!createShadowMap)
                effect.Parameters["ShadowMap"].SetValue(shadowRenderTarget);
        }
        // Draw the mesh
        mesh.Draw();
    }
}

Any help would be greatly appreciated, thanks.

\$\endgroup\$
6
  • 2
    \$\begingroup\$ Have you tried stepping through the code? It looks to me like your effect.Techniques[techniqueName] is null. \$\endgroup\$ Mar 20, 2012 at 15:54
  • \$\begingroup\$ Can you pastebin your shader code? \$\endgroup\$ Mar 20, 2012 at 16:21
  • \$\begingroup\$ hey..ye i debugged the code before and it says its null, but what I don't get is why does it work with his model and not with mine. Even with the other tutorials I keep getting the same errors. \$\endgroup\$
    – Kurt Ricci
    Mar 20, 2012 at 16:21
  • \$\begingroup\$ @LuisEstrada, here is the shader code, but as I've mentioned before I'm using the shader code found in the sample and all I'm doing is just changing his model to mine, and that is when it gives me the errors: pastebin.com/M5N6ivTh \$\endgroup\$
    – Kurt Ricci
    Mar 20, 2012 at 16:24
  • 1
    \$\begingroup\$ Make sure your project settings for graphics are set to HiDef, and not Reach, the shadow mapping demo doesn't work in Reach settings because XNA 4.0 Reach profile doesn't allow for a render target format that supports the def that the shadow mapping technique needs. \$\endgroup\$
    – Nic Foster
    Mar 22, 2012 at 4:33

1 Answer 1

2
\$\begingroup\$

I had a hunch, which I've explained in the comments but I'm pretty certain I've figured it out.

The ShadowMappingExample uses a custom pipeline to add the ShadowMap effect to all the models. This way you can access the shadowmapping shader via

 foreach (ModelMesh mesh in model.Meshes)
            {                    
                // Loop over effects in the mesh
                foreach (Effect effect in mesh.Effects)

You have let Visual Studio know that you want your own models to be imported using the ShadowMappingExample pipeline and not using the default model pipeline that just applies BasicEffect as shader.

To do this set ContentProcessor to CustomEffectModelProcessor instead of just Model.

Here's a screenshot, it should be in the lower right corner of Visual Studio when you've selected your model.

enter image description here

Now to explain why you got the null reference error: this happens because the default processor applies the BasicEffect shader, which doesn't have the technique which you are trying to set as active.

Hope this explains everything.

\$\endgroup\$
2

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .