TangentVertex.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. //-----------------------------------------------------------------------------
  2. // TangentVertex.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. using System;
  10. using System.Collections;
  11. using System.Text;
  12. namespace RacingGame.Graphics
  13. {
  14. /// <summary>
  15. /// Tangent vertex format for shader vertex format used all over the place.
  16. /// It contains: Position, Normal vector, texture coords, tangent vector.
  17. /// </summary>
  18. public struct TangentVertex : IVertexType
  19. {
  20. /// <summary>
  21. /// Position
  22. /// </summary>
  23. public Vector3 pos;
  24. /// <summary>
  25. /// Texture coordinates
  26. /// </summary>
  27. public Vector2 uv;
  28. /// <summary>
  29. /// Normal
  30. /// </summary>
  31. public Vector3 normal;
  32. /// <summary>
  33. /// Tangent
  34. /// </summary>
  35. public Vector3 tangent;
  36. /*fixed number to prevent having us to use unsafe code
  37. /// <summary>
  38. /// Stride size, in XNA called SizeInBytes. I'm just conforming with that.
  39. /// Btw: How is this supposed to work without using unsafe AND
  40. /// without using System.Runtime.InteropServices.Marshal.SizeOf?
  41. /// </summary>
  42. public static unsafe int SizeInBytes
  43. {
  44. get
  45. {
  46. return (int)sizeof(TangentVertex);
  47. }
  48. }
  49. */
  50. /// <summary>
  51. /// Stride size, in XNA called SizeInBytes. I'm just conforming with that.
  52. /// </summary>
  53. public static int SizeInBytes
  54. {
  55. get
  56. {
  57. // 4 bytes per float:
  58. // 3 floats pos, 2 floats uv, 3 floats normal and 3 float tangent.
  59. return 4 * (3 + 2 + 3 + 3);
  60. }
  61. }
  62. /// <summary>
  63. /// U texture coordinate
  64. /// </summary>
  65. /// <returns>Float</returns>
  66. public float U
  67. {
  68. get
  69. {
  70. return uv.X;
  71. }
  72. }
  73. /// <summary>
  74. /// V texture coordinate
  75. /// </summary>
  76. /// <returns>Float</returns>
  77. public float V
  78. {
  79. get
  80. {
  81. return uv.Y;
  82. }
  83. }
  84. /// <summary>
  85. /// Create tangent vertex
  86. /// </summary>
  87. /// <param name="setPos">Set position</param>
  88. /// <param name="setU">Set u texture coordinate</param>
  89. /// <param name="setV">Set v texture coordinate</param>
  90. /// <param name="setNormal">Set normal</param>
  91. /// <param name="setTangent">Set tangent</param>
  92. public TangentVertex(
  93. Vector3 setPos,
  94. float setU, float setV,
  95. Vector3 setNormal,
  96. Vector3 setTangent)
  97. {
  98. pos = setPos;
  99. uv = new Vector2(setU, setV);
  100. normal = setNormal;
  101. tangent = setTangent;
  102. }
  103. /// <summary>
  104. /// Create tangent vertex
  105. /// </summary>
  106. /// <param name="setPos">Set position</param>
  107. /// <param name="setUv">Set uv texture coordinates</param>
  108. /// <param name="setNormal">Set normal</param>
  109. /// <param name="setTangent">Set tangent</param>
  110. public TangentVertex(
  111. Vector3 setPos,
  112. Vector2 setUv,
  113. Vector3 setNormal,
  114. Vector3 setTangent)
  115. {
  116. pos = setPos;
  117. uv = setUv;
  118. normal = setNormal;
  119. tangent = setTangent;
  120. }
  121. /// <summary>
  122. /// To string
  123. /// </summary>
  124. public override string ToString()
  125. {
  126. return "TangentVertex(pos=" + pos + ", " +
  127. "u=" + uv.X + ", " +
  128. "v=" + uv.Y + ", " +
  129. "normal=" + normal + ", " +
  130. "tangent=" + tangent + ")";
  131. }
  132. /// <summary>
  133. /// Vertex elements for Mesh.Clone
  134. /// </summary>
  135. private static readonly VertexElement[] VertexElements =
  136. GenerateVertexElements();
  137. /// <summary>
  138. /// Vertex declaration for vertex buffers.
  139. /// </summary>
  140. public static readonly VertexDeclaration VertexDeclaration = new VertexDeclaration
  141. (
  142. // Construct new vertex declaration with tangent info
  143. // First the normal stuff (we should already have that)
  144. new VertexElement(0, VertexElementFormat.Vector3,
  145. VertexElementUsage.Position, 0),
  146. new VertexElement(12, VertexElementFormat.Vector2,
  147. VertexElementUsage.TextureCoordinate, 0),
  148. new VertexElement(20, VertexElementFormat.Vector3,
  149. VertexElementUsage.Normal, 0),
  150. // And now the tangent
  151. new VertexElement(32, VertexElementFormat.Vector3,
  152. VertexElementUsage.Tangent, 0)
  153. );
  154. //Implement the IVertexType interface so that we can get the vertex
  155. //declaration straight from our custom vertex!
  156. VertexDeclaration IVertexType.VertexDeclaration
  157. {
  158. get { return VertexDeclaration; }
  159. }
  160. /// <summary>
  161. /// Generate vertex declaration
  162. /// </summary>
  163. private static VertexElement[] GenerateVertexElements()
  164. {
  165. VertexElement[] decl = new VertexElement[]
  166. {
  167. // Construct new vertex declaration with tangent info
  168. // First the normal stuff (we should already have that)
  169. new VertexElement(0, VertexElementFormat.Vector3,
  170. VertexElementUsage.Position, 0),
  171. new VertexElement(12, VertexElementFormat.Vector2,
  172. VertexElementUsage.TextureCoordinate, 0),
  173. new VertexElement(20, VertexElementFormat.Vector3,
  174. VertexElementUsage.Normal, 0),
  175. // And now the tangent
  176. new VertexElement(32, VertexElementFormat.Vector3,
  177. VertexElementUsage.Tangent, 0),
  178. };
  179. return decl;
  180. }
  181. /// <summary>
  182. /// Returns true if declaration is tangent vertex declaration.
  183. /// </summary>
  184. public static bool IsTangentVertexDeclaration(
  185. VertexElement[] declaration)
  186. {
  187. if (declaration == null)
  188. throw new ArgumentNullException("declaration");
  189. return
  190. declaration.Length == 4 &&
  191. declaration[0].VertexElementUsage == VertexElementUsage.Position &&
  192. declaration[1].VertexElementUsage ==
  193. VertexElementUsage.TextureCoordinate &&
  194. declaration[2].VertexElementUsage == VertexElementUsage.Normal &&
  195. declaration[3].VertexElementUsage == VertexElementUsage.Tangent;
  196. }
  197. }
  198. }