ChocoMan

Untitled

Sep 7th, 2012
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.07 KB | None | 0 0
  1. namespace XNAGame2
  2. {
  3.    
  4.     public class Game1 : Microsoft.Xna.Framework.Game
  5.     {
  6.         GraphicsDeviceManager graphics;
  7.         SpriteBatch spriteBatch;
  8.  
  9.         InputGame pInput = new InputGame();
  10.  
  11.         private static int screenWidth = 800;
  12.         private static int screenHieght = 600;
  13.         private static float aspectRatio = screenWidth / screenHieght;
  14.         private static float angleInRadians;
  15.         private static float camYaw = 0;
  16.         private static float camPitch = 0;
  17.         private static float camRoll = 0;
  18.         private static float camX = 0f;
  19.         private static float camY = 400f;
  20.         private static float camZ = -500f;
  21.  
  22.         private static Vector3 modelPosition;
  23.         private static Vector3 camPosition = new Vector3(camX, camY, camZ);
  24.         private static Vector3 camTarget = new Vector3(modelPosition.X, modelPosition.Y, modelPosition.Z);
  25.         private static Vector3 camUpVector = Vector3.Up;
  26.  
  27.        
  28.  
  29.         private static Model model;
  30.         private Model terrain;
  31.  
  32.         Matrix world;
  33.         Matrix view;
  34.         Matrix projection;
  35.         //Matrix world = Matrix.CreateTranslation(new Vector3(0, 0, 0));
  36.        // Matrix view = Matrix.CreateLookAt(camPosition, camTarget, camUpVector);
  37.        // Matrix projection = Matrix.CreatePerspectiveFieldOfView(
  38.         // MathHelper.ToRadians(45), aspectRatio, 0.1f, 10000f);
  39.         Matrix camYawPitch = Matrix.CreateFromYawPitchRoll(camYaw, camPitch, camRoll);
  40.  
  41.         public Game1()
  42.         {
  43.             graphics = new GraphicsDeviceManager(this);
  44.             this.graphics.PreferredBackBufferWidth = screenWidth;
  45.             this.graphics.PreferredBackBufferHeight = screenHieght;
  46.             //this.graphics.IsFullScreen = true;
  47.             Content.RootDirectory = "Content";
  48.         }
  49.  
  50.         protected override void Initialize()
  51.         {
  52.             // TODO: Add your initialization logic here
  53.  
  54.             base.Initialize();
  55.         }
  56.  
  57.        
  58.         protected override void LoadContent()
  59.         {
  60.            
  61.             spriteBatch = new SpriteBatch(GraphicsDevice);
  62.             model = Content.Load<Model>("Models//shockmodel");
  63.             terrain = Content.Load<Model>("Models//terraintest");
  64.             modelPosition = new Vector3(0, 0, 0);
  65.             angleInRadians = 0;
  66.  
  67.  
  68.             aspectRatio = graphics.GraphicsDevice.Viewport.AspectRatio;
  69.         }
  70.  
  71.        
  72.         protected override void UnloadContent()
  73.         {
  74.            
  75.         }
  76.  
  77.        
  78.         protected override void Update(GameTime gameTime)
  79.         {
  80.            
  81.             // Allows the game to exit
  82.             if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed
  83.                 || Keyboard.GetState().IsKeyDown(Keys.Escape))
  84.                 this.Exit();
  85.  
  86.             modelPosition += new Vector3(0, 0.5f, 0);
  87.             angle();
  88.             world = Matrix.CreateRotationY(angleInRadians) * Matrix.CreateTranslation(modelPosition);
  89.             pInput.gamePadConnection();
  90.  
  91.             base.Update(gameTime);
  92.         }
  93.  
  94.         protected override void Draw(GameTime gameTime)
  95.         {
  96.             GraphicsDevice.Clear(Color.CornflowerBlue);
  97.             DrawModel(model);
  98.             DrawTerrain(terrain);
  99.             base.Draw(gameTime);
  100.         }
  101.  
  102.         private void DrawModel(Model obj)
  103.         {
  104.             Matrix[] transforms = new Matrix[obj.Bones.Count];
  105.             obj.CopyAbsoluteBoneTransformsTo(transforms);
  106.  
  107.             foreach (ModelMesh mesh in obj.Meshes)
  108.             {
  109.                 foreach (BasicEffect effect in mesh.Effects)
  110.                 {
  111.                     effect.EnableDefaultLighting();
  112.                     //effect.LightingEnabled = true;
  113.                     //effect.AmbientLightColor = new Vector3(0.1f, 0.05f, 0.2f);
  114.                     //effect.EmissiveColor = new Vector3(0, 0, 0.15f);
  115.                     effect.FogEnabled = true;
  116.                     effect.FogColor = Color.CornflowerBlue.ToVector3();
  117.                     effect.FogStart = 550.75f;
  118.                     effect.FogEnd = 700.0f;
  119.                     effect.World = matrixWorld();
  120.                     effect.View = matrixView();
  121.                     effect.Projection = matrixProjection();
  122.                 }
  123.                 prepare3d();
  124.                 mesh.Draw();
  125.             }  
  126.         }
  127.  
  128.         private void DrawTerrain(Model terrain)
  129.         {
  130.             Matrix[] transforms = new Matrix[terrain.Bones.Count];
  131.             terrain.CopyAbsoluteBoneTransformsTo(transforms);
  132.  
  133.             foreach (ModelMesh mesh in terrain.Meshes)
  134.             {
  135.                 foreach (BasicEffect effect in mesh.Effects)
  136.                 {
  137.                     effect.EnableDefaultLighting();
  138.                     effect.PreferPerPixelLighting = true;
  139.                     effect.World = matrixWorld();
  140.                     effect.View = matrixView();
  141.                     effect.Projection = matrixProjection();
  142.                 }
  143.                 prepare3d();
  144.                 mesh.Draw();
  145.             }
  146.         }
  147.  
  148.         public float angle()
  149.         {
  150.             angleInRadians += 0.01f;
  151.             return angleInRadians;
  152.         }
  153.         public Matrix matrixWorld()
  154.         {
  155.             world = Matrix.CreateTranslation(new Vector3(0, 0, 0));
  156.             return world;
  157.         }
  158.  
  159.         public Matrix matrixView()
  160.         {
  161.             view = Matrix.CreateLookAt(camPosition, camTarget, camUpVector);
  162.             return view;
  163.         }
  164.  
  165.         public Matrix matrixProjection()
  166.         {
  167.             Matrix projection = Matrix.CreatePerspectiveFieldOfView(
  168.              MathHelper.ToRadians(45), aspectRatio, 0.1f, 10000f);
  169.             return projection;
  170.         }
  171.         public void prepare3d()
  172.         {
  173.             // Reset values to correct rendering
  174.             GraphicsDevice.BlendState = BlendState.Opaque;
  175.             GraphicsDevice.DepthStencilState = DepthStencilState.Default;
  176.             GraphicsDevice.SamplerStates[0] = SamplerState.LinearWrap;
  177.         }
  178.     } // end Class
  179. } // end Namespace
Advertisement
Add Comment
Please, Sign In to add comment