TextureTransform.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2012, assimp team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the assimp team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the assimp team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ----------------------------------------------------------------------
  32. */
  33. /** @file Definition of a helper step that processes texture transformations */
  34. #ifndef AI_TEXTURE_TRANSFORM_H_INCLUDED
  35. #define AI_TEXTURE_TRANSFORM_H_INCLUDED
  36. #include "BaseImporter.h"
  37. #include "BaseProcess.h"
  38. struct aiNode;
  39. namespace Assimp {
  40. #define AI_TT_UV_IDX_LOCK_TBD 0xffffffff
  41. #define AI_TT_UV_IDX_LOCK_NONE 0xeeeeeeee
  42. #define AI_TT_ROTATION_EPSILON ((float)AI_DEG_TO_RAD(0.5))
  43. // ---------------------------------------------------------------------------
  44. /** Small helper structure representing a shortcut into the material list
  45. * to be able to update some values quickly.
  46. */
  47. struct TTUpdateInfo
  48. {
  49. TTUpdateInfo() :
  50. directShortcut (NULL)
  51. , mat (NULL)
  52. , semantic (0)
  53. , index (0)
  54. {}
  55. //! Direct shortcut, if available
  56. unsigned int* directShortcut;
  57. //! Material
  58. aiMaterial *mat;
  59. //! Texture type and index
  60. unsigned int semantic, index;
  61. };
  62. // ---------------------------------------------------------------------------
  63. /** Helper class representing texture coordinate transformations
  64. */
  65. struct STransformVecInfo : public aiUVTransform
  66. {
  67. STransformVecInfo()
  68. : uvIndex (0)
  69. , mapU (aiTextureMapMode_Wrap)
  70. , mapV (aiTextureMapMode_Wrap)
  71. , lockedPos (AI_TT_UV_IDX_LOCK_NONE)
  72. {}
  73. //! Source texture coordinate index
  74. unsigned int uvIndex;
  75. //! Texture mapping mode in the u, v direction
  76. aiTextureMapMode mapU,mapV;
  77. //! Locked destination UV index
  78. //! AI_TT_UV_IDX_LOCK_TBD - to be determined
  79. //! AI_TT_UV_IDX_LOCK_NONE - none (default)
  80. unsigned int lockedPos;
  81. //! Update info - shortcuts into all materials
  82. //! that are referencing this transform setup
  83. std::list<TTUpdateInfo> updateList;
  84. // -------------------------------------------------------------------
  85. /** Compare two transform setups
  86. */
  87. inline bool operator== (const STransformVecInfo& other) const
  88. {
  89. // We use a small epsilon here
  90. const static float epsilon = 0.05f;
  91. if (fabs( mTranslation.x - other.mTranslation.x ) > epsilon ||
  92. fabs( mTranslation.y - other.mTranslation.y ) > epsilon)
  93. {
  94. return false;
  95. }
  96. if (fabs( mScaling.x - other.mScaling.x ) > epsilon ||
  97. fabs( mScaling.y - other.mScaling.y ) > epsilon)
  98. {
  99. return false;
  100. }
  101. if (fabs( mRotation - other.mRotation) > epsilon)
  102. {
  103. return false;
  104. }
  105. return true;
  106. }
  107. inline bool operator!= (const STransformVecInfo& other) const
  108. {
  109. return !(*this == other);
  110. }
  111. // -------------------------------------------------------------------
  112. /** Returns whether this is an untransformed texture coordinate set
  113. */
  114. inline bool IsUntransformed() const
  115. {
  116. return (1.0f == mScaling.x && 1.f == mScaling.y &&
  117. !mTranslation.x && !mTranslation.y &&
  118. mRotation < AI_TT_ROTATION_EPSILON);
  119. }
  120. // -------------------------------------------------------------------
  121. /** Build a 3x3 matrix from the transformations
  122. */
  123. inline void GetMatrix(aiMatrix3x3& mOut)
  124. {
  125. mOut = aiMatrix3x3();
  126. if (1.0f != mScaling.x || 1.0f != mScaling.y)
  127. {
  128. aiMatrix3x3 mScale;
  129. mScale.a1 = mScaling.x;
  130. mScale.b2 = mScaling.y;
  131. mOut = mScale;
  132. }
  133. if (mRotation)
  134. {
  135. aiMatrix3x3 mRot;
  136. mRot.a1 = mRot.b2 = cos(mRotation);
  137. mRot.a2 = mRot.b1 = sin(mRotation);
  138. mRot.a2 = -mRot.a2;
  139. mOut *= mRot;
  140. }
  141. if (mTranslation.x || mTranslation.y)
  142. {
  143. aiMatrix3x3 mTrans;
  144. mTrans.a3 = mTranslation.x;
  145. mTrans.b3 = mTranslation.y;
  146. mOut *= mTrans;
  147. }
  148. }
  149. };
  150. // ---------------------------------------------------------------------------
  151. /** Helper step to compute final UV coordinate sets if there are scalings
  152. * or rotations in the original data read from the file.
  153. */
  154. class TextureTransformStep : public BaseProcess
  155. {
  156. public:
  157. TextureTransformStep();
  158. ~TextureTransformStep();
  159. public:
  160. // -------------------------------------------------------------------
  161. bool IsActive( unsigned int pFlags) const;
  162. // -------------------------------------------------------------------
  163. void Execute( aiScene* pScene);
  164. // -------------------------------------------------------------------
  165. void SetupProperties(const Importer* pImp);
  166. protected:
  167. // -------------------------------------------------------------------
  168. /** Preprocess a specific UV transformation setup
  169. *
  170. * @param info Transformation setup to be preprocessed.
  171. */
  172. void PreProcessUVTransform(STransformVecInfo& info);
  173. private:
  174. unsigned int configFlags;
  175. };
  176. }
  177. #endif //! AI_TEXTURE_TRANSFORM_H_INCLUDED