Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question
2  
Have you tried stepping through the code? It looks to me like your effect.Techniques[techniqueName] is null. –  John McDonald Mar 20 '12 at 15:54
    
Can you pastebin your shader code? –  Luis Estrada Mar 20 '12 at 16:21
    
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. –  Kurt Ricci Mar 20 '12 at 16:21
    
@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 –  Kurt Ricci Mar 20 '12 at 16:24
1  
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. –  Nic Foster Mar 22 '12 at 4:33
show 1 more comment

1 Answer 1

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.

share|improve this answer
    
Hey, I've tried what you said, but it gives an error before it builds: i1099.photobucket.com/albums/g395/krt_ricci/Capture-2.png. Do you have any idea what this error means? thanks –  Kurt Ricci Mar 20 '12 at 18:21
1  
@KurtRicci your model must have a texture applied. –  r2d2rigo Mar 20 '12 at 19:23
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.