ValidateDataStructure.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (ASSIMP)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2008, ASSIMP Development Team
  6. All rights reserved.
  7. Redistribution and use of this software in source and binary forms,
  8. with or without modification, are permitted provided that the following
  9. conditions are met:
  10. * Redistributions of source code must retain the above
  11. copyright notice, this list of conditions and the
  12. following disclaimer.
  13. * Redistributions in binary form must reproduce the above
  14. copyright notice, this list of conditions and the
  15. following disclaimer in the documentation and/or other
  16. materials provided with the distribution.
  17. * Neither the name of the ASSIMP team, nor the names of its
  18. contributors may be used to endorse or promote products
  19. derived from this software without specific prior
  20. written permission of the ASSIMP Development Team.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ---------------------------------------------------------------------------
  33. */
  34. /** @file Implementation of the post processing step to validate
  35. * the data structure returned by Assimp
  36. */
  37. #include "AssimpPCH.h"
  38. // internal headers
  39. #include "ValidateDataStructure.h"
  40. #include "BaseImporter.h"
  41. #include "fast_atof.h"
  42. // CRT headers
  43. #include <stdarg.h>
  44. using namespace Assimp;
  45. // ------------------------------------------------------------------------------------------------
  46. // Constructor to be privately used by Importer
  47. ValidateDSProcess::ValidateDSProcess()
  48. {
  49. // nothing to do here
  50. }
  51. // ------------------------------------------------------------------------------------------------
  52. // Destructor, private as well
  53. ValidateDSProcess::~ValidateDSProcess()
  54. {
  55. // nothing to do here
  56. }
  57. // ------------------------------------------------------------------------------------------------
  58. // Returns whether the processing step is present in the given flag field.
  59. bool ValidateDSProcess::IsActive( unsigned int pFlags) const
  60. {
  61. return (pFlags & aiProcess_ValidateDataStructure) != 0;
  62. }
  63. // ------------------------------------------------------------------------------------------------
  64. void ValidateDSProcess::ReportError(const char* msg,...)
  65. {
  66. ai_assert(NULL != msg);
  67. va_list args;
  68. va_start(args,msg);
  69. char szBuffer[3000];
  70. int iLen;
  71. iLen = vsprintf(szBuffer,msg,args);
  72. if (0 >= iLen)
  73. {
  74. // :-) should not happen ...
  75. throw new ImportErrorException("Idiot ... learn coding!");
  76. }
  77. va_end(args);
  78. #ifdef _DEBUG
  79. aiAssert( false,szBuffer,__LINE__,__FILE__ );
  80. #endif
  81. throw new ImportErrorException("Validation failed: " + std::string(szBuffer,iLen));
  82. }
  83. // ------------------------------------------------------------------------------------------------
  84. void ValidateDSProcess::ReportWarning(const char* msg,...)
  85. {
  86. ai_assert(NULL != msg);
  87. va_list args;
  88. va_start(args,msg);
  89. char szBuffer[3000];
  90. int iLen;
  91. iLen = vsprintf(szBuffer,msg,args);
  92. if (0 >= iLen)
  93. {
  94. // :-) should not happen ...
  95. throw new ImportErrorException("Idiot ... learn coding!");
  96. }
  97. va_end(args);
  98. DefaultLogger::get()->warn("Validation warning: " + std::string(szBuffer,iLen));
  99. }
  100. // ------------------------------------------------------------------------------------------------
  101. inline int HasNameMatch(const aiString& in, aiNode* node)
  102. {
  103. int result = (node->mName == in ? 1 : 0 );
  104. for (unsigned int i = 0; i < node->mNumChildren;++i)
  105. {
  106. result += HasNameMatch(in,node->mChildren[i]);
  107. }
  108. return result;
  109. }
  110. // ------------------------------------------------------------------------------------------------
  111. template <typename T>
  112. inline void ValidateDSProcess::DoValidation(T** parray, unsigned int size,
  113. const char* firstName, const char* secondName)
  114. {
  115. // validate all entries
  116. if (size)
  117. {
  118. if (!parray)
  119. {
  120. ReportError("aiScene::%s is NULL (aiScene::%s is %i)",
  121. firstName, secondName, size);
  122. }
  123. for (unsigned int i = 0; i < size;++i)
  124. {
  125. if (!parray[i])
  126. {
  127. ReportError("aiScene::%s[%i] is NULL (aiScene::%s is %i)",
  128. firstName,i,secondName,size);
  129. }
  130. Validate(parray[i]);
  131. }
  132. }
  133. }
  134. // ------------------------------------------------------------------------------------------------
  135. template <typename T>
  136. inline void ValidateDSProcess::DoValidationEx(T** parray, unsigned int size,
  137. const char* firstName, const char* secondName)
  138. {
  139. // validate all entries
  140. if (size)
  141. {
  142. if (!parray)
  143. {
  144. ReportError("aiScene::%s is NULL (aiScene::%s is %i)",
  145. firstName, secondName, size);
  146. }
  147. for (unsigned int i = 0; i < size;++i)
  148. {
  149. if (!parray[i])
  150. {
  151. ReportError("aiScene::%s[%i] is NULL (aiScene::%s is %i)",
  152. firstName,i,secondName,size);
  153. }
  154. Validate(parray[i]);
  155. // check whether there are duplicate names
  156. for (unsigned int a = i+1; a < size;++a)
  157. {
  158. if (parray[i]->mName == parray[a]->mName)
  159. {
  160. this->ReportError("aiScene::%s[%i] has the same name as "
  161. "aiScene::%s[%i]",firstName, i,secondName, a);
  162. }
  163. }
  164. }
  165. }
  166. }
  167. // ------------------------------------------------------------------------------------------------
  168. template <typename T>
  169. inline void ValidateDSProcess::DoValidationWithNameCheck(T** array,
  170. unsigned int size, const char* firstName,
  171. const char* secondName)
  172. {
  173. // validate all entries
  174. DoValidationEx(array,size,firstName,secondName);
  175. for (unsigned int i = 0; i < size;++i)
  176. {
  177. int res = HasNameMatch(array[i]->mName,mScene->mRootNode);
  178. if (!res)
  179. {
  180. ReportError("aiScene::%s[%i] has no corresponding node in the scene graph (%s)",
  181. firstName,i,array[i]->mName.data);
  182. }
  183. else if (1 != res)
  184. {
  185. ReportError("aiScene::%s[%i]: there are more than one nodes with %s as name",
  186. firstName,i,array[i]->mName.data);
  187. }
  188. }
  189. }
  190. // ------------------------------------------------------------------------------------------------
  191. // Executes the post processing step on the given imported data.
  192. void ValidateDSProcess::Execute( aiScene* pScene)
  193. {
  194. this->mScene = pScene;
  195. DefaultLogger::get()->debug("ValidateDataStructureProcess begin");
  196. // validate the node graph of the scene
  197. Validate(pScene->mRootNode);
  198. // at least one of the mXXX arrays must be non-empty or we'll flag
  199. // the sebe as invalid
  200. bool has = false;
  201. // validate all meshes
  202. if (pScene->mNumMeshes)
  203. {
  204. has = true;
  205. DoValidation(pScene->mMeshes,pScene->mNumMeshes,"mMeshes","mNumMeshes");
  206. }
  207. else if (!(mScene->mFlags & AI_SCENE_FLAGS_INCOMPLETE))
  208. {
  209. ReportError("aiScene::mNumMeshes is 0. At least one mesh must be there");
  210. }
  211. // validate all animations
  212. if (pScene->mNumAnimations)
  213. {
  214. has = true;
  215. DoValidation(pScene->mAnimations,pScene->mNumAnimations,
  216. "mAnimations","mNumAnimations");
  217. }
  218. // validate all cameras
  219. if (pScene->mNumCameras)
  220. {
  221. has = true;
  222. DoValidationWithNameCheck(pScene->mCameras,pScene->mNumCameras,
  223. "mCameras","mNumCameras");
  224. }
  225. // validate all lights
  226. if (pScene->mNumLights)
  227. {
  228. has = true;
  229. DoValidationWithNameCheck(pScene->mLights,pScene->mNumLights,
  230. "mLights","mNumLights");
  231. }
  232. // validate all materials
  233. if (pScene->mNumMaterials)
  234. {
  235. has = true;
  236. DoValidation(pScene->mMaterials,pScene->mNumMaterials,"mMaterials","mNumMaterials");
  237. }
  238. else if (!(mScene->mFlags & AI_SCENE_FLAGS_INCOMPLETE))
  239. {
  240. ReportError("aiScene::mNumMaterials is 0. At least one material must be there");
  241. }
  242. if (!has)ReportError("The aiScene data structure is empty");
  243. DefaultLogger::get()->debug("ValidateDataStructureProcess end");
  244. }
  245. // ------------------------------------------------------------------------------------------------
  246. void ValidateDSProcess::Validate( const aiLight* pLight)
  247. {
  248. if (pLight->mType == aiLightSource_UNDEFINED)
  249. ReportError("aiLight::mType is aiLightSource_UNDEFINED");
  250. if (!pLight->mAttenuationConstant &&
  251. !pLight->mAttenuationLinear &&
  252. !pLight->mAttenuationQuadratic)
  253. {
  254. ReportWarning("aiLight::mAttenuationXXX - all are zero");
  255. }
  256. if (pLight->mAngleInnerCone > pLight->mAngleOuterCone)
  257. ReportError("aiLight::mAngleInnerCone is larger than aiLight::mAngleOuterCone");
  258. if (pLight->mColorDiffuse.IsBlack() && pLight->mColorAmbient.IsBlack()
  259. && pLight->mColorSpecular.IsBlack())
  260. {
  261. ReportWarning("aiLight::mColorXXX - all are black and won't have any influence");
  262. }
  263. }
  264. // ------------------------------------------------------------------------------------------------
  265. void ValidateDSProcess::Validate( const aiCamera* pCamera)
  266. {
  267. if (pCamera->mClipPlaneFar <= pCamera->mClipPlaneNear)
  268. ReportError("aiCamera::mClipPlaneFar must be >= aiCamera::mClipPlaneNear");
  269. if (!pCamera->mHorizontalFOV || pCamera->mHorizontalFOV >= (float)AI_MATH_PI)
  270. ReportError("%f is not a valid value for aiCamera::mHorizontalFOV",pCamera->mHorizontalFOV);
  271. }
  272. // ------------------------------------------------------------------------------------------------
  273. void ValidateDSProcess::Validate( const aiMesh* pMesh)
  274. {
  275. // validate the material index of the mesh
  276. if (pMesh->mMaterialIndex >= this->mScene->mNumMaterials)
  277. {
  278. this->ReportError("aiMesh::mMaterialIndex is invalid (value: %i maximum: %i)",
  279. pMesh->mMaterialIndex,this->mScene->mNumMaterials-1);
  280. }
  281. for (unsigned int i = 0; i < pMesh->mNumFaces; ++i)
  282. {
  283. aiFace& face = pMesh->mFaces[i];
  284. if (pMesh->mPrimitiveTypes)
  285. {
  286. switch (face.mNumIndices)
  287. {
  288. case 0:
  289. this->ReportError("aiMesh::mFaces[%i].mNumIndices is 0",i);
  290. case 1:
  291. if (0 == (pMesh->mPrimitiveTypes & aiPrimitiveType_POINT))
  292. {
  293. this->ReportError("aiMesh::mFaces[%i] is a POINT but aiMesh::mPrimtiveTypes "
  294. "does not report the POINT flag",i);
  295. }
  296. break;
  297. case 2:
  298. if (0 == (pMesh->mPrimitiveTypes & aiPrimitiveType_LINE))
  299. {
  300. this->ReportError("aiMesh::mFaces[%i] is a LINE but aiMesh::mPrimtiveTypes "
  301. "does not report the LINE flag",i);
  302. }
  303. break;
  304. case 3:
  305. if (0 == (pMesh->mPrimitiveTypes & aiPrimitiveType_TRIANGLE))
  306. {
  307. this->ReportError("aiMesh::mFaces[%i] is a TRIANGLE but aiMesh::mPrimtiveTypes "
  308. "does not report the TRIANGLE flag",i);
  309. }
  310. break;
  311. default:
  312. if (0 == (pMesh->mPrimitiveTypes & aiPrimitiveType_POLYGON))
  313. {
  314. this->ReportError("aiMesh::mFaces[%i] is a POLYGON but aiMesh::mPrimtiveTypes "
  315. "does not report the POLYGON flag",i);
  316. }
  317. break;
  318. };
  319. }
  320. if (!face.mIndices)this->ReportError("aiMesh::mFaces[%i].mIndices is NULL",i);
  321. }
  322. // positions must always be there ...
  323. if (!pMesh->mNumVertices || !pMesh->mVertices && !mScene->mFlags)
  324. {
  325. this->ReportError("The mesh contains no vertices");
  326. }
  327. // if tangents are there there must also be bitangent vectors ...
  328. if ((pMesh->mTangents != NULL) != (pMesh->mBitangents != NULL))
  329. {
  330. this->ReportError("If there are tangents there must also be bitangent vectors");
  331. }
  332. // faces, too
  333. if (!pMesh->mNumFaces || !pMesh->mFaces && !mScene->mFlags)
  334. {
  335. this->ReportError("The mesh contains no faces");
  336. }
  337. // now check whether the face indexing layout is correct:
  338. // unique vertices, pseudo-indexed.
  339. std::vector<bool> abRefList;
  340. abRefList.resize(pMesh->mNumVertices,false);
  341. for (unsigned int i = 0; i < pMesh->mNumFaces;++i)
  342. {
  343. aiFace& face = pMesh->mFaces[i];
  344. for (unsigned int a = 0; a < face.mNumIndices;++a)
  345. {
  346. if (face.mIndices[a] >= pMesh->mNumVertices)
  347. {
  348. this->ReportError("aiMesh::mFaces[%i]::mIndices[%i] is out of range",i,a);
  349. }
  350. // the MSB flag is temporarily used by the extra verbose
  351. // mode to tell us that the JoinVerticesProcess might have
  352. // been executed already.
  353. if ( !(this->mScene->mFlags & AI_SCENE_FLAGS_NON_VERBOSE_FORMAT ) && abRefList[face.mIndices[a]])
  354. {
  355. ReportError("aiMesh::mVertices[%i] is referenced twice - second "
  356. "time by aiMesh::mFaces[%i]::mIndices[%i]",face.mIndices[a],i,a);
  357. }
  358. abRefList[face.mIndices[a]] = true;
  359. }
  360. }
  361. // check whether there are vertices that aren't referenced by a face
  362. bool b = false;
  363. for (unsigned int i = 0; i < pMesh->mNumVertices;++i)
  364. {
  365. if (!abRefList[i])b = true;
  366. }
  367. abRefList.clear();
  368. if (b)ReportWarning("There are unreferenced vertices");
  369. // texture channel 2 may not be set if channel 1 is zero ...
  370. {
  371. unsigned int i = 0;
  372. for (;i < AI_MAX_NUMBER_OF_TEXTURECOORDS;++i)
  373. {
  374. if (!pMesh->HasTextureCoords(i))break;
  375. }
  376. for (;i < AI_MAX_NUMBER_OF_TEXTURECOORDS;++i)
  377. if (pMesh->HasTextureCoords(i))
  378. {
  379. ReportError("Texture coordinate channel %i exists "
  380. "although the previous channel was NULL.",i);
  381. }
  382. }
  383. // the same for the vertex colors
  384. {
  385. unsigned int i = 0;
  386. for (;i < AI_MAX_NUMBER_OF_COLOR_SETS;++i)
  387. {
  388. if (!pMesh->HasVertexColors(i))break;
  389. }
  390. for (;i < AI_MAX_NUMBER_OF_COLOR_SETS;++i)
  391. if (pMesh->HasVertexColors(i))
  392. {
  393. ReportError("Vertex color channel %i is exists "
  394. "although the previous channel was NULL.",i);
  395. }
  396. }
  397. // now validate all bones
  398. if (pMesh->HasBones())
  399. {
  400. if (!pMesh->mBones)
  401. {
  402. ReportError("aiMesh::mBones is NULL (aiMesh::mNumBones is %i)",
  403. pMesh->mNumBones);
  404. }
  405. float* afSum = NULL;
  406. if (pMesh->mNumVertices)
  407. {
  408. afSum = new float[pMesh->mNumVertices];
  409. for (unsigned int i = 0; i < pMesh->mNumVertices;++i)
  410. afSum[i] = 0.0f;
  411. }
  412. // check whether there are duplicate bone names
  413. for (unsigned int i = 0; i < pMesh->mNumBones;++i)
  414. {
  415. if (!pMesh->mBones[i])
  416. {
  417. delete[] afSum;
  418. this->ReportError("aiMesh::mBones[%i] is NULL (aiMesh::mNumBones is %i)",
  419. i,pMesh->mNumBones);
  420. }
  421. Validate(pMesh,pMesh->mBones[i],afSum);
  422. for (unsigned int a = i+1; a < pMesh->mNumBones;++a)
  423. {
  424. if (pMesh->mBones[i]->mName == pMesh->mBones[a]->mName)
  425. {
  426. delete[] afSum;
  427. this->ReportError("aiMesh::mBones[%i] has the same name as "
  428. "aiMesh::mBones[%i]",i,a);
  429. }
  430. }
  431. }
  432. // check whether all bone weights for a vertex sum to 1.0 ...
  433. for (unsigned int i = 0; i < pMesh->mNumVertices;++i)
  434. {
  435. if (afSum[i] && (afSum[i] <= 0.995 || afSum[i] >= 1.005))
  436. {
  437. ReportWarning("aiMesh::mVertices[%i]: bone weight sum != 1.0 (sum is %f)",i,afSum[i]);
  438. }
  439. }
  440. delete[] afSum;
  441. }
  442. }
  443. // ------------------------------------------------------------------------------------------------
  444. void ValidateDSProcess::Validate( const aiMesh* pMesh,
  445. const aiBone* pBone,float* afSum)
  446. {
  447. this->Validate(&pBone->mName);
  448. if (!pBone->mNumWeights)
  449. {
  450. this->ReportError("aiBone::mNumWeights is zero");
  451. }
  452. // check whether all vertices affected by this bone are valid
  453. for (unsigned int i = 0; i < pBone->mNumWeights;++i)
  454. {
  455. if (pBone->mWeights[i].mVertexId > pMesh->mNumVertices)
  456. {
  457. this->ReportError("aiBone::mWeights[%i].mVertexId is out of range",i);
  458. }
  459. else if (!pBone->mWeights[i].mWeight || pBone->mWeights[i].mWeight > 1.0f)
  460. {
  461. this->ReportWarning("aiBone::mWeights[%i].mWeight has an invalid value",i);
  462. }
  463. afSum[pBone->mWeights[i].mVertexId] += pBone->mWeights[i].mWeight;
  464. }
  465. }
  466. // ------------------------------------------------------------------------------------------------
  467. void ValidateDSProcess::Validate( const aiAnimation* pAnimation)
  468. {
  469. this->Validate(&pAnimation->mName);
  470. // validate all materials
  471. if (pAnimation->mNumChannels)
  472. {
  473. if (!pAnimation->mChannels)
  474. {
  475. this->ReportError("aiAnimation::mChannels is NULL (aiAnimation::mNumChannels is %i)",
  476. pAnimation->mNumChannels);
  477. }
  478. for (unsigned int i = 0; i < pAnimation->mNumChannels;++i)
  479. {
  480. if (!pAnimation->mChannels[i])
  481. {
  482. this->ReportError("aiAnimation::mChannels[%i] is NULL (aiAnimation::mNumChannels is %i)",
  483. i, pAnimation->mNumChannels);
  484. }
  485. this->Validate(pAnimation, pAnimation->mChannels[i]);
  486. }
  487. }
  488. else this->ReportError("aiAnimation::mNumChannels is 0. At least one node animation channel must be there.");
  489. // Animation duration is allowed to be zero in cases where the anim contains only a single key frame.
  490. // if (!pAnimation->mDuration)this->ReportError("aiAnimation::mDuration is zero");
  491. }
  492. // ------------------------------------------------------------------------------------------------
  493. void ValidateDSProcess::SearchForInvalidTextures(const aiMaterial* pMaterial,
  494. const char* szType)
  495. {
  496. ai_assert(NULL != szType);
  497. // search all keys of the material ...
  498. // textures must be specified with rising indices (e.g. diffuse #2 may not be
  499. // specified if diffuse #1 is not there ...)
  500. // "$tex.file.<szType>[<index>]"
  501. char szBaseBuf[512];
  502. int iLen;
  503. iLen = ::sprintf(szBaseBuf,"$tex.file.%s",szType);
  504. if (0 >= iLen)return;
  505. int iNumIndices = 0;
  506. int iIndex = -1;
  507. for (unsigned int i = 0; i < pMaterial->mNumProperties;++i)
  508. {
  509. aiMaterialProperty* prop = pMaterial->mProperties[i];
  510. if (0 == ASSIMP_strincmp( prop->mKey.data, szBaseBuf, iLen ))
  511. {
  512. const char* sz = &prop->mKey.data[iLen];
  513. if (*sz)
  514. {
  515. ++sz;
  516. iIndex = std::max(iIndex, (int)strtol10(sz,0));
  517. ++iNumIndices;
  518. }
  519. if (aiPTI_String != prop->mType)
  520. this->ReportError("Material property %s is expected to be a string",prop->mKey.data);
  521. }
  522. }
  523. if (iIndex +1 != iNumIndices)
  524. {
  525. this->ReportError("%s #%i is set, but there are only %i %s textures",
  526. szType,iIndex,iNumIndices,szType);
  527. }
  528. if (!iNumIndices)return;
  529. // now check whether all UV indices are valid ...
  530. iLen = ::sprintf(szBaseBuf,"$tex.uvw.%s",szType);
  531. if (0 >= iLen)return;
  532. bool bNoSpecified = true;
  533. for (unsigned int i = 0; i < pMaterial->mNumProperties;++i)
  534. {
  535. aiMaterialProperty* prop = pMaterial->mProperties[i];
  536. if (0 == ASSIMP_strincmp( prop->mKey.data, szBaseBuf, iLen ))
  537. {
  538. if (aiPTI_Integer != prop->mType || sizeof(int) > prop->mDataLength)
  539. this->ReportError("Material property %s is expected to be an integer",prop->mKey.data);
  540. const char* sz = &prop->mKey.data[iLen];
  541. if (*sz)
  542. {
  543. ++sz;
  544. iIndex = strtol10(sz,NULL);
  545. bNoSpecified = false;
  546. // ignore UV indices for texture channel that are not there ...
  547. if (iIndex >= iNumIndices)
  548. {
  549. // get the value
  550. iIndex = *((unsigned int*)prop->mData);
  551. // check whether there is a mesh using this material
  552. // which has not enough UV channels ...
  553. for (unsigned int a = 0; a < mScene->mNumMeshes;++a)
  554. {
  555. aiMesh* mesh = this->mScene->mMeshes[a];
  556. if(mesh->mMaterialIndex == (unsigned int)iIndex)
  557. {
  558. int iChannels = 0;
  559. while (mesh->HasTextureCoords(iChannels))++iChannels;
  560. if (iIndex >= iChannels)
  561. {
  562. this->ReportError("Invalid UV index: %i (key %s). Mesh %i has only %i UV channels",
  563. iIndex,prop->mKey.data,a,iChannels);
  564. }
  565. }
  566. }
  567. }
  568. }
  569. }
  570. }
  571. if (bNoSpecified)
  572. {
  573. // Assume that all textures are using the first UV channel
  574. for (unsigned int a = 0; a < mScene->mNumMeshes;++a)
  575. {
  576. aiMesh* mesh = this->mScene->mMeshes[a];
  577. if(mesh->mMaterialIndex == (unsigned int)iIndex)
  578. {
  579. if (!mesh->mTextureCoords[0])
  580. {
  581. // This is a special case ... it could be that the
  582. // original mesh format intended the use of a special
  583. // mapping here.
  584. ReportWarning("UV-mapped texture, but there are no UV coords");
  585. }
  586. }
  587. }
  588. }
  589. }
  590. // ------------------------------------------------------------------------------------------------
  591. void ValidateDSProcess::Validate( const aiMaterial* pMaterial)
  592. {
  593. // check whether there are material keys that are obviously not legal
  594. for (unsigned int i = 0; i < pMaterial->mNumProperties;++i)
  595. {
  596. const aiMaterialProperty* prop = pMaterial->mProperties[i];
  597. if (!prop)
  598. {
  599. this->ReportError("aiMaterial::mProperties[%i] is NULL (aiMaterial::mNumProperties is %i)",
  600. i,pMaterial->mNumProperties);
  601. }
  602. if (!prop->mDataLength || !prop->mData)
  603. {
  604. this->ReportError("aiMaterial::mProperties[%i].mDataLength or "
  605. "aiMaterial::mProperties[%i].mData is 0",i,i);
  606. }
  607. // check all predefined types
  608. if (aiPTI_String == prop->mType)
  609. {
  610. // FIX: strings are now stored in a less expensive way ...
  611. if (prop->mDataLength < sizeof(size_t) + ((const aiString*)prop->mData)->length + 1)
  612. {
  613. this->ReportError("aiMaterial::mProperties[%i].mDataLength is "
  614. "too small to contain a string (%i, needed: %i)",
  615. i,prop->mDataLength,sizeof(aiString));
  616. }
  617. this->Validate((const aiString*)prop->mData);
  618. }
  619. else if (aiPTI_Float == prop->mType)
  620. {
  621. if (prop->mDataLength < sizeof(float))
  622. {
  623. this->ReportError("aiMaterial::mProperties[%i].mDataLength is "
  624. "too small to contain a float (%i, needed: %i)",
  625. i,prop->mDataLength,sizeof(float));
  626. }
  627. }
  628. else if (aiPTI_Integer == prop->mType)
  629. {
  630. if (prop->mDataLength < sizeof(int))
  631. {
  632. this->ReportError("aiMaterial::mProperties[%i].mDataLength is "
  633. "too small to contain an integer (%i, needed: %i)",
  634. i,prop->mDataLength,sizeof(int));
  635. }
  636. }
  637. // TODO: check whether there is a key with an unknown name ...
  638. }
  639. // make some more specific tests
  640. float fTemp;
  641. int iShading;
  642. if (AI_SUCCESS == aiGetMaterialInteger( pMaterial,AI_MATKEY_SHADING_MODEL,&iShading))
  643. {
  644. switch ((aiShadingMode)iShading)
  645. {
  646. case aiShadingMode_Blinn:
  647. case aiShadingMode_CookTorrance:
  648. case aiShadingMode_Phong:
  649. if (AI_SUCCESS != aiGetMaterialFloat(pMaterial,AI_MATKEY_SHININESS,&fTemp))
  650. {
  651. this->ReportWarning("A specular shading model is specified but there is no "
  652. "AI_MATKEY_SHININESS key");
  653. }
  654. if (AI_SUCCESS == aiGetMaterialFloat(pMaterial,AI_MATKEY_SHININESS_STRENGTH,&fTemp) && !fTemp)
  655. {
  656. this->ReportWarning("A specular shading model is specified but the value of the "
  657. "AI_MATKEY_SHININESS_STRENGTH key is 0.0");
  658. }
  659. break;
  660. default: ;
  661. };
  662. }
  663. if (AI_SUCCESS == aiGetMaterialFloat( pMaterial,AI_MATKEY_OPACITY,&fTemp))
  664. {
  665. if (!fTemp)
  666. ReportWarning("Material is fully transparent ... are you sure you REALLY want this?");
  667. }
  668. // check whether there are invalid texture keys
  669. SearchForInvalidTextures(pMaterial,"diffuse");
  670. SearchForInvalidTextures(pMaterial,"specular");
  671. SearchForInvalidTextures(pMaterial,"ambient");
  672. SearchForInvalidTextures(pMaterial,"emissive");
  673. SearchForInvalidTextures(pMaterial,"opacity");
  674. SearchForInvalidTextures(pMaterial,"shininess");
  675. SearchForInvalidTextures(pMaterial,"normals");
  676. SearchForInvalidTextures(pMaterial,"height");
  677. }
  678. // ------------------------------------------------------------------------------------------------
  679. void ValidateDSProcess::Validate( const aiTexture* pTexture)
  680. {
  681. // the data section may NEVER be NULL
  682. if (!pTexture->pcData)
  683. {
  684. this->ReportError("aiTexture::pcData is NULL");
  685. }
  686. if (pTexture->mHeight)
  687. {
  688. if (!pTexture->mWidth)this->ReportError("aiTexture::mWidth is zero "
  689. "(aiTexture::mHeight is %i, uncompressed texture)",pTexture->mHeight);
  690. }
  691. else
  692. {
  693. if (!pTexture->mWidth)this->ReportError("aiTexture::mWidth is zero (compressed texture)");
  694. if ('.' == pTexture->achFormatHint[0])
  695. {
  696. char szTemp[5];
  697. szTemp[0] = pTexture->achFormatHint[0];
  698. szTemp[1] = pTexture->achFormatHint[1];
  699. szTemp[2] = pTexture->achFormatHint[2];
  700. szTemp[3] = pTexture->achFormatHint[3];
  701. szTemp[4] = '\0';
  702. this->ReportWarning("aiTexture::achFormatHint should contain a file extension "
  703. "without a leading dot (format hint: %s).",szTemp);
  704. }
  705. }
  706. const char* sz = pTexture->achFormatHint;
  707. if ( sz[0] >= 'A' && sz[0] <= 'Z' ||
  708. sz[1] >= 'A' && sz[1] <= 'Z' ||
  709. sz[2] >= 'A' && sz[2] <= 'Z' ||
  710. sz[3] >= 'A' && sz[3] <= 'Z')
  711. {
  712. this->ReportError("aiTexture::achFormatHint contains non-lowercase characters");
  713. }
  714. }
  715. // ------------------------------------------------------------------------------------------------
  716. void ValidateDSProcess::Validate( const aiAnimation* pAnimation,
  717. const aiNodeAnim* pNodeAnim)
  718. {
  719. this->Validate(&pNodeAnim->mNodeName);
  720. #if 0
  721. // check whether there is a bone with this name ...
  722. unsigned int i = 0;
  723. for (; i < this->mScene->mNumMeshes;++i)
  724. {
  725. aiMesh* mesh = this->mScene->mMeshes[i];
  726. for (unsigned int a = 0; a < mesh->mNumBones;++a)
  727. {
  728. if (mesh->mBones[a]->mName == pNodeAnim->mBoneName)
  729. goto __break_out;
  730. }
  731. }
  732. __break_out:
  733. if (i == this->mScene->mNumMeshes)
  734. {
  735. this->ReportWarning("aiNodeAnim::mBoneName is \"%s\". However, no bone with this name was found",
  736. pNodeAnim->mBoneName.data);
  737. }
  738. if (!pNodeAnim->mNumPositionKeys && !pNodeAnim->mNumRotationKeys && !pNodeAnim->mNumScalingKeys)
  739. {
  740. this->ReportWarning("A bone animation channel has no keys");
  741. }
  742. #endif
  743. // otherwise check whether one of the keys exceeds the total duration of the animation
  744. if (pNodeAnim->mNumPositionKeys)
  745. {
  746. if (!pNodeAnim->mPositionKeys)
  747. {
  748. this->ReportError("aiNodeAnim::mPositionKeys is NULL (aiNodeAnim::mNumPositionKeys is %i)",
  749. pNodeAnim->mNumPositionKeys);
  750. }
  751. double dLast = -0.1;
  752. for (unsigned int i = 0; i < pNodeAnim->mNumPositionKeys;++i)
  753. {
  754. if (pNodeAnim->mPositionKeys[i].mTime > pAnimation->mDuration)
  755. {
  756. this->ReportError("aiNodeAnim::mPositionKeys[%i].mTime (%.5f) is larger "
  757. "than aiAnimation::mDuration (which is %.5f)",i,
  758. (float)pNodeAnim->mPositionKeys[i].mTime,
  759. (float)pAnimation->mDuration);
  760. }
  761. if (pNodeAnim->mPositionKeys[i].mTime <= dLast)
  762. {
  763. this->ReportWarning("aiNodeAnim::mPositionKeys[%i].mTime (%.5f) is smaller "
  764. "than aiAnimation::mPositionKeys[%i] (which is %.5f)",i,
  765. (float)pNodeAnim->mPositionKeys[i].mTime,
  766. i-1, (float)dLast);
  767. }
  768. dLast = pNodeAnim->mPositionKeys[i].mTime;
  769. }
  770. }
  771. // rotation keys
  772. if (pNodeAnim->mNumRotationKeys)
  773. {
  774. if (!pNodeAnim->mRotationKeys)
  775. {
  776. this->ReportError("aiNodeAnim::mRotationKeys is NULL (aiNodeAnim::mNumRotationKeys is %i)",
  777. pNodeAnim->mNumRotationKeys);
  778. }
  779. double dLast = -0.1;
  780. for (unsigned int i = 0; i < pNodeAnim->mNumRotationKeys;++i)
  781. {
  782. if (pNodeAnim->mRotationKeys[i].mTime > pAnimation->mDuration)
  783. {
  784. this->ReportError("aiNodeAnim::mRotationKeys[%i].mTime (%.5f) is larger "
  785. "than aiAnimation::mDuration (which is %.5f)",i,
  786. (float)pNodeAnim->mRotationKeys[i].mTime,
  787. (float)pAnimation->mDuration);
  788. }
  789. if (pNodeAnim->mRotationKeys[i].mTime <= dLast)
  790. {
  791. this->ReportWarning("aiNodeAnim::mRotationKeys[%i].mTime (%.5f) is smaller "
  792. "than aiAnimation::mRotationKeys[%i] (which is %.5f)",i,
  793. (float)pNodeAnim->mRotationKeys[i].mTime,
  794. i-1, (float)dLast);
  795. }
  796. dLast = pNodeAnim->mRotationKeys[i].mTime;
  797. }
  798. }
  799. // scaling keys
  800. if (pNodeAnim->mNumScalingKeys)
  801. {
  802. if (!pNodeAnim->mScalingKeys)
  803. {
  804. this->ReportError("aiNodeAnim::mScalingKeys is NULL (aiNodeAnim::mNumScalingKeys is %i)",
  805. pNodeAnim->mNumScalingKeys);
  806. }
  807. double dLast = -0.1;
  808. for (unsigned int i = 0; i < pNodeAnim->mNumScalingKeys;++i)
  809. {
  810. if (pNodeAnim->mScalingKeys[i].mTime > pAnimation->mDuration)
  811. {
  812. this->ReportError("aiNodeAnim::mScalingKeys[%i].mTime (%.5f) is larger "
  813. "than aiAnimation::mDuration (which is %.5f)",i,
  814. (float)pNodeAnim->mScalingKeys[i].mTime,
  815. (float)pAnimation->mDuration);
  816. }
  817. if (pNodeAnim->mScalingKeys[i].mTime <= dLast)
  818. {
  819. this->ReportWarning("aiNodeAnim::mScalingKeys[%i].mTime (%.5f) is smaller "
  820. "than aiAnimation::mScalingKeys[%i] (which is %.5f)",i,
  821. (float)pNodeAnim->mScalingKeys[i].mTime,
  822. i-1, (float)dLast);
  823. }
  824. dLast = pNodeAnim->mScalingKeys[i].mTime;
  825. }
  826. }
  827. }
  828. // ------------------------------------------------------------------------------------------------
  829. void ValidateDSProcess::Validate( const aiNode* pNode)
  830. {
  831. if (!pNode)this->ReportError("A node of the scenegraph is NULL");
  832. if (pNode != this->mScene->mRootNode && !pNode->mParent)
  833. this->ReportError("A node has no valid parent (aiNode::mParent is NULL)");
  834. this->Validate(&pNode->mName);
  835. // validate all meshes
  836. if (pNode->mNumMeshes)
  837. {
  838. if (!pNode->mMeshes)
  839. {
  840. this->ReportError("aiNode::mMeshes is NULL (aiNode::mNumMeshes is %i)",
  841. pNode->mNumMeshes);
  842. }
  843. std::vector<bool> abHadMesh;
  844. abHadMesh.resize(this->mScene->mNumMeshes,false);
  845. for (unsigned int i = 0; i < pNode->mNumMeshes;++i)
  846. {
  847. if (pNode->mMeshes[i] >= this->mScene->mNumMeshes)
  848. {
  849. this->ReportError("aiNode::mMeshes[%i] is out of range (maximum is %i)",
  850. pNode->mMeshes[i],this->mScene->mNumMeshes-1);
  851. }
  852. if (abHadMesh[pNode->mMeshes[i]])
  853. {
  854. this->ReportError("aiNode::mMeshes[%i] is already referenced by this node (value: %i)",
  855. i,pNode->mMeshes[i]);
  856. }
  857. abHadMesh[pNode->mMeshes[i]] = true;
  858. }
  859. }
  860. if (pNode->mNumChildren)
  861. {
  862. if (!pNode->mChildren)
  863. {
  864. this->ReportError("aiNode::mChildren is NULL (aiNode::mNumChildren is %i)",
  865. pNode->mNumChildren);
  866. }
  867. for (unsigned int i = 0; i < pNode->mNumChildren;++i)
  868. {
  869. this->Validate(pNode->mChildren[i]);
  870. }
  871. }
  872. }
  873. // ------------------------------------------------------------------------------------------------
  874. void ValidateDSProcess::Validate( const aiString* pString)
  875. {
  876. if (pString->length > MAXLEN)
  877. {
  878. this->ReportError("aiString::length is too large (%i, maximum is %i)",
  879. pString->length,MAXLEN);
  880. }
  881. const char* sz = pString->data;
  882. while (true)
  883. {
  884. if ('\0' == *sz)
  885. {
  886. if (pString->length != (unsigned int)(sz-pString->data))
  887. this->ReportError("aiString::data is invalid: the terminal zero is at a wrong offset");
  888. break;
  889. }
  890. else if (sz >= &pString->data[MAXLEN])
  891. this->ReportError("aiString::data is invalid. There is no terminal character");
  892. ++sz;
  893. }
  894. }