mikktspace.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /** \file
  2. * \ingroup mikktspace
  3. */
  4. /**
  5. * Copyright (C) 2011 by Morten S. Mikkelsen
  6. *
  7. * This software is provided 'as-is', without any express or implied
  8. * warranty. In no event will the authors be held liable for any damages
  9. * arising from the use of this software.
  10. *
  11. * Permission is granted to anyone to use this software for any purpose,
  12. * including commercial applications, and to alter it and redistribute it
  13. * freely, subject to the following restrictions:
  14. *
  15. * 1. The origin of this software must not be misrepresented; you must not
  16. * claim that you wrote the original software. If you use this software
  17. * in a product, an acknowledgment in the product documentation would be
  18. * appreciated but is not required.
  19. * 2. Altered source versions must be plainly marked as such, and must not be
  20. * misrepresented as being the original software.
  21. * 3. This notice may not be removed or altered from any source distribution.
  22. */
  23. #ifndef __MIKKTSPACE_H__
  24. #define __MIKKTSPACE_H__
  25. #ifdef __cplusplus
  26. extern "C" {
  27. #endif
  28. /* Author: Morten S. Mikkelsen
  29. * Version: 1.0
  30. *
  31. * The files mikktspace.h and mikktspace.c are designed to be
  32. * stand-alone files and it is important that they are kept this way.
  33. * Not having dependencies on structures/classes/libraries specific
  34. * to the program, in which they are used, allows them to be copied
  35. * and used as is into any tool, program or plugin.
  36. * The code is designed to consistently generate the same
  37. * tangent spaces, for a given mesh, in any tool in which it is used.
  38. * This is done by performing an internal welding step and subsequently an order-independent
  39. * evaluation of tangent space for meshes consisting of triangles and quads.
  40. * This means faces can be received in any order and the same is true for
  41. * the order of vertices of each face. The generated result will not be affected
  42. * by such reordering. Additionally, whether degenerate (vertices or texture coordinates)
  43. * primitives are present or not will not affect the generated results either.
  44. * Once tangent space calculation is done the vertices of degenerate primitives will simply
  45. * inherit tangent space from neighboring non degenerate primitives.
  46. * The analysis behind this implementation can be found in my master's thesis
  47. * which is available for download --> http://image.diku.dk/projects/media/morten.mikkelsen.08.pdf
  48. * Note that though the tangent spaces at the vertices are generated in an order-independent way,
  49. * by this implementation, the interpolated tangent space is still affected by which diagonal is
  50. * chosen to split each quad. A sensible solution is to have your tools pipeline always
  51. * split quads by the shortest diagonal. This choice is order-independent and works with mirroring.
  52. * If these have the same length then compare the diagonals defined by the texture coordinates.
  53. * XNormal which is a tool for baking normal maps allows you to write your own tangent space plugin
  54. * and also quad triangulator plugin.
  55. */
  56. typedef int tbool;
  57. typedef struct SMikkTSpaceContext SMikkTSpaceContext;
  58. typedef struct {
  59. // Returns the number of faces (triangles/quads) on the mesh to be processed.
  60. int (*m_getNumFaces)(const SMikkTSpaceContext *pContext);
  61. // Returns the number of vertices on face number iFace
  62. // iFace is a number in the range {0, 1, ..., getNumFaces()-1}
  63. int (*m_getNumVerticesOfFace)(const SMikkTSpaceContext *pContext, const int iFace);
  64. // returns the position/normal/texcoord of the referenced face of vertex number iVert.
  65. // iVert is in the range {0,1,2} for triangles and {0,1,2,3} for quads.
  66. void (*m_getPosition)(const SMikkTSpaceContext *pContext,
  67. float fvPosOut[],
  68. const int iFace,
  69. const int iVert);
  70. void (*m_getNormal)(const SMikkTSpaceContext *pContext,
  71. float fvNormOut[],
  72. const int iFace,
  73. const int iVert);
  74. void (*m_getTexCoord)(const SMikkTSpaceContext *pContext,
  75. float fvTexcOut[],
  76. const int iFace,
  77. const int iVert);
  78. // either (or both) of the two setTSpace callbacks can be set.
  79. // The call-back m_setTSpaceBasic() is sufficient for basic normal mapping.
  80. // This function is used to return the tangent and fSign to the application.
  81. // fvTangent is a unit length vector.
  82. // For normal maps it is sufficient to use the following simplified version of the bitangent
  83. // which is generated at pixel/vertex level.
  84. // bitangent = fSign * cross(vN, tangent);
  85. // Note that the results are returned unindexed. It is possible to generate a new index list
  86. // But averaging/overwriting tangent spaces by using an already existing index list WILL produce
  87. // INCRORRECT results.
  88. // DO NOT! use an already existing index list.
  89. void (*m_setTSpaceBasic)(const SMikkTSpaceContext *pContext,
  90. const float fvTangent[],
  91. const float fSign,
  92. const int iFace,
  93. const int iVert);
  94. // This function is used to return tangent space results to the application.
  95. // fvTangent and fvBiTangent are unit length vectors and fMagS and fMagT are their
  96. // true magnitudes which can be used for relief mapping effects.
  97. // fvBiTangent is the "real" bitangent and thus may not be perpendicular to fvTangent.
  98. // However, both are perpendicular to the vertex normal.
  99. // For normal maps it is sufficient to use the following simplified version of the bitangent
  100. // which is generated at pixel/vertex level.
  101. // fSign = bIsOrientationPreserving ? 1.0f : (-1.0f);
  102. // bitangent = fSign * cross(vN, tangent);
  103. // Note that the results are returned unindexed. It is possible to generate a new index list
  104. // But averaging/overwriting tangent spaces by using an already existing index list WILL produce
  105. // INCRORRECT results. DO NOT! use an already existing index list.
  106. void (*m_setTSpace)(const SMikkTSpaceContext *pContext,
  107. const float fvTangent[],
  108. const float fvBiTangent[],
  109. const float fMagS,
  110. const float fMagT,
  111. const tbool bIsOrientationPreserving,
  112. const int iFace,
  113. const int iVert);
  114. } SMikkTSpaceInterface;
  115. struct SMikkTSpaceContext {
  116. // initialized with callback functions
  117. SMikkTSpaceInterface *m_pInterface;
  118. // pointer to client side mesh data etc.
  119. // (passed as the first parameter with every interface call)
  120. void *m_pUserData;
  121. };
  122. // these are both thread safe!
  123. // Default (recommended) fAngularThreshold is 180 degrees (which means threshold disabled)
  124. tbool genTangSpaceDefault(const SMikkTSpaceContext *pContext);
  125. tbool genTangSpace(const SMikkTSpaceContext *pContext, const float fAngularThreshold);
  126. // To avoid visual errors (distortions/unwanted hard edges in lighting), when using sampled normal
  127. // maps, the normal map sampler must use the exact inverse of the pixel shader transformation.
  128. // The most efficient transformation we can possibly do in the pixel shader is achieved by using,
  129. // directly, the "unnormalized" interpolated tangent, bitangent and vertex normal: vT, vB and vN.
  130. // pixel shader (fast transform out)
  131. // vNout = normalize( vNt.x * vT + vNt.y * vB + vNt.z * vN );
  132. // where vNt is the tangent space normal. The normal map sampler must likewise use the
  133. // interpolated and "unnormalized" tangent, bitangent and vertex normal to be compliant with the
  134. // pixel shader. sampler does (exact inverse of pixel shader):
  135. // float3 row0 = cross(vB, vN);
  136. // float3 row1 = cross(vN, vT);
  137. // float3 row2 = cross(vT, vB);
  138. // float fSign = dot(vT, row0)<0 ? -1 : 1;
  139. // vNt = normalize( fSign * float3(dot(vNout,row0), dot(vNout,row1), dot(vNout,row2)) );
  140. // where vNout is the sampled normal in some chosen 3D space.
  141. //
  142. // Should you choose to reconstruct the bitangent in the pixel shader instead
  143. // of the vertex shader, as explained earlier, then be sure to do this in the normal map sampler
  144. // also. Finally, beware of quad triangulations. If the normal map sampler doesn't use the same
  145. // triangulation of quads as your renderer then problems will occur since the interpolated tangent
  146. // spaces will differ eventhough the vertex level tangent spaces match. This can be solved either
  147. // by triangulating before sampling/exporting or by using the order-independent choice of diagonal
  148. // for splitting quads suggested earlier. However, this must be used both by the sampler and your
  149. // tools/rendering pipeline.
  150. #ifdef __cplusplus
  151. }
  152. #endif
  153. #endif