EvolvedShape.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // EvolvedShape.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 System;
  11. using System.Collections.Generic;
  12. using System.Text;
  13. using Microsoft.Xna.Framework.Graphics;
  14. using Microsoft.Xna.Framework;
  15. using System.Diagnostics;
  16. #endregion
  17. namespace Spacewar
  18. {
  19. /// <summary>
  20. /// The shapes that this class can draw
  21. /// </summary>
  22. public enum EvolvedShapes
  23. {
  24. /// <summary>
  25. /// Ship models used ingame and in selection screen
  26. /// </summary>
  27. Ship,
  28. /// <summary>
  29. /// Asteroid models
  30. /// </summary>
  31. Asteroid,
  32. /// <summary>
  33. /// Projectile models
  34. /// </summary>
  35. Projectile,
  36. /// <summary>
  37. /// Weapon models for the selection screen
  38. /// </summary>
  39. Weapon,
  40. }
  41. /// <summary>
  42. /// Evolved shapes represents the 3d models used by the evolved variation of the game. They are all lit using the
  43. /// same shaders and have similar input and construction methods.
  44. /// </summary>
  45. class EvolvedShape : Shape
  46. {
  47. //Materials
  48. private static Color white = Color.White;
  49. private static Color body = white; //body has texture so don't need material
  50. private static Color pipes = white; //pipes has texture so don't need material
  51. private static Color cockpit1 = new Color((byte)(.529f * 255), 255, 255, 0);
  52. private static Color cockpit2 = new Color(255, 255, (byte)(.373f * 255), 0);
  53. private static Color engines = new Color((byte)(.925f * 255), (byte)(.529f * 255), 255, 0);
  54. private Texture2D texture;
  55. private TextureCube reflection1;
  56. private TextureCube reflection2;
  57. private int skinNumber;
  58. private int shapeNumber;
  59. private EvolvedShapes shape = EvolvedShapes.Projectile;
  60. private PlayerIndex player;
  61. private Model model; //New content Pipeline Mesh
  62. private Texture2D blackTexture;
  63. string[] modelNames = null;
  64. string[] textureNames = null;
  65. private int scene; //0 is in game 1 is selection screens
  66. private Color[] material;
  67. private bool[] useReflection2;
  68. private Effect effect;
  69. private EffectParameter worldParam;
  70. private EffectParameter inverseWorldParam;
  71. private EffectParameter worldViewProjectionParam;
  72. private EffectParameter viewPositionParam;
  73. private EffectParameter skinTextureParam;
  74. private EffectParameter normalTextureParam;
  75. private EffectParameter reflectionTextureParam;
  76. private EffectParameter ambientParam;
  77. private EffectParameter directionalDirectionParam;
  78. private EffectParameter directionalColorParam;
  79. private EffectParameter pointPositionParam;
  80. private EffectParameter pointColorParam;
  81. private EffectParameter pointFactorParam;
  82. private EffectParameter materialParam;
  83. #region filenames
  84. private static string[,] shipMesh = new string[,]
  85. {
  86. {
  87. @"models\p1_pencil",
  88. @"models\p1_saucer",
  89. @"models\p1_wedge",
  90. },
  91. {
  92. @"models\p2_pencil",
  93. @"models\p2_saucer",
  94. @"models\p2_wedge",
  95. }
  96. };
  97. private static string[,] shipDiffuse = new string[,]
  98. {
  99. {
  100. @"textures\pencil_p1_diff_v{0}",
  101. @"textures\saucer_p1_diff_v{0}",
  102. @"textures\wedge_p1_diff_v{0}",
  103. },
  104. {
  105. @"textures\pencil_p2_diff_v{0}",
  106. @"textures\saucer_p2_diff_v{0}",
  107. @"textures\wedge_p2_diff_v{0}",
  108. }
  109. };
  110. private static string[,] shipNormal = new String[,]
  111. {
  112. {
  113. @"textures\pencil_p1_norm_1",
  114. @"textures\saucer_p1_norm_1",
  115. @"textures\wedge_p1_norm_1",
  116. },
  117. {
  118. @"textures\pencil_p2_norm_1",
  119. @"textures\saucer_p2_norm_1",
  120. @"textures\wedge_p2_norm_1",
  121. }
  122. };
  123. private static string[,] shipReflection = new String[,]
  124. {
  125. {
  126. @"textures\p1_reflection_cubemap",
  127. "",
  128. },
  129. {
  130. @"textures\p2_reflection_cubemap1",
  131. @"textures\p2_reflection_cubemap2",
  132. }
  133. };
  134. private static string[] projectileMeshes = new String[]
  135. {
  136. @"models\pea_proj",
  137. @"models\mgun_proj",
  138. @"models\mgun_proj",
  139. @"models\p1_rocket_proj",
  140. @"models\bfg_proj",
  141. };
  142. private static string[] projectileDiffuse = new String[]
  143. {
  144. @"textures\pea_proj",
  145. @"textures\pea_proj",
  146. @"textures\mgun_proj",
  147. @"textures\rocket_proj",
  148. @"textures\bfg_proj",
  149. };
  150. private static string[] asteroidMeshes = new String[]
  151. {
  152. @"models\asteroid1",
  153. @"models\asteroid2",
  154. };
  155. private static string[] asteroidDiffuse = new String[]
  156. {
  157. @"textures\asteroid1",
  158. @"textures\asteroid2",
  159. };
  160. private static string[][] weaponMeshes = new String[][]
  161. {
  162. new string[]
  163. {
  164. @"models\p1_pea",
  165. @"models\p1_mgun",
  166. @"models\p1_dual",
  167. @"models\p1_rocket",
  168. @"models\p1_bfg",
  169. },
  170. new string[]
  171. {
  172. @"models\p2_pea",
  173. @"models\p2_mgun",
  174. @"models\p2_dual",
  175. @"models\p2_rocket",
  176. @"models\p2_bfg",
  177. }
  178. };
  179. private static string[,][] weaponDiffuse = new String[,][]
  180. {
  181. {
  182. new string[]
  183. {
  184. @"textures\p1_back" // Used by p1_pea
  185. },
  186. new string[]
  187. {
  188. @"textures\p1_back", @"textures\p1_dual" //Used by p1_mgun
  189. },
  190. new string[]
  191. {
  192. @"textures\p1_dual", @"textures\p1_back" //Used by p1_dual
  193. },
  194. new string[]
  195. {
  196. @"textures\p1_back", @"textures\p1_rocket" //Used by p1_rocket
  197. },
  198. new string[]
  199. {
  200. @"textures\p1_bfg", "", @"textures\p1_back" //Used by the p1_bfg
  201. }
  202. },
  203. {
  204. new string[]
  205. {
  206. @"textures\p2_back", @"textures\p2_back" // Used by p2_pea
  207. },
  208. new string[]
  209. {
  210. @"textures\p2_back", @"textures\p2_back", @"textures\p2_dual" // Used by p2_mgun
  211. },
  212. new string[]
  213. {
  214. @"textures\p2_back", @"textures\p2_back", @"textures\p2_dual" // Used by p2_dual
  215. },
  216. new string[]
  217. {
  218. @"textures\p2_rocket", @"textures\p2_back", @"textures\p2_back" // Used by p2_rocket
  219. },
  220. new string[]
  221. {
  222. @"textures\p2_back", @"textures\p2_back", @"textures\p2_bfg", @"textures\p2_back" // Used by p2_bfg
  223. }
  224. }
  225. };
  226. private Color[,][] shipMaterials = new Color[,][]
  227. {
  228. { //player 1 ships
  229. new Color[] {body, engines, cockpit1, cockpit1},
  230. new Color[] {body, cockpit1, engines},
  231. new Color[] {body, cockpit1, engines},
  232. },
  233. { //player 2 ships
  234. new Color[] {cockpit2, pipes, body},
  235. new Color[] {pipes, body, cockpit2},
  236. new Color[] {pipes, cockpit2, body},
  237. }
  238. };
  239. private bool[,][] shipUsesReflection2 = new bool[,][]
  240. {
  241. { //player 1 ships - always use 1st reflection map
  242. new bool[] {false, false, false, false},
  243. new bool[] {false, false, false},
  244. new bool[] {false, false, false},
  245. },
  246. { //player 2 ships
  247. new bool[] {true, true, false},
  248. new bool[] {true, false, true},
  249. new bool[] {true, true, false},
  250. }
  251. };
  252. #endregion
  253. public EvolvedShape(Game game, EvolvedShapes shape, PlayerIndex player, int shipNumber, int skinNumber, LightingType scene)
  254. : base(game)
  255. {
  256. Debug.Assert(shape == EvolvedShapes.Ship, "Constructor should only be called with Ship");
  257. this.shape = shape;
  258. this.shapeNumber = shipNumber;
  259. this.skinNumber = skinNumber;
  260. this.player = player;
  261. this.scene = (int)scene;
  262. CreateShip();
  263. }
  264. public EvolvedShape(Game game, EvolvedShapes shape, PlayerIndex player, int shapeNumber, LightingType scene)
  265. : base(game)
  266. {
  267. Debug.Assert(shape == EvolvedShapes.Weapon, "Constructor should only be called with Weapon");
  268. this.player = player;
  269. this.shape = shape;
  270. this.shapeNumber = shapeNumber;
  271. this.scene = (int)scene;
  272. CreateShape();
  273. }
  274. public EvolvedShape(Game game, EvolvedShapes shape, int shapeNumber, LightingType scene)
  275. : base(game)
  276. {
  277. this.shape = shape;
  278. this.shapeNumber = shapeNumber;
  279. this.scene = (int)scene;
  280. CreateShape();
  281. }
  282. public override void Create()
  283. {
  284. //Load the correct shader and set up the parameters
  285. OnCreateDevice();
  286. }
  287. public override void OnCreateDevice()
  288. {
  289. effect = SpacewarGame.ContentManager.Load<Effect>(SpacewarGame.Settings.MediaPath + @"shaders\ship");
  290. worldParam = effect.Parameters["world"];
  291. inverseWorldParam = effect.Parameters["inverseWorld"];
  292. worldViewProjectionParam = effect.Parameters["worldViewProjection"];
  293. viewPositionParam = effect.Parameters["viewPosition"];
  294. skinTextureParam = effect.Parameters["SkinTexture"];
  295. normalTextureParam = effect.Parameters["NormalMapTexture"];
  296. reflectionTextureParam = effect.Parameters["ReflectionTexture"];
  297. ambientParam = effect.Parameters["Ambient"];
  298. directionalDirectionParam = effect.Parameters["DirectionalDirection"];
  299. directionalColorParam = effect.Parameters["DirectionalColor"];
  300. pointPositionParam = effect.Parameters["PointPosition"];
  301. pointColorParam = effect.Parameters["PointColor"];
  302. pointFactorParam = effect.Parameters["PointFactor"];
  303. materialParam = effect.Parameters["Material"];
  304. blackTexture = SpacewarGame.ContentManager.Load<Texture2D>(SpacewarGame.Settings.MediaPath + @"Textures\Black");
  305. SetupEffect();
  306. }
  307. private void SetupEffect()
  308. {
  309. ambientParam.SetValue(SpacewarGame.Settings.ShipLights[scene].Ambient);
  310. directionalDirectionParam.SetValue(SpacewarGame.Settings.ShipLights[scene].DirectionalDirection);
  311. directionalColorParam.SetValue(SpacewarGame.Settings.ShipLights[scene].DirectionalColor);
  312. pointPositionParam.SetValue(SpacewarGame.Settings.ShipLights[scene].PointPosition);
  313. pointColorParam.SetValue(SpacewarGame.Settings.ShipLights[scene].PointColor);
  314. pointFactorParam.SetValue(SpacewarGame.Settings.ShipLights[scene].PointFactor);
  315. normalTextureParam.SetValue((Texture2D)null); //Normal maps not currently used
  316. }
  317. public void CreateShip()
  318. {
  319. //Point to the right material array for this ship
  320. material = shipMaterials[(int)player, shapeNumber];
  321. //Point to the right cube map mapping
  322. useReflection2 = shipUsesReflection2[(int)player, shapeNumber];
  323. SetupEffect();
  324. }
  325. public void CreateShape()
  326. {
  327. switch (shape)
  328. {
  329. case EvolvedShapes.Projectile:
  330. modelNames = projectileMeshes;
  331. textureNames = projectileDiffuse;
  332. break;
  333. case EvolvedShapes.Asteroid:
  334. modelNames = asteroidMeshes;
  335. textureNames = asteroidDiffuse;
  336. break;
  337. case EvolvedShapes.Weapon:
  338. modelNames = weaponMeshes[(int)player];
  339. textureNames = weaponDiffuse[(int)player, shapeNumber];
  340. break;
  341. default:
  342. //Should never get here
  343. Debug.Assert(true, "EvolvedShape:CreateShape - bad EvolvedShape passed in");
  344. break;
  345. }
  346. SetupEffect();
  347. }
  348. public override void Render()
  349. {
  350. base.Render();
  351. worldParam.SetValue(World);
  352. inverseWorldParam.SetValue(Matrix.Invert(World));
  353. worldViewProjectionParam.SetValue(World * SpacewarGame.Camera.View * SpacewarGame.Camera.Projection);
  354. viewPositionParam.SetValue(new Vector4(SpacewarGame.Camera.ViewPosition, 0));
  355. if (shape == EvolvedShapes.Ship)
  356. {
  357. //Model
  358. model = SpacewarGame.ContentManager.Load<Model>(SpacewarGame.Settings.MediaPath + shipMesh[(int)player, shapeNumber]);
  359. //Matching Textures
  360. texture = SpacewarGame.ContentManager.Load<Texture2D>(SpacewarGame.Settings.MediaPath + String.Format(shipDiffuse[(int)player, shapeNumber], (skinNumber + 1)));
  361. //_normal = SpacewarGame.TextureCache[shipNormal[(int)_player, _shapeNumber]]; //Normal maps not currently used
  362. reflection1 = SpacewarGame.ContentManager.Load<TextureCube>(SpacewarGame.Settings.MediaPath + shipReflection[(int)player, 0]);
  363. if (player == PlayerIndex.Two) //player 2 ship has 2 reflection maps
  364. {
  365. reflection2 = SpacewarGame.ContentManager.Load<TextureCube>(SpacewarGame.Settings.MediaPath + shipReflection[(int)player, 1]);
  366. }
  367. }
  368. else
  369. {
  370. //Model
  371. model = SpacewarGame.ContentManager.Load<Model>(SpacewarGame.Settings.MediaPath + modelNames[shapeNumber]);
  372. //Matching Textures
  373. if (shape == EvolvedShapes.Asteroid || shape == EvolvedShapes.Projectile)
  374. texture = SpacewarGame.ContentManager.Load<Texture2D>(SpacewarGame.Settings.MediaPath + textureNames[shapeNumber]);
  375. }
  376. int i = 0;
  377. foreach (ModelMesh modelMesh in model.Meshes)
  378. {
  379. foreach (ModelMeshPart meshPart in modelMesh.MeshParts)
  380. {
  381. if (shape == EvolvedShapes.Weapon)
  382. {
  383. if (string.IsNullOrEmpty(textureNames[i]))
  384. {
  385. texture = null;
  386. }
  387. else
  388. {
  389. texture = SpacewarGame.ContentManager.Load<Texture2D>(SpacewarGame.Settings.MediaPath + textureNames[i]);
  390. }
  391. }
  392. skinTextureParam.SetValue(texture);
  393. if (shape == EvolvedShapes.Ship)
  394. {
  395. //Ships have materials and reflection maps
  396. materialParam.SetValue(material[i].ToVector4());
  397. if (material[i] != Color.White)
  398. skinTextureParam.SetValue(blackTexture);
  399. //Choose the correct reflection map for this subset
  400. reflectionTextureParam.SetValue(useReflection2[i] ? reflection2 : reflection1);
  401. }
  402. else
  403. {
  404. //Material is white
  405. materialParam.SetValue(Color.White.ToVector4());
  406. reflectionTextureParam.SetValue((Texture2D)null);
  407. }
  408. effect.Techniques[0].Passes[0].Apply();
  409. if (meshPart.PrimitiveCount > 0)
  410. {
  411. IGraphicsDeviceService graphicsService = (IGraphicsDeviceService)GameInstance.Services.GetService(typeof(IGraphicsDeviceService));
  412. GraphicsDevice gd = graphicsService.GraphicsDevice;
  413. gd.SetVertexBuffer(meshPart.VertexBuffer);
  414. gd.Indices = meshPart.IndexBuffer;
  415. gd.DrawIndexedPrimitives(PrimitiveType.TriangleList, meshPart.VertexOffset, 0, meshPart.NumVertices, meshPart.StartIndex, meshPart.PrimitiveCount);
  416. }
  417. i++;
  418. }
  419. }
  420. }
  421. /// <summary>
  422. /// Preloads all the in game meshes and textures
  423. /// </summary>
  424. public static void Preload()
  425. {
  426. }
  427. }
  428. }