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/** @file Implementation of the post processing step "MakeVerboseFormat"
43*/
44
45
46#include "MakeVerboseFormat.h"
47#include <assimp/scene.h>
48#include <assimp/DefaultLogger.hpp>
49
50using namespace Assimp;
51
52// ------------------------------------------------------------------------------------------------
53MakeVerboseFormatProcess::MakeVerboseFormatProcess()
54{
55 // nothing to do here
56}
57// ------------------------------------------------------------------------------------------------
58MakeVerboseFormatProcess::~MakeVerboseFormatProcess()
59{
60 // nothing to do here
61}
62// ------------------------------------------------------------------------------------------------
63// Executes the post processing step on the given imported data.
64void MakeVerboseFormatProcess::Execute( aiScene* pScene)
65{
66 ai_assert(NULL != pScene);
67 DefaultLogger::get()->debug("MakeVerboseFormatProcess begin");
68
69 bool bHas = false;
70 for( unsigned int a = 0; a < pScene->mNumMeshes; a++)
71 {
72 if( MakeVerboseFormat( pScene->mMeshes[a]))
73 bHas = true;
74 }
75 if (bHas) DefaultLogger::get()->info("MakeVerboseFormatProcess finished. There was much work to do ...");
76 else DefaultLogger::get()->debug("MakeVerboseFormatProcess. There was nothing to do.");
77
78 pScene->mFlags &= ~AI_SCENE_FLAGS_NON_VERBOSE_FORMAT;
79
80}
81// ------------------------------------------------------------------------------------------------
82// Executes the post processing step on the given imported data.
83bool MakeVerboseFormatProcess::MakeVerboseFormat(aiMesh* pcMesh)
84{
85 ai_assert(NULL != pcMesh);
86
87 unsigned int iOldNumVertices = pcMesh->mNumVertices;
88 const unsigned int iNumVerts = pcMesh->mNumFaces*3;
89
90 aiVector3D* pvPositions = new aiVector3D[ iNumVerts ];
91
92 aiVector3D* pvNormals = NULL;
93 if (pcMesh->HasNormals())
94 {
95 pvNormals = new aiVector3D[iNumVerts];
96 }
97 aiVector3D* pvTangents = NULL, *pvBitangents = NULL;
98 if (pcMesh->HasTangentsAndBitangents())
99 {
100 pvTangents = new aiVector3D[iNumVerts];
101 pvBitangents = new aiVector3D[iNumVerts];
102 }
103
104 aiVector3D* apvTextureCoords[AI_MAX_NUMBER_OF_TEXTURECOORDS] = {0};
105 aiColor4D* apvColorSets[AI_MAX_NUMBER_OF_COLOR_SETS] = {0};
106
107 unsigned int p = 0;
108 while (pcMesh->HasTextureCoords(p))
109 apvTextureCoords[p++] = new aiVector3D[iNumVerts];
110
111 p = 0;
112 while (pcMesh->HasVertexColors(p))
113 apvColorSets[p++] = new aiColor4D[iNumVerts];
114
115 // allocate enough memory to hold output bones and vertex weights ...
116 std::vector<aiVertexWeight>* newWeights = new std::vector<aiVertexWeight>[pcMesh->mNumBones];
117 for (unsigned int i = 0;i < pcMesh->mNumBones;++i) {
118 newWeights[i].reserve(pcMesh->mBones[i]->mNumWeights*3);
119 }
120
121 // iterate through all faces and build a clean list
122 unsigned int iIndex = 0;
123 for (unsigned int a = 0; a< pcMesh->mNumFaces;++a)
124 {
125 aiFace* pcFace = &pcMesh->mFaces[a];
126 for (unsigned int q = 0; q < pcFace->mNumIndices;++q,++iIndex)
127 {
128 // need to build a clean list of bones, too
129 for (unsigned int i = 0;i < pcMesh->mNumBones;++i)
130 {
131 for (unsigned int a = 0; a < pcMesh->mBones[i]->mNumWeights;a++)
132 {
133 const aiVertexWeight& w = pcMesh->mBones[i]->mWeights[a];
134 if(pcFace->mIndices[q] == w.mVertexId)
135 {
136 aiVertexWeight wNew;
137 wNew.mVertexId = iIndex;
138 wNew.mWeight = w.mWeight;
139 newWeights[i].push_back(wNew);
140 }
141 }
142 }
143
144 pvPositions[iIndex] = pcMesh->mVertices[pcFace->mIndices[q]];
145
146 if (pcMesh->HasNormals())
147 {
148 pvNormals[iIndex] = pcMesh->mNormals[pcFace->mIndices[q]];
149 }
150 if (pcMesh->HasTangentsAndBitangents())
151 {
152 pvTangents[iIndex] = pcMesh->mTangents[pcFace->mIndices[q]];
153 pvBitangents[iIndex] = pcMesh->mBitangents[pcFace->mIndices[q]];
154 }
155
156 unsigned int p = 0;
157 while (pcMesh->HasTextureCoords(p))
158 {
159 apvTextureCoords[p][iIndex] = pcMesh->mTextureCoords[p][pcFace->mIndices[q]];
160 ++p;
161 }
162 p = 0;
163 while (pcMesh->HasVertexColors(p))
164 {
165 apvColorSets[p][iIndex] = pcMesh->mColors[p][pcFace->mIndices[q]];
166 ++p;
167 }
168 pcFace->mIndices[q] = iIndex;
169 }
170 }
171
172
173
174 // build output vertex weights
175 for (unsigned int i = 0;i < pcMesh->mNumBones;++i)
176 {
177 delete [] pcMesh->mBones[i]->mWeights;
178 if (!newWeights[i].empty()) {
179 pcMesh->mBones[i]->mWeights = new aiVertexWeight[newWeights[i].size()];
180 aiVertexWeight *weightToCopy = &( newWeights[i][0] );
181 memcpy(pcMesh->mBones[i]->mWeights, weightToCopy,
182 sizeof(aiVertexWeight) * newWeights[i].size());
183 } else {
184 pcMesh->mBones[i]->mWeights = NULL;
185 }
186 }
187 delete[] newWeights;
188
189 // delete the old members
190 delete[] pcMesh->mVertices;
191 pcMesh->mVertices = pvPositions;
192
193 p = 0;
194 while (pcMesh->HasTextureCoords(p))
195 {
196 delete[] pcMesh->mTextureCoords[p];
197 pcMesh->mTextureCoords[p] = apvTextureCoords[p];
198 ++p;
199 }
200 p = 0;
201 while (pcMesh->HasVertexColors(p))
202 {
203 delete[] pcMesh->mColors[p];
204 pcMesh->mColors[p] = apvColorSets[p];
205 ++p;
206 }
207 pcMesh->mNumVertices = iNumVerts;
208
209 if (pcMesh->HasNormals())
210 {
211 delete[] pcMesh->mNormals;
212 pcMesh->mNormals = pvNormals;
213 }
214 if (pcMesh->HasTangentsAndBitangents())
215 {
216 delete[] pcMesh->mTangents;
217 pcMesh->mTangents = pvTangents;
218 delete[] pcMesh->mBitangents;
219 pcMesh->mBitangents = pvBitangents;
220 }
221 return (pcMesh->mNumVertices != iOldNumVertices);
222}
223