Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2012
    Posts
    1

    Exclamation XNA control throws an exception as a GraphicsDeviceControl

    For some reason the Red X (An exception) is thrown and I cannot work out why in this GraphicsDeviceControl:
    Code:
    #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, Camera.cs:
    Code:
    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.

  2. #2
    Join Date
    Aug 2007
    Location
    Montreal, Canada
    Posts
    616

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •