2
0

Spaceship.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // Spaceship.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. #region Using Statements
  10. using Microsoft.Xna.Framework;
  11. using Microsoft.Xna.Framework.Content;
  12. using Microsoft.Xna.Framework.Graphics;
  13. using Graphics3DSample;
  14. using System;
  15. #endregion
  16. namespace Graphics3DSample
  17. {
  18. /// <summary>
  19. /// Helper class for drawing a spaceship model with animated wheels and turret.
  20. /// </summary>
  21. public class Spaceship
  22. {
  23. #region Fields
  24. // The XNA framework Model object that we are going to display.
  25. Model spaceshipModel;
  26. // Array holding all the bone transform matrices for the entire model.
  27. // We could just allocate this locally inside the Draw method, but it
  28. // is more efficient to reuse a single array, as this avoids creating
  29. // unnecessary garbage.
  30. Matrix[] boneTransforms;
  31. // Spaceship drawing parameters
  32. private Matrix projection;
  33. private Matrix rotation;
  34. private Matrix view;
  35. private bool[] lights;
  36. bool isTextureEnabled;
  37. bool isPerPixelLightingEnabled;
  38. #endregion
  39. #region Initialization
  40. /// <summary>
  41. /// Loads the spaceship model.
  42. /// </summary>
  43. public void Load(ContentManager content)
  44. {
  45. // Load the spaceship model from the ContentManager.
  46. spaceshipModel = content.Load<Model>("Models/spaceship");
  47. // Allocate the transform matrix array.
  48. boneTransforms = new Matrix[spaceshipModel.Bones.Count];
  49. }
  50. #endregion
  51. #region Public accessors
  52. /// <summary>
  53. /// Gets or sets the projection matrix value.
  54. /// </summary>
  55. public Matrix Projection
  56. {
  57. get { return projection; }
  58. set { projection = value; }
  59. }
  60. /// <summary>
  61. /// Gets or sets the rotation matrix value.
  62. /// </summary>
  63. public Matrix Rotation
  64. {
  65. get { return rotation; }
  66. set { rotation = value; }
  67. }
  68. /// <summary>
  69. /// Gets or sets the rotation matrix value.
  70. /// </summary>
  71. public bool IsTextureEnabled
  72. {
  73. get { return isTextureEnabled; }
  74. set { isTextureEnabled = value; }
  75. }
  76. /// <summary>
  77. /// Gets or sets the view matrix value.
  78. /// </summary>
  79. public Matrix View
  80. {
  81. get { return view; }
  82. set { view = value; }
  83. }
  84. /// <summary>
  85. /// Gets or sets the lights states.
  86. /// </summary>
  87. public bool[] Lights
  88. {
  89. get { return lights; }
  90. set { lights = value; }
  91. }
  92. /// <summary>
  93. /// Gets or sets the per pixel lighting preferences
  94. /// </summary>
  95. public bool IsPerPixelLightingEnabled
  96. {
  97. get { return isPerPixelLightingEnabled; }
  98. set { isPerPixelLightingEnabled = value; }
  99. }
  100. #endregion
  101. #region Draw
  102. /// <summary>
  103. /// Draws the spaceship model, using the current drawing parameters.
  104. /// </summary>
  105. public void Draw()
  106. {
  107. // Set the world matrix as the root transform of the model.
  108. spaceshipModel.Root.Transform = Rotation;
  109. // Look up combined bone matrices for the entire model.
  110. spaceshipModel.CopyAbsoluteBoneTransformsTo(boneTransforms);
  111. // Draw the model.
  112. foreach (ModelMesh mesh in spaceshipModel.Meshes)
  113. {
  114. foreach (BasicEffect effect in mesh.Effects)
  115. {
  116. effect.World = boneTransforms[mesh.ParentBone.Index];
  117. effect.View = View;
  118. effect.Projection = Projection;
  119. SetEffectLights(effect, Lights);
  120. SetEffectPerPixelLightingEnabled(effect);
  121. effect.TextureEnabled = IsTextureEnabled;
  122. }
  123. mesh.Draw();
  124. }
  125. }
  126. /// <summary>
  127. /// Sets effect's per pixel lighting preference
  128. /// </summary>
  129. /// <param name="effect"></param>
  130. private void SetEffectPerPixelLightingEnabled(BasicEffect effect)
  131. {
  132. effect.PreferPerPixelLighting = isPerPixelLightingEnabled;
  133. }
  134. /// <summary>
  135. /// Sets effects lighting properties
  136. /// </summary>
  137. /// <param name="effect"></param>
  138. /// <param name="lights"></param>
  139. private void SetEffectLights(BasicEffect effect, bool[] lights)
  140. {
  141. effect.Alpha = 1.0f;
  142. effect.DiffuseColor = new Vector3(0.75f, 0.75f, 0.75f);
  143. effect.SpecularColor = new Vector3(0.25f, 0.25f, 0.25f);
  144. effect.SpecularPower = 5.0f;
  145. effect.AmbientLightColor = new Vector3(0.75f, 0.75f, 0.75f);
  146. effect.DirectionalLight0.Enabled = lights[0];
  147. effect.DirectionalLight0.DiffuseColor = Vector3.One;
  148. effect.DirectionalLight0.Direction = Vector3.Normalize(new Vector3(1, -1, 0));
  149. effect.DirectionalLight0.SpecularColor = Vector3.One;
  150. effect.DirectionalLight1.Enabled = lights[1];
  151. effect.DirectionalLight1.DiffuseColor = new Vector3(0.5f, 0.5f, 0.5f);
  152. effect.DirectionalLight1.Direction = Vector3.Normalize(new Vector3(-1, -1, 0));
  153. effect.DirectionalLight1.SpecularColor = new Vector3(1f, 1f, 1f);
  154. effect.DirectionalLight2.Enabled = lights[2];
  155. effect.DirectionalLight2.DiffuseColor = new Vector3(0.3f, 0.3f, 0.3f);
  156. effect.DirectionalLight2.Direction = Vector3.Normalize(new Vector3(-1, -1, -1));
  157. effect.DirectionalLight2.SpecularColor = new Vector3(0.3f, 0.3f, 0.3f);
  158. effect.LightingEnabled = true;
  159. }
  160. #endregion
  161. }
  162. }