Spaceship.cs 5.8 KB

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