QuadDrawer.cs 2.4 KB

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