XNA Creators Club Online
Page 1 of 1 (3 items)
Sort Posts: Previous Next

Flightsim code still

Last post 11/22/2009 4:45 PM by Byron Nelson. 2 replies.
  • 11/21/2009 2:54 PM

    Flightsim code still

    My last post on this got no replies, and my updating of the question made it a bit messy, so thought I would write a new one.  Even with my research into the problem, I still cant honestly work out what the problem is.  Basically, if I have all this code in one class, it works, and the image is displayed on the screen.  But seperating the camera code into its own class, not changing any code, just passing params it requires, the only thing displayed on the screen is a purple background.  Is there any specific reason for this, the code is

    using System;
    using System.Collections.Generic;
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Audio;
    using Microsoft.Xna.Framework.Content;
    using Microsoft.Xna.Framework.GamerServices;
    using Microsoft.Xna.Framework.Graphics;
    using Microsoft.Xna.Framework.Input;
    using Microsoft.Xna.Framework.Net;
    using Microsoft.Xna.Framework.Storage;

    namespace Work2
    {
        public class Game1 : Microsoft.Xna.Framework.Game
        {
            GraphicsDeviceManager graphics;
            GraphicsDevice device;

            Effect effect;
            Matrix viewMatrix;
            Matrix projectionMatrix;

            Texture2D sceneryTexture;
            int[,] floorPlan;

            VertexBuffer cityVertexBuffer;
            VertexDeclaration texturedVertexDeclaration;

            Camera camera = new Camera();
            public Game1()
            {
                graphics = new GraphicsDeviceManager(this);
                Content.RootDirectory = "Content";
            }

            protected override void Initialize()
            {
                graphics.PreferredBackBufferWidth = 500;
                graphics.PreferredBackBufferHeight = 500;
                graphics.IsFullScreen = false;
                graphics.ApplyChanges();
                Window.Title = "Riemer's XNA Tutorials -- Series 2";

                LoadFloorPlan();

                base.Initialize();
            }

            protected override void LoadContent()
            {
                device = graphics.GraphicsDevice;


                effect = Content.Load<Effect>("effects");

                sceneryTexture = Content.Load<Texture2D>("texturemap");
                SetUpVertices();

                camera.SetUpCamera(device, viewMatrix, projectionMatrix);
            }

            private void LoadFloorPlan()
            {
                floorPlan = new int[,]
                 {
                     {0,0,0},
                     {0,1,0},
                     {0,0,0},
                 };
            }

            private void SetUpVertices()
            {
                int cityWidth = floorPlan.GetLength(0);
                int cityLength = floorPlan.GetLength(1);


                List<VertexPositionNormalTexture> verticesList = new List<VertexPositionNormalTexture>();
                for (int x = 0; x < cityWidth; x++)
                {
                    for (int z = 0; z < cityLength; z++)
                    {
                        int imagesInTexture = 11;
                        if (floorPlan[x, z] == 0)
                        {
                            verticesList.Add(new VertexPositionNormalTexture(new Vector3(x, 0, -z), new Vector3(0, 1, 0), new Vector2(0, 1)));
                            verticesList.Add(new VertexPositionNormalTexture(new Vector3(x, 0, -z - 1), new Vector3(0, 1, 0), new Vector2(0, 0)));
                            verticesList.Add(new VertexPositionNormalTexture(new Vector3(x + 1, 0, -z), new Vector3(0, 1, 0), new Vector2(1.0f / imagesInTexture, 1)));

                            verticesList.Add(new VertexPositionNormalTexture(new Vector3(x, 0, -z - 1), new Vector3(0, 1, 0), new Vector2(0, 0)));
                            verticesList.Add(new VertexPositionNormalTexture(new Vector3(x + 1, 0, -z - 1), new Vector3(0, 1, 0), new Vector2(1.0f / imagesInTexture, 0)));
                            verticesList.Add(new VertexPositionNormalTexture(new Vector3(x + 1, 0, -z), new Vector3(0, 1, 0), new Vector2(1.0f / imagesInTexture, 1)));
                        }
                    }
                }

                cityVertexBuffer = new VertexBuffer(device, verticesList.Count * VertexPositionNormalTexture.SizeInBytes, BufferUsage.WriteOnly);

                cityVertexBuffer.SetData<VertexPositionNormalTexture>(verticesList.ToArray());
                texturedVertexDeclaration = new VertexDeclaration(device, VertexPositionNormalTexture.VertexElements);
            }


            protected override void UnloadContent()
            {
            }

            protected override void Update(GameTime gameTime)
            {
                camera.UpdateCamera(device, viewMatrix, projectionMatrix);
                base.Update(gameTime);
            }

            protected override void Draw(GameTime gameTime)
            {
                device.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.DarkSlateBlue, 1.0f, 0);

                DrawCity();

                base.Draw(gameTime);
            }

            private void DrawCity()
            {
                effect.CurrentTechnique = effect.Techniques["Textured"];
                effect.Parameters["xWorld"].SetValue(Matrix.Identity);
                effect.Parameters["xView"].SetValue(viewMatrix);
                effect.Parameters["xProjection"].SetValue(projectionMatrix);
                effect.Parameters["xTexture"].SetValue(sceneryTexture);
                effect.Begin();
                foreach (EffectPass pass in effect.CurrentTechnique.Passes)
                {
                    pass.Begin();
                    device.VertexDeclaration = texturedVertexDeclaration;
                    device.Vertices[0].SetSource(cityVertexBuffer, 0, VertexPositionNormalTexture.SizeInBytes);
                    device.DrawPrimitives(PrimitiveType.TriangleList, 0, cityVertexBuffer.SizeInBytes / VertexPositionNormalTexture.SizeInBytes / 3);
                    pass.End();
                }
                effect.End();
            }
        }
    }


    using System;
    using System.Collections.Generic;
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Audio;
    using Microsoft.Xna.Framework.Content;
    using Microsoft.Xna.Framework.GamerServices;
    using Microsoft.Xna.Framework.Graphics;
    using Microsoft.Xna.Framework.Input;
    using Microsoft.Xna.Framework.Net;
    using Microsoft.Xna.Framework.Storage;

    namespace Work2
    {
        class Camera
        {
            Vector3 xwingPosition = new Vector3(8, 1, -3);
            Quaternion xwingRotation = Quaternion.Identity;

            public void SetUpCamera(GraphicsDevice device, Matrix viewMatrix, Matrix projectionMatrix)
            {
                viewMatrix = Matrix.CreateLookAt(new Vector3(3, 5, 2), new Vector3(2, 0, -1), new Vector3(0, 1, 0));
                projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, device.Viewport.AspectRatio, 0.2f, 500.0f);
            }

            public void UpdateCamera(GraphicsDevice device, Matrix viewMatrix, Matrix projectionMatrix)
            {
                Vector3 campos = new Vector3(0, 0.1f, 0.6f);
                campos = Vector3.Transform(campos, Matrix.CreateFromQuaternion(xwingRotation));
                campos += xwingPosition;

                Vector3 camup = new Vector3(0, 1, 0);
                camup = Vector3.Transform(camup, Matrix.CreateFromQuaternion(xwingRotation));

                viewMatrix = Matrix.CreateLookAt(campos, xwingPosition, camup);
                projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, device.Viewport.AspectRatio, 0.2f, 500.0f);
            }
        }
    }



    Cheers
  • 11/22/2009 4:04 PM In reply to

    Re: Flightsim code still

    You should use the Format Code Block tool, it makes it much easier to read code.

    The Matrix type is a Value Type, this means if you pass a Matrix into a method, you are passing a copy, so if the method changes any values on the Matrix, the changes are not applied to the original Matrix, and they get lost as soon as the method ends.

    This is what is happening with your SetupCamera and UpdateCamera methods. They are not changing the values of the matrices on your Game1 class.

    You need to put the view and projection matrices on the Camera class, and then expose them with public property getters and then the Game1 class gets the matrices from the camera.

     
    class Camera 
         Matrix viewMatrix; 
         Matrix projectionMatrix; 
     
         public Matrix ViewMatrix { get { return viewMatrix; } } 
         public Matrix ProjectionMatrix { get { return projectionMatrix; } } 
     
    class Game1 
    .... 
            private void DrawCity() 
            { 
                ... 
                effect.Parameters["xView"].SetValue(camera.ViewMatrix); 
                effect.Parameters["xProjection"].SetValue(camera.ProjectionMatrix); 
       
     
     

    Game hobbyist hell-bent on coding a diabolical Matrix
  • 11/22/2009 4:45 PM In reply to

    Re: Flightsim code still

    .. what Craig said, or you could keep your code as is, but pass the matrices into the update by reference (use ref keyword).  Its like passing a pointer to the matrix values, so when you change them in your function you'll change the original values too.

    Best,
    Byron
    ..shaders make you feel... powerful, or very very stupid.
    http://drjbn.spaces.live.com/
Page 1 of 1 (3 items) Previous Next