# Why does my MonoGame mesh throw an unhandled exception when trying to draw?

I am using the '3D Models' chapter of Learning XNA 4.0 to further understand 3D implementation in XNA, but I am running into an unhandled exception, when I try to draw each mesh.

Here is my basic Draw() function. It determines the amount of mesh transforms inside my model, and proceeds to draw each one, using a default BasicEffect.

public virtual void Draw(Camera camera)
{
Matrix[] transforms = new Matrix[model.Bones.Count];
model.CopyAbsoluteBoneTransformsTo(transforms);

foreach (ModelMesh mesh in model.Meshes)
{
foreach (BasicEffect effect in mesh.Effects)
{
effect.EnableDefaultLighting();
effect.Projection = camera.projectionMatrix;
effect.View = camera.viewMatrix;
effect.World = GetWorld() * mesh.ParentBone.Transform;
}

mesh.Draw();
}
}


When I attempt to run, I get stopped at mesh.Draw();, with the error

An unhandled exception of type 'System.InvalidOperationException' occurred in MonoGame.Framework.dll

Additional information: An error occurred while preparing to draw. This is probably because the current vertex declaration does not include all the elements required by the current vertex shader. The current vertex declaration includes these elements: SV_Position0, TEXCOORD0, COLOR0.

The model was provided by our lecturer. I have confirmed that the model shouldn't have any problems drawing. I have also found a similar issue to the one I am having, on the MonoGame forums, however the user reported having a current vertex declaration that includes "SVPOSITION0, TEXCOORD0, COLOR0", and the solution appeared to be changing "SVPOSITION0" to "SV_Position0".

While I am unsure how to directly change the vertex declaration, it appears I should already have the correct vertex declaration.

Why won't my mesh draw?

If I remove the effect.EnableDefaultLighting() line, it works, no problem. However, I then do not have light, and everything comes up as black. If I replace my ground.x model with the spaceship.x model supplied by Microsoft, it works with lighting. I presume my issue is with my model, however, I can not access spaceship.x to read in plain english, as I can with my ground.x.

Ground.x

xof 0303txt 0032
template Vector
{
<3d82ab5e-62da-11cf-ab39-0020af71e433>
FLOAT x;
FLOAT y;
FLOAT z;
}

template MeshFace
{
<3d82ab5f-62da-11cf-ab39-0020af71e433>
DWORD nFaceVertexIndices;
array DWORD faceVertexIndices[nFaceVertexIndices];
}

template Mesh
{
<3d82ab44-62da-11cf-ab39-0020af71e433>
DWORD nVertices;
array Vector vertices[nVertices];
DWORD nFaces;
array MeshFace faces[nFaces];
[...]
}

template Coords2d
{
<f6f23f44-7686-11cf-8f52-0040333594a3>
FLOAT u;
FLOAT v;
}

template MeshTextureCoords
{
<f6f23f40-7686-11cf-8f52-0040333594a3>
DWORD nTextureCoords;
array Coords2d;
textureCoords[nTextureCoords];
}

Mesh
{
4;
65536.00000;0.000000;-65536.00000;,
-65536.00000;0.000000;-65536.00000;,
-65536.00000;0.000000;65536.00000;,
65536.00000;0.000000;65536.00000;;
2;
3;0,1,2;,
3;0,2,3;;

MeshTextureCoords
{
4;
0.000000;0.000000;,
0.000000;1024.000000;,
1024.000000;1024.000000;,
1024.000000;0.000000;;
}

MeshMaterialList
{
1;
2;
0,
0;

Material
{
0.800000;0.800000;0.800000;1.000000;;
0.000000;
0.000000;0.000000;0.000000;;
0.000000;0.000000;0.000000;;

TextureFilename{"sanddf.png";}
}
}
}

• This is probably because the current vertex declaration does not include all the elements required by the current vertex shader. The current vertex declaration includes these elements: SV_Position0, TEXCOORD0, COLOR0. – MichaelHouse Aug 28 '15 at 13:28

I don't know how deep u actually get into 3D development, but the error message is quite clear, at this spot, if u understand what is written.

To draw something on a graphics card, u need Shaders to define how sth is interpreted on the graphics card to draw it.

There is allways a definition for structures u share between ur code and the graphcis card.

In this case, the defined Structure for a vertex define in one of ur shaders seems to have less fields then u try to hand to the draw method.

So to solve this u will have to upload ur shaders, so we can have a look at and the structure of ur Vertex Struct in ur code.

Or u do it by urself, as for now, I'm sure. u know what's going on, u will be able to fix it.

// effect.EnableDefaultLighting();


Disabling this line fixed the problem.

Remembering that in previous graphics applications, vectors also need normal co-ordinates in order to allow lighting, I turned off "EnableDefaultLighting". I had noticed there was only position, color and texture inputs being reported in the vertex declaration.

Whether this is the correct reasoning behind it, it solved the problem, immediately.