DeferredSampleComponent.cs 6.7 KB

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