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