VBScreenHelper.cs 8.7 KB

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