Blender V2.61 - r43446

mikktspace.h

Go to the documentation of this file.
00001 
00024 #ifndef __MIKKTSPACE_H__
00025 #define __MIKKTSPACE_H__
00026 
00027 
00028 #ifdef __cplusplus
00029 extern "C" {
00030 #endif
00031 
00032 /* Author: Morten S. Mikkelsen
00033  * Version: 1.0
00034  *
00035  * The files mikktspace.h and mikktspace.c are designed to be
00036  * stand-alone files and it is important that they are kept this way.
00037  * Not having dependencies on structures/classes/libraries specific
00038  * to the program, in which they are used, allows them to be copied
00039  * and used as is into any tool, program or plugin.
00040  * The code is designed to consistently generate the same
00041  * tangent spaces, for a given mesh, in any tool in which it is used.
00042  * This is done by performing an internal welding step and subsequently an order-independent evaluation
00043  * of tangent space for meshes consisting of triangles and quads.
00044  * This means faces can be received in any order and the same is true for
00045  * the order of vertices of each face. The generated result will not be affected
00046  * by such reordering. Additionally, whether degenerate (vertices or texture coordinates)
00047  * primitives are present or not will not affect the generated results either.
00048  * Once tangent space calculation is done the vertices of degenerate primitives will simply
00049  * inherit tangent space from neighboring non degenerate primitives.
00050  * The analysis behind this implementation can be found in my master's thesis
00051  * which is available for download --> http://image.diku.dk/projects/media/morten.mikkelsen.08.pdf
00052  * Note that though the tangent spaces at the vertices are generated in an order-independent way,
00053  * by this implementation, the interpolated tangent space is still affected by which diagonal is
00054  * chosen to split each quad. A sensible solution is to have your tools pipeline always
00055  * split quads by the shortest diagonal. This choice is order-independent and works with mirroring.
00056  * If these have the same length then compare the diagonals defined by the texture coordinates.
00057  * XNormal which is a tool for baking normal maps allows you to write your own tangent space plugin
00058  * and also quad triangulator plugin.
00059  */
00060 
00061 
00062 typedef int tbool;
00063 typedef struct SMikkTSpaceContext SMikkTSpaceContext;
00064 
00065 typedef struct
00066 {
00067     // Returns the number of faces (triangles/quads) on the mesh to be processed.
00068     int (*m_getNumFaces)(const SMikkTSpaceContext * pContext);
00069 
00070     // Returns the number of vertices on face number iFace
00071     // iFace is a number in the range {0, 1, ..., getNumFaces()-1}
00072     int (*m_getNumVerticesOfFace)(const SMikkTSpaceContext * pContext, const int iFace);
00073 
00074     // returns the position/normal/texcoord of the referenced face of vertex number iVert.
00075     // iVert is in the range {0,1,2} for triangles and {0,1,2,3} for quads.
00076     void (*m_getPosition)(const SMikkTSpaceContext * pContext, float fvPosOut[], const int iFace, const int iVert);
00077     void (*m_getNormal)(const SMikkTSpaceContext * pContext, float fvNormOut[], const int iFace, const int iVert);
00078     void (*m_getTexCoord)(const SMikkTSpaceContext * pContext, float fvTexcOut[], const int iFace, const int iVert);
00079 
00080     // either (or both) of the two setTSpace callbacks can be set.
00081     // The call-back m_setTSpaceBasic() is sufficient for basic normal mapping.
00082 
00083     // This function is used to return the tangent and fSign to the application.
00084     // fvTangent is a unit length vector.
00085     // For normal maps it is sufficient to use the following simplified version of the bitangent which is generated at pixel/vertex level.
00086     // bitangent = fSign * cross(vN, tangent);
00087     // Note that the results are returned unindexed. It is possible to generate a new index list
00088     // But averaging/overwriting tangent spaces by using an already existing index list WILL produce INCRORRECT results.
00089     // DO NOT! use an already existing index list.
00090     void (*m_setTSpaceBasic)(const SMikkTSpaceContext * pContext, const float fvTangent[], const float fSign, const int iFace, const int iVert);
00091 
00092     // This function is used to return tangent space results to the application.
00093     // fvTangent and fvBiTangent are unit length vectors and fMagS and fMagT are their
00094     // true magnitudes which can be used for relief mapping effects.
00095     // fvBiTangent is the "real" bitangent and thus may not be perpendicular to fvTangent.
00096     // However, both are perpendicular to the vertex normal.
00097     // For normal maps it is sufficient to use the following simplified version of the bitangent which is generated at pixel/vertex level.
00098     // fSign = bIsOrientationPreserving ? 1.0f : (-1.0f);
00099     // bitangent = fSign * cross(vN, tangent);
00100     // Note that the results are returned unindexed. It is possible to generate a new index list
00101     // But averaging/overwriting tangent spaces by using an already existing index list WILL produce INCRORRECT results.
00102     // DO NOT! use an already existing index list.
00103     void (*m_setTSpace)(const SMikkTSpaceContext * pContext, const float fvTangent[], const float fvBiTangent[], const float fMagS, const float fMagT,
00104                         const tbool bIsOrientationPreserving, const int iFace, const int iVert);
00105 } SMikkTSpaceInterface;
00106 
00107 struct SMikkTSpaceContext
00108 {
00109     SMikkTSpaceInterface * m_pInterface;    // initialized with callback functions
00110     void * m_pUserData;                     // pointer to client side mesh data etc. (passed as the first parameter with every interface call)
00111 };
00112 
00113 // these are both thread safe!
00114 tbool genTangSpaceDefault(const SMikkTSpaceContext * pContext); // Default (recommended) fAngularThreshold is 180 degrees (which means threshold disabled)
00115 tbool genTangSpace(const SMikkTSpaceContext * pContext, const float fAngularThreshold);
00116 
00117 
00118 // To avoid visual errors (distortions/unwanted hard edges in lighting), when using sampled normal maps, the
00119 // normal map sampler must use the exact inverse of the pixel shader transformation.
00120 // The most efficient transformation we can possibly do in the pixel shader is
00121 // achieved by using, directly, the "unnormalized" interpolated tangent, bitangent and vertex normal: vT, vB and vN.
00122 // pixel shader (fast transform out)
00123 // vNout = normalize( vNt.x * vT + vNt.y * vB + vNt.z * vN );
00124 // where vNt is the tangent space normal. The normal map sampler must likewise use the
00125 // interpolated and "unnormalized" tangent, bitangent and vertex normal to be compliant with the pixel shader.
00126 // sampler does (exact inverse of pixel shader):
00127 // float3 row0 = cross(vB, vN);
00128 // float3 row1 = cross(vN, vT);
00129 // float3 row2 = cross(vT, vB);
00130 // float fSign = dot(vT, row0)<0 ? -1 : 1;
00131 // vNt = normalize( fSign * float3(dot(vNout,row0), dot(vNout,row1), dot(vNout,row2)) );
00132 // where vNout is the sampled normal in some chosen 3D space.
00133 //
00134 // Should you choose to reconstruct the bitangent in the pixel shader instead
00135 // of the vertex shader, as explained earlier, then be sure to do this in the normal map sampler also.
00136 // Finally, beware of quad triangulations. If the normal map sampler doesn't use the same triangulation of
00137 // quads as your renderer then problems will occur since the interpolated tangent spaces will differ
00138 // eventhough the vertex level tangent spaces match. This can be solved either by triangulating before
00139 // sampling/exporting or by using the order-independent choice of diagonal for splitting quads suggested earlier.
00140 // However, this must be used both by the sampler and your tools/rendering pipeline.
00141 
00142 #ifdef __cplusplus
00143 }
00144 #endif
00145 
00146 #endif