Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2013
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 22.82 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.InteropServices;
  4. using Microsoft.Xna.Framework;
  5. using Microsoft.Xna.Framework.Audio;
  6. using Microsoft.Xna.Framework.Content;
  7. using Microsoft.Xna.Framework.GamerServices;
  8. using Microsoft.Xna.Framework.Graphics;
  9. using Microsoft.Xna.Framework.Input;
  10. using Microsoft.Xna.Framework.Net;
  11. using Microsoft.Xna.Framework.Storage;
  12.  
  13. namespace MyFirst3D
  14. {
  15.     /// <summary>
  16.     /// This is the main type for your game
  17.     /// </summary>
  18.     public class Game1 : Microsoft.Xna.Framework.Game
  19.     {
  20.  
  21.         public struct VertexPositionColorNormal
  22.         {
  23.             public Vector3 Position;
  24.             public Color Color;
  25.             public Vector3 Normal;
  26.  
  27.             public readonly static VertexDeclaration VertexDeclaration = new VertexDeclaration
  28.             (
  29.                 new VertexElement(0, VertexElementFormat.Vector3, VertexElementUsage.Position, 0),
  30.                 new VertexElement(sizeof(float) * 3, VertexElementFormat.Color, VertexElementUsage.Color, 0),
  31.                 new VertexElement(sizeof(float) * 3 + 4, VertexElementFormat.Vector3, VertexElementUsage.Normal, 0)
  32.             );
  33.         }
  34.  
  35.         [DllImport("user32.dll", CharSet = CharSet.Auto)]
  36.         public static extern uint MessageBox(IntPtr hWnd, String text, String caption, uint type);
  37.  
  38.         //*** Camera Part***\\
  39.         float leftrightRot = MathHelper.PiOver2;
  40.         float updownRot = -MathHelper.Pi / 10.0f;
  41.         const float rotationSpeed = 0.3f;
  42.         const float moveSpeed = 30.0f;
  43.         //*************************************\\
  44.         VertexBuffer myVertexBuffer;
  45.         IndexBuffer myIndexBuffer;
  46.         GraphicsDeviceManager graphics;
  47.         GraphicsDevice device;
  48.         SpriteBatch spriteBatch;
  49.         Model myModel;
  50.         float aspectRatio;
  51.         FpsMonitor fpsm;
  52.         private SpriteFont Arial10;
  53.         Vector3 cameraPosition;
  54.         MouseState previousState;
  55.         MouseState currentState;
  56.         const float MAXDELAY = 0.5f; // seconds
  57.         DateTime previousClick;
  58.         Effect effect;
  59.         VertexPositionColorNormal[] vertices;
  60.         Matrix viewMatrix;
  61.         Matrix projectionMatrix;
  62.         int[] indices;
  63.         Matrix worldMatrix;
  64.         Matrix rotation;
  65.         MouseState originalMouseState;
  66.  
  67.         private float angle = 0f;
  68.         private float angleRightLeft = 0f;
  69.         private float angleUpDown = 0f;
  70.         private int terrainWidth = 4;
  71.         private int terrainHeight = 3;
  72.         private float[,] heightData;
  73.  
  74.         public Game1()
  75.         {
  76.             graphics = new GraphicsDeviceManager(this);
  77.             Content.RootDirectory = "Content";
  78.         }
  79.  
  80.         /// <summary>
  81.         /// Allows the game to perform any initialization it needs to before starting to run.
  82.         /// This is where it can query for any required services and load any non-graphic
  83.         /// related content.  Calling base.Initialize will enumerate through any components
  84.         /// and initialize them as well.
  85.         /// </summary>
  86.         protected override void Initialize()
  87.         {
  88.             // TODO: Add your initialization logic here
  89.  
  90.             base.Initialize();
  91.  
  92.             //*** Camera Part***\\
  93.             Matrix cameraRotation = Matrix.CreateRotationX(updownRot) * Matrix.CreateRotationY(leftrightRot);
  94.  
  95.             Vector3 cameraOriginalTarget = new Vector3(0, 0, -1);
  96.             Vector3 cameraRotatedTarget = Vector3.Transform(cameraOriginalTarget, cameraRotation);
  97.             Vector3 cameraFinalTarget = cameraPosition + cameraRotatedTarget;
  98.  
  99.             Vector3 cameraOriginalUpVector = new Vector3(0, 1, 0);
  100.             Vector3 cameraRotatedUpVector = Vector3.Transform(cameraOriginalUpVector, cameraRotation);
  101.  
  102.             viewMatrix = Matrix.CreateLookAt(cameraPosition, cameraFinalTarget, cameraRotatedUpVector);
  103.             //*******************************************************************************************\\
  104.  
  105.             rotation = Matrix.Identity;
  106.             fpsm = new FpsMonitor(); // in constructor
  107.             this.IsMouseVisible = true;
  108.             cameraPosition = new Vector3(0.0f, 50.0f, 5000.0f);
  109.             //this.graphics.PreferredBackBufferWidth = 1920;
  110.             //this.graphics.PreferredBackBufferHeight = 1080;
  111.             //this.graphics.IsFullScreen = true;
  112.             this.graphics.ApplyChanges();
  113.         }
  114.  
  115.         /// <summary>
  116.         /// LoadContent will be called once per game and is the place to load
  117.         /// all of your content.
  118.         /// </summary>
  119.         protected override void LoadContent()
  120.         {
  121.             spriteBatch = new SpriteBatch(GraphicsDevice);
  122.             device = graphics.GraphicsDevice;
  123.             Mouse.SetPosition(GraphicsDevice.Viewport.Width / 2, GraphicsDevice.Viewport.Height / 2);//device.Viewport.Width / 2, device.Viewport.Height / 2);
  124.             originalMouseState = Mouse.GetState();
  125.             currentState = Mouse.GetState();
  126.             UpdateViewMatrix(viewMatrix);
  127.             // The aspect ratio determines how to scale 3d to 2d projection.
  128.             // Create a new SpriteBatch, which can be used to draw textures.
  129.             // Create a new SpriteBatch, which can be used to draw textures.
  130.             spriteBatch = new SpriteBatch(GraphicsDevice);
  131.             effect = Content.Load<Effect>("effects"); SetUpCamera();
  132.             Texture2D heightMap = Content.Load<Texture2D>("heightmap"); LoadHeightData(heightMap);
  133.             SetUpVertices();
  134.             SetUpIndices();
  135.             CalculateNormals();
  136.             myModel = Content.Load<Model>("Models\\p1_wedge");
  137.             aspectRatio = graphics.GraphicsDevice.Viewport.AspectRatio;
  138.             Arial10 = Content.Load<SpriteFont>("Consolas");
  139.             // TODO: use this.Content to load your game content here
  140.             worldMatrix = Matrix.CreateTranslation(-terrainWidth / 2.0f, 0, terrainHeight / 2.0f) * Matrix.CreateRotationY(angle);
  141.             CopyToBuffers();
  142.         }
  143.  
  144.         /// <summary>
  145.         /// UnloadContent will be called once per game and is the place to unload
  146.         /// all content.
  147.         /// </summary>
  148.         protected override void UnloadContent()
  149.         {
  150.             // TODO: Unload any non ContentManager content here
  151.         }
  152.  
  153.         /// <summary>
  154.         /// Allows the game to run logic such as updating the world,
  155.         /// checking for collisions, gathering input, and playing audio.
  156.         /// </summary>
  157.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  158.         protected override void Update(GameTime gameTime)
  159.         {
  160.             //angle += 0.005f;
  161.             fpsm.Update();// in update I think first line
  162.             // Allows the game to exit
  163.             if (GamePad.GetState(PlayerIndex.One).Buttons.Back ==
  164.                 ButtonState.Pressed)
  165.                 this.Exit();
  166.  
  167.             float timeDifference = (float)gameTime.ElapsedGameTime.TotalMilliseconds / 1000.0f;
  168.             ProcessInput(timeDifference);
  169.             ProcessInputCamera(timeDifference);
  170.             UpdateViewMatrix(viewMatrix);
  171.             modelRotation += (float)gameTime.ElapsedGameTime.TotalMilliseconds *
  172.                 MathHelper.ToRadians(0.1f);
  173.             base.Update(gameTime);
  174.         }
  175.  
  176.         /// <summary>
  177.         /// This is called when the game should draw itself.
  178.         /// </summary>
  179.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  180.         // Set the position of the model in world space, and set the rotation.
  181.         Vector3 modelPosition = Vector3.Zero;
  182.         float modelRotation = 0.0f;
  183.  
  184.         // Set the position of the camera in world space, for our view matrix.
  185.         //Vector3 cameraPosition = new Vector3(0.0f, 50.0f, 5000.0f);
  186.  
  187.         protected override void Draw(GameTime gameTime)
  188.         {
  189.             graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
  190.             //device.Clear(Color.Black);
  191.             device.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.Black, 1.0f, 0);
  192.             RasterizerState rs = new RasterizerState();
  193.             rs.CullMode = CullMode.None;
  194.  
  195.             // When your run this code, you will indeed see a nicely colored network of lines. When we want to see the whole colored terrain, we just have to remove this line (or set it to FillMode.Solid):
  196.             //rs.FillMode = FillMode.WireFrame;
  197.             device.RasterizerState = rs;
  198.             //Matrix worldMatrix = Matrix.CreateTranslation(-terrainWidth / 2.0f, 0, terrainHeight / 2.0f);
  199.             //worldMatrix = Matrix.CreateTranslation(-terrainWidth / 2.0f, 0, terrainHeight / 2.0f) * Matrix.CreateRotationY(angle);
  200.             /*if (rotationDirection == true)
  201.             {
  202.                 worldMatrix = Matrix.CreateFromAxisAngle(this.rotation.Left, angleRightLeft);
  203.                 worldMatrix = Matrix.CreateTranslation(-terrainWidth / 2.0f, 0, terrainHeight / 2.0f) * Matrix.CreateRotationY(angleRightLeft);
  204.             }
  205.             else
  206.             {
  207.                 worldMatrix = Matrix.CreateTranslation(-terrainWidth / 2.0f, 0, terrainHeight / 2.0f) * Matrix.CreateRotationX(angleUpDown);
  208.             }*/
  209.             worldMatrix = Matrix.CreateTranslation(-terrainWidth / 2.0f, 0, terrainHeight / 2.0f) * Matrix.CreateRotationX(angleUpDown) * Matrix.CreateRotationY(angleRightLeft);
  210.             effect.CurrentTechnique = effect.Techniques["Colored"];
  211.             Vector3 lightDirection = new Vector3(1.0f, -1.0f, -1.0f);
  212.             lightDirection.Normalize();
  213.             effect.Parameters["xLightDirection"].SetValue(lightDirection);
  214.             effect.Parameters["xAmbient"].SetValue(0.6f); effect.Parameters["xEnableLighting"].SetValue(true);        
  215.             effect.Parameters["xView"].SetValue(viewMatrix);
  216.             effect.Parameters["xProjection"].SetValue(projectionMatrix);
  217.             effect.Parameters["xWorld"].SetValue(worldMatrix);
  218.  
  219.             foreach (EffectPass pass in effect.CurrentTechnique.Passes)
  220.             {
  221.                 pass.Apply();
  222.                 //device.DrawUserIndexedPrimitives(PrimitiveType.TriangleList, vertices, 0, vertices.Length, indices, 0, indices.Length / 3, VertexPositionColorNormal.VertexDeclaration);
  223.                 device.Indices = myIndexBuffer;
  224.                 device.SetVertexBuffer(myVertexBuffer);
  225.                 device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, vertices.Length, 0, indices.Length / 3);
  226.             }
  227.             // Copy any parent transforms.
  228.             Matrix[] transforms = new Matrix[myModel.Bones.Count];
  229.             myModel.CopyAbsoluteBoneTransformsTo(transforms);
  230.  
  231.             // Draw the model. A model can have multiple meshes, so loop.
  232.             foreach (ModelMesh mesh in myModel.Meshes)
  233.             {
  234.                 // This is where the mesh orientation is set, as well
  235.                 // as our camera and projection.
  236.                 foreach (BasicEffect Effect in mesh.Effects)
  237.                 {
  238.                     Effect.EnableDefaultLighting();
  239.                     Effect.World = transforms[mesh.ParentBone.Index] *
  240.                         Matrix.CreateRotationY(modelRotation)
  241.                         * Matrix.CreateTranslation(modelPosition);
  242.                     Effect.View = Matrix.CreateLookAt(cameraPosition,
  243.                         Vector3.Zero, Vector3.Up);
  244.                     Effect.Projection = Matrix.CreatePerspectiveFieldOfView(
  245.                         MathHelper.ToRadians(45.0f), aspectRatio,
  246.                         1.0f, 10000.0f);
  247.                 }
  248.                 // Draw the mesh, using the effects set above.
  249.                 mesh.Draw();
  250.             }
  251.             base.Draw(gameTime);
  252.             spriteBatch.Begin();  // in draw Last lines probably even after base.Draw(gameTime);
  253.             fpsm.Draw(spriteBatch, Arial10, new Vector2(5, 5), Color.White);
  254.             spriteBatch.End();
  255.         }
  256.  
  257.         /*private void UpdateViewMatrix()
  258.         {
  259.            
  260.         }*/
  261.  
  262.         private void ProcessInput(float amount)
  263.         {
  264.             previousState = currentState;
  265.             currentState = Mouse.GetState();
  266.             Vector3 moveVector = new Vector3(0 , 0 , 0);
  267.             KeyboardState keyState = Keyboard.GetState();
  268.             if (keyState.IsKeyDown(Keys.E))
  269.             {
  270.                 this.Exit();
  271.             }
  272.             if (keyState.IsKeyDown(Keys.Up) || keyState.IsKeyDown(Keys.W))
  273.             {
  274.                 //cameraPosition += new Vector3(0.0f, 50.0f, +100);
  275.                 angleUpDown += 0.05f;
  276.             }
  277.  
  278.             if (keyState.IsKeyDown(Keys.Down) || keyState.IsKeyDown(Keys.S))
  279.             {
  280.                 //cameraPosition -= new Vector3(0.0f, 50.0f, +100);wswdawds
  281.                 angleUpDown -= 0.05f;
  282.             }
  283.  
  284.             if (keyState.IsKeyDown(Keys.Right) || keyState.IsKeyDown(Keys.D))
  285.             {
  286.                 //cameraPosition += new Vector3(1, 0, 0);
  287.                 angleRightLeft += 0.05f;
  288.             }
  289.  
  290.             if (keyState.IsKeyDown(Keys.Left) || keyState.IsKeyDown(Keys.A))
  291.             {
  292.                 //cameraPosition += new Vector3(-1, 0, 0);
  293.                 angleRightLeft -= 0.05f;
  294.             }
  295.  
  296.             if (keyState.IsKeyDown(Keys.Q))
  297.                 cameraPosition += new Vector3(0, 1, 0);
  298.             if (keyState.IsKeyDown(Keys.Z))
  299.                 cameraPosition += new Vector3(0, -1, 0);
  300.             if (keyState.IsKeyDown(Keys.Escape))
  301.             {
  302.                 this.graphics.PreferredBackBufferWidth = 800;
  303.                 this.graphics.PreferredBackBufferHeight = 600;
  304.                 this.graphics.IsFullScreen = false;
  305.                 this.graphics.ApplyChanges();
  306.             }
  307.             if (this.graphics.PreferredBackBufferWidth < 1920)
  308.             {
  309.                 /*if (WasMouseLeftClick())
  310.                 {
  311.                     changeScreenNode(this.graphics, 1920, 1080, true);
  312.                 }*/
  313.  
  314.                 if (WasDoubleClick())
  315.                 {
  316.                     changeScreenNode(this.graphics, 1920, 1080, true);
  317.                 }
  318.             }
  319.  
  320.             if (WasMouseLeftClick())
  321.             {
  322.                 previousClick = DateTime.Now;
  323.             }
  324.         }
  325.  
  326.  
  327.         private void changeScreenNode(GraphicsDeviceManager gdm , int width , int height , bool fullscreen)
  328.         {
  329.             gdm.PreferredBackBufferWidth = width;
  330.             gdm.PreferredBackBufferHeight = height;
  331.             gdm.IsFullScreen = fullscreen;
  332.             gdm.ApplyChanges();
  333.         }
  334.  
  335.         bool WasMouseLeftClick()
  336.         {
  337.             return (previousState.LeftButton == ButtonState.Pressed) && (currentState.LeftButton == ButtonState.Released);
  338.         }
  339.  
  340.         bool WasDoubleClick()
  341.         {
  342.             return WasMouseLeftClick() // We have at least one click, and
  343.                 && (DateTime.Now - previousClick).TotalSeconds < MAXDELAY;
  344.         }
  345.  
  346.         private void SetUpVertices()
  347.         {
  348.             // This part detect the minimum and maximum heights in our image. We will store these in the minHeight and maxHeight variables.
  349.             vertices = new VertexPositionColorNormal[terrainWidth * terrainHeight];
  350.             float minHeight = float.MaxValue;
  351.             float maxHeight = float.MinValue;
  352.             for (int x = 0; x < terrainWidth; x++)
  353.             {
  354.                 for (int y = 0; y < terrainHeight; y++)
  355.                 {
  356.                     if (heightData[x, y] < minHeight)
  357.                         minHeight = heightData[x, y];
  358.                     if (heightData[x, y] > maxHeight)
  359.                         maxHeight = heightData[x, y];
  360.                     vertices[x + y * terrainWidth].Position = new Vector3(x, heightData[x, y], -y);
  361.  
  362.                     if (heightData[x, y] < minHeight + (maxHeight - minHeight) / 4)
  363.                         vertices[x + y * terrainWidth].Color = Color.Blue;
  364.                     else if (heightData[x, y] < minHeight + (maxHeight - minHeight) * 2 / 4)
  365.                         vertices[x + y * terrainWidth].Color = Color.Green;
  366.                     else if (heightData[x, y] < minHeight + (maxHeight - minHeight) * 3 / 4)
  367.                         vertices[x + y * terrainWidth].Color = Color.Brown;
  368.                     else
  369.                         vertices[x + y * terrainWidth].Color = Color.White;
  370.                 }
  371.             }
  372.             // You check for each point of our grid if the current point’s height is below the current minHeight or above the current maxHeight. If it is, store the current height in the corresponding variable.
  373.             // With these variables filled, you can specify the 4 regions of your colors
  374.  
  375.  
  376.  
  377.             /*vertices = new VertexPositionColor[terrainWidth * terrainHeight];
  378.             for (int x = 0; x < terrainWidth; x++)
  379.             {
  380.                 for (int y = 0; y < terrainHeight; y++)
  381.                 {
  382.                     vertices[x + y * terrainWidth].Position = new Vector3(x, heightData[x, y], -y);
  383.                     vertices[x + y * terrainWidth].Color = Color.White;
  384.                 }
  385.             }*/
  386.         }
  387.  
  388.         private void SetUpIndices()
  389.         {
  390.             indices = new int[(terrainWidth - 1) * (terrainHeight - 1) * 6];
  391.             int counter = 0;
  392.             for (int y = 0; y < terrainHeight - 1; y++)
  393.             {
  394.                 for (int x = 0; x < terrainWidth - 1; x++)
  395.                 {
  396.                     int lowerLeft = x + y * terrainWidth;
  397.                     int lowerRight = (x + 1) + y * terrainWidth;
  398.                     int topLeft = x + (y + 1) * terrainWidth;
  399.                     int topRight = (x + 1) + (y + 1) * terrainWidth;
  400.  
  401.                     indices[counter++] = topLeft;
  402.                     indices[counter++] = lowerRight;
  403.                     indices[counter++] = lowerLeft;
  404.  
  405.                     indices[counter++] = topLeft;
  406.                     indices[counter++] = topRight;
  407.                     indices[counter++] = lowerRight;
  408.                 }
  409.             }
  410.         }
  411.  
  412.         private void LoadHeightData(Texture2D heightMap)
  413.         {
  414.             terrainWidth = heightMap.Width;
  415.             terrainHeight = heightMap.Height;
  416.  
  417.             Color[] heightMapColors = new Color[terrainWidth * terrainHeight];
  418.             heightMap.GetData(heightMapColors);
  419.  
  420.             heightData = new float[terrainWidth, terrainHeight];
  421.             for (int x = 0; x < terrainWidth; x++)
  422.                 for (int y = 0; y < terrainHeight; y++)
  423.                     heightData[x, y] = heightMapColors[x + y * terrainWidth].R / 5.0f;
  424.         }
  425.  
  426.         private void SetUpCamera()
  427.         {
  428.             viewMatrix = Matrix.CreateLookAt(new Vector3(60, 80, -80), new Vector3(0, 0, 0), new Vector3(0, 1, 0));
  429.             projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, device.Viewport.AspectRatio, 1.0f, 300.0f);
  430.         }
  431.  
  432.         private void CalculateNormals()
  433.         {
  434.             for (int i = 0; i < vertices.Length; i++)
  435.                 vertices[i].Normal = new Vector3(0, 0, 0);
  436.             for (int i = 0; i < indices.Length / 3; i++)
  437.             {
  438.                 int index1 = indices[i * 3];
  439.                 int index2 = indices[i * 3 + 1];
  440.                 int index3 = indices[i * 3 + 2];
  441.  
  442.                 Vector3 side1 = vertices[index1].Position - vertices[index3].Position;
  443.                 Vector3 side2 = vertices[index1].Position - vertices[index2].Position;
  444.                 Vector3 normal = Vector3.Cross(side1, side2);
  445.  
  446.                 vertices[index1].Normal += normal;
  447.                 vertices[index2].Normal += normal;
  448.                 vertices[index3].Normal += normal;
  449.             }
  450.             for (int i = 0; i < vertices.Length; i++)
  451.                 vertices[i].Normal.Normalize();
  452.         }
  453.  
  454.         private void CopyToBuffers()
  455.         {
  456.             myVertexBuffer = new VertexBuffer(device, VertexPositionColorNormal.VertexDeclaration, vertices.Length, BufferUsage.WriteOnly);
  457.             myVertexBuffer.SetData(vertices);
  458.             myIndexBuffer = new IndexBuffer(device, typeof(int), indices.Length, BufferUsage.WriteOnly);
  459.             myIndexBuffer.SetData(indices);
  460.         }
  461.  
  462.         //*** Camera Part***\\
  463.         private void ProcessInputCamera(float amount)
  464.         {
  465.             MouseState currentMouseState = Mouse.GetState();
  466.             if (currentMouseState != originalMouseState)
  467.             {
  468.                 float xDifference = currentMouseState.X - originalMouseState.X;
  469.                 float yDifference = currentMouseState.Y - originalMouseState.Y;
  470.                 leftrightRot -= rotationSpeed * xDifference * amount;
  471.                 updownRot -= rotationSpeed * yDifference * amount;
  472.                 Mouse.SetPosition(device.Viewport.Width / 2, device.Viewport.Height / 2);
  473.             }
  474.  
  475.             Vector3 moveVector = new Vector3(0, 0, 0);
  476.             KeyboardState keyState = Keyboard.GetState();
  477.             if (keyState.IsKeyDown(Keys.Up) || keyState.IsKeyDown(Keys.W))
  478.                 moveVector += new Vector3(0, 0, -1);
  479.             if (keyState.IsKeyDown(Keys.Down) || keyState.IsKeyDown(Keys.S))
  480.                 moveVector += new Vector3(0, 0, 1);
  481.             if (keyState.IsKeyDown(Keys.Right) || keyState.IsKeyDown(Keys.D))
  482.                 moveVector += new Vector3(1, 0, 0);
  483.             if (keyState.IsKeyDown(Keys.Left) || keyState.IsKeyDown(Keys.A))
  484.                 moveVector += new Vector3(-1, 0, 0);
  485.             if (keyState.IsKeyDown(Keys.Q))
  486.                 moveVector += new Vector3(0, 1, 0);
  487.             if (keyState.IsKeyDown(Keys.Z))
  488.                 moveVector += new Vector3(0, -1, 0);
  489.             AddToCameraPosition(moveVector * amount);
  490.         }
  491.  
  492.         private void AddToCameraPosition(Vector3 vectorToAdd)
  493.         {
  494.             Matrix cameraRotation = Matrix.CreateRotationX(updownRot) * Matrix.CreateRotationY(leftrightRot);
  495.             Vector3 rotatedVector = Vector3.Transform(vectorToAdd, cameraRotation);
  496.             cameraPosition += moveSpeed * rotatedVector;
  497.             UpdateViewMatrix(viewMatrix);
  498.         }
  499.  
  500.         private void UpdateViewMatrix(Matrix viewMatrix)
  501.         {
  502.             Matrix cameraRotation = Matrix.CreateRotationX(updownRot) * Matrix.CreateRotationY(leftrightRot);
  503.  
  504.             Vector3 cameraOriginalTarget = new Vector3(0, 0, -1);
  505.             Vector3 cameraRotatedTarget = Vector3.Transform(cameraOriginalTarget, cameraRotation);
  506.             Vector3 cameraFinalTarget = cameraPosition + cameraRotatedTarget;
  507.  
  508.             Vector3 cameraOriginalUpVector = new Vector3(0, 1, 0);
  509.             Vector3 cameraRotatedUpVector = Vector3.Transform(cameraOriginalUpVector, cameraRotation);
  510.  
  511.             viewMatrix = Matrix.CreateLookAt(cameraPosition, cameraFinalTarget, cameraRotatedUpVector);
  512.         }
  513.         //************************************************************************************************\\
  514.     }
  515.  
  516.  
  517. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement