1/*
2---------------------------------------------------------------------------
3Open Asset Import Library (assimp)
4---------------------------------------------------------------------------
5
6Copyright (c) 2006-2017, assimp team
7
8
9All rights reserved.
10
11Redistribution and use of this software in source and binary forms,
12with or without modification, are permitted provided that the following
13conditions are met:
14
15* Redistributions of source code must retain the above
16 copyright notice, this list of conditions and the
17 following disclaimer.
18
19* Redistributions in binary form must reproduce the above
20 copyright notice, this list of conditions and the
21 following disclaimer in the documentation and/or other
22 materials provided with the distribution.
23
24* Neither the name of the assimp team, nor the names of its
25 contributors may be used to endorse or promote products
26 derived from this software without specific prior
27 written permission of the assimp team.
28
29THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40---------------------------------------------------------------------------
41*/
42
43/** @file MakeLeftHandedProcess.cpp
44 * @brief Implementation of the post processing step to convert all
45 * imported data to a left-handed coordinate system.
46 *
47 * Face order & UV flip are also implemented here, for the sake of a
48 * better location.
49 */
50
51
52#include "ConvertToLHProcess.h"
53#include <assimp/scene.h>
54#include <assimp/postprocess.h>
55#include <assimp/DefaultLogger.hpp>
56
57using namespace Assimp;
58
59#ifndef ASSIMP_BUILD_NO_MAKELEFTHANDED_PROCESS
60
61// ------------------------------------------------------------------------------------------------
62// Constructor to be privately used by Importer
63MakeLeftHandedProcess::MakeLeftHandedProcess()
64: BaseProcess() {
65 // empty
66}
67
68// ------------------------------------------------------------------------------------------------
69// Destructor, private as well
70MakeLeftHandedProcess::~MakeLeftHandedProcess() {
71 // empty
72}
73
74// ------------------------------------------------------------------------------------------------
75// Returns whether the processing step is present in the given flag field.
76bool MakeLeftHandedProcess::IsActive( unsigned int pFlags) const
77{
78 return 0 != (pFlags & aiProcess_MakeLeftHanded);
79}
80
81// ------------------------------------------------------------------------------------------------
82// Executes the post processing step on the given imported data.
83void MakeLeftHandedProcess::Execute( aiScene* pScene)
84{
85 // Check for an existent root node to proceed
86 ai_assert(pScene->mRootNode != NULL);
87 DefaultLogger::get()->debug("MakeLeftHandedProcess begin");
88
89 // recursively convert all the nodes
90 ProcessNode( pScene->mRootNode, aiMatrix4x4());
91
92 // process the meshes accordingly
93 for( unsigned int a = 0; a < pScene->mNumMeshes; ++a)
94 ProcessMesh( pScene->mMeshes[a]);
95
96 // process the materials accordingly
97 for( unsigned int a = 0; a < pScene->mNumMaterials; ++a)
98 ProcessMaterial( pScene->mMaterials[a]);
99
100 // transform all animation channels as well
101 for( unsigned int a = 0; a < pScene->mNumAnimations; a++)
102 {
103 aiAnimation* anim = pScene->mAnimations[a];
104 for( unsigned int b = 0; b < anim->mNumChannels; b++)
105 {
106 aiNodeAnim* nodeAnim = anim->mChannels[b];
107 ProcessAnimation( nodeAnim);
108 }
109 }
110 DefaultLogger::get()->debug("MakeLeftHandedProcess finished");
111}
112
113// ------------------------------------------------------------------------------------------------
114// Recursively converts a node, all of its children and all of its meshes
115void MakeLeftHandedProcess::ProcessNode( aiNode* pNode, const aiMatrix4x4& pParentGlobalRotation)
116{
117 // mirror all base vectors at the local Z axis
118 pNode->mTransformation.c1 = -pNode->mTransformation.c1;
119 pNode->mTransformation.c2 = -pNode->mTransformation.c2;
120 pNode->mTransformation.c3 = -pNode->mTransformation.c3;
121 pNode->mTransformation.c4 = -pNode->mTransformation.c4;
122
123 // now invert the Z axis again to keep the matrix determinant positive.
124 // The local meshes will be inverted accordingly so that the result should look just fine again.
125 pNode->mTransformation.a3 = -pNode->mTransformation.a3;
126 pNode->mTransformation.b3 = -pNode->mTransformation.b3;
127 pNode->mTransformation.c3 = -pNode->mTransformation.c3;
128 pNode->mTransformation.d3 = -pNode->mTransformation.d3; // useless, but anyways...
129
130 // continue for all children
131 for( size_t a = 0; a < pNode->mNumChildren; ++a ) {
132 ProcessNode( pNode->mChildren[ a ], pParentGlobalRotation * pNode->mTransformation );
133 }
134}
135
136// ------------------------------------------------------------------------------------------------
137// Converts a single mesh to left handed coordinates.
138void MakeLeftHandedProcess::ProcessMesh( aiMesh* pMesh)
139{
140 // mirror positions, normals and stuff along the Z axis
141 for( size_t a = 0; a < pMesh->mNumVertices; ++a)
142 {
143 pMesh->mVertices[a].z *= -1.0f;
144 if( pMesh->HasNormals())
145 pMesh->mNormals[a].z *= -1.0f;
146 if( pMesh->HasTangentsAndBitangents())
147 {
148 pMesh->mTangents[a].z *= -1.0f;
149 pMesh->mBitangents[a].z *= -1.0f;
150 }
151 }
152
153 // mirror offset matrices of all bones
154 for( size_t a = 0; a < pMesh->mNumBones; ++a)
155 {
156 aiBone* bone = pMesh->mBones[a];
157 bone->mOffsetMatrix.a3 = -bone->mOffsetMatrix.a3;
158 bone->mOffsetMatrix.b3 = -bone->mOffsetMatrix.b3;
159 bone->mOffsetMatrix.d3 = -bone->mOffsetMatrix.d3;
160 bone->mOffsetMatrix.c1 = -bone->mOffsetMatrix.c1;
161 bone->mOffsetMatrix.c2 = -bone->mOffsetMatrix.c2;
162 bone->mOffsetMatrix.c4 = -bone->mOffsetMatrix.c4;
163 }
164
165 // mirror bitangents as well as they're derived from the texture coords
166 if( pMesh->HasTangentsAndBitangents())
167 {
168 for( unsigned int a = 0; a < pMesh->mNumVertices; a++)
169 pMesh->mBitangents[a] *= -1.0f;
170 }
171}
172
173// ------------------------------------------------------------------------------------------------
174// Converts a single material to left handed coordinates.
175void MakeLeftHandedProcess::ProcessMaterial( aiMaterial* _mat)
176{
177 aiMaterial* mat = (aiMaterial*)_mat;
178 for (unsigned int a = 0; a < mat->mNumProperties;++a) {
179 aiMaterialProperty* prop = mat->mProperties[a];
180
181 // Mapping axis for UV mappings?
182 if (!::strcmp( prop->mKey.data, "$tex.mapaxis")) {
183 ai_assert( prop->mDataLength >= sizeof(aiVector3D)); /* something is wrong with the validation if we end up here */
184 aiVector3D* pff = (aiVector3D*)prop->mData;
185
186 pff->z *= -1.f;
187 }
188 }
189}
190
191// ------------------------------------------------------------------------------------------------
192// Converts the given animation to LH coordinates.
193void MakeLeftHandedProcess::ProcessAnimation( aiNodeAnim* pAnim)
194{
195 // position keys
196 for( unsigned int a = 0; a < pAnim->mNumPositionKeys; a++)
197 pAnim->mPositionKeys[a].mValue.z *= -1.0f;
198
199 // rotation keys
200 for( unsigned int a = 0; a < pAnim->mNumRotationKeys; a++)
201 {
202 /* That's the safe version, but the float errors add up. So we try the short version instead
203 aiMatrix3x3 rotmat = pAnim->mRotationKeys[a].mValue.GetMatrix();
204 rotmat.a3 = -rotmat.a3; rotmat.b3 = -rotmat.b3;
205 rotmat.c1 = -rotmat.c1; rotmat.c2 = -rotmat.c2;
206 aiQuaternion rotquat( rotmat);
207 pAnim->mRotationKeys[a].mValue = rotquat;
208 */
209 pAnim->mRotationKeys[a].mValue.x *= -1.0f;
210 pAnim->mRotationKeys[a].mValue.y *= -1.0f;
211 }
212}
213
214#endif // !! ASSIMP_BUILD_NO_MAKELEFTHANDED_PROCESS
215#ifndef ASSIMP_BUILD_NO_FLIPUVS_PROCESS
216// # FlipUVsProcess
217
218// ------------------------------------------------------------------------------------------------
219// Constructor to be privately used by Importer
220FlipUVsProcess::FlipUVsProcess()
221{}
222
223// ------------------------------------------------------------------------------------------------
224// Destructor, private as well
225FlipUVsProcess::~FlipUVsProcess()
226{}
227
228// ------------------------------------------------------------------------------------------------
229// Returns whether the processing step is present in the given flag field.
230bool FlipUVsProcess::IsActive( unsigned int pFlags) const
231{
232 return 0 != (pFlags & aiProcess_FlipUVs);
233}
234
235// ------------------------------------------------------------------------------------------------
236// Executes the post processing step on the given imported data.
237void FlipUVsProcess::Execute( aiScene* pScene)
238{
239 DefaultLogger::get()->debug("FlipUVsProcess begin");
240 for (unsigned int i = 0; i < pScene->mNumMeshes;++i)
241 ProcessMesh(pScene->mMeshes[i]);
242
243 for (unsigned int i = 0; i < pScene->mNumMaterials;++i)
244 ProcessMaterial(pScene->mMaterials[i]);
245 DefaultLogger::get()->debug("FlipUVsProcess finished");
246}
247
248// ------------------------------------------------------------------------------------------------
249// Converts a single material
250void FlipUVsProcess::ProcessMaterial (aiMaterial* _mat)
251{
252 aiMaterial* mat = (aiMaterial*)_mat;
253 for (unsigned int a = 0; a < mat->mNumProperties;++a) {
254 aiMaterialProperty* prop = mat->mProperties[a];
255 if( !prop ) {
256 DefaultLogger::get()->debug( "Property is null" );
257 continue;
258 }
259
260 // UV transformation key?
261 if (!::strcmp( prop->mKey.data, "$tex.uvtrafo")) {
262 ai_assert( prop->mDataLength >= sizeof(aiUVTransform)); /* something is wrong with the validation if we end up here */
263 aiUVTransform* uv = (aiUVTransform*)prop->mData;
264
265 // just flip it, that's everything
266 uv->mTranslation.y *= -1.f;
267 uv->mRotation *= -1.f;
268 }
269 }
270}
271
272// ------------------------------------------------------------------------------------------------
273// Converts a single mesh
274void FlipUVsProcess::ProcessMesh( aiMesh* pMesh)
275{
276 // mirror texture y coordinate
277 for( unsigned int a = 0; a < AI_MAX_NUMBER_OF_TEXTURECOORDS; a++) {
278 if( !pMesh->HasTextureCoords( a ) ) {
279 break;
280 }
281
282 for( unsigned int b = 0; b < pMesh->mNumVertices; b++ ) {
283 pMesh->mTextureCoords[ a ][ b ].y = 1.0f - pMesh->mTextureCoords[ a ][ b ].y;
284 }
285 }
286}
287
288#endif // !ASSIMP_BUILD_NO_FLIPUVS_PROCESS
289#ifndef ASSIMP_BUILD_NO_FLIPWINDING_PROCESS
290// # FlipWindingOrderProcess
291
292// ------------------------------------------------------------------------------------------------
293// Constructor to be privately used by Importer
294FlipWindingOrderProcess::FlipWindingOrderProcess()
295{}
296
297// ------------------------------------------------------------------------------------------------
298// Destructor, private as well
299FlipWindingOrderProcess::~FlipWindingOrderProcess()
300{}
301
302// ------------------------------------------------------------------------------------------------
303// Returns whether the processing step is present in the given flag field.
304bool FlipWindingOrderProcess::IsActive( unsigned int pFlags) const
305{
306 return 0 != (pFlags & aiProcess_FlipWindingOrder);
307}
308
309// ------------------------------------------------------------------------------------------------
310// Executes the post processing step on the given imported data.
311void FlipWindingOrderProcess::Execute( aiScene* pScene)
312{
313 DefaultLogger::get()->debug("FlipWindingOrderProcess begin");
314 for (unsigned int i = 0; i < pScene->mNumMeshes;++i)
315 ProcessMesh(pScene->mMeshes[i]);
316 DefaultLogger::get()->debug("FlipWindingOrderProcess finished");
317}
318
319// ------------------------------------------------------------------------------------------------
320// Converts a single mesh
321void FlipWindingOrderProcess::ProcessMesh( aiMesh* pMesh)
322{
323 // invert the order of all faces in this mesh
324 for( unsigned int a = 0; a < pMesh->mNumFaces; a++)
325 {
326 aiFace& face = pMesh->mFaces[a];
327 for( unsigned int b = 0; b < face.mNumIndices / 2; b++)
328 std::swap( face.mIndices[b], face.mIndices[ face.mNumIndices - 1 - b]);
329 }
330}
331
332#endif // !! ASSIMP_BUILD_NO_FLIPWINDING_PROCESS
333