Quad.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using System;
  2. using Microsoft.Xna.Framework;
  3. using Microsoft.Xna.Framework.Graphics;
  4. namespace TexturedQuad.Core
  5. {
  6. public struct Quad
  7. {
  8. public Vector3 Origin;
  9. public Vector3 UpperLeft;
  10. public Vector3 LowerLeft;
  11. public Vector3 UpperRight;
  12. public Vector3 LowerRight;
  13. public Vector3 Normal;
  14. public Vector3 Up;
  15. public Vector3 Left;
  16. public VertexPositionNormalTexture[] Vertices;
  17. public short[] Indexes;
  18. /// <summary>
  19. /// Initializes a new quad with the specified origin, normal, up vector, width, and height.
  20. /// Calculates the quad's corners and fills the vertex and index buffers.
  21. /// </summary>
  22. /// <param name="origin">The center of the quad.</param>
  23. /// <param name="normal">The normal vector of the quad's surface.</param>
  24. /// <param name="up">The up direction for the quad.</param>
  25. /// <param name="width">The width of the quad.</param>
  26. /// <param name="height">The height of the quad.</param>
  27. public Quad(Vector3 origin, Vector3 normal, Vector3 up, float width, float height)
  28. {
  29. Vertices = new VertexPositionNormalTexture[4];
  30. Indexes = new short[6];
  31. Origin = origin;
  32. Normal = normal;
  33. Up = up;
  34. Left = Vector3.Cross(normal, Up);
  35. Vector3 uppercenter = (Up * height / 2) + origin;
  36. UpperLeft = uppercenter + (Left * width / 2);
  37. UpperRight = uppercenter - (Left * width / 2);
  38. LowerLeft = UpperLeft - (Up * height);
  39. LowerRight = UpperRight - (Up * height);
  40. FillVertices();
  41. }
  42. /// <summary>
  43. /// Fills the quad's vertex and index buffers with positions, normals, and texture coordinates.
  44. /// </summary>
  45. private void FillVertices()
  46. {
  47. Vector2 textureUpperLeft = new Vector2(0.0f, 0.0f);
  48. Vector2 textureUpperRight = new Vector2(1.0f, 0.0f);
  49. Vector2 textureLowerLeft = new Vector2(0.0f, 1.0f);
  50. Vector2 textureLowerRight = new Vector2(1.0f, 1.0f);
  51. for (int i = 0; i < Vertices.Length; i++)
  52. {
  53. Vertices[i].Normal = Normal;
  54. }
  55. Vertices[0].Position = LowerLeft;
  56. Vertices[0].TextureCoordinate = textureLowerLeft;
  57. Vertices[1].Position = UpperLeft;
  58. Vertices[1].TextureCoordinate = textureUpperLeft;
  59. Vertices[2].Position = LowerRight;
  60. Vertices[2].TextureCoordinate = textureLowerRight;
  61. Vertices[3].Position = UpperRight;
  62. Vertices[3].TextureCoordinate = textureUpperRight;
  63. Indexes[0] = 0;
  64. Indexes[1] = 1;
  65. Indexes[2] = 2;
  66. Indexes[3] = 2;
  67. Indexes[4] = 1;
  68. Indexes[5] = 3;
  69. }
  70. }
  71. }