TextureTransform.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. Open Asset Import Library (ASSIMP)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2008, ASSIMP Development 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 Development 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 class that processes texture transformations */
  34. #ifndef AI_TEXTURE_TRANSFORM_H_INCLUDED
  35. #define AI_TEXTURE_TRANSFORM_H_INCLUDED
  36. #include "BaseImporter.h"
  37. #include "../include/aiTypes.h"
  38. #include "../include/aiMaterial.h"
  39. #include "../include/aiMesh.h"
  40. struct aiNode;
  41. #include "3DSHelper.h"
  42. namespace Assimp
  43. {
  44. using namespace Assimp::Dot3DS;
  45. // ---------------------------------------------------------------------------
  46. /** Helper class representing texture coordinate transformations
  47. */
  48. struct STransformVecInfo
  49. {
  50. //! Construction. The resulting matrix is the identity
  51. STransformVecInfo ()
  52. :
  53. fScaleU(1.0f),fScaleV(1.0f),
  54. fOffsetU(0.0f),fOffsetV(0.0f),
  55. fRotation(0.0f),
  56. iUVIndex(0)
  57. {}
  58. //! Texture coordinate scaling in the x-direction
  59. float fScaleU;
  60. //! Texture coordinate scaling in the y-direction
  61. float fScaleV;
  62. //! Texture coordinate offset in the x-direction
  63. float fOffsetU;
  64. //! Texture coordinate offset in the y-direction
  65. float fOffsetV;
  66. //! Texture coordinate rotation, clockwise, in radians
  67. float fRotation;
  68. //! Source texture coordinate index
  69. unsigned int iUVIndex;
  70. //! List of all textures that use this texture
  71. //! coordinate transformations
  72. std::vector<Dot3DS::Texture*> pcTextures;
  73. // -------------------------------------------------------------------
  74. /** Returns whether this is an untransformed texture coordinate set
  75. */
  76. inline bool IsUntransformed() const
  77. {
  78. return 1.0f == fScaleU && 1.0f == fScaleV &&
  79. !fOffsetU && !fOffsetV && !fRotation;
  80. }
  81. // -------------------------------------------------------------------
  82. /** Build a 3x3 matrix from the transformations
  83. */
  84. inline void GetMatrix(aiMatrix3x3& mOut)
  85. {
  86. mOut = aiMatrix3x3();
  87. if (1.0f != this->fScaleU || 1.0f != this->fScaleV)
  88. {
  89. aiMatrix3x3 mScale;
  90. mScale.a1 = this->fScaleU;
  91. mScale.b2 = this->fScaleV;
  92. mOut = mScale;
  93. }
  94. if (this->fRotation)
  95. {
  96. aiMatrix3x3 mRot;
  97. mRot.a1 = mRot.b2 = cosf(this->fRotation);
  98. mRot.a2 = mRot.b1 = sinf(this->fRotation);
  99. mRot.a2 = -mRot.a2;
  100. mOut *= mRot;
  101. }
  102. if (this->fOffsetU || this->fOffsetV)
  103. {
  104. aiMatrix3x3 mTrans;
  105. mTrans.a3 = this->fOffsetU;
  106. mTrans.b3 = this->fOffsetV;
  107. mOut *= mTrans;
  108. }
  109. }
  110. };
  111. // ---------------------------------------------------------------------------
  112. /** Helper class used by the ASE/ASK and 3DS loaders to handle texture
  113. * coordinate transformations correctly (such as offsets, scaling)
  114. */
  115. class ASSIMP_API TextureTransform
  116. {
  117. //! Constructor, it is not possible to create instances of this class
  118. TextureTransform() {}
  119. public:
  120. // -------------------------------------------------------------------
  121. /** Returns true if a texture requires UV transformations
  122. * \param rcIn Input texture
  123. */
  124. inline static bool HasUVTransform(
  125. const Dot3DS::Texture& rcIn)
  126. {
  127. return (rcIn.mOffsetU || rcIn.mOffsetV ||
  128. 1.0f != rcIn.mScaleU || 1.0f != rcIn.mScaleV || rcIn.mRotation);
  129. }
  130. // -------------------------------------------------------------------
  131. /** Must be called before HasUVTransform(rcIn) is called
  132. * \param rcIn Input texture
  133. */
  134. static void PreProcessUVTransform(
  135. Dot3DS::Texture& rcIn);
  136. // -------------------------------------------------------------------
  137. /** Check whether the texture coordinate transformation of
  138. * a texture is already contained in a given list
  139. * \param rasVec List of transformations
  140. * \param pcTex Pointer to the texture
  141. */
  142. static void AddToList(std::vector<STransformVecInfo>& rasVec,
  143. Dot3DS::Texture* pcTex);
  144. // -------------------------------------------------------------------
  145. /** Get a full list of all texture coordinate offsets required
  146. * for a material
  147. * \param materials List of materials to be processed
  148. */
  149. static void ApplyScaleNOffset(std::vector<Dot3DS::Material>& materials);
  150. // -------------------------------------------------------------------
  151. /** Get a full list of all texture coordinate offsets required
  152. * for a material
  153. * \param material Material to be processed
  154. */
  155. static void ApplyScaleNOffset(Dot3DS::Material& material);
  156. // -------------------------------------------------------------------
  157. /** Precompute as many texture coordinate transformations as possible
  158. * \param pcMesh Mesh containing the texture coordinate data
  159. * \param pcSrc Input material. Must have been passed to
  160. * ApplyScaleNOffset
  161. */
  162. static void BakeScaleNOffset(aiMesh* pcMesh, Dot3DS::Material* pcSrc);
  163. // -------------------------------------------------------------------
  164. /** Setup the correct UV source for a material
  165. * \param pcMat Final material to be changed
  166. * \param pcMatIn Input material, unconverted
  167. */
  168. static void SetupMatUVSrc (aiMaterial* pcMat,
  169. const Dot3DS::Material* pcMatIn);
  170. };
  171. };
  172. #endif //! AI_TEXTURE_TRANSFORM_H_INCLUDED