Advertisement
Guest User

Sandbox - GameModel

a guest
Mar 5th, 2012
400
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.00 KB | None | 0 0
  1. namespace Sandbox.Graphics
  2. {
  3.     using Microsoft.Xna.Framework;
  4.     using Microsoft.Xna.Framework.Graphics;
  5.  
  6.     /// <summary>
  7.     /// Wraps the <see cref="Microsoft.Xna.Framework.Graphics.Model"/> class in a form that handles rendering,
  8.     /// updating and transformation.
  9.     /// </summary>
  10.     public class GameModel : DrawableGameComponent
  11.     {
  12.         private readonly string mAssetName;
  13.         private readonly ICamera mCamera;
  14.         private Model mModel;
  15.         private Vector3 mCenter;
  16.  
  17.         private Vector3 mScale = Vector3.Zero;
  18.         private Vector3 mRotation = Vector3.Zero;
  19.         private Vector3 mTranslation = Vector3.Zero;
  20.  
  21.         private Matrix mScaleMatrix = Matrix.Identity;
  22.         private Matrix mRotationMatrix = Matrix.Identity;
  23.         private Matrix mTranslationMatrix = Matrix.Identity;
  24.  
  25.         #region Initialization
  26.  
  27.         public GameModel(string assetName, ICamera camera, Game game)
  28.             : base(game)
  29.         {
  30.             mAssetName = assetName;
  31.             mCamera = camera;
  32.         }
  33.  
  34.         #endregion
  35.  
  36.         #region Properties
  37.  
  38.         /// <summary>
  39.         /// Gets the name of the asset.
  40.         /// </summary>
  41.         public string AssetName
  42.         {
  43.             get { return mAssetName; }
  44.         }
  45.  
  46.         /// <summary>
  47.         /// Gets the center of the model.
  48.         /// </summary>
  49.         public Vector3 Center
  50.         {
  51.             get { return mCenter; }
  52.         }
  53.  
  54.         /// <summary>
  55.         /// Gets or sets the scale, along each axis, of the model.
  56.         /// </summary>
  57.         public Vector3 ScaleSize
  58.         {
  59.             get { return mScale; }
  60.             set { mScale = value; }
  61.         }
  62.  
  63.         /// <summary>
  64.         /// Gets or sets the rotation, in each axis, of the model.
  65.         /// </summary>
  66.         public Vector3 Rotation
  67.         {
  68.             get { return mRotation; }
  69.             set { mRotation = value; }
  70.         }
  71.  
  72.         /// <summary>
  73.         /// Gets or sets the translation, along each axis, of the model.
  74.         /// </summary>
  75.         public Vector3 Translation
  76.         {
  77.             get { return mTranslation; }
  78.             set { mTranslation = value; }
  79.         }
  80.  
  81.         /// <summary>
  82.         /// Gets the transform of the model.
  83.         /// </summary>
  84.         public Matrix Transform
  85.         {
  86.             get { return mScaleMatrix * mRotationMatrix * mTranslationMatrix; }
  87.         }
  88.  
  89.         /// <summary>
  90.         /// Gets the position of the <see cref="Sandbox.Graphics.GameModel" /> in three dimensional world space.
  91.         /// </summary>
  92.         public Vector3 Position
  93.         {
  94.             get { return mCenter + mTranslation; }
  95.         }
  96.  
  97.         #endregion
  98.  
  99.         #region DrawableGameComponent Overrides
  100.  
  101.         protected override void LoadContent()
  102.         {
  103.             // Load the model
  104.             mModel = this.Game.Content.Load<Model>(mAssetName);
  105.  
  106.             // Work out the origin of the model
  107.             BoundingSphere boundingInfo = mModel.Meshes[0].BoundingSphere;
  108.             for (int i = 1; i < mModel.Meshes.Count; i++)
  109.             {
  110.                 boundingInfo = BoundingSphere.CreateMerged(boundingInfo, mModel.Meshes[i].BoundingSphere);
  111.             }
  112.  
  113.             mCenter = boundingInfo.Center;
  114.  
  115.             base.LoadContent();
  116.         }
  117.  
  118.         public override void Update(GameTime gameTime)
  119.         {
  120.             mScaleMatrix = Matrix.CreateScale(mScale);
  121.             mRotationMatrix = Matrix.CreateFromYawPitchRoll(mRotation.Y, mRotation.X, mRotation.Z);
  122.             mTranslationMatrix = Matrix.CreateTranslation(mTranslation);
  123.  
  124.             base.Update(gameTime);
  125.         }
  126.  
  127.         public override void Draw(GameTime gameTime)
  128.         {
  129.             // Compute the matrices
  130.             Matrix modelViewMatrix = this.Transform * mCamera.View;
  131.             Matrix[] boneTransforms = new Matrix[mModel.Bones.Count];
  132.             mModel.CopyAbsoluteBoneTransformsTo(boneTransforms);
  133.  
  134.             // Render the model
  135.             foreach (ModelMesh mesh in mModel.Meshes)
  136.             {
  137.                 foreach (BasicEffect effect in mesh.Effects)
  138.                 {
  139.                     effect.EnableDefaultLighting();
  140.  
  141.                     effect.World = mCamera.World;
  142.                     effect.View = boneTransforms[mesh.ParentBone.Index] * modelViewMatrix;
  143.                     effect.Projection = mCamera.Projection;
  144.                 }
  145.  
  146.                 mesh.Draw();
  147.             }
  148.  
  149.             base.Draw(gameTime);
  150.         }
  151.  
  152.         #endregion
  153.  
  154.         #region Transformations
  155.  
  156.         /// <summary>
  157.         /// Scales the model to the specified size.
  158.         /// </summary>
  159.         /// <param name="scale">Size to scale the model to.</param>
  160.         public void Scale(float scale)
  161.         {
  162.             Scale(scale, scale, scale);
  163.         }
  164.  
  165.         /// <summary>
  166.         /// Scales the model to the specified size.
  167.         /// </summary>
  168.         /// <param name="scaleX">Size to scale the model to in the x-axis.</param>
  169.         /// <param name="scaleY">Size to scale the model to in the y-axis.</param>
  170.         /// <param name="scaleZ">Size to scale the model to in the z-axis.</param>
  171.         public void Scale(float scaleX, float scaleY, float scaleZ)
  172.         {
  173.             mScale.X += scaleX;
  174.             mScale.Y += scaleY;
  175.             mScale.Z += scaleZ;
  176.         }
  177.  
  178.         /// <summary>
  179.         /// Rotates the model about the y-axis.
  180.         /// </summary>
  181.         /// <param name="amount">Amount (in radians) to rotate the model by.</param>
  182.         public void RotateYaw(float amount)
  183.         {
  184.             mRotation.Y += amount;
  185.             mRotationMatrix *= Matrix.CreateRotationY(amount);
  186.         }
  187.  
  188.         /// <summary>
  189.         /// Rotates the model about the x-axis.
  190.         /// </summary>
  191.         /// <param name="amount">Amount (in radians) to rotate the model by.</param>
  192.         public void RotatePitch(float amount)
  193.         {
  194.             mRotation.X += amount;
  195.         }
  196.  
  197.         /// <summary>
  198.         /// Rotates the model about the z-axis.
  199.         /// </summary>
  200.         /// <param name="amount">Amount (in radians) to rotate the model by.</param>
  201.         public void RotateRoll(float amount)
  202.         {
  203.             mRotation.Z += amount;
  204.         }
  205.  
  206.         /// <summary>
  207.         /// Translates the model by the specified amounts in each axis.
  208.         /// </summary>
  209.         /// <param name="x">Amount to translate the model along the x-axis.</param>
  210.         /// <param name="y">Amount to translate the model along the y-axis.</param>
  211.         /// <param name="z">Amount to translate the model along the z-axis.</param>
  212.         public void Translate(float x, float y, float z)
  213.         {
  214.             mTranslation.X += x;
  215.             mTranslation.Y += y;
  216.             mTranslation.Z += z;
  217.         }
  218.  
  219.         #endregion
  220.  
  221.         #region Object Overrides
  222.  
  223.         public override string ToString()
  224.         {
  225.             return this.AssetName;
  226.         }
  227.  
  228.         #endregion
  229.     }
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement