BsMeshUtility.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. /** Performs various operations on mesh geometry. */
  11. class BS_CORE_EXPORT MeshUtility
  12. {
  13. public:
  14. /**
  15. * Calculates per-vertex normals based on the provided vertices and indices.
  16. *
  17. * @param[in] vertices Set of vertices containing vertex positions.
  18. * @param[in] indices Set of indices containing indexes into vertex array for each triangle.
  19. * @param[in] numVertices Number of vertices in the @p vertices array.
  20. * @param[in] numIndices Number of indices in the @p indices array. Must be a multiple of three.
  21. * @param[out] normals Pre-allocated buffer that will contain the calculated normals. Must be the same size
  22. * as the vertex array.
  23. * @param[in] indexSize Size of a single index in the indices array, in bytes.
  24. *
  25. * @note
  26. * Vertices should be split before calling this method if there are any discontinuities. (for example a vertex on a
  27. * corner of a cube should be split into three vertices used by three triangles in order for the normals to be
  28. * valid.)
  29. */
  30. static void calculateNormals(Vector3* vertices, UINT8* indices, UINT32 numVertices,
  31. UINT32 numIndices, Vector3* normals, UINT32 indexSize = 4);
  32. /**
  33. * Calculates per-vertex tangents and bitangents based on the provided vertices, uv coordinates and indices.
  34. *
  35. * @param[in] vertices Set of vertices containing vertex positions.
  36. * @param[in] normals Set of normals to use when calculating tangents. Must the the same length as the number
  37. * of vertices.
  38. * @param[in] uv Set of UV coordinates to use when calculating tangents. Must the the same length as the
  39. * number of vertices.
  40. * @param[in] indices Set of indices containing indexes into vertex array for each triangle.
  41. * @param[in] numVertices Number of vertices in the @p vertices, @p normals and @p uv arrays.
  42. * @param[in] numIndices Number of indices in the @p indices array. Must be a multiple of three.
  43. * @param[out] tangents Pre-allocated buffer that will contain the calculated tangents. Must be the same size
  44. * as the vertex array.
  45. * @param[out] bitangents Pre-allocated buffer that will contain the calculated bitangents. Must be the same size
  46. * as the vertex array.
  47. * @param[in] indexSize Size of a single index in the indices array, in bytes.
  48. *
  49. * @note
  50. * Vertices should be split before calling this method if there are any discontinuities. (for example a vertex on a
  51. * corner of a cube should be split into three vertices used by three triangles in order for the normals to be
  52. * valid.)
  53. */
  54. static void calculateTangents(Vector3* vertices, Vector3* normals, Vector2* uv, UINT8* indices, UINT32 numVertices,
  55. UINT32 numIndices, Vector3* tangents, Vector3* bitangents, UINT32 indexSize = 4);
  56. /**
  57. * Calculates per-vertex tangent space (normal, tangent, bitangent) based on the provided vertices, uv coordinates
  58. * and indices.
  59. *
  60. * @param[in] vertices Set of vertices containing vertex positions.
  61. * @param[in] uv Set of UV coordinates to use when calculating tangents.
  62. * @param[in] indices Set of indices containing indexes into vertex array for each triangle.
  63. * @param[in] numVertices Number of vertices in the "vertices" array.
  64. * @param[in] numIndices Number of indices in the "indices" array. Must be a multiple of three.
  65. * @param[out] normals Pre-allocated buffer that will contain the calculated normals. Must be the same size
  66. * as the vertex array.
  67. * @param[out] tangents Pre-allocated buffer that will contain the calculated tangents. Must be the same size
  68. * as the vertex array.
  69. * @param[out] bitangents Pre-allocated buffer that will contain the calculated bitangents. Must be the same size
  70. * as the vertex array.
  71. * @param[in] indexSize Size of a single index in the indices array, in bytes.
  72. *
  73. * @note
  74. * Vertices should be split before calling this method if there are any discontinuities. (for example. a vertex on
  75. * a corner of a cube should be split into three vertices used by three triangles in order for the normals to be
  76. * valid.)
  77. */
  78. static void calculateTangentSpace(Vector3* vertices, Vector2* uv, UINT8* indices, UINT32 numVertices,
  79. UINT32 numIndices, Vector3* normals, Vector3* tangents, Vector3* bitangents, UINT32 indexSize = 4);
  80. /**
  81. * Clips a set of two-dimensional vertices and uv coordinates against a set of arbitrary planes.
  82. *
  83. * @param[in] vertices A set of vertices in Vector2 format. Each vertex should be @p vertexStride bytes
  84. * from each other.
  85. * @param[in] uvs A set of UV coordinates in Vector2 format. Each coordinate should be
  86. * @p vertexStride bytes from each other. Can be null if UV is not needed.
  87. * @param[in] numTris Number of triangles to clip (must be number of vertices/uvs / 3).
  88. * @param[in] vertexStride Distance in bytes between two separate vertex or UV values in the provided
  89. * @p vertices and @p uvs buffers.
  90. * @param[in] clipPlanes A set of planes to clip the vertices against. Since the vertices are
  91. * two-dimensional the plane's Z coordinate should be zero.
  92. * @param[in] writeCallback Callback that will be triggered when clipped vertices and UV coordinates are
  93. * generated and need to be stored. Vertices are always generate in tuples of
  94. * three, forming a single triangle.
  95. */
  96. static void clip2D(UINT8* vertices, UINT8* uvs, UINT32 numTris, UINT32 vertexStride, const Vector<Plane>& clipPlanes,
  97. const std::function<void(Vector2*, Vector2*, UINT32)>& writeCallback);
  98. /**
  99. * Clips a set of three-dimensional vertices and uv coordinates against a set of arbitrary planes.
  100. *
  101. * @param[in] vertices A set of vertices in Vector3 format. Each vertex should be @p vertexStride bytes
  102. * from each other.
  103. * @param[in] uvs A set of UV coordinates in Vector2 format. Each coordinate should be
  104. * @p vertexStride bytes from each other. Can be null if UV is not needed.
  105. * @param[in] numTris Number of triangles to clip (must be number of vertices/uvs / 3).
  106. * @param[in] vertexStride Distance in bytes between two separate vertex or UV values in the provided
  107. * @p vertices and @p uvs buffers.
  108. * @param[in] clipPlanes A set of planes to clip the vertices against.
  109. * @param[in] writeCallback Callback that will be triggered when clipped vertices and UV coordinates are
  110. * generated and need to be stored. Vertices are always generate in tuples of
  111. * three, forming a single triangle.
  112. */
  113. static void clip3D(UINT8* vertices, UINT8* uvs, UINT32 numTris, UINT32 vertexStride, const Vector<Plane>& clipPlanes,
  114. const std::function<void(Vector3*, Vector2*, UINT32)>& writeCallback);
  115. };
  116. /** @} */
  117. }