SampleGrid.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // SampleGrid.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 System.Collections.Generic;
  12. using Microsoft.Xna.Framework;
  13. using Microsoft.Xna.Framework.Audio;
  14. using Microsoft.Xna.Framework.Graphics;
  15. using Microsoft.Xna.Framework.Input;
  16. using Microsoft.Xna.Framework.Content;
  17. using Microsoft.Xna.Framework.Storage;
  18. #endregion
  19. namespace PerPixelLightingSample
  20. {
  21. public class SampleGrid : IDisposable
  22. {
  23. #region Fields
  24. private int gridSize;
  25. private float gridScale;
  26. private Color gridColor;
  27. private bool isDisposed;
  28. // Rendering
  29. private VertexBuffer vertexBuffer;
  30. private int vertexCount;
  31. private int primitiveCount;
  32. private BasicEffect effect;
  33. private Matrix projection, view, world;
  34. private GraphicsDevice device;
  35. #endregion
  36. #region Public Properties
  37. public Color GridColor
  38. {
  39. get { return gridColor; }
  40. set { gridColor = value; }
  41. }
  42. public int GridSize
  43. {
  44. get { return gridSize; }
  45. set { gridSize = value; }
  46. }
  47. public float GridScale
  48. {
  49. get { return gridScale; }
  50. set { gridScale = value; }
  51. }
  52. public Matrix ProjectionMatrix
  53. {
  54. get { return projection; }
  55. set { projection = value; }
  56. }
  57. public Matrix WorldMatrix
  58. {
  59. get { return world; }
  60. set { world = value; }
  61. }
  62. public Matrix ViewMatrix
  63. {
  64. get { return view; }
  65. set { view = value; }
  66. }
  67. #endregion
  68. #region Constructors and Loading
  69. public SampleGrid()
  70. {
  71. gridSize = 16;
  72. gridScale = 32f;
  73. gridColor = new Color(0xFF, 0xFF, 0xFF, 0xFF);
  74. world = Matrix.Identity;
  75. view = Matrix.Identity;
  76. projection = Matrix.Identity;
  77. }
  78. public void UnloadGraphicsContent()
  79. {
  80. if (this.vertexBuffer != null)
  81. {
  82. vertexBuffer.Dispose();
  83. vertexBuffer = null;
  84. }
  85. if (effect != null)
  86. {
  87. effect.Dispose();
  88. effect = null;
  89. }
  90. }
  91. public void LoadGraphicsContent(GraphicsDevice graphicsDevice)
  92. {
  93. device = graphicsDevice;
  94. effect = new BasicEffect(device);
  95. int gridSize1 = this.gridSize + 1;
  96. this.primitiveCount = gridSize1 * 2;
  97. this.vertexCount = this.primitiveCount * 2;
  98. VertexPositionColor[] vertices = new VertexPositionColor[this.vertexCount];
  99. float length = (float)gridSize * gridScale;
  100. float halfLength = length * 0.5f;
  101. int index = 0;
  102. for (int i = 0; i < gridSize1; ++i)
  103. {
  104. vertices[index++] = new VertexPositionColor(new Vector3(
  105. -halfLength, 0.0f, i * this.gridScale - halfLength), this.gridColor);
  106. vertices[index++] = new VertexPositionColor(new Vector3(
  107. halfLength, 0.0f, i * this.gridScale - halfLength), this.gridColor);
  108. vertices[index++] = new VertexPositionColor(new Vector3(
  109. i * this.gridScale - halfLength, 0.0f, -halfLength), this.gridColor);
  110. vertices[index++] = new VertexPositionColor(new Vector3(
  111. i * this.gridScale - halfLength, 0.0f, halfLength), this.gridColor);
  112. }
  113. this.vertexBuffer = new VertexBuffer(device, typeof(VertexPositionColor),
  114. this.vertexCount,
  115. BufferUsage.WriteOnly);
  116. this.vertexBuffer.SetData<VertexPositionColor>(vertices);
  117. }
  118. ~SampleGrid()
  119. {
  120. Dispose(false);
  121. }
  122. public void Dispose()
  123. {
  124. Dispose(true);
  125. GC.SuppressFinalize(this);
  126. }
  127. private void Dispose(bool disposing)
  128. {
  129. if (!isDisposed)
  130. {
  131. if (disposing)
  132. {
  133. //if we're manually disposing,
  134. //then managed content should be unloaded
  135. UnloadGraphicsContent();
  136. }
  137. isDisposed = true;
  138. }
  139. }
  140. #endregion
  141. #region Drawing
  142. public void Draw()
  143. {
  144. effect.World = world;
  145. effect.View = view;
  146. effect.Projection = projection;
  147. effect.VertexColorEnabled = true;
  148. effect.LightingEnabled = false;
  149. device.SetVertexBuffer(this.vertexBuffer);
  150. for (int i = 0; i < this.effect.CurrentTechnique.Passes.Count; ++i)
  151. {
  152. this.effect.CurrentTechnique.Passes[i].Apply();
  153. device.DrawPrimitives(PrimitiveType.LineList, 0, this.primitiveCount);
  154. }
  155. }
  156. #endregion
  157. }
  158. }