none
XNA control throws an exception as a GraphicsDeviceControl

    Question

  • For some reason the Red X (Not implemented exception) is thrown and I cannot work out why in this GraphicsDeviceControl:

    #region File Description
    //-----------------------------------------------------------------------------
    //SimulationControl.cs
    //-----------------------------------------------------------------------------
    #endregion
    
    #region Using Statements
    using System;
    
    using System.Diagnostics;
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Audio;
    using Microsoft.Xna.Framework.Content;
    using Microsoft.Xna.Framework.Graphics;
    using Microsoft.Xna.Framework.Input;
    using Microsoft.Xna.Framework.GamerServices;
    using Microsoft.Xna.Framework.Media;
    using Microsoft.Xna.Framework.Net;
    using Microsoft.Xna.Framework.Storage;
    using Application = System.Windows.Forms.Application;
    #endregion
    
    namespace _3DProjectileMotion
    {
        /// <summary>
        /// Example control inherits from GraphicsDeviceControl, which allows it to
        /// render using a GraphicsDevice. This control shows how to draw animating
        /// 3D graphics inside a WinForms application. It hooks the Application.Idle
        /// event, using this to invalidate the control, which will cause the animation
        /// to constantly redraw.
        /// </summary>
        /// 
        
    
    class SimulationControl : GraphicsDeviceControl
        {
            BasicEffect effect;
            Stopwatch timer;
            ContentManager content;
            
            Camera camera = new Camera();
            Matrix cubeWorld;
            Matrix coneWorld;
            
            Model cubeModel;
            Model coneModel;
            
    
    
            
            // Vertex positions and colors used to display a spinning triangle.
            public VertexPositionColor[] Vertices =
            {
                new VertexPositionColor(new Vector3(9, 7, 6), Color.Black),
                new VertexPositionColor(new Vector3(8, 5, 4), Color.Black),
                new VertexPositionColor(new Vector3(7, 21, 12), Color.Black),
            };
    
    
            /// <summary>
            /// Initializes the control.
            /// </summary>
            ///
            protected override void Initialize()
            {
                content = new ContentManager(Services, "Content");
                cubeModel = content.Load<Model>("CubeModel");
                coneModel = content.Load<Model>("ConeModel");
    
                cubeWorld = Matrix.Identity;
                coneWorld = Matrix.Identity;
    
                // Create our effect.
                effect = new BasicEffect(GraphicsDevice);
    
                effect.VertexColorEnabled = true;
    
                // Start the animation timer.
                timer = Stopwatch.StartNew();
    
                // Hook the idle event to constantly redraw our animation.
                Application.Idle += delegate { Invalidate(); };
    
               
            }
            
            
            /// <summary>
            /// Draws the control.
            /// </summary>
            protected override void Draw()
            {
                GraphicsDevice.Clear(Color.LightSteelBlue);
    
                // Set renderstates.
                GraphicsDevice.RasterizerState = RasterizerState.CullNone;
    
                // Draw the triangle.
                effect.CurrentTechnique.Passes[0].Apply();
    
                GraphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleList,
                                                  Vertices, 0, 1);
                //Draws both models
                DrawModel(cubeModel, cubeWorld);
                DrawModel(coneModel, coneWorld);
            }
    
            protected  void Update(GameTime gameTime)
            {
                KeyboardState keyBoardState = Keyboard.GetState();
    
                //Rotate Cube along its Up Vector
                if (keyBoardState.IsKeyDown(Keys.X))
                {
                    cubeWorld = Matrix.CreateFromAxisAngle(Vector3.Up, .02f) * cubeWorld;
                }
                if (keyBoardState.IsKeyDown(Keys.Z))
                {
                    cubeWorld = Matrix.CreateFromAxisAngle(Vector3.Up, -.02f) * cubeWorld;
                }
    
                //Move Cube Forward, Back, Left, and Right
                if (keyBoardState.IsKeyDown(Keys.Up))
                {
                    cubeWorld *= Matrix.CreateTranslation(cubeWorld.Forward);
                }
                if (keyBoardState.IsKeyDown(Keys.Down))
                {
                    cubeWorld *= Matrix.CreateTranslation(cubeWorld.Backward);
                }
                if (keyBoardState.IsKeyDown(Keys.Left))
                {
                    cubeWorld *= Matrix.CreateTranslation(-cubeWorld.Right);
                }
                if (keyBoardState.IsKeyDown(Keys.Right))
                {
                    cubeWorld *= Matrix.CreateTranslation(cubeWorld.Right);
                }
            }
            private void DrawModel(Model model, Matrix worldMatrix)
            {
                Matrix[] modelTransforms = new Matrix[model.Bones.Count];
                model.CopyAbsoluteBoneTransformsTo(modelTransforms);
    
                foreach (ModelMesh mesh in model.Meshes)
                {
                    foreach (BasicEffect effect in mesh.Effects)
                    {
                        effect.EnableDefaultLighting();
                        effect.World = modelTransforms[mesh.ParentBone.Index] * worldMatrix;
                        effect.View = camera.viewMatrix;
                        effect.Projection = camera.projectionMatrix;
                    }
                    mesh.Draw();
                }
            }
        }
    }
    

    This uses functions defined in a seperate class, called Camera.cs:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Graphics;
    using Microsoft.Xna.Framework.Input;
    
    namespace _3DProjectileMotion
    {
        public class Camera
        {
            private Vector3 position;
            private Vector3 target;
            public Matrix viewMatrix, projectionMatrix;
    
            public Camera()
            {
                ResetCamera();
            }
    
            public void ResetCamera()
            {
                position = new Vector3(0, 0, 50);
                target = new Vector3();
    
                viewMatrix = Matrix.Identity;
                projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45.0f), 16 / 9, .5f, 500f);
            }
    
            public void Update()
            {
                UpdateViewMatrix();
            }
    
            private void UpdateViewMatrix()
            {
                viewMatrix = Matrix.CreateLookAt(position, target, Vector3.Up);
            }
    
    
        }
    }
    

    I cannot work out what is going wrong, and because no errors are being thrown, presumably it is syntaxically correct. 

    Any help would be appreciated.

    Monday, March 12, 2012 10:27 AM

All replies

  • Hi,

    I am really wondering: You get a NotImplementedException? What is the exact stacktrace?

    In your code is no throw new NotImplementedException(); so that code is not the reason for this exception.

    Often templates have such code. Or if you let the IDE autoimplement an interface. So check your code for NotImplementedException to find out, where you still have areas with code missing.

    Or if it is inside a library, you have to find out where this is thrown and check out that you used the library correctly. Maybe ask the vendor what is going wrong.

    With kind regards,

    Konrad

    Monday, March 12, 2012 10:53 AM
  • I've checked all the other classes, and there aren't any throw NotImplimentedExceptions in the code, as far as I am aware. I haven't used any libraries other than the stock C# ones Visual Studio puts in, however I have added the GraphicsDeviceControl.cs class and the GraphicsDeviceService.cs ones, which are freely available on MSDN.

    I'm quite new to XNA, so I have no idea what exception it is, a red X just appears on the control. It is just similar to a not implimented exception. Also, I'm not sure how to get the stacktrace in Visual Studion 2010, I can't seem to find it.
    Monday, March 12, 2012 2:50 PM
  • Bump
    Monday, March 12, 2012 8:08 PM
  • Bump

    Tuesday, March 13, 2012 7:51 PM
  • Hi theBIGmac666,

    You can consider posting it at the following more appropriate forum for more efficient responses. Thanks!

    http://forums.create.msdn.com/forums/default.aspx?GroupID=6


    Bob Shen [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, March 14, 2012 2:37 AM