mikktspace.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /** \file mikktspace/mikktspace.h
  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 evaluation
  39. * 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, float fvPosOut[], const int iFace, const int iVert);
  67. void (*m_getNormal)(const SMikkTSpaceContext * pContext, float fvNormOut[], const int iFace, const int iVert);
  68. void (*m_getTexCoord)(const SMikkTSpaceContext * pContext, float fvTexcOut[], const int iFace, const int iVert);
  69. // either (or both) of the two setTSpace callbacks can be set.
  70. // The call-back m_setTSpaceBasic() is sufficient for basic normal mapping.
  71. // This function is used to return the tangent and fSign to the application.
  72. // fvTangent is a unit length vector.
  73. // For normal maps it is sufficient to use the following simplified version of the bitangent which is generated at pixel/vertex level.
  74. // bitangent = fSign * cross(vN, tangent);
  75. // Note that the results are returned unindexed. It is possible to generate a new index list
  76. // But averaging/overwriting tangent spaces by using an already existing index list WILL produce INCRORRECT results.
  77. // DO NOT! use an already existing index list.
  78. void (*m_setTSpaceBasic)(const SMikkTSpaceContext * pContext, const float fvTangent[], const float fSign, const int iFace, const int iVert);
  79. // This function is used to return tangent space results to the application.
  80. // fvTangent and fvBiTangent are unit length vectors and fMagS and fMagT are their
  81. // true magnitudes which can be used for relief mapping effects.
  82. // fvBiTangent is the "real" bitangent and thus may not be perpendicular to fvTangent.
  83. // However, both are perpendicular to the vertex normal.
  84. // For normal maps it is sufficient to use the following simplified version of the bitangent which is generated at pixel/vertex level.
  85. // fSign = bIsOrientationPreserving ? 1.0f : (-1.0f);
  86. // bitangent = fSign * cross(vN, tangent);
  87. // Note that the results are returned unindexed. It is possible to generate a new index list
  88. // But averaging/overwriting tangent spaces by using an already existing index list WILL produce INCRORRECT results.
  89. // DO NOT! use an already existing index list.
  90. void (*m_setTSpace)(const SMikkTSpaceContext * pContext, const float fvTangent[], const float fvBiTangent[], const float fMagS, const float fMagT,
  91. const tbool bIsOrientationPreserving, const int iFace, const int iVert);
  92. } SMikkTSpaceInterface;
  93. struct SMikkTSpaceContext
  94. {
  95. SMikkTSpaceInterface * m_pInterface; // initialized with callback functions
  96. void * m_pUserData; // pointer to client side mesh data etc. (passed as the first parameter with every interface call)
  97. };
  98. // these are both thread safe!
  99. tbool genTangSpaceDefault(const SMikkTSpaceContext * pContext); // Default (recommended) fAngularThreshold is 180 degrees (which means threshold disabled)
  100. tbool genTangSpace(const SMikkTSpaceContext * pContext, const float fAngularThreshold);
  101. // To avoid visual errors (distortions/unwanted hard edges in lighting), when using sampled normal maps, the
  102. // normal map sampler must use the exact inverse of the pixel shader transformation.
  103. // The most efficient transformation we can possibly do in the pixel shader is
  104. // achieved by using, directly, the "unnormalized" interpolated tangent, bitangent and vertex normal: vT, vB and vN.
  105. // pixel shader (fast transform out)
  106. // vNout = normalize( vNt.x * vT + vNt.y * vB + vNt.z * vN );
  107. // where vNt is the tangent space normal. The normal map sampler must likewise use the
  108. // interpolated and "unnormalized" tangent, bitangent and vertex normal to be compliant with the pixel shader.
  109. // sampler does (exact inverse of pixel shader):
  110. // float3 row0 = cross(vB, vN);
  111. // float3 row1 = cross(vN, vT);
  112. // float3 row2 = cross(vT, vB);
  113. // float fSign = dot(vT, row0)<0 ? -1 : 1;
  114. // vNt = normalize( fSign * float3(dot(vNout,row0), dot(vNout,row1), dot(vNout,row2)) );
  115. // where vNout is the sampled normal in some chosen 3D space.
  116. //
  117. // Should you choose to reconstruct the bitangent in the pixel shader instead
  118. // of the vertex shader, as explained earlier, then be sure to do this in the normal map sampler also.
  119. // Finally, beware of quad triangulations. If the normal map sampler doesn't use the same triangulation of
  120. // quads as your renderer then problems will occur since the interpolated tangent spaces will differ
  121. // eventhough the vertex level tangent spaces match. This can be solved either by triangulating before
  122. // sampling/exporting or by using the order-independent choice of diagonal for splitting quads suggested earlier.
  123. // However, this must be used both by the sampler and your tools/rendering pipeline.
  124. #ifdef __cplusplus
  125. }
  126. #endif
  127. #endif