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