QuadDrawer.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // QuadDrawer.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 Microsoft.Xna.Framework;
  11. using Microsoft.Xna.Framework.Graphics;
  12. #endregion
  13. namespace Audio3D
  14. {
  15. /// <summary>
  16. /// Helper for drawing 3D quadrilaterals. This is used to draw the cat
  17. /// and dog billboard sprites, and also the checkered ground polygon.
  18. /// </summary>
  19. class QuadDrawer
  20. {
  21. #region Fields
  22. GraphicsDevice graphicsDevice;
  23. AlphaTestEffect effect;
  24. VertexPositionTexture[] vertices;
  25. #endregion
  26. /// <summary>
  27. /// Constructs a new quadrilateral drawing worker.
  28. /// </summary>
  29. public QuadDrawer(GraphicsDevice device)
  30. {
  31. graphicsDevice = device;
  32. effect = new AlphaTestEffect(device);
  33. effect.AlphaFunction = CompareFunction.Greater;
  34. effect.ReferenceAlpha = 128;
  35. // Preallocate an array of four vertices.
  36. vertices = new VertexPositionTexture[4];
  37. vertices[0].Position = new Vector3(1, 1, 0);
  38. vertices[1].Position = new Vector3(-1, 1, 0);
  39. vertices[2].Position = new Vector3(1, -1, 0);
  40. vertices[3].Position = new Vector3(-1, -1, 0);
  41. }
  42. /// <summary>
  43. /// Draws a quadrilateral as part of the 3D world.
  44. /// </summary>
  45. public void DrawQuad(Texture2D texture, float textureRepeats,
  46. Matrix world, Matrix view, Matrix projection)
  47. {
  48. // Set our effect to use the specified texture and camera matrices.
  49. effect.Texture = texture;
  50. effect.World = world;
  51. effect.View = view;
  52. effect.Projection = projection;
  53. // Update our vertex array to use the specified number of texture repeats.
  54. vertices[0].TextureCoordinate = new Vector2(0, 0);
  55. vertices[1].TextureCoordinate = new Vector2(textureRepeats, 0);
  56. vertices[2].TextureCoordinate = new Vector2(0, textureRepeats);
  57. vertices[3].TextureCoordinate = new Vector2(textureRepeats, textureRepeats);
  58. // Draw the quad.
  59. effect.CurrentTechnique.Passes[0].Apply();
  60. graphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleStrip, vertices, 0, 2);
  61. }
  62. }
  63. }