Game1.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // SplitScreenGame.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 Microsoft.Xna.Framework;
  12. using Microsoft.Xna.Framework.Graphics;
  13. using Microsoft.Xna.Framework.Input;
  14. #endregion
  15. namespace Samples.Deferred
  16. {
  17. public class Game1 : Microsoft.Xna.Framework.Game
  18. {
  19. GraphicsDeviceManager graphics;
  20. SpriteBatch spriteBatch;
  21. SpriteFont font;
  22. KeyboardState previousKeyboardState;
  23. bool useLightA = true;
  24. bool useLightB = true;
  25. bool useLightC = true;
  26. bool rotate = true;
  27. Vector3 cameraPosition;
  28. Matrix world;
  29. Matrix projection;
  30. Matrix view;
  31. Spaceship spaceship;
  32. Vector3 spaceshipPos = Vector3.Zero;
  33. float time = 0;
  34. DeferredRendering _deferredRendering;
  35. const float LightAIntensity = 10f;
  36. const float LightBIntensity = 1f;
  37. const float LightCIntensity = 3f;
  38. float lightAcurrentIntensity = LightAIntensity;
  39. float lightBcurrentIntensity = LightBIntensity;
  40. float lightCcurrentIntensity = LightCIntensity;
  41. public Game1()
  42. {
  43. graphics = new GraphicsDeviceManager(this);
  44. Content.RootDirectory = "Content";
  45. graphics.GraphicsProfile = GraphicsProfile.HiDef;
  46. #if WINDOWS_PHONE
  47. graphics.IsFullScreen = true;
  48. TargetElapsedTime = TimeSpan.FromTicks(333333);
  49. #endif
  50. }
  51. protected override void LoadContent()
  52. {
  53. // Create and load our tank
  54. spaceship = new Spaceship();
  55. spaceship.Load(Content);
  56. spriteBatch = new SpriteBatch(GraphicsDevice);
  57. font = Content.Load<SpriteFont>("font");
  58. projection = Matrix.CreatePerspectiveFieldOfView(
  59. MathHelper.PiOver4, GraphicsDevice.Viewport.AspectRatio, 2000f, 6000f);
  60. _deferredRendering = new DeferredRendering(GraphicsDevice, Content);
  61. }
  62. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  63. protected override void Update(GameTime gameTime)
  64. {
  65. KeyboardState keyState = Keyboard.GetState();
  66. GamePadState gamePadState = GamePad.GetState(PlayerIndex.One);
  67. if (keyState.IsKeyDown(Keys.Escape) || gamePadState.Buttons.Back == ButtonState.Pressed)
  68. Exit();
  69. if (keyState.IsKeyDown(Keys.F1) && !previousKeyboardState.IsKeyDown(Keys.F1))
  70. useLightA = !useLightA;
  71. if (keyState.IsKeyDown(Keys.F2) && !previousKeyboardState.IsKeyDown(Keys.F2))
  72. useLightB = !useLightB;
  73. if (keyState.IsKeyDown(Keys.F3) && !previousKeyboardState.IsKeyDown(Keys.F3))
  74. useLightC = !useLightC;
  75. if (keyState.IsKeyDown(Keys.F4) && !previousKeyboardState.IsKeyDown(Keys.F4))
  76. rotate = !rotate;
  77. if (rotate)
  78. time += (float)gameTime.ElapsedGameTime.TotalSeconds;
  79. float lightAtargetIntensity = (useLightA) ? LightAIntensity : 0f;
  80. float lightBtargetIntensity = (useLightB) ? LightBIntensity : 0f;
  81. float lightCtargetIntensity = (useLightC) ? LightCIntensity : 0f;
  82. lightAcurrentIntensity += (lightAtargetIntensity - lightAcurrentIntensity) * (0.1f);
  83. lightBcurrentIntensity += (lightBtargetIntensity - lightBcurrentIntensity) * (0.1f);
  84. lightCcurrentIntensity += (lightCtargetIntensity - lightCcurrentIntensity) * (0.1f);
  85. world = Matrix.CreateFromAxisAngle(Vector3.Up, time);
  86. cameraPosition = new Vector3(0, 2800f, 2800f);
  87. view = Matrix.CreateLookAt(
  88. cameraPosition,
  89. Vector3.Zero,
  90. Vector3.Up);
  91. previousKeyboardState = keyState;
  92. base.Update(gameTime);
  93. }
  94. protected override void Draw(GameTime gameTime)
  95. {
  96. _deferredRendering.SetGBuffer();
  97. _deferredRendering.ClearGBuffer();
  98. //draw models
  99. GraphicsDevice.BlendState = BlendState.Opaque;
  100. GraphicsDevice.DepthStencilState = DepthStencilState.Default;
  101. spaceship.DrawDeferred(GraphicsDevice, _deferredRendering.basicEffect, world, view, projection);
  102. _deferredRendering.ResolveGBuffer();
  103. GraphicsDevice.SetRenderTarget(_deferredRendering.lightRT);
  104. GraphicsDevice.Clear(Color.Transparent);
  105. // Draw lights
  106. //float ambient = 0.1f;
  107. //GraphicsDevice.Clear(new Color(ambient, ambient, ambient, 0f));
  108. var lightPos = spaceshipPos + new Vector3(2000,100,0);
  109. _deferredRendering.DrawPointLight(
  110. lightPos, Color.Goldenrod, 2000f, lightAcurrentIntensity,
  111. view, projection, cameraPosition);
  112. var light2Pos = spaceshipPos + new Vector3(0, 800, 1000);
  113. _deferredRendering.DrawPointLight(
  114. light2Pos, Color.CornflowerBlue, 900f, lightBcurrentIntensity,
  115. view, projection, cameraPosition);
  116. var spotLightPos = spaceshipPos + new Vector3(-1000,1000,0);
  117. var lightDirection = spaceshipPos - spotLightPos;
  118. _deferredRendering.DrawSpotLight(
  119. spotLightPos, Color.White, 1000f, lightCcurrentIntensity, lightDirection,
  120. MathHelper.ToRadians(3f), MathHelper.ToRadians(3f),
  121. view, projection, cameraPosition);
  122. _deferredRendering.Combine();
  123. spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.Opaque, SamplerState.PointClamp, DepthStencilState.None, RasterizerState.CullNone);
  124. _deferredRendering.DrawRTs(spriteBatch);
  125. spriteBatch.End();
  126. spriteBatch.Begin();
  127. spriteBatch.DrawString(font, String.Format("[F1] PointLight A - ({0})", useLightA ? "ON" : "OFF"), new Vector2(20, 20), Color.White);
  128. spriteBatch.DrawString(font, String.Format("[F2] PointLight B - ({0})", useLightB ? "ON" : "OFF"), new Vector2(20, 40), Color.White);
  129. spriteBatch.DrawString(font, String.Format("[F3] SpotLight C - ({0})", useLightC ? "ON" : "OFF"), new Vector2(20, 60), Color.White);
  130. spriteBatch.DrawString(font, String.Format("[F4] Rotate - ({0})", rotate ? "ON" : "OFF"), new Vector2(20, 80), Color.White);
  131. spriteBatch.End();
  132. base.Draw(gameTime);
  133. }
  134. }
  135. }