Advertisement
Guest User

modelTransforms

a guest
Mar 1st, 2012
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.84 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  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.Media;
  11.  
  12. namespace WindowsGame1
  13. {
  14.     /// <summary>
  15.     /// This is the main type for your game
  16.     /// </summary>
  17.     public class Game1 : Microsoft.Xna.Framework.Game
  18.     {
  19.         GraphicsDeviceManager graphics;
  20.         SpriteBatch spriteBatch;
  21.  
  22.  
  23.  
  24.         Model model;
  25.  
  26.         Vector3 cameraPosition;
  27.         Matrix view, projection;
  28.        
  29.  
  30.         public Game1()
  31.         {
  32.             graphics = new GraphicsDeviceManager(this);
  33.             Content.RootDirectory = "Content";
  34.         }
  35.  
  36.         /// <summary>
  37.         /// Allows the game to perform any initialization it needs to before starting to run.
  38.         /// This is where it can query for any required services and load any non-graphic
  39.         /// related content.  Calling base.Initialize will enumerate through any components
  40.         /// and initialize them as well.
  41.         /// </summary>
  42.         protected override void Initialize()
  43.         {
  44.             //fill in the appropriate values below
  45.             cameraPosition=new Vector3(/*X position from max, Z position from max, -Y position from max*/);
  46.  
  47.             projection = Matrix.CreatePerspectiveFieldOfView(0.78f /* == 45deg*/, graphics.GraphicsDevice.Viewport.AspectRatio, 1, 10000);
  48.  
  49.             base.Initialize();
  50.         }
  51.  
  52.         /// <summary>
  53.         /// LoadContent will be called once per game and is the place to load
  54.         /// all of your content.
  55.         /// </summary>
  56.         protected override void LoadContent()
  57.         {
  58.             // Create a new SpriteBatch, which can be used to draw textures.
  59.             spriteBatch = new SpriteBatch(GraphicsDevice);
  60.  
  61.             model = Content.Load<Model>("modelFileName");
  62.            
  63.         }
  64.  
  65.         /// <summary>
  66.         /// UnloadContent will be called once per game and is the place to unload
  67.         /// all content.
  68.         /// </summary>
  69.         protected override void UnloadContent()
  70.         {
  71.             // TODO: Unload any non ContentManager content here
  72.         }
  73.  
  74.         /// <summary>
  75.         /// Allows the game to run logic such as updating the world,
  76.         /// checking for collisions, gathering input, and playing audio.
  77.         /// </summary>
  78.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  79.         protected override void Update(GameTime gameTime)
  80.         {
  81.             // Allows the game to exit
  82.             if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
  83.                 this.Exit();
  84.  
  85.             view = Matrix.CreateLookAt(cameraPosition, Vector3.Zero, Vector3.Up);
  86.  
  87.             base.Update(gameTime);
  88.         }
  89.  
  90.         /// <summary>
  91.         /// This is called when the game should draw itself.
  92.         /// </summary>
  93.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  94.         protected override void Draw(GameTime gameTime)
  95.         {
  96.             GraphicsDevice.Clear(Color.CornflowerBlue);
  97.  
  98.             Matrix[] modelTransforms;
  99.             modelTransforms = new Matrix[model.Bones.Count];
  100.             model.CopyAbsoluteBoneTransformsTo(modelTransforms);
  101.  
  102.             foreach (ModelMesh mesh in model.Meshes)
  103.             {
  104.                 foreach (BasicEffect effect in mesh.Effects)
  105.                 {
  106.                     effect.Projection = projection;
  107.                     effect.View = view;
  108.                     effect.World = modelTransforms[mesh.ParentBone.Index];
  109.                 }
  110.                 mesh.Draw();
  111.             }
  112.  
  113.             base.Draw(gameTime);
  114.         }
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement