/RacingGameWindows1/RacingGame/RacingGame/Shaders/VBScreenHelper.cs

https://bitbucket.org/totallyevil/racing-game-kit · C# · 254 lines · 150 code · 20 blank · 84 comment · 9 complexity · b053ac5f047f60abac8543ab1e9d2ac0 MD5 · raw file

  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // VBScreenHelper.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. #region Using directives
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Text;
  13. using RacingGame.Graphics;
  14. using Microsoft.Xna.Framework;
  15. using Microsoft.Xna.Framework.Graphics;
  16. #endregion
  17. namespace RacingGame.Shaders
  18. {
  19. /// <summary>
  20. /// The VBScreenHelper class helps you to create and use vertex buffers for
  21. /// on screen rendering used in PreScreen and PostScreen shaders.
  22. /// You don't have to create a VB for every shader anymore, just call
  23. /// VBScreenHelper.Render() and everything will be created for you and
  24. /// reused if same parameters are requested again!
  25. /// Supports also Grid screen rendering required for radial motion blur.
  26. /// </summary>
  27. public static class VBScreenHelper
  28. {
  29. #region VBScreen helper class
  30. /// <summary>
  31. /// VBScreen holds all data for the vbScreens list to reuse existing
  32. /// VBScreens. Handles also the VB, creation and rendering.
  33. /// </summary>
  34. private class VBScreen
  35. {
  36. #region Variables
  37. /// <summary>
  38. /// Vertex buffer to render stuff on screen.
  39. /// </summary>
  40. private VertexBuffer vbScreen;
  41. #endregion
  42. #region Constructor
  43. /// <summary>
  44. /// Create VB screen
  45. /// </summary>
  46. public VBScreen()
  47. {
  48. VertexPositionTexture[] vertices = new VertexPositionTexture[]
  49. {
  50. new VertexPositionTexture(
  51. new Vector3(-1.0f, -1.0f, 0.5f),
  52. new Vector2(0, 1)),
  53. new VertexPositionTexture(
  54. new Vector3(-1.0f, 1.0f, 0.5f),
  55. new Vector2(0, 0)),
  56. new VertexPositionTexture(
  57. new Vector3(1.0f, -1.0f, 0.5f),
  58. new Vector2(1, 1)),
  59. new VertexPositionTexture(
  60. new Vector3(1.0f, 1.0f, 0.5f),
  61. new Vector2(1, 0)),
  62. };
  63. vbScreen = new VertexBuffer(
  64. BaseGame.Device,
  65. typeof(VertexPositionTexture),
  66. vertices.Length,
  67. BufferUsage.WriteOnly);
  68. vbScreen.SetData(vertices);
  69. }
  70. #endregion
  71. #region Render
  72. /// <summary>
  73. /// Render
  74. /// </summary>
  75. public void Render()
  76. {
  77. // Rendering is pretty straight forward (if you know how anyway).
  78. BaseGame.Device.SetVertexBuffer(vbScreen);
  79. BaseGame.Device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2);
  80. }
  81. #endregion
  82. }
  83. #endregion
  84. #region GridScreen helper class
  85. /// <summary>
  86. /// Another vertex and index buffer for a screen grid, basically
  87. /// used for the same purpose as VBScreen, but allows us to create
  88. /// a grid (e.g. 10x10), very useful for advanced post screen shaders.
  89. /// </summary>
  90. private class GridScreen
  91. {
  92. #region Variables
  93. /// <summary>
  94. /// Grid dimension
  95. /// </summary>
  96. int gridWidth, gridHeight;
  97. /// <summary>
  98. /// Index buffer
  99. /// </summary>
  100. IndexBuffer indexBuffer = null;
  101. /// <summary>
  102. /// Vertex buffer
  103. /// </summary>
  104. VertexBuffer vertexBuffer = null;
  105. #endregion
  106. #region Constructor
  107. /// <summary>
  108. /// Create grid screen
  109. /// </summary>
  110. /// <param name="setGridDimension">Set grid dimension</param>
  111. public GridScreen(int setGridWidth, int setGridHeight)
  112. {
  113. if (setGridWidth < 2 ||
  114. setGridHeight < 2)
  115. throw new ArgumentException(
  116. "setGridWidth=" + setGridWidth + ", setGridHeight=" + setGridHeight,
  117. "Grid size must be at least (2, 2).");
  118. gridWidth = setGridWidth;
  119. gridHeight = setGridHeight;
  120. // Create vertex buffer
  121. // fix
  122. //vertexBuffer = new VertexBuffer(
  123. // BaseGame.Device,
  124. // typeof(VertexPositionTexture),
  125. // gridWidth * gridHeight,
  126. // ResourceUsage.WriteOnly,
  127. // ResourceManagementMode.Automatic);
  128. vertexBuffer = new VertexBuffer(
  129. BaseGame.Device,
  130. typeof(VertexPositionTexture),
  131. gridWidth * gridHeight,
  132. BufferUsage.WriteOnly);
  133. // Create all vertices
  134. VertexPositionTexture[] vertices =
  135. new VertexPositionTexture[gridWidth * gridHeight];
  136. // Just simply create all vertices of the grid
  137. for (int x = 0; x < gridWidth; x++)
  138. for (int y = 0; y < gridHeight; y++)
  139. {
  140. vertices[x + y * gridWidth] =
  141. new VertexPositionTexture(new Vector3(
  142. -1.0f + 2.0f * (float)x / (float)(gridWidth - 1),
  143. -1.0f + 2.0f * (float)y / (float)(gridHeight - 1),
  144. 0.5f),
  145. new Vector2((float)x / (float)(gridWidth - 1),
  146. // XNA expect bottom up for the screen rendering.
  147. 1.0f - ((float)y / (float)(gridHeight - 1))));
  148. }
  149. vertexBuffer.SetData(vertices);
  150. // Index buffer
  151. // fix
  152. //indexBuffer = new IndexBuffer(
  153. // BaseGame.Device,
  154. // typeof(ushort),
  155. // (gridWidth - 1) * (gridHeight - 1) * 2 * 3,
  156. // ResourceUsage.WriteOnly,
  157. // ResourceManagementMode.Automatic);
  158. indexBuffer = new IndexBuffer(
  159. BaseGame.Device,
  160. typeof(ushort),
  161. (gridWidth - 1) * (gridHeight - 1) * 2 * 3,
  162. BufferUsage.WriteOnly);
  163. ushort[] indices = new ushort[
  164. (gridWidth - 1) * (gridHeight - 1) * 3 * 2];
  165. // Just simply create all indices of the grid
  166. int num = 0;
  167. for (int x = 0; x < gridWidth - 1; x++)
  168. for (int y = 0; y < gridHeight - 1; y++)
  169. {
  170. ushort index1 = (ushort)(x + y * gridWidth);
  171. ushort index2 = (ushort)((x + 1) + y * gridWidth);
  172. ushort index3 = (ushort)((x + 1) + (y + 1) * gridWidth);
  173. ushort index4 = (ushort)(x + (y + 1) * gridWidth);
  174. indices[num] = index1;
  175. indices[num + 1] = index3;
  176. indices[num + 2] = index2;
  177. indices[num + 3] = index1;
  178. indices[num + 4] = index4;
  179. indices[num + 5] = index3;
  180. num += 6;
  181. }
  182. indexBuffer.SetData(indices);
  183. }
  184. #endregion
  185. #region Render
  186. /// <summary>
  187. /// Render
  188. /// </summary>
  189. public void Render()
  190. {
  191. // Rendering is pretty straight forward (if you know how anyway).
  192. BaseGame.Device.SetVertexBuffer(vertexBuffer);
  193. BaseGame.Device.Indices = indexBuffer;
  194. BaseGame.Device.DrawIndexedPrimitives(PrimitiveType.TriangleList,
  195. 0, 0, gridWidth * gridHeight,
  196. 0, (gridWidth - 1) * (gridHeight - 1) * 2);
  197. }
  198. #endregion
  199. }
  200. #endregion
  201. #region Render
  202. /// <summary>
  203. /// Vb screen instance
  204. /// </summary>
  205. static VBScreen vbScreenInstance = null;
  206. /// <summary>
  207. /// Just render a vertex buffer with the screen coordinates.
  208. /// No subTexelSize stuff is performed, do that in the fx file.
  209. /// </summary>
  210. public static void Render()
  211. {
  212. if (vbScreenInstance == null)
  213. vbScreenInstance = new VBScreen();
  214. vbScreenInstance.Render();
  215. }
  216. #endregion
  217. #region Render 10x10 screen grid
  218. /// <summary>
  219. /// Grid screen 1 0x 10 instance
  220. /// </summary>
  221. static GridScreen gridScreen10x10Instance = null;
  222. /// <summary>
  223. /// Just render a 10x10 grid with help of GridScreen on the screen.
  224. /// No subTexelSize stuff is performed, do that in the fx file.
  225. /// </summary>
  226. public static void Render10x10Grid()
  227. {
  228. if (gridScreen10x10Instance == null)
  229. gridScreen10x10Instance = new GridScreen(10, 10);
  230. gridScreen10x10Instance.Render();
  231. }
  232. #endregion
  233. }
  234. }