BsMeshUtility.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsCorePrerequisites.h"
  5. namespace BansheeEngine
  6. {
  7. /** @addtogroup Utility-Core
  8. * @{
  9. */
  10. /** Normal packed in a 32-bit structure. */
  11. union PackedNormal
  12. {
  13. struct
  14. {
  15. UINT8 x;
  16. UINT8 y;
  17. UINT8 z;
  18. UINT8 w;
  19. };
  20. UINT32 packed;
  21. };
  22. /** Performs various operations on mesh geometry. */
  23. class BS_CORE_EXPORT MeshUtility
  24. {
  25. public:
  26. /**
  27. * Calculates per-vertex normals based on the provided vertices and indices.
  28. *
  29. * @param[in] vertices Set of vertices containing vertex positions.
  30. * @param[in] indices Set of indices containing indexes into vertex array for each triangle.
  31. * @param[in] numVertices Number of vertices in the @p vertices array.
  32. * @param[in] numIndices Number of indices in the @p indices array. Must be a multiple of three.
  33. * @param[out] normals Pre-allocated buffer that will contain the calculated normals. Must be the same size
  34. * as the vertex array.
  35. * @param[in] indexSize Size of a single index in the indices array, in bytes.
  36. *
  37. * @note
  38. * Vertices should be split before calling this method if there are any discontinuities. (for example a vertex on a
  39. * corner of a cube should be split into three vertices used by three triangles in order for the normals to be
  40. * valid.)
  41. */
  42. static void calculateNormals(Vector3* vertices, UINT8* indices, UINT32 numVertices,
  43. UINT32 numIndices, Vector3* normals, UINT32 indexSize = 4);
  44. /**
  45. * Calculates per-vertex tangents and bitangents based on the provided vertices, uv coordinates and indices.
  46. *
  47. * @param[in] vertices Set of vertices containing vertex positions.
  48. * @param[in] normals Set of normals to use when calculating tangents. Must the the same length as the number
  49. * of vertices.
  50. * @param[in] uv Set of UV coordinates to use when calculating tangents. Must the the same length as the
  51. * number of vertices.
  52. * @param[in] indices Set of indices containing indexes into vertex array for each triangle.
  53. * @param[in] numVertices Number of vertices in the @p vertices, @p normals and @p uv arrays.
  54. * @param[in] numIndices Number of indices in the @p indices array. Must be a multiple of three.
  55. * @param[out] tangents Pre-allocated buffer that will contain the calculated tangents. Must be the same size
  56. * as the vertex array.
  57. * @param[out] bitangents Pre-allocated buffer that will contain the calculated bitangents. Must be the same size
  58. * as the vertex array.
  59. * @param[in] indexSize Size of a single index in the indices array, in bytes.
  60. *
  61. * @note
  62. * Vertices should be split before calling this method if there are any discontinuities. (for example a vertex on a
  63. * corner of a cube should be split into three vertices used by three triangles in order for the normals to be
  64. * valid.)
  65. */
  66. static void calculateTangents(Vector3* vertices, Vector3* normals, Vector2* uv, UINT8* indices, UINT32 numVertices,
  67. UINT32 numIndices, Vector3* tangents, Vector3* bitangents, UINT32 indexSize = 4);
  68. /**
  69. * Calculates per-vertex tangent space (normal, tangent, bitangent) based on the provided vertices, uv coordinates
  70. * and indices.
  71. *
  72. * @param[in] vertices Set of vertices containing vertex positions.
  73. * @param[in] uv Set of UV coordinates to use when calculating tangents.
  74. * @param[in] indices Set of indices containing indexes into vertex array for each triangle.
  75. * @param[in] numVertices Number of vertices in the "vertices" array.
  76. * @param[in] numIndices Number of indices in the "indices" array. Must be a multiple of three.
  77. * @param[out] normals Pre-allocated buffer that will contain the calculated normals. Must be the same size
  78. * as the vertex array.
  79. * @param[out] tangents Pre-allocated buffer that will contain the calculated tangents. Must be the same size
  80. * as the vertex array.
  81. * @param[out] bitangents Pre-allocated buffer that will contain the calculated bitangents. Must be the same size
  82. * as the vertex array.
  83. * @param[in] indexSize Size of a single index in the indices array, in bytes.
  84. *
  85. * @note
  86. * Vertices should be split before calling this method if there are any discontinuities. (for example. a vertex on
  87. * a corner of a cube should be split into three vertices used by three triangles in order for the normals to be
  88. * valid.)
  89. */
  90. static void calculateTangentSpace(Vector3* vertices, Vector2* uv, UINT8* indices, UINT32 numVertices,
  91. UINT32 numIndices, Vector3* normals, Vector3* tangents, Vector3* bitangents, UINT32 indexSize = 4);
  92. /**
  93. * Clips a set of two-dimensional vertices and uv coordinates against a set of arbitrary planes.
  94. *
  95. * @param[in] vertices A set of vertices in Vector2 format. Each vertex should be @p vertexStride bytes
  96. * from each other.
  97. * @param[in] uvs A set of UV coordinates in Vector2 format. Each coordinate should be
  98. * @p vertexStride bytes from each other. Can be null if UV is not needed.
  99. * @param[in] numTris Number of triangles to clip (must be number of vertices/uvs / 3).
  100. * @param[in] vertexStride Distance in bytes between two separate vertex or UV values in the provided
  101. * @p vertices and @p uvs buffers.
  102. * @param[in] clipPlanes A set of planes to clip the vertices against. Since the vertices are
  103. * two-dimensional the plane's Z coordinate should be zero.
  104. * @param[in] writeCallback Callback that will be triggered when clipped vertices and UV coordinates are
  105. * generated and need to be stored. Vertices are always generate in tuples of
  106. * three, forming a single triangle.
  107. */
  108. static void clip2D(UINT8* vertices, UINT8* uvs, UINT32 numTris, UINT32 vertexStride, const Vector<Plane>& clipPlanes,
  109. const std::function<void(Vector2*, Vector2*, UINT32)>& writeCallback);
  110. /**
  111. * Clips a set of three-dimensional vertices and uv coordinates against a set of arbitrary planes.
  112. *
  113. * @param[in] vertices A set of vertices in Vector3 format. Each vertex should be @p vertexStride bytes
  114. * from each other.
  115. * @param[in] uvs A set of UV coordinates in Vector2 format. Each coordinate should be
  116. * @p vertexStride bytes from each other. Can be null if UV is not needed.
  117. * @param[in] numTris Number of triangles to clip (must be number of vertices/uvs / 3).
  118. * @param[in] vertexStride Distance in bytes between two separate vertex or UV values in the provided
  119. * @p vertices and @p uvs buffers.
  120. * @param[in] clipPlanes A set of planes to clip the vertices against.
  121. * @param[in] writeCallback Callback that will be triggered when clipped vertices and UV coordinates are
  122. * generated and need to be stored. Vertices are always generate in tuples of
  123. * three, forming a single triangle.
  124. */
  125. static void clip3D(UINT8* vertices, UINT8* uvs, UINT32 numTris, UINT32 vertexStride, const Vector<Plane>& clipPlanes,
  126. const std::function<void(Vector3*, Vector2*, UINT32)>& writeCallback);
  127. /**
  128. * Encodes normals from 32-bit float format into 4D 8-bit packed format.
  129. *
  130. * @param[in] source Buffer containing data to encode. Must have @p count entries.
  131. * @param[out] destination Buffer to output the data to. Must have @p count entries, each 32-bits.
  132. * @param[in] count Number of entries in the @p source and @p destination arrays.
  133. * @param[in] stride Distance between two entries in the @p destination buffer, in bytes.
  134. */
  135. static void packNormals(Vector3* source, UINT8* destination, UINT32 count, UINT32 stride);
  136. /**
  137. * Encodes normals from 32-bit float format into 4D 8-bit packed format.
  138. *
  139. * @param[in] source Buffer containing data to encode. Must have @p count entries.
  140. * @param[out] destination Buffer to output the data to. Must have @p count entries, each 32-bits.
  141. * @param[in] count Number of entries in the @p source and @p destination arrays.
  142. * @param[in] stride Distance between two entries in the @p destination buffer, in bytes.
  143. */
  144. static void packNormals(Vector4* source, UINT8* destination, UINT32 count, UINT32 stride);
  145. /**
  146. * Decodes normals from 4D 8-bit packed format into a 32-bit float format.
  147. *
  148. * @param[in] source Buffer containing data to encode. Must have @p count entries, each 32-bits.
  149. * @param[out] destination Buffer to output the data to. Must have @p count entries.
  150. * @param[in] count Number of entries in the @p source and @p destination arrays.
  151. * @param[in] stride Distance between two entries in the @p source buffer, in bytes.
  152. */
  153. static void unpackNormals(UINT8* source, Vector3* destination, UINT32 count, UINT32 stride);
  154. /**
  155. * Decodes normals from 4D 8-bit packed format into a 32-bit float format.
  156. *
  157. * @param[in] source Buffer containing data to encode. Must have @p count entries, each 32-bits.
  158. * @param[out] destination Buffer to output the data to. Must have @p count entries.
  159. * @param[in] count Number of entries in the @p source and @p destination arrays.
  160. * @param[in] stride Distance between two entries in the @p source buffer, in bytes.
  161. */
  162. static void unpackNormals(UINT8* source, Vector4* destination, UINT32 count, UINT32 stride);
  163. };
  164. /** @} */
  165. }