ValidateDataStructure.cpp 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036
  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. else if (pScene->mMeshes)
  212. {
  213. ReportError("aiScene::mMeshes is non-null although there are no meshes");
  214. }
  215. // validate all animations
  216. if (pScene->mNumAnimations)
  217. {
  218. has = true;
  219. DoValidation(pScene->mAnimations,pScene->mNumAnimations,
  220. "mAnimations","mNumAnimations");
  221. }
  222. else if (pScene->mAnimations)
  223. {
  224. ReportError("aiScene::mAnimations is non-null although there are no animations");
  225. }
  226. // validate all cameras
  227. if (pScene->mNumCameras)
  228. {
  229. has = true;
  230. DoValidationWithNameCheck(pScene->mCameras,pScene->mNumCameras,
  231. "mCameras","mNumCameras");
  232. }
  233. else if (pScene->mCameras)
  234. {
  235. ReportError("aiScene::mCameras is non-null although there are no cameras");
  236. }
  237. // validate all lights
  238. if (pScene->mNumLights)
  239. {
  240. has = true;
  241. DoValidationWithNameCheck(pScene->mLights,pScene->mNumLights,
  242. "mLights","mNumLights");
  243. }
  244. else if (pScene->mLights)
  245. {
  246. ReportError("aiScene::mLights is non-null although there are no lights");
  247. }
  248. // validate all materials
  249. if (pScene->mNumMaterials)
  250. {
  251. has = true;
  252. DoValidation(pScene->mMaterials,pScene->mNumMaterials,"mMaterials","mNumMaterials");
  253. }
  254. else if (!(mScene->mFlags & AI_SCENE_FLAGS_INCOMPLETE))
  255. {
  256. ReportError("aiScene::mNumMaterials is 0. At least one material must be there");
  257. }
  258. else if (pScene->mMaterials)
  259. {
  260. ReportError("aiScene::mMaterials is non-null although there are no materials");
  261. }
  262. if (!has)ReportError("The aiScene data structure is empty");
  263. DefaultLogger::get()->debug("ValidateDataStructureProcess end");
  264. }
  265. // ------------------------------------------------------------------------------------------------
  266. void ValidateDSProcess::Validate( const aiLight* pLight)
  267. {
  268. if (pLight->mType == aiLightSource_UNDEFINED)
  269. ReportError("aiLight::mType is aiLightSource_UNDEFINED");
  270. if (!pLight->mAttenuationConstant &&
  271. !pLight->mAttenuationLinear &&
  272. !pLight->mAttenuationQuadratic)
  273. {
  274. ReportWarning("aiLight::mAttenuationXXX - all are zero");
  275. }
  276. if (pLight->mAngleInnerCone > pLight->mAngleOuterCone)
  277. ReportError("aiLight::mAngleInnerCone is larger than aiLight::mAngleOuterCone");
  278. if (pLight->mColorDiffuse.IsBlack() && pLight->mColorAmbient.IsBlack()
  279. && pLight->mColorSpecular.IsBlack())
  280. {
  281. ReportWarning("aiLight::mColorXXX - all are black and won't have any influence");
  282. }
  283. }
  284. // ------------------------------------------------------------------------------------------------
  285. void ValidateDSProcess::Validate( const aiCamera* pCamera)
  286. {
  287. if (pCamera->mClipPlaneFar <= pCamera->mClipPlaneNear)
  288. ReportError("aiCamera::mClipPlaneFar must be >= aiCamera::mClipPlaneNear");
  289. if (!pCamera->mHorizontalFOV || pCamera->mHorizontalFOV >= (float)AI_MATH_PI)
  290. ReportError("%f is not a valid value for aiCamera::mHorizontalFOV",pCamera->mHorizontalFOV);
  291. }
  292. // ------------------------------------------------------------------------------------------------
  293. void ValidateDSProcess::Validate( const aiMesh* pMesh)
  294. {
  295. // validate the material index of the mesh
  296. if (pMesh->mMaterialIndex >= this->mScene->mNumMaterials)
  297. {
  298. this->ReportError("aiMesh::mMaterialIndex is invalid (value: %i maximum: %i)",
  299. pMesh->mMaterialIndex,this->mScene->mNumMaterials-1);
  300. }
  301. for (unsigned int i = 0; i < pMesh->mNumFaces; ++i)
  302. {
  303. aiFace& face = pMesh->mFaces[i];
  304. if (pMesh->mPrimitiveTypes)
  305. {
  306. switch (face.mNumIndices)
  307. {
  308. case 0:
  309. this->ReportError("aiMesh::mFaces[%i].mNumIndices is 0",i);
  310. case 1:
  311. if (0 == (pMesh->mPrimitiveTypes & aiPrimitiveType_POINT))
  312. {
  313. this->ReportError("aiMesh::mFaces[%i] is a POINT but aiMesh::mPrimtiveTypes "
  314. "does not report the POINT flag",i);
  315. }
  316. break;
  317. case 2:
  318. if (0 == (pMesh->mPrimitiveTypes & aiPrimitiveType_LINE))
  319. {
  320. this->ReportError("aiMesh::mFaces[%i] is a LINE but aiMesh::mPrimtiveTypes "
  321. "does not report the LINE flag",i);
  322. }
  323. break;
  324. case 3:
  325. if (0 == (pMesh->mPrimitiveTypes & aiPrimitiveType_TRIANGLE))
  326. {
  327. this->ReportError("aiMesh::mFaces[%i] is a TRIANGLE but aiMesh::mPrimtiveTypes "
  328. "does not report the TRIANGLE flag",i);
  329. }
  330. break;
  331. default:
  332. if (0 == (pMesh->mPrimitiveTypes & aiPrimitiveType_POLYGON))
  333. {
  334. this->ReportError("aiMesh::mFaces[%i] is a POLYGON but aiMesh::mPrimtiveTypes "
  335. "does not report the POLYGON flag",i);
  336. }
  337. break;
  338. };
  339. }
  340. if (!face.mIndices)this->ReportError("aiMesh::mFaces[%i].mIndices is NULL",i);
  341. }
  342. // positions must always be there ...
  343. if (!pMesh->mNumVertices || !pMesh->mVertices && !mScene->mFlags)
  344. {
  345. this->ReportError("The mesh contains no vertices");
  346. }
  347. // if tangents are there there must also be bitangent vectors ...
  348. if ((pMesh->mTangents != NULL) != (pMesh->mBitangents != NULL))
  349. {
  350. this->ReportError("If there are tangents there must also be bitangent vectors");
  351. }
  352. // faces, too
  353. if (!pMesh->mNumFaces || !pMesh->mFaces && !mScene->mFlags)
  354. {
  355. this->ReportError("The mesh contains no faces");
  356. }
  357. // now check whether the face indexing layout is correct:
  358. // unique vertices, pseudo-indexed.
  359. std::vector<bool> abRefList;
  360. abRefList.resize(pMesh->mNumVertices,false);
  361. for (unsigned int i = 0; i < pMesh->mNumFaces;++i)
  362. {
  363. aiFace& face = pMesh->mFaces[i];
  364. for (unsigned int a = 0; a < face.mNumIndices;++a)
  365. {
  366. if (face.mIndices[a] >= pMesh->mNumVertices)
  367. {
  368. ReportError("aiMesh::mFaces[%i]::mIndices[%i] is out of range",i,a);
  369. }
  370. // the MSB flag is temporarily used by the extra verbose
  371. // mode to tell us that the JoinVerticesProcess might have
  372. // been executed already.
  373. if ( !(this->mScene->mFlags & AI_SCENE_FLAGS_NON_VERBOSE_FORMAT ) && abRefList[face.mIndices[a]])
  374. {
  375. ReportError("aiMesh::mVertices[%i] is referenced twice - second "
  376. "time by aiMesh::mFaces[%i]::mIndices[%i]",face.mIndices[a],i,a);
  377. }
  378. abRefList[face.mIndices[a]] = true;
  379. }
  380. }
  381. // check whether there are vertices that aren't referenced by a face
  382. bool b = false;
  383. for (unsigned int i = 0; i < pMesh->mNumVertices;++i)
  384. {
  385. if (!abRefList[i])b = true;
  386. }
  387. abRefList.clear();
  388. if (b)ReportWarning("There are unreferenced vertices");
  389. // texture channel 2 may not be set if channel 1 is zero ...
  390. {
  391. unsigned int i = 0;
  392. for (;i < AI_MAX_NUMBER_OF_TEXTURECOORDS;++i)
  393. {
  394. if (!pMesh->HasTextureCoords(i))break;
  395. }
  396. for (;i < AI_MAX_NUMBER_OF_TEXTURECOORDS;++i)
  397. if (pMesh->HasTextureCoords(i))
  398. {
  399. ReportError("Texture coordinate channel %i exists "
  400. "although the previous channel was NULL.",i);
  401. }
  402. }
  403. // the same for the vertex colors
  404. {
  405. unsigned int i = 0;
  406. for (;i < AI_MAX_NUMBER_OF_COLOR_SETS;++i)
  407. {
  408. if (!pMesh->HasVertexColors(i))break;
  409. }
  410. for (;i < AI_MAX_NUMBER_OF_COLOR_SETS;++i)
  411. if (pMesh->HasVertexColors(i))
  412. {
  413. ReportError("Vertex color channel %i is exists "
  414. "although the previous channel was NULL.",i);
  415. }
  416. }
  417. // now validate all bones
  418. if (pMesh->mNumBones)
  419. {
  420. if (!pMesh->mBones)
  421. {
  422. ReportError("aiMesh::mBones is NULL (aiMesh::mNumBones is %i)",
  423. pMesh->mNumBones);
  424. }
  425. float* afSum = NULL;
  426. if (pMesh->mNumVertices)
  427. {
  428. afSum = new float[pMesh->mNumVertices];
  429. for (unsigned int i = 0; i < pMesh->mNumVertices;++i)
  430. afSum[i] = 0.0f;
  431. }
  432. // check whether there are duplicate bone names
  433. for (unsigned int i = 0; i < pMesh->mNumBones;++i)
  434. {
  435. if (!pMesh->mBones[i])
  436. {
  437. delete[] afSum;
  438. this->ReportError("aiMesh::mBones[%i] is NULL (aiMesh::mNumBones is %i)",
  439. i,pMesh->mNumBones);
  440. }
  441. Validate(pMesh,pMesh->mBones[i],afSum);
  442. for (unsigned int a = i+1; a < pMesh->mNumBones;++a)
  443. {
  444. if (pMesh->mBones[i]->mName == pMesh->mBones[a]->mName)
  445. {
  446. delete[] afSum;
  447. this->ReportError("aiMesh::mBones[%i] has the same name as "
  448. "aiMesh::mBones[%i]",i,a);
  449. }
  450. }
  451. }
  452. // check whether all bone weights for a vertex sum to 1.0 ...
  453. for (unsigned int i = 0; i < pMesh->mNumVertices;++i)
  454. {
  455. if (afSum[i] && (afSum[i] <= 0.94 || afSum[i] >= 1.05))
  456. {
  457. ReportWarning("aiMesh::mVertices[%i]: bone weight sum != 1.0 (sum is %f)",i,afSum[i]);
  458. }
  459. }
  460. delete[] afSum;
  461. }
  462. else if (pMesh->mBones)
  463. {
  464. ReportError("aiMesh::mBones is non-null although there are no bones");
  465. }
  466. }
  467. // ------------------------------------------------------------------------------------------------
  468. void ValidateDSProcess::Validate( const aiMesh* pMesh,
  469. const aiBone* pBone,float* afSum)
  470. {
  471. this->Validate(&pBone->mName);
  472. if (!pBone->mNumWeights)
  473. {
  474. this->ReportError("aiBone::mNumWeights is zero");
  475. }
  476. // check whether all vertices affected by this bone are valid
  477. for (unsigned int i = 0; i < pBone->mNumWeights;++i)
  478. {
  479. if (pBone->mWeights[i].mVertexId >= pMesh->mNumVertices)
  480. {
  481. this->ReportError("aiBone::mWeights[%i].mVertexId is out of range",i);
  482. }
  483. else if (!pBone->mWeights[i].mWeight || pBone->mWeights[i].mWeight > 1.0f)
  484. {
  485. this->ReportWarning("aiBone::mWeights[%i].mWeight has an invalid value",i);
  486. }
  487. afSum[pBone->mWeights[i].mVertexId] += pBone->mWeights[i].mWeight;
  488. }
  489. }
  490. // ------------------------------------------------------------------------------------------------
  491. void ValidateDSProcess::Validate( const aiAnimation* pAnimation)
  492. {
  493. this->Validate(&pAnimation->mName);
  494. // validate all materials
  495. if (pAnimation->mNumChannels)
  496. {
  497. if (!pAnimation->mChannels)
  498. {
  499. this->ReportError("aiAnimation::mChannels is NULL (aiAnimation::mNumChannels is %i)",
  500. pAnimation->mNumChannels);
  501. }
  502. for (unsigned int i = 0; i < pAnimation->mNumChannels;++i)
  503. {
  504. if (!pAnimation->mChannels[i])
  505. {
  506. this->ReportError("aiAnimation::mChannels[%i] is NULL (aiAnimation::mNumChannels is %i)",
  507. i, pAnimation->mNumChannels);
  508. }
  509. this->Validate(pAnimation, pAnimation->mChannels[i]);
  510. }
  511. }
  512. else this->ReportError("aiAnimation::mNumChannels is 0. At least one node animation channel must be there.");
  513. // Animation duration is allowed to be zero in cases where the anim contains only a single key frame.
  514. // if (!pAnimation->mDuration)this->ReportError("aiAnimation::mDuration is zero");
  515. }
  516. // ------------------------------------------------------------------------------------------------
  517. void ValidateDSProcess::SearchForInvalidTextures(const aiMaterial* pMaterial,
  518. aiTextureType type)
  519. {
  520. const char* szType = NULL;
  521. switch (type)
  522. {
  523. case aiTextureType_DIFFUSE:
  524. szType = "Diffuse";
  525. break;
  526. case aiTextureType_SPECULAR:
  527. szType = "Specular";
  528. break;
  529. case aiTextureType_AMBIENT:
  530. szType = "Ambient";
  531. break;
  532. case aiTextureType_EMISSIVE:
  533. szType = "Emissive";
  534. break;
  535. case aiTextureType_OPACITY:
  536. szType = "Opacity";
  537. break;
  538. case aiTextureType_SHININESS:
  539. szType = "Shininess";
  540. break;
  541. case aiTextureType_NORMALS:
  542. szType = "Normals";
  543. break;
  544. case aiTextureType_HEIGHT:
  545. szType = "Height";
  546. break;
  547. default:
  548. break;
  549. };
  550. // ****************************************************************************
  551. // Search all keys of the material ...
  552. // textures must be specified with ascending indices
  553. // (e.g. diffuse #2 may not be specified if diffuse #1 is not there ...)
  554. // ****************************************************************************
  555. int iNumIndices = 0;
  556. int iIndex = -1;
  557. for (unsigned int i = 0; i < pMaterial->mNumProperties;++i)
  558. {
  559. aiMaterialProperty* prop = pMaterial->mProperties[i];
  560. if (!::strcmp(prop->mKey.data,"$tex.file") && prop->mSemantic == type)
  561. {
  562. iIndex = std::max(iIndex, (int) prop->mIndex);
  563. ++iNumIndices;
  564. if (aiPTI_String != prop->mType)
  565. this->ReportError("Material property %s is expected to be a string",prop->mKey.data);
  566. }
  567. }
  568. if (iIndex +1 != iNumIndices)
  569. {
  570. ReportError("%s #%i is set, but there are only %i %s textures",
  571. szType,iIndex,iNumIndices,szType);
  572. }
  573. if (!iNumIndices)return;
  574. std::vector<aiTextureMapping> mappings(iNumIndices);
  575. // Now check whether all UV indices are valid ...
  576. bool bNoSpecified = true;
  577. for (unsigned int i = 0; i < pMaterial->mNumProperties;++i)
  578. {
  579. aiMaterialProperty* prop = pMaterial->mProperties[i];
  580. if (prop->mSemantic != type)continue;
  581. if ((int)prop->mIndex >= iNumIndices)
  582. {
  583. ReportError("Found texture property with index %i, although there "
  584. "are only %i textures of type %s",
  585. prop->mIndex, iNumIndices, szType);
  586. }
  587. if (!::strcmp(prop->mKey.data,"$tex.mapping"))
  588. {
  589. if (aiPTI_Integer != prop->mType || prop->mDataLength < sizeof(aiTextureMapping))
  590. {
  591. ReportError("Material property %s%i is expected to be an integer (size is %i)",
  592. prop->mKey.data,prop->mIndex,prop->mDataLength);
  593. }
  594. mappings[prop->mIndex] = *((aiTextureMapping*)prop->mData);
  595. }
  596. else if (!::strcmp(prop->mKey.data,"$tex.uvtrafo"))
  597. {
  598. if (aiPTI_Float != prop->mType || prop->mDataLength < sizeof(aiUVTransform))
  599. {
  600. ReportError("Material property %s%i is expected to be 5 floats large (size is %i)",
  601. prop->mKey.data,prop->mIndex, prop->mDataLength);
  602. }
  603. mappings[prop->mIndex] = *((aiTextureMapping*)prop->mData);
  604. }
  605. else if (!::strcmp(prop->mKey.data,"$tex.uvwsrc"))
  606. {
  607. if (aiPTI_Integer != prop->mType || sizeof(int) > prop->mDataLength)
  608. {
  609. ReportError("Material property %s%i is expected to be an integer (size is %i)",
  610. prop->mKey.data,prop->mIndex,prop->mDataLength);
  611. }
  612. bNoSpecified = false;
  613. // Ignore UV indices for texture channels that are not there ...
  614. // Get the value
  615. iIndex = *((unsigned int*)prop->mData);
  616. // Check whether there is a mesh using this material
  617. // which has not enough UV channels ...
  618. for (unsigned int a = 0; a < mScene->mNumMeshes;++a)
  619. {
  620. aiMesh* mesh = this->mScene->mMeshes[a];
  621. if(mesh->mMaterialIndex == (unsigned int)i)
  622. {
  623. int iChannels = 0;
  624. while (mesh->HasTextureCoords(iChannels))++iChannels;
  625. if (iIndex >= iChannels)
  626. {
  627. ReportError("Invalid UV index: %i (key %s). Mesh %i has only %i UV channels",
  628. iIndex,prop->mKey.data,a,iChannels);
  629. }
  630. }
  631. }
  632. }
  633. }
  634. if (bNoSpecified)
  635. {
  636. // Assume that all textures are using the first UV channel
  637. for (unsigned int a = 0; a < mScene->mNumMeshes;++a)
  638. {
  639. aiMesh* mesh = mScene->mMeshes[a];
  640. if(mesh->mMaterialIndex == (unsigned int)iIndex && mappings[0] == aiTextureMapping_UV)
  641. {
  642. if (!mesh->mTextureCoords[0])
  643. {
  644. // This is a special case ... it could be that the
  645. // original mesh format intended the use of a special
  646. // mapping here.
  647. ReportWarning("UV-mapped texture, but there are no UV coords");
  648. }
  649. }
  650. }
  651. }
  652. }
  653. // ------------------------------------------------------------------------------------------------
  654. void ValidateDSProcess::Validate( const aiMaterial* pMaterial)
  655. {
  656. // check whether there are material keys that are obviously not legal
  657. for (unsigned int i = 0; i < pMaterial->mNumProperties;++i)
  658. {
  659. const aiMaterialProperty* prop = pMaterial->mProperties[i];
  660. if (!prop)
  661. {
  662. this->ReportError("aiMaterial::mProperties[%i] is NULL (aiMaterial::mNumProperties is %i)",
  663. i,pMaterial->mNumProperties);
  664. }
  665. if (!prop->mDataLength || !prop->mData)
  666. {
  667. this->ReportError("aiMaterial::mProperties[%i].mDataLength or "
  668. "aiMaterial::mProperties[%i].mData is 0",i,i);
  669. }
  670. // check all predefined types
  671. if (aiPTI_String == prop->mType)
  672. {
  673. // FIX: strings are now stored in a less expensive way ...
  674. if (prop->mDataLength < sizeof(size_t) + ((const aiString*)prop->mData)->length + 1)
  675. {
  676. this->ReportError("aiMaterial::mProperties[%i].mDataLength is "
  677. "too small to contain a string (%i, needed: %i)",
  678. i,prop->mDataLength,sizeof(aiString));
  679. }
  680. this->Validate((const aiString*)prop->mData);
  681. }
  682. else if (aiPTI_Float == prop->mType)
  683. {
  684. if (prop->mDataLength < sizeof(float))
  685. {
  686. this->ReportError("aiMaterial::mProperties[%i].mDataLength is "
  687. "too small to contain a float (%i, needed: %i)",
  688. i,prop->mDataLength,sizeof(float));
  689. }
  690. }
  691. else if (aiPTI_Integer == prop->mType)
  692. {
  693. if (prop->mDataLength < sizeof(int))
  694. {
  695. this->ReportError("aiMaterial::mProperties[%i].mDataLength is "
  696. "too small to contain an integer (%i, needed: %i)",
  697. i,prop->mDataLength,sizeof(int));
  698. }
  699. }
  700. // TODO: check whether there is a key with an unknown name ...
  701. }
  702. // make some more specific tests
  703. float fTemp;
  704. int iShading;
  705. if (AI_SUCCESS == aiGetMaterialInteger( pMaterial,AI_MATKEY_SHADING_MODEL,&iShading))
  706. {
  707. switch ((aiShadingMode)iShading)
  708. {
  709. case aiShadingMode_Blinn:
  710. case aiShadingMode_CookTorrance:
  711. case aiShadingMode_Phong:
  712. if (AI_SUCCESS != aiGetMaterialFloat(pMaterial,AI_MATKEY_SHININESS,&fTemp))
  713. {
  714. this->ReportWarning("A specular shading model is specified but there is no "
  715. "AI_MATKEY_SHININESS key");
  716. }
  717. if (AI_SUCCESS == aiGetMaterialFloat(pMaterial,AI_MATKEY_SHININESS_STRENGTH,&fTemp) && !fTemp)
  718. {
  719. this->ReportWarning("A specular shading model is specified but the value of the "
  720. "AI_MATKEY_SHININESS_STRENGTH key is 0.0");
  721. }
  722. break;
  723. default: ;
  724. };
  725. }
  726. if (AI_SUCCESS == aiGetMaterialFloat( pMaterial,AI_MATKEY_OPACITY,&fTemp))
  727. {
  728. if (!fTemp)
  729. ReportWarning("Material is fully transparent ... are you sure you REALLY want this?");
  730. }
  731. // check whether there are invalid texture keys
  732. SearchForInvalidTextures(pMaterial,aiTextureType_DIFFUSE);
  733. SearchForInvalidTextures(pMaterial,aiTextureType_SPECULAR);
  734. SearchForInvalidTextures(pMaterial,aiTextureType_AMBIENT);
  735. SearchForInvalidTextures(pMaterial,aiTextureType_EMISSIVE);
  736. SearchForInvalidTextures(pMaterial,aiTextureType_OPACITY);
  737. SearchForInvalidTextures(pMaterial,aiTextureType_SHININESS);
  738. SearchForInvalidTextures(pMaterial,aiTextureType_HEIGHT);
  739. SearchForInvalidTextures(pMaterial,aiTextureType_NORMALS);
  740. }
  741. // ------------------------------------------------------------------------------------------------
  742. void ValidateDSProcess::Validate( const aiTexture* pTexture)
  743. {
  744. // the data section may NEVER be NULL
  745. if (!pTexture->pcData)
  746. {
  747. this->ReportError("aiTexture::pcData is NULL");
  748. }
  749. if (pTexture->mHeight)
  750. {
  751. if (!pTexture->mWidth)this->ReportError("aiTexture::mWidth is zero "
  752. "(aiTexture::mHeight is %i, uncompressed texture)",pTexture->mHeight);
  753. }
  754. else
  755. {
  756. if (!pTexture->mWidth)this->ReportError("aiTexture::mWidth is zero (compressed texture)");
  757. if ('.' == pTexture->achFormatHint[0])
  758. {
  759. char szTemp[5];
  760. szTemp[0] = pTexture->achFormatHint[0];
  761. szTemp[1] = pTexture->achFormatHint[1];
  762. szTemp[2] = pTexture->achFormatHint[2];
  763. szTemp[3] = pTexture->achFormatHint[3];
  764. szTemp[4] = '\0';
  765. this->ReportWarning("aiTexture::achFormatHint should contain a file extension "
  766. "without a leading dot (format hint: %s).",szTemp);
  767. }
  768. }
  769. const char* sz = pTexture->achFormatHint;
  770. if (sz[0] >= 'A' && sz[0] <= 'Z' ||
  771. sz[1] >= 'A' && sz[1] <= 'Z' ||
  772. sz[2] >= 'A' && sz[2] <= 'Z' ||
  773. sz[3] >= 'A' && sz[3] <= 'Z')
  774. {
  775. this->ReportError("aiTexture::achFormatHint contains non-lowercase characters");
  776. }
  777. }
  778. // ------------------------------------------------------------------------------------------------
  779. void ValidateDSProcess::Validate( const aiAnimation* pAnimation,
  780. const aiNodeAnim* pNodeAnim)
  781. {
  782. Validate(&pNodeAnim->mNodeName);
  783. // otherwise check whether one of the keys exceeds the total duration of the animation
  784. if (pNodeAnim->mNumPositionKeys)
  785. {
  786. if (!pNodeAnim->mPositionKeys)
  787. {
  788. this->ReportError("aiNodeAnim::mPositionKeys is NULL (aiNodeAnim::mNumPositionKeys is %i)",
  789. pNodeAnim->mNumPositionKeys);
  790. }
  791. double dLast = -10e10;
  792. for (unsigned int i = 0; i < pNodeAnim->mNumPositionKeys;++i)
  793. {
  794. if (pNodeAnim->mPositionKeys[i].mTime > pAnimation->mDuration)
  795. {
  796. ReportError("aiNodeAnim::mPositionKeys[%i].mTime (%.5f) is larger "
  797. "than aiAnimation::mDuration (which is %.5f)",i,
  798. (float)pNodeAnim->mPositionKeys[i].mTime,
  799. (float)pAnimation->mDuration);
  800. }
  801. if (i && pNodeAnim->mPositionKeys[i].mTime <= dLast)
  802. {
  803. ReportWarning("aiNodeAnim::mPositionKeys[%i].mTime (%.5f) is smaller "
  804. "than aiAnimation::mPositionKeys[%i] (which is %.5f)",i,
  805. (float)pNodeAnim->mPositionKeys[i].mTime,
  806. i-1, (float)dLast);
  807. }
  808. dLast = pNodeAnim->mPositionKeys[i].mTime;
  809. }
  810. }
  811. // rotation keys
  812. if (pNodeAnim->mNumRotationKeys)
  813. {
  814. if (!pNodeAnim->mRotationKeys)
  815. {
  816. this->ReportError("aiNodeAnim::mRotationKeys is NULL (aiNodeAnim::mNumRotationKeys is %i)",
  817. pNodeAnim->mNumRotationKeys);
  818. }
  819. double dLast = -10e10;
  820. for (unsigned int i = 0; i < pNodeAnim->mNumRotationKeys;++i)
  821. {
  822. if (pNodeAnim->mRotationKeys[i].mTime > pAnimation->mDuration)
  823. {
  824. ReportError("aiNodeAnim::mRotationKeys[%i].mTime (%.5f) is larger "
  825. "than aiAnimation::mDuration (which is %.5f)",i,
  826. (float)pNodeAnim->mRotationKeys[i].mTime,
  827. (float)pAnimation->mDuration);
  828. }
  829. if (i && pNodeAnim->mRotationKeys[i].mTime <= dLast)
  830. {
  831. ReportWarning("aiNodeAnim::mRotationKeys[%i].mTime (%.5f) is smaller "
  832. "than aiAnimation::mRotationKeys[%i] (which is %.5f)",i,
  833. (float)pNodeAnim->mRotationKeys[i].mTime,
  834. i-1, (float)dLast);
  835. }
  836. dLast = pNodeAnim->mRotationKeys[i].mTime;
  837. }
  838. }
  839. // scaling keys
  840. if (pNodeAnim->mNumScalingKeys)
  841. {
  842. if (!pNodeAnim->mScalingKeys)
  843. {
  844. this->ReportError("aiNodeAnim::mScalingKeys is NULL (aiNodeAnim::mNumScalingKeys is %i)",
  845. pNodeAnim->mNumScalingKeys);
  846. }
  847. double dLast = -10e10;
  848. for (unsigned int i = 0; i < pNodeAnim->mNumScalingKeys;++i)
  849. {
  850. if (pNodeAnim->mScalingKeys[i].mTime > pAnimation->mDuration)
  851. {
  852. ReportError("aiNodeAnim::mScalingKeys[%i].mTime (%.5f) is larger "
  853. "than aiAnimation::mDuration (which is %.5f)",i,
  854. (float)pNodeAnim->mScalingKeys[i].mTime,
  855. (float)pAnimation->mDuration);
  856. }
  857. if (i && pNodeAnim->mScalingKeys[i].mTime <= dLast)
  858. {
  859. ReportWarning("aiNodeAnim::mScalingKeys[%i].mTime (%.5f) is smaller "
  860. "than aiAnimation::mScalingKeys[%i] (which is %.5f)",i,
  861. (float)pNodeAnim->mScalingKeys[i].mTime,
  862. i-1, (float)dLast);
  863. }
  864. dLast = pNodeAnim->mScalingKeys[i].mTime;
  865. }
  866. }
  867. if (!pNodeAnim->mNumScalingKeys && !pNodeAnim->mNumRotationKeys &&
  868. !pNodeAnim->mNumPositionKeys)
  869. {
  870. ReportError("A node animation channel must have at least one subtrack");
  871. }
  872. }
  873. // ------------------------------------------------------------------------------------------------
  874. void ValidateDSProcess::Validate( const aiNode* pNode)
  875. {
  876. if (!pNode)ReportError("A node of the scenegraph is NULL");
  877. if (pNode != mScene->mRootNode && !pNode->mParent)
  878. this->ReportError("A node has no valid parent (aiNode::mParent is NULL)");
  879. this->Validate(&pNode->mName);
  880. // validate all meshes
  881. if (pNode->mNumMeshes)
  882. {
  883. if (!pNode->mMeshes)
  884. {
  885. ReportError("aiNode::mMeshes is NULL (aiNode::mNumMeshes is %i)",
  886. pNode->mNumMeshes);
  887. }
  888. std::vector<bool> abHadMesh;
  889. abHadMesh.resize(mScene->mNumMeshes,false);
  890. for (unsigned int i = 0; i < pNode->mNumMeshes;++i)
  891. {
  892. if (pNode->mMeshes[i] >= mScene->mNumMeshes)
  893. {
  894. ReportError("aiNode::mMeshes[%i] is out of range (maximum is %i)",
  895. pNode->mMeshes[i],mScene->mNumMeshes-1);
  896. }
  897. if (abHadMesh[pNode->mMeshes[i]])
  898. {
  899. ReportError("aiNode::mMeshes[%i] is already referenced by this node (value: %i)",
  900. i,pNode->mMeshes[i]);
  901. }
  902. abHadMesh[pNode->mMeshes[i]] = true;
  903. }
  904. }
  905. if (pNode->mNumChildren)
  906. {
  907. if (!pNode->mChildren)
  908. {
  909. this->ReportError("aiNode::mChildren is NULL (aiNode::mNumChildren is %i)",
  910. pNode->mNumChildren);
  911. }
  912. for (unsigned int i = 0; i < pNode->mNumChildren;++i)
  913. {
  914. Validate(pNode->mChildren[i]);
  915. }
  916. }
  917. }
  918. // ------------------------------------------------------------------------------------------------
  919. void ValidateDSProcess::Validate( const aiString* pString)
  920. {
  921. if (pString->length > MAXLEN)
  922. {
  923. this->ReportError("aiString::length is too large (%i, maximum is %i)",
  924. pString->length,MAXLEN);
  925. }
  926. const char* sz = pString->data;
  927. while (true)
  928. {
  929. if ('\0' == *sz)
  930. {
  931. if (pString->length != (unsigned int)(sz-pString->data))
  932. ReportError("aiString::data is invalid: the terminal zero is at a wrong offset");
  933. break;
  934. }
  935. else if (sz >= &pString->data[MAXLEN])
  936. ReportError("aiString::data is invalid. There is no terminal character");
  937. ++sz;
  938. }
  939. }