VertexData.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace CommonUI.Display
  5. {
  6. public struct VertexPoint
  7. {
  8. public float X, Y;
  9. public uint Color;
  10. public float U, V;
  11. public VertexPoint(float x, float y, uint color, float u, float v)
  12. {
  13. this.X = x;
  14. this.Y = y;
  15. this.Color = color;
  16. this.U = u;
  17. this.V = v;
  18. }
  19. }
  20. /// <summary>
  21. /// 顶点绘制方式
  22. /// </summary>
  23. public enum VertexTopology : byte
  24. {
  25. /// <summary>
  26. /// 绘制三角形。
  27. /// </summary>
  28. TRIANGLES = 4,
  29. /// <summary>
  30. /// 绘制三角形带。
  31. /// </summary>
  32. TRIANGLE_STRIP = 5,
  33. /// <summary>
  34. /// 绘制四边形。
  35. /// </summary>
  36. QUADS = 7,
  37. /// <summary>
  38. /// 绘制线。
  39. /// </summary>
  40. LINES = 1,
  41. }
  42. /// <summary>
  43. /// 顶点缓冲区,
  44. /// 一个完整定点信息包括,Position 2F, Color uint, TexCoords 2F
  45. /// </summary>
  46. public interface VertexBuffer : IDisposable
  47. {
  48. /// <summary>
  49. /// 顶点数量
  50. /// </summary>
  51. int Count { get; }
  52. int IndicesCount { get; }
  53. /// <summary>
  54. /// Appends the vertices from another VertexData object.
  55. /// </summary>
  56. /// <param name="data"></param>
  57. void Append(VertexBuffer data);
  58. void Append(float x, float y, uint color, float u, float v);
  59. void Append(VertexPoint vertex);
  60. /// <summary>
  61. /// Updates the vertex point of a vertex.
  62. /// </summary>
  63. /// <param name="index"></param>
  64. /// <param name="vertex"></param>
  65. void SetVertex(int index, VertexPoint vertex);
  66. /// <summary>
  67. /// Updates the position values of a vertex.
  68. /// </summary>
  69. /// <param name="index"></param>
  70. /// <param name="x"></param>
  71. /// <param name="y"></param>
  72. void SetPosition(int index, float x, float y);
  73. /// <summary>
  74. /// Updates the RGB color values of a vertex (alpha is not changed).
  75. /// </summary>
  76. /// <param name="index"></param>
  77. /// <param name="color"></param>
  78. void SetColor(int index, uint color);
  79. /// <summary>
  80. /// Updates the texture coordinates of a vertex (range 0-1).
  81. /// </summary>
  82. /// <param name="index"></param>
  83. /// <param name="u"></param>
  84. /// <param name="v"></param>
  85. void SetTexCoords(int index, float u, float v);
  86. /// <summary>
  87. /// Sets the index buffer for the submesh.
  88. /// </summary>
  89. /// <param name="indices"></param>
  90. /// <param name="topology"></param>
  91. void SetIndices(int[] indices, VertexTopology topology);
  92. void SetIndices(int index, int[] indices);
  93. void AddIndicesQuard(int a, int b, int c, int d);
  94. void Optimize();
  95. void Clear();
  96. #region TRANSFORM
  97. void translate(float x, float y);
  98. void rotate(float angle);
  99. void scale(float sx, float sy);
  100. void pushTransform();
  101. void popTransform();
  102. #endregion
  103. }
  104. public static class VertexUtils
  105. {
  106. // -----------------------------------------------------------------------------------------
  107. // utility functions
  108. // -----------------------------------------------------------------------------------------
  109. public static void SetTexCoords(VertexBuffer buffer, int index, Image src, float sx, float sy)
  110. {
  111. buffer.SetTexCoords(index, src.MaxU * sx / src.Width, src.MaxV * sy / src.Height);
  112. }
  113. static public void AddImageQuard(VertexBuffer buffer, Image src, uint rgba, float sx1, float sy1, float sw, float sh, Trans trans, float dx1, float dy1)
  114. {
  115. uint color = rgba;
  116. float dx2 = dx1 + sw;
  117. float dy2 = dy1 + sh;
  118. float sx2 = sx1 + sw;
  119. float sy2 = sy1 + sh;
  120. TransformTrans(ref sx1, ref sy1, ref sx2, ref sy2, trans, ref dx1, ref dy1, ref dx2, ref dy2);
  121. float u1 = src.MaxU * sx1 / src.Width;
  122. float v1 = src.MaxV * sy1 / src.Height;
  123. float u2 = src.MaxU * sx2 / src.Width;
  124. float v2 = src.MaxV * sy2 / src.Height;
  125. int i = buffer.Count;
  126. buffer.Append(dx1, dy1, color, u1, v1);
  127. buffer.Append(dx2, dy1, color, u2, v1);
  128. buffer.Append(dx2, dy2, color, u2, v2);
  129. buffer.Append(dx1, dy2, color, u1, v2);
  130. buffer.AddIndicesQuard(i + 0, i + 1, i + 2, i + 3);
  131. }
  132. public static void SetImageQuard(VertexBuffer buffer, int index, Image src, uint rgba, float sx1, float sy1, float sw, float sh, Trans trans, float dx1, float dy1)
  133. {
  134. float dx2 = dx1 + sw;
  135. float dy2 = dy1 + sh;
  136. float sx2 = sx1 + sw;
  137. float sy2 = sy1 + sh;
  138. TransformTrans(ref sx1, ref sy1, ref sx2, ref sy2, trans, ref dx1, ref dy1, ref dx2, ref dy2);
  139. float u1 = src.MaxU * sx1 / src.Width;
  140. float v1 = src.MaxV * sy1 / src.Height;
  141. float u2 = src.MaxU * sx2 / src.Width;
  142. float v2 = src.MaxV * sy2 / src.Height;
  143. buffer.SetVertex(index + 0, new VertexPoint(dx1, dy1, rgba, u1, v1));
  144. buffer.SetVertex(index + 1, new VertexPoint(dx2, dy1, rgba, u2, v1));
  145. buffer.SetVertex(index + 2, new VertexPoint(dx2, dy2, rgba, u2, v2));
  146. buffer.SetVertex(index + 3, new VertexPoint(dx1, dy2, rgba, u1, v2));
  147. }
  148. public static void TransformTrans(ref float sx1, ref float sy1, ref float sx2, ref float sy2, Trans trans, ref float dx1, ref float dy1, ref float dx2, ref float dy2)
  149. {
  150. switch (trans)
  151. {
  152. case Trans.TRANS_NONE:
  153. break;
  154. case Trans.TRANS_MIRROR:
  155. break;
  156. }
  157. }
  158. }
  159. }