SampleGame.cs 6.6 KB

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