Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace Sandbox.Graphics
- {
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- /// <summary>
- /// Wraps the <see cref="Microsoft.Xna.Framework.Graphics.Model"/> class in a form that handles rendering,
- /// updating and transformation.
- /// </summary>
- public class GameModel : DrawableGameComponent
- {
- private readonly string mAssetName;
- private readonly ICamera mCamera;
- private Model mModel;
- private Vector3 mCenter;
- private Vector3 mScale = Vector3.Zero;
- private Vector3 mRotation = Vector3.Zero;
- private Vector3 mTranslation = Vector3.Zero;
- private Matrix mScaleMatrix = Matrix.Identity;
- private Matrix mRotationMatrix = Matrix.Identity;
- private Matrix mTranslationMatrix = Matrix.Identity;
- #region Initialization
- public GameModel(string assetName, ICamera camera, Game game)
- : base(game)
- {
- mAssetName = assetName;
- mCamera = camera;
- }
- #endregion
- #region Properties
- /// <summary>
- /// Gets the name of the asset.
- /// </summary>
- public string AssetName
- {
- get { return mAssetName; }
- }
- /// <summary>
- /// Gets the center of the model.
- /// </summary>
- public Vector3 Center
- {
- get { return mCenter; }
- }
- /// <summary>
- /// Gets or sets the scale, along each axis, of the model.
- /// </summary>
- public Vector3 ScaleSize
- {
- get { return mScale; }
- set { mScale = value; }
- }
- /// <summary>
- /// Gets or sets the rotation, in each axis, of the model.
- /// </summary>
- public Vector3 Rotation
- {
- get { return mRotation; }
- set { mRotation = value; }
- }
- /// <summary>
- /// Gets or sets the translation, along each axis, of the model.
- /// </summary>
- public Vector3 Translation
- {
- get { return mTranslation; }
- set { mTranslation = value; }
- }
- /// <summary>
- /// Gets the transform of the model.
- /// </summary>
- public Matrix Transform
- {
- get { return mScaleMatrix * mRotationMatrix * mTranslationMatrix; }
- }
- /// <summary>
- /// Gets the position of the <see cref="Sandbox.Graphics.GameModel" /> in three dimensional world space.
- /// </summary>
- public Vector3 Position
- {
- get { return mCenter + mTranslation; }
- }
- #endregion
- #region DrawableGameComponent Overrides
- protected override void LoadContent()
- {
- // Load the model
- mModel = this.Game.Content.Load<Model>(mAssetName);
- // Work out the origin of the model
- BoundingSphere boundingInfo = mModel.Meshes[0].BoundingSphere;
- for (int i = 1; i < mModel.Meshes.Count; i++)
- {
- boundingInfo = BoundingSphere.CreateMerged(boundingInfo, mModel.Meshes[i].BoundingSphere);
- }
- mCenter = boundingInfo.Center;
- base.LoadContent();
- }
- public override void Update(GameTime gameTime)
- {
- mScaleMatrix = Matrix.CreateScale(mScale);
- mRotationMatrix = Matrix.CreateFromYawPitchRoll(mRotation.Y, mRotation.X, mRotation.Z);
- mTranslationMatrix = Matrix.CreateTranslation(mTranslation);
- base.Update(gameTime);
- }
- public override void Draw(GameTime gameTime)
- {
- // Compute the matrices
- Matrix modelViewMatrix = this.Transform * mCamera.View;
- Matrix[] boneTransforms = new Matrix[mModel.Bones.Count];
- mModel.CopyAbsoluteBoneTransformsTo(boneTransforms);
- // Render the model
- foreach (ModelMesh mesh in mModel.Meshes)
- {
- foreach (BasicEffect effect in mesh.Effects)
- {
- effect.EnableDefaultLighting();
- effect.World = mCamera.World;
- effect.View = boneTransforms[mesh.ParentBone.Index] * modelViewMatrix;
- effect.Projection = mCamera.Projection;
- }
- mesh.Draw();
- }
- base.Draw(gameTime);
- }
- #endregion
- #region Transformations
- /// <summary>
- /// Scales the model to the specified size.
- /// </summary>
- /// <param name="scale">Size to scale the model to.</param>
- public void Scale(float scale)
- {
- Scale(scale, scale, scale);
- }
- /// <summary>
- /// Scales the model to the specified size.
- /// </summary>
- /// <param name="scaleX">Size to scale the model to in the x-axis.</param>
- /// <param name="scaleY">Size to scale the model to in the y-axis.</param>
- /// <param name="scaleZ">Size to scale the model to in the z-axis.</param>
- public void Scale(float scaleX, float scaleY, float scaleZ)
- {
- mScale.X += scaleX;
- mScale.Y += scaleY;
- mScale.Z += scaleZ;
- }
- /// <summary>
- /// Rotates the model about the y-axis.
- /// </summary>
- /// <param name="amount">Amount (in radians) to rotate the model by.</param>
- public void RotateYaw(float amount)
- {
- mRotation.Y += amount;
- mRotationMatrix *= Matrix.CreateRotationY(amount);
- }
- /// <summary>
- /// Rotates the model about the x-axis.
- /// </summary>
- /// <param name="amount">Amount (in radians) to rotate the model by.</param>
- public void RotatePitch(float amount)
- {
- mRotation.X += amount;
- }
- /// <summary>
- /// Rotates the model about the z-axis.
- /// </summary>
- /// <param name="amount">Amount (in radians) to rotate the model by.</param>
- public void RotateRoll(float amount)
- {
- mRotation.Z += amount;
- }
- /// <summary>
- /// Translates the model by the specified amounts in each axis.
- /// </summary>
- /// <param name="x">Amount to translate the model along the x-axis.</param>
- /// <param name="y">Amount to translate the model along the y-axis.</param>
- /// <param name="z">Amount to translate the model along the z-axis.</param>
- public void Translate(float x, float y, float z)
- {
- mTranslation.X += x;
- mTranslation.Y += y;
- mTranslation.Z += z;
- }
- #endregion
- #region Object Overrides
- public override string ToString()
- {
- return this.AssetName;
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement