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";}
}
}
}