Quad.cs 6.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // Quad.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. using System;
  10. using System.Collections.Generic;
  11. using Microsoft.Xna.Framework;
  12. using Microsoft.Xna.Framework.Audio;
  13. using Microsoft.Xna.Framework.Graphics;
  14. using Microsoft.Xna.Framework.Input;
  15. using Microsoft.Xna.Framework.Content;
  16. namespace TexturedQuad
  17. {
  18. public struct Quad
  19. {
  20. public Vector3 Origin;
  21. public Vector3 UpperLeft;
  22. public Vector3 LowerLeft;
  23. public Vector3 UpperRight;
  24. public Vector3 LowerRight;
  25. public Vector3 Normal;
  26. public Vector3 Up;
  27. public Vector3 Left;
  28. public VertexPositionNormalTexture[] Vertices;
  29. // public int[] Indexes;
  30. public short[] Indexes;
  31. public Quad( Vector3 origin, Vector3 normal, Vector3 up,
  32. float width, float height )
  33. {
  34. Vertices = new VertexPositionNormalTexture[4];
  35. Indexes = new short[6];
  36. Origin = origin;
  37. Normal = normal;
  38. Up = up;
  39. // Calculate the quad corners
  40. Left = Vector3.Cross( normal, Up );
  41. Vector3 uppercenter = (Up * height / 2) + origin;
  42. UpperLeft = uppercenter + (Left * width / 2);
  43. UpperRight = uppercenter - (Left * width / 2);
  44. LowerLeft = UpperLeft - (Up * height);
  45. LowerRight = UpperRight - (Up * height);
  46. FillVertices();
  47. }
  48. private void FillVertices()
  49. {
  50. // Fill in texture coordinates to display full texture
  51. // on quad
  52. Vector2 textureUpperLeft = new Vector2( 0.0f, 0.0f );
  53. Vector2 textureUpperRight = new Vector2( 1.0f, 0.0f );
  54. Vector2 textureLowerLeft = new Vector2( 0.0f, 1.0f );
  55. Vector2 textureLowerRight = new Vector2( 1.0f, 1.0f );
  56. // Provide a normal for each vertex
  57. for (int i = 0; i < Vertices.Length; i++)
  58. {
  59. Vertices[i].Normal = Normal;
  60. }
  61. // Set the position and texture coordinate for each
  62. // vertex
  63. Vertices[0].Position = LowerLeft;
  64. Vertices[0].TextureCoordinate = textureLowerLeft;
  65. Vertices[1].Position = UpperLeft;
  66. Vertices[1].TextureCoordinate = textureUpperLeft;
  67. Vertices[2].Position = LowerRight;
  68. Vertices[2].TextureCoordinate = textureLowerRight;
  69. Vertices[3].Position = UpperRight;
  70. Vertices[3].TextureCoordinate = textureUpperRight;
  71. // Set the index buffer for each vertex, using
  72. // clockwise winding
  73. Indexes[0] = 0;
  74. Indexes[1] = 1;
  75. Indexes[2] = 2;
  76. Indexes[3] = 2;
  77. Indexes[4] = 1;
  78. Indexes[5] = 3;
  79. }
  80. }
  81. }