vrshoot
view libs/assimp/MakeVerboseFormat.cpp @ 0:b2f14e535253
initial commit
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 01 Feb 2014 19:58:19 +0200 |
parents | |
children |
line source
1 /*
2 ---------------------------------------------------------------------------
3 Open Asset Import Library (assimp)
4 ---------------------------------------------------------------------------
6 Copyright (c) 2006-2012, assimp team
8 All rights reserved.
10 Redistribution and use of this software in source and binary forms,
11 with or without modification, are permitted provided that the following
12 conditions are met:
14 * Redistributions of source code must retain the above
15 copyright notice, this list of conditions and the
16 following disclaimer.
18 * Redistributions in binary form must reproduce the above
19 copyright notice, this list of conditions and the
20 following disclaimer in the documentation and/or other
21 materials provided with the distribution.
23 * Neither the name of the assimp team, nor the names of its
24 contributors may be used to endorse or promote products
25 derived from this software without specific prior
26 written permission of the assimp team.
28 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 ---------------------------------------------------------------------------
40 */
41 /** @file Implementation of the post processing step "MakeVerboseFormat"
42 */
44 #include "AssimpPCH.h"
45 #include "MakeVerboseFormat.h"
47 using namespace Assimp;
49 // ------------------------------------------------------------------------------------------------
50 MakeVerboseFormatProcess::MakeVerboseFormatProcess()
51 {
52 // nothing to do here
53 }
54 // ------------------------------------------------------------------------------------------------
55 MakeVerboseFormatProcess::~MakeVerboseFormatProcess()
56 {
57 // nothing to do here
58 }
59 // ------------------------------------------------------------------------------------------------
60 // Executes the post processing step on the given imported data.
61 void MakeVerboseFormatProcess::Execute( aiScene* pScene)
62 {
63 ai_assert(NULL != pScene);
64 DefaultLogger::get()->debug("MakeVerboseFormatProcess begin");
66 bool bHas = false;
67 for( unsigned int a = 0; a < pScene->mNumMeshes; a++)
68 {
69 if( MakeVerboseFormat( pScene->mMeshes[a]))
70 bHas = true;
71 }
72 if (bHas) DefaultLogger::get()->info("MakeVerboseFormatProcess finished. There was much work to do ...");
73 else DefaultLogger::get()->debug("MakeVerboseFormatProcess. There was nothing to do.");
75 pScene->mFlags &= ~AI_SCENE_FLAGS_NON_VERBOSE_FORMAT;
77 }
78 // ------------------------------------------------------------------------------------------------
79 // Executes the post processing step on the given imported data.
80 bool MakeVerboseFormatProcess::MakeVerboseFormat(aiMesh* pcMesh)
81 {
82 ai_assert(NULL != pcMesh);
84 unsigned int iOldNumVertices = pcMesh->mNumVertices;
85 const unsigned int iNumVerts = pcMesh->mNumFaces*3;
87 aiVector3D* pvPositions = new aiVector3D[ iNumVerts ];
89 aiVector3D* pvNormals = NULL;
90 if (pcMesh->HasNormals())
91 {
92 pvNormals = new aiVector3D[iNumVerts];
93 }
94 aiVector3D* pvTangents = NULL, *pvBitangents = NULL;
95 if (pcMesh->HasTangentsAndBitangents())
96 {
97 pvTangents = new aiVector3D[iNumVerts];
98 pvBitangents = new aiVector3D[iNumVerts];
99 }
101 aiVector3D* apvTextureCoords[AI_MAX_NUMBER_OF_TEXTURECOORDS] = {0};
102 aiColor4D* apvColorSets[AI_MAX_NUMBER_OF_COLOR_SETS] = {0};
104 unsigned int p = 0;
105 while (pcMesh->HasTextureCoords(p))
106 apvTextureCoords[p++] = new aiVector3D[iNumVerts];
108 p = 0;
109 while (pcMesh->HasVertexColors(p))
110 apvColorSets[p++] = new aiColor4D[iNumVerts];
112 // allocate enough memory to hold output bones and vertex weights ...
113 std::vector<aiVertexWeight>* newWeights = new std::vector<aiVertexWeight>[pcMesh->mNumBones];
114 for (unsigned int i = 0;i < pcMesh->mNumBones;++i) {
115 newWeights[i].reserve(pcMesh->mBones[i]->mNumWeights*3);
116 }
118 // iterate through all faces and build a clean list
119 unsigned int iIndex = 0;
120 for (unsigned int a = 0; a< pcMesh->mNumFaces;++a)
121 {
122 aiFace* pcFace = &pcMesh->mFaces[a];
123 for (unsigned int q = 0; q < pcFace->mNumIndices;++q,++iIndex)
124 {
125 // need to build a clean list of bones, too
126 for (unsigned int i = 0;i < pcMesh->mNumBones;++i)
127 {
128 for (unsigned int a = 0; a < pcMesh->mBones[i]->mNumWeights;a++)
129 {
130 const aiVertexWeight& w = pcMesh->mBones[i]->mWeights[a];
131 if(pcFace->mIndices[q] == w.mVertexId)
132 {
133 aiVertexWeight wNew;
134 wNew.mVertexId = iIndex;
135 wNew.mWeight = w.mWeight;
136 newWeights[i].push_back(wNew);
137 }
138 }
139 }
141 pvPositions[iIndex] = pcMesh->mVertices[pcFace->mIndices[q]];
143 if (pcMesh->HasNormals())
144 {
145 pvNormals[iIndex] = pcMesh->mNormals[pcFace->mIndices[q]];
146 }
147 if (pcMesh->HasTangentsAndBitangents())
148 {
149 pvTangents[iIndex] = pcMesh->mTangents[pcFace->mIndices[q]];
150 pvBitangents[iIndex] = pcMesh->mBitangents[pcFace->mIndices[q]];
151 }
153 unsigned int p = 0;
154 while (pcMesh->HasTextureCoords(p))
155 {
156 apvTextureCoords[p][iIndex] = pcMesh->mTextureCoords[p][pcFace->mIndices[q]];
157 ++p;
158 }
159 p = 0;
160 while (pcMesh->HasVertexColors(p))
161 {
162 apvColorSets[p][iIndex] = pcMesh->mColors[p][pcFace->mIndices[q]];
163 ++p;
164 }
165 pcFace->mIndices[q] = iIndex;
166 }
167 }
169 // build output vertex weights
170 for (unsigned int i = 0;i < pcMesh->mNumBones;++i)
171 {
172 delete pcMesh->mBones[i]->mWeights;
173 if (!newWeights[i].empty())
174 {
175 pcMesh->mBones[i]->mWeights = new aiVertexWeight[newWeights[i].size()];
176 memcpy(pcMesh->mBones[i]->mWeights,&newWeights[i][0],
177 sizeof(aiVertexWeight) * newWeights[i].size());
178 }
179 else pcMesh->mBones[i]->mWeights = NULL;
180 }
182 // delete the old members
183 delete[] pcMesh->mVertices;
184 pcMesh->mVertices = pvPositions;
186 p = 0;
187 while (pcMesh->HasTextureCoords(p))
188 {
189 delete pcMesh->mTextureCoords[p];
190 pcMesh->mTextureCoords[p] = apvTextureCoords[p];
191 ++p;
192 }
193 p = 0;
194 while (pcMesh->HasVertexColors(p))
195 {
196 delete pcMesh->mColors[p];
197 pcMesh->mColors[p] = apvColorSets[p];
198 ++p;
199 }
200 pcMesh->mNumVertices = iNumVerts;
202 if (pcMesh->HasNormals())
203 {
204 delete[] pcMesh->mNormals;
205 pcMesh->mNormals = pvNormals;
206 }
207 if (pcMesh->HasTangentsAndBitangents())
208 {
209 delete[] pcMesh->mTangents;
210 pcMesh->mTangents = pvTangents;
211 delete[] pcMesh->mBitangents;
212 pcMesh->mBitangents = pvBitangents;
213 }
214 return (pcMesh->mNumVertices != iOldNumVertices);
215 }