The message:
Unable to cast object of type 'Microsoft.Xna.Framework.Graphics.Effect' to type 'Microsoft.Xna.Framework.Graphics.BasicEffect'
The code:
foreach (ModelMesh mesh in xwingModel.Meshes)
{
// This is where the mesh orientation is set, as well
// as our camera and projection.
foreach (BasicEffect Effects in mesh.Effects)
{
Effects.EnableDefaultLighting();
Effects.World = transforms[mesh.ParentBone.Index] *
//Matrix.CreateRotationY(modelRotation)
Matrix.CreateTranslation(modelPosition);
Effects.View = Matrix.CreateLookAt(cameraPosition,
Vector3.Zero, Vector3.Up);
Effects.Projection = Matrix.CreatePerspectiveFieldOfView(
MathHelper.ToRadians(45.0f), aspectRatio,
1.0f, 10000.0f);
}
// Draw the mesh, using the effects set above.
mesh.Draw();
}
The exception is on this part:
BasicEffect Effects
The full exception message:
System.InvalidCastException was unhandled
HResult=-2147467262
Message=Unable to cast object of type 'Microsoft.Xna.Framework.Graphics.Effect' to type 'Microsoft.Xna.Framework.Graphics.BasicEffect'.
Source=MyFlightSimulator
StackTrace:
at MyFlightSimulator.Game1.DrawModel() in Game1.cs:line 279
at MyFlightSimulator.Game1.Draw(GameTime gameTime) in Game1.cs:line 133
at Microsoft.Xna.Framework.Game.DrawFrame()
at Microsoft.Xna.Framework.Game.Tick()
at Microsoft.Xna.Framework.Game.HostIdle(Object sender, EventArgs e)
at Microsoft.Xna.Framework.GameHost.OnIdle()
at Microsoft.Xna.Framework.WindowsGameHost.RunOneFrame()
at Microsoft.Xna.Framework.WindowsGameHost.ApplicationIdle(Object sender, EventArgs e)
at System.Windows.Forms.Application.ThreadContext.System.Windows.Forms.UnsafeNativeMethods.IMsoComponent.FDoIdle(Int32 grfidlef)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at Microsoft.Xna.Framework.WindowsGameHost.Run()
at Microsoft.Xna.Framework.Game.RunGame(Boolean useBlockingRun)
at Microsoft.Xna.Framework.Game.Run()
at MyFlightSimulator.Program.Main(String[] args) in Program.cs:line 15
InnerException:
Game1 line 279 is:
foreach (BasicEffect Effects in mesh.Effects)
Program line 15 is:
game.Run();
This is the complete method code:
private void DrawModel()
{
Matrix[] transforms = new Matrix[xwingModel.Bones.Count];
xwingModel.CopyAbsoluteBoneTransformsTo(transforms);
// Draw the model. A model can have multiple meshes, so loop.
foreach (ModelMesh mesh in xwingModel.Meshes)
{
// This is where the mesh orientation is set, as well
// as our camera and projection.
foreach (BasicEffect Effects in mesh.Effects)
{
Effects.EnableDefaultLighting();
Effects.World = transforms[mesh.ParentBone.Index] *
//Matrix.CreateRotationY(modelRotation)
Matrix.CreateTranslation(modelPosition);
Effects.View = Matrix.CreateLookAt(cameraPosition,
Vector3.Zero, Vector3.Up);
Effects.Projection = Matrix.CreatePerspectiveFieldOfView(
MathHelper.ToRadians(45.0f), aspectRatio,
1.0f, 10000.0f);
}
// Draw the mesh, using the effects set above.
mesh.Draw();
}
}