colladaUtils.h 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _COLLADA_UTILS_H_
  23. #define _COLLADA_UTILS_H_
  24. #ifdef _MSC_VER
  25. #pragma warning(disable : 4786) // disable warning about long debug symbol names
  26. #pragma warning(disable : 4355) // disable "'this' : used in base member initializer list" warnings
  27. #endif
  28. #ifndef _MMATRIX_H_
  29. #include "math/mMatrix.h"
  30. #endif
  31. #ifndef _MQUAT_H_
  32. #include "math/mQuat.h"
  33. #endif
  34. #ifndef _TVECTOR_H_
  35. #include "core/util/tVector.h"
  36. #endif
  37. #ifndef _TSSHAPE_LOADER_H_
  38. #include "ts/loader/tsShapeLoader.h"
  39. #endif
  40. #ifndef _OPTIMIZEDPOLYLIST_H_
  41. #include "collision/optimizedPolyList.h"
  42. #endif
  43. #ifndef TINYXML_INCLUDED
  44. #include "tinyxml.h"
  45. #endif
  46. #ifndef _CONSOLE_H_
  47. #include "console/console.h"
  48. #endif
  49. #ifndef _TSSHAPEINSTANCE_H_
  50. #include "ts/tsShapeInstance.h"
  51. #endif
  52. #include "platform/tmm_off.h"
  53. #include "dae.h"
  54. #include "dae/daeErrorHandler.h"
  55. #include "dae/domAny.h"
  56. #include "dom/domProfile_COMMON.h"
  57. #include "dom/domMaterial.h"
  58. #include "dom/domGeometry.h"
  59. #include "dom/domMorph.h"
  60. #include "dom/domNode.h"
  61. #include "dom/domCOLLADA.h"
  62. #include "platform/tmm_on.h"
  63. #include "core/strings/findMatch.h"
  64. namespace ColladaUtils
  65. {
  66. struct ImportOptions
  67. {
  68. enum eLodType
  69. {
  70. DetectDTS = 0,
  71. SingleSize,
  72. TrailingNumber,
  73. NumLodTypes
  74. };
  75. enum eAnimTimingType
  76. {
  77. FrameCount = 0,
  78. Seconds = 1,
  79. Milliseconds = 1000
  80. };
  81. domUpAxisType upAxis; // Override for the collada <up_axis> element
  82. F32 unit; // Override for the collada <unit> element
  83. eLodType lodType; // LOD type option
  84. S32 singleDetailSize; // Detail size for all meshes in the model
  85. String matNamePrefix; // Prefix to apply to collada material names
  86. String alwaysImport; // List of node names (with wildcards) to import, even if in the neverImport list
  87. String neverImport; // List of node names (with wildcards) to ignore on loading
  88. String alwaysImportMesh; // List of mesh names (with wildcards) to import, even if in the neverImportMesh list
  89. String neverImportMesh; // List of mesh names (with wildcards) to ignore on loading
  90. String neverImportMat; // List of material names (with wildcards) to ignore on loading
  91. bool ignoreNodeScale; // Ignore <scale> elements in <node>s
  92. bool adjustCenter; // Translate model so origin is at the center
  93. bool adjustFloor; // Translate model so origin is at the bottom
  94. bool forceUpdateMaterials; // Force update of materials.cs
  95. bool useDiffuseNames; // Use diffuse texture as the material name
  96. // Assimp specific preprocess import options
  97. bool convertLeftHanded; // Convert to left handed coordinate system.
  98. bool calcTangentSpace; // Calculate tangents and bitangents, if possible.
  99. bool genUVCoords; // Convert spherical, cylindrical, box and planar mapping to proper UVs.
  100. bool transformUVCoords; // Preprocess UV transformations (scaling, translation ...)
  101. bool flipUVCoords; // This step flips all UV coordinates along the y-axis and adjusts material settings
  102. // and bitangents accordingly.\nAssimp uses TL(0,0):BR(1,1). T3D uses TL(0,1):BR(1,0).
  103. bool findInstances; // Search for instanced meshes and remove them by references to one master.
  104. bool limitBoneWeights; // Limit bone weights to 4 per vertex.
  105. bool joinIdenticalVerts; // Identifies and joins identical vertex data sets within all imported meshes.
  106. bool reverseWindingOrder; // This step adjusts the output face winding order to be clockwise. The default face winding order is counter clockwise.
  107. bool invertNormals; // Reverse the normal vector direction for all normals.
  108. bool removeRedundantMats; // Removes redundant materials.
  109. eAnimTimingType animTiming; // How to import timing data as frames, seconds or milliseconds
  110. S32 animFPS; // FPS value to use if timing is set in frames and the animations does not have an fps set
  111. ImportOptions()
  112. {
  113. reset();
  114. }
  115. void reset()
  116. {
  117. upAxis = UPAXISTYPE_COUNT;
  118. unit = -1.0f;
  119. lodType = TrailingNumber;
  120. singleDetailSize = 2;
  121. matNamePrefix = "";
  122. alwaysImport = "";
  123. neverImport = String(Con::getVariable("$TSShapeConstructor::neverImport"));
  124. alwaysImportMesh = "";
  125. neverImportMesh = String(Con::getVariable("$TSShapeConstructor::neverImportMesh"));
  126. neverImportMat = String(Con::getVariable("$TSShapeConstructor::neverImportMat"));
  127. ignoreNodeScale = false;
  128. adjustCenter = false;
  129. adjustFloor = false;
  130. forceUpdateMaterials = false;
  131. useDiffuseNames = false;
  132. convertLeftHanded = false;
  133. calcTangentSpace = false;
  134. genUVCoords = false;
  135. transformUVCoords = false;
  136. flipUVCoords = true;
  137. findInstances = false;
  138. limitBoneWeights = false;
  139. joinIdenticalVerts = true;
  140. reverseWindingOrder = true;
  141. invertNormals = false;
  142. removeRedundantMats = true;
  143. animTiming = Seconds;
  144. animFPS = 30;
  145. }
  146. };
  147. ImportOptions& getOptions();
  148. struct ExportData
  149. {
  150. struct detailLevel
  151. {
  152. OptimizedPolyList mesh;
  153. S32 size;
  154. Map<int, int> materialRefList;
  155. };
  156. struct meshLODData
  157. {
  158. Vector<detailLevel> meshDetailLevels;
  159. TSShapeInstance* shapeInst;
  160. MatrixF meshTransform;
  161. SceneObject* originatingObject;
  162. Point3F scale;
  163. S32 hasDetailLevel(S32 size)
  164. {
  165. for (U32 i = 0; i < meshDetailLevels.size(); ++i)
  166. {
  167. U32 mdlSize = meshDetailLevels[i].size;
  168. if (mdlSize == size)
  169. return i;
  170. }
  171. return -1;
  172. }
  173. meshLODData() : shapeInst(nullptr), meshTransform(true), originatingObject(nullptr), scale(0)
  174. {}
  175. };
  176. struct colMesh
  177. {
  178. OptimizedPolyList mesh;
  179. String colMeshName;
  180. };
  181. Vector<detailLevel> detailLevels;
  182. Vector<meshLODData> meshData;
  183. Vector<colMesh> colMeshes;
  184. Vector<BaseMatInstance*> materials;
  185. void processData();
  186. S32 hasDetailLevel(U32 dl)
  187. {
  188. for (U32 i = 0; i < detailLevels.size(); i++)
  189. {
  190. if (detailLevels[i].size == dl)
  191. return i;
  192. }
  193. return -1;
  194. }
  195. S32 hasMaterialInstance(BaseMatInstance* matInst)
  196. {
  197. for (U32 i = 0; i < materials.size(); i++)
  198. {
  199. if (materials[i] == matInst)
  200. return i;
  201. }
  202. return -1;
  203. }
  204. S32 numberOfDetailLevels()
  205. {
  206. Vector<S32> detailLevelIdxs;
  207. for (U32 i = 0; i < meshData.size(); ++i)
  208. {
  209. for (U32 d = 0; d < meshData[i].meshDetailLevels.size(); ++d)
  210. {
  211. detailLevelIdxs.push_back_unique(meshData[i].meshDetailLevels[d].size);
  212. }
  213. }
  214. return detailLevelIdxs.size();
  215. }
  216. static S32 _Sort(const S32 *p1, const S32 *p2)
  217. {
  218. S32 e1 = (*p1);
  219. S32 e2 = (*p2);
  220. if (e1 > e2)
  221. return 1;
  222. else if (e1 < e2)
  223. return -1;
  224. return 0;
  225. }
  226. S32 getDetailLevelSize(U32 detailIdx)
  227. {
  228. Vector<S32> detailLevelIdxs;
  229. for (U32 i = 0; i < meshData.size(); ++i)
  230. {
  231. for (U32 d = 0; d < meshData[i].meshDetailLevels.size(); ++d)
  232. {
  233. S32 mdlSize = meshData[i].meshDetailLevels[d].size;
  234. detailLevelIdxs.push_back_unique(mdlSize);
  235. }
  236. }
  237. if (detailIdx >= detailLevelIdxs.size())
  238. return -1;
  239. detailLevelIdxs.sort(&_Sort);
  240. return detailLevelIdxs[detailIdx];
  241. }
  242. };
  243. void convertTransform(MatrixF& m);
  244. void collapsePath(std::string& path);
  245. // Apply the set of Collada conditioners (suited for loading Collada models into Torque)
  246. void applyConditioners(domCOLLADA* root);
  247. const domProfile_COMMON* findEffectCommonProfile(const domEffect* effect);
  248. const domCommon_color_or_texture_type_complexType* findEffectDiffuse(const domEffect* effect);
  249. const domCommon_color_or_texture_type_complexType* findEffectSpecular(const domEffect* effect);
  250. const domFx_sampler2D_common_complexType* getTextureSampler(const domEffect* effect, const domCommon_color_or_texture_type_complexType* texture);
  251. String getSamplerImagePath(const domEffect* effect, const domFx_sampler2D_common_complexType* sampler2D);
  252. String resolveImagePath(const domImage* image);
  253. // Collada export helper functions
  254. Torque::Path findTexture(const Torque::Path& diffuseMap);
  255. void exportColladaHeader(TiXmlElement* rootNode);
  256. void exportColladaMaterials(TiXmlElement* rootNode, const OptimizedPolyList& mesh, Vector<String>& matNames, const Torque::Path& colladaFile);
  257. void exportColladaTriangles(TiXmlElement* meshNode, const OptimizedPolyList& mesh, const String& meshName, const Vector<String>& matNames);
  258. void exportColladaMesh(TiXmlElement* rootNode, const OptimizedPolyList& mesh, const String& meshName, const Vector<String>& matNames);
  259. void exportColladaScene(TiXmlElement* rootNode, const String& meshName, const Vector<String>& matNames);
  260. void exportColladaMaterials(TiXmlElement* rootNode, const ExportData& exportData, const Torque::Path& colladaFile);
  261. void exportColladaMesh(TiXmlElement* rootNode, const ExportData& exportData, const String& meshName);
  262. void exportColladaCollisionTriangles(TiXmlElement* meshNode, const ExportData& exportData, const U32 collisionIdx);
  263. void exportColladaTriangles(TiXmlElement* meshNode, const ExportData& exportData, const U32 detailLevel, const String& meshName);
  264. void exportColladaScene(TiXmlElement* rootNode, const ExportData& exportData, const String& meshName);
  265. // Export an OptimizedPolyList to a simple Collada file
  266. void exportToCollada(const Torque::Path& colladaFile, const OptimizedPolyList& mesh, const String& meshName = String::EmptyString);
  267. void exportToCollada(const Torque::Path& colladaFile, const ExportData& exportData);
  268. };
  269. //-----------------------------------------------------------------------------
  270. // Helper Classes
  271. //
  272. // The Collada DOM uses a different class for each XML element, and there is very
  273. // little class inheritance, even though many elements have the same attributes
  274. // and children. This makes the DOM a bit ugly to work with, and the following
  275. // templates attempt to make this situation a bit nicer by providing a common way
  276. // to access common elements, while retaining the strong typing of the DOM classes.
  277. //-----------------------------------------------------------------------------
  278. /// Convert from the Collada transform types to a Torque MatrixF
  279. template<class T> inline MatrixF vecToMatrixF(const domListOfFloats& vec) { return MatrixF(true); }
  280. /// Collada <translate>: [x_translate, y_translate, z_translate]
  281. template<> inline MatrixF vecToMatrixF<domTranslate>(const domListOfFloats& vec)
  282. {
  283. MatrixF mat(true);
  284. mat.setPosition(Point3F(vec[0], vec[1], vec[2]));
  285. return mat;
  286. }
  287. /// Collada <scale>: [x_scale, y_scale, z_scale]
  288. template<> inline MatrixF vecToMatrixF<domScale>(const domListOfFloats& vec)
  289. {
  290. MatrixF mat(true);
  291. mat.scale(Point3F(vec[0], vec[1], vec[2]));
  292. return mat;
  293. }
  294. /// Collada <rotate>: [rotation_axis, angle_in_degrees]
  295. template<> inline MatrixF vecToMatrixF<domRotate>(const domListOfFloats& vec)
  296. {
  297. AngAxisF aaxis(Point3F(vec[0], vec[1], vec[2]), -(vec[3] * M_PI) / 180.0f);
  298. MatrixF mat(true);
  299. aaxis.setMatrix(&mat);
  300. return mat;
  301. }
  302. /// Collada <matrix>: same form as TGE (woohoo!)
  303. template<> inline MatrixF vecToMatrixF<domMatrix>(const domListOfFloats& vec)
  304. {
  305. MatrixF mat;
  306. for (S32 i = 0; i < 16; i++)
  307. mat[i] = vec[i];
  308. return mat;
  309. }
  310. /// Collada <skew>: [angle_in_degrees, rotation_axis, translation_axis]
  311. /// skew transform code adapted from GMANMatrix4 implementation
  312. template<> inline MatrixF vecToMatrixF<domSkew>(const domListOfFloats& vec)
  313. {
  314. F32 angle = -(vec[0] * M_PI) / 180.0f;
  315. Point3F rotAxis(vec[1], vec[2], vec[3]);
  316. Point3F transAxis(vec[4], vec[5], vec[6]);
  317. transAxis.normalize();
  318. Point3F a1 = transAxis * mDot(rotAxis, transAxis);
  319. Point3F a2 = rotAxis - a1;
  320. a2.normalize();
  321. F32 an1 = mDot(rotAxis, a2);
  322. F32 an2 = mDot(rotAxis, transAxis);
  323. F32 rx = an1 * mCos(angle) - an2 * mSin(angle);
  324. F32 ry = an1 * mSin(angle) + an2 * mCos(angle);
  325. // Check for rotation parallel to translation
  326. F32 alpha = (an1 == 0) ? 0 : (ry/rx - an2/an1);
  327. MatrixF mat(true);
  328. mat(0,0) = a2.x * transAxis.x * alpha + 1.0;
  329. mat(1,0) = a2.y * transAxis.x * alpha;
  330. mat(2,0) = a2.z * transAxis.x * alpha;
  331. mat(0,1) = a2.x * transAxis.y * alpha;
  332. mat(1,1) = a2.y * transAxis.y * alpha + 1.0;
  333. mat(2,1) = a2.z * transAxis.y * alpha;
  334. mat(0,2) = a2.x * transAxis.z * alpha;
  335. mat(1,2) = a2.y * transAxis.z * alpha;
  336. mat(2,2) = a2.z * transAxis.z * alpha + 1.0;
  337. return mat;
  338. }
  339. /// Collada <lookat>: [eye, target, up]
  340. template<> inline MatrixF vecToMatrixF<domLookat>(const domListOfFloats& vec)
  341. {
  342. Point3F eye(vec[0], vec[1], vec[2]);
  343. Point3F target(vec[3], vec[4], vec[5]);
  344. Point3F up(vec[6], vec[7], vec[8]);
  345. Point3F fwd = target - eye;
  346. fwd.normalizeSafe();
  347. Point3F right = mCross(fwd, up);
  348. right.normalizeSafe();
  349. up = mCross(right, fwd);
  350. up.normalizeSafe();
  351. MatrixF mat(true);
  352. mat.setColumn(0, right);
  353. mat.setColumn(1, fwd);
  354. mat.setColumn(2, up);
  355. mat.setColumn(3, eye);
  356. return mat;
  357. }
  358. //-----------------------------------------------------------------------------
  359. /// Try to get a name for the element using the following attributes (in order):
  360. /// name, sid, id, "null"
  361. template<class T> inline const char* _GetNameOrId(const T* element)
  362. {
  363. return element ? (element->getName() ? element->getName() : (element->getId() ? element->getId() : "null")) : "null";
  364. }
  365. template<> inline const char* _GetNameOrId(const domInstance_geometry* element)
  366. {
  367. return element ? (element->getName() ? element->getName() : (element->getSid() ? element->getSid() : "null")) : "null";
  368. }
  369. template<> inline const char* _GetNameOrId(const domInstance_controller* element)
  370. {
  371. return element ? (element->getName() ? element->getName() : (element->getSid() ? element->getSid() : "null")) : "null";
  372. }
  373. //-----------------------------------------------------------------------------
  374. // Collada <source>s are extremely flexible, and thus difficult to access in a nice
  375. // way. This class attempts to provide a clean interface to convert Collada source
  376. // data to the appropriate Torque data structure without losing any of the flexibility
  377. // of the underlying Collada DOM.
  378. //
  379. // Some of the conversions we need to handle are:
  380. // - daeString to const char*
  381. // - daeIDRef to const char*
  382. // - double to F32
  383. // - double to Point2F
  384. // - double to Point3F
  385. // - double to MatrixF
  386. //
  387. // The _SourceReader object is initialized with a list of parameter names that it
  388. // tries to match to <param> elements in the source accessor to figure out how to
  389. // pull values out of the 1D source array. Note that no type checking of any kind
  390. // is done until we actually try to extract values from the source.
  391. class _SourceReader
  392. {
  393. const domSource* source; // the wrapped Collada source
  394. const domAccessor* accessor; // shortcut to the source accessor
  395. Vector<U32> offsets; // offset of each of the desired values to pull from the source array
  396. public:
  397. _SourceReader() : source(0), accessor(0) {}
  398. void reset()
  399. {
  400. source = 0;
  401. accessor = 0;
  402. offsets.clear();
  403. }
  404. //------------------------------------------------------
  405. // Initialize the _SourceReader object
  406. bool initFromSource(const domSource* src, const char* paramNames[] = 0)
  407. {
  408. source = src;
  409. accessor = source->getTechnique_common()->getAccessor();
  410. offsets.clear();
  411. // The source array has groups of values in a 1D stream => need to map the
  412. // input param names to source params to determine the offset within the
  413. // group for each desired value
  414. U32 paramCount = 0;
  415. while (paramNames && paramNames[paramCount][0]) {
  416. // lookup the index of the source param that matches the input param
  417. offsets.push_back(paramCount);
  418. for (U32 iParam = 0; iParam < accessor->getParam_array().getCount(); iParam++) {
  419. if (accessor->getParam_array()[iParam]->getName() &&
  420. dStrEqual(accessor->getParam_array()[iParam]->getName(), paramNames[paramCount])) {
  421. offsets.last() = iParam;
  422. break;
  423. }
  424. }
  425. paramCount++;
  426. }
  427. // If no input params were specified, just map the source params directly
  428. if (!offsets.size()) {
  429. for (S32 iParam = 0; iParam < accessor->getParam_array().getCount(); iParam++)
  430. offsets.push_back(iParam);
  431. }
  432. return true;
  433. }
  434. //------------------------------------------------------
  435. // Shortcut to the size of the array (should be the number of destination objects)
  436. S32 size() const { return accessor ? accessor->getCount() : 0; }
  437. // Get the number of elements per group in the source
  438. S32 stride() const { return accessor ? accessor->getStride() : 0; }
  439. //------------------------------------------------------
  440. // Get a pointer to the start of a group of values (index advances by stride)
  441. //template<class T> T getArrayData(S32 index) const { return 0; }
  442. const double* getStringArrayData(S32 index) const
  443. {
  444. if ((index >= 0) && (index < size())) {
  445. if (source->getFloat_array())
  446. return &source->getFloat_array()->getValue()[index*stride()];
  447. }
  448. return 0;
  449. }
  450. //------------------------------------------------------
  451. // Read a single value from the source array
  452. //template<class T> T getValue(S32 index) const { return T; }
  453. const char* getStringValue(S32 index) const
  454. {
  455. if ((index >= 0) && (index < size())) {
  456. // could be plain strings or IDREFs
  457. if (source->getName_array())
  458. return source->getName_array()->getValue()[index*stride()];
  459. else if (source->getIDREF_array())
  460. return source->getIDREF_array()->getValue()[index*stride()].getID();
  461. }
  462. return "";
  463. }
  464. F32 getFloatValue(S32 index) const
  465. {
  466. F32 value(0);
  467. if (const double* data = getStringArrayData(index))
  468. return data[offsets[0]];
  469. return value;
  470. }
  471. Point2F getPoint2FValue(S32 index) const
  472. {
  473. Point2F value(0, 0);
  474. if (const double* data = getStringArrayData(index))
  475. value.set(data[offsets[0]], data[offsets[1]]);
  476. return value;
  477. }
  478. Point3F getPoint3FValue(S32 index) const
  479. {
  480. Point3F value(1, 0, 0);
  481. if (const double* data = getStringArrayData(index))
  482. value.set(data[offsets[0]], data[offsets[1]], data[offsets[2]]);
  483. return value;
  484. }
  485. ColorI getColorIValue(S32 index) const
  486. {
  487. ColorI value(255, 255, 255, 255);
  488. if (const double* data = getStringArrayData(index))
  489. {
  490. value.red = data[offsets[0]] * 255.0;
  491. value.green = data[offsets[1]] * 255.0;
  492. value.blue = data[offsets[2]] * 255.0;
  493. if ( stride() == 4 )
  494. value.alpha = data[offsets[3]] * 255.0;
  495. }
  496. return value;
  497. }
  498. MatrixF getMatrixFValue(S32 index) const
  499. {
  500. MatrixF value(true);
  501. if (const double* data = getStringArrayData(index)) {
  502. for (S32 i = 0; i < 16; i++)
  503. value[i] = data[i];
  504. }
  505. return value;
  506. }
  507. };
  508. //-----------------------------------------------------------------------------
  509. // Collada geometric primitives: Use the BasePrimitive class to access the
  510. // different primitive types in a nice way.
  511. class BasePrimitive
  512. {
  513. public:
  514. virtual ~BasePrimitive() { }
  515. /// Return true if the element is a geometric primitive type
  516. static bool isPrimitive(const daeElement* element)
  517. {
  518. switch (element->getElementType()) {
  519. case COLLADA_TYPE::TRIANGLES: case COLLADA_TYPE::POLYLIST:
  520. case COLLADA_TYPE::POLYGONS: case COLLADA_TYPE::TRIFANS:
  521. case COLLADA_TYPE::TRISTRIPS: case COLLADA_TYPE::CAPSULE:
  522. case COLLADA_TYPE::CYLINDER: case COLLADA_TYPE::LINES:
  523. case COLLADA_TYPE::LINESTRIPS: case COLLADA_TYPE::PLANE:
  524. case COLLADA_TYPE::SPLINE: case COLLADA_TYPE::SPHERE:
  525. case COLLADA_TYPE::TAPERED_CAPSULE: case COLLADA_TYPE::TAPERED_CYLINDER:
  526. return true;
  527. }
  528. return false;
  529. }
  530. /// Return true if the element is a supported primitive type
  531. static bool isSupportedPrimitive(const daeElement* element)
  532. {
  533. switch (element->getElementType()) {
  534. case COLLADA_TYPE::TRIANGLES:
  535. case COLLADA_TYPE::TRISTRIPS:
  536. case COLLADA_TYPE::TRIFANS:
  537. case COLLADA_TYPE::POLYLIST:
  538. case COLLADA_TYPE::POLYGONS:
  539. return true;
  540. }
  541. return false;
  542. }
  543. /// Construct a child class based on the type of Collada element
  544. static BasePrimitive* get(const daeElement* element);
  545. /// Methods to be implemented for each supported Collada geometric element
  546. virtual const char* getElementName() = 0;
  547. virtual const char* getMaterial() = 0;
  548. virtual const domInputLocalOffset_Array& getInputs() = 0;
  549. virtual S32 getStride() const = 0;
  550. virtual const domListOfUInts *getTriangleData() = 0;
  551. };
  552. /// Template child class for supported Collada primitive elements
  553. template<class T> class ColladaPrimitive : public BasePrimitive
  554. {
  555. T* primitive;
  556. domListOfUInts *pTriangleData;
  557. S32 stride;
  558. public:
  559. ColladaPrimitive(const daeElement* e) : pTriangleData(0)
  560. {
  561. // Cast to geometric primitive element
  562. primitive = daeSafeCast<T>(const_cast<daeElement*>(e));
  563. // Determine stride
  564. stride = 0;
  565. for (S32 iInput = 0; iInput < getInputs().getCount(); iInput++) {
  566. if (getInputs()[iInput]->getOffset() >= stride)
  567. stride = getInputs()[iInput]->getOffset() + 1;
  568. }
  569. }
  570. ~ColladaPrimitive()
  571. {
  572. delete pTriangleData;
  573. }
  574. /// Most primitives can use these common implementations
  575. const char* getElementName() { return primitive->getElementName(); }
  576. const char* getMaterial() { return (FindMatch::isMatchMultipleExprs(ColladaUtils::getOptions().neverImportMat, primitive->getMaterial(), false)) ? NULL : primitive->getMaterial(); }
  577. const domInputLocalOffset_Array& getInputs() { return primitive->getInput_array(); }
  578. S32 getStride() const { return stride; }
  579. /// Each supported primitive needs to implement this method (and convert
  580. /// to triangles if required)
  581. const domListOfUInts *getTriangleData() { return NULL; }
  582. };
  583. //-----------------------------------------------------------------------------
  584. // <triangles>
  585. template<> inline const domListOfUInts *ColladaPrimitive<domTriangles>::getTriangleData()
  586. {
  587. // Return the <p> integer list directly
  588. return (primitive->getP() ? &(primitive->getP()->getValue()) : NULL);
  589. }
  590. //-----------------------------------------------------------------------------
  591. // <tristrips>
  592. template<> inline const domListOfUInts *ColladaPrimitive<domTristrips>::getTriangleData()
  593. {
  594. if (!pTriangleData)
  595. {
  596. // Convert strips to triangles
  597. pTriangleData = new domListOfUInts();
  598. for (S32 iStrip = 0; iStrip < primitive->getCount(); iStrip++) {
  599. domP* P = primitive->getP_array()[iStrip];
  600. // Ignore invalid P arrays
  601. if (!P || !P->getValue().getCount())
  602. continue;
  603. domUint* pSrcData = &(P->getValue()[0]);
  604. size_t numTriangles = (P->getValue().getCount() / stride) - 2;
  605. // Convert the strip back to a triangle list
  606. domUint* v0 = pSrcData;
  607. for (S32 iTri = 0; iTri < numTriangles; iTri++, v0 += stride) {
  608. if (iTri & 0x1)
  609. {
  610. // CW triangle
  611. pTriangleData->appendArray(stride, v0);
  612. pTriangleData->appendArray(stride, v0 + 2*stride);
  613. pTriangleData->appendArray(stride, v0 + stride);
  614. }
  615. else
  616. {
  617. // CCW triangle
  618. pTriangleData->appendArray(stride*3, v0);
  619. }
  620. }
  621. }
  622. }
  623. return pTriangleData;
  624. }
  625. //-----------------------------------------------------------------------------
  626. // <trifans>
  627. template<> inline const domListOfUInts *ColladaPrimitive<domTrifans>::getTriangleData()
  628. {
  629. if (!pTriangleData)
  630. {
  631. // Convert strips to triangles
  632. pTriangleData = new domListOfUInts();
  633. for (S32 iStrip = 0; iStrip < primitive->getCount(); iStrip++) {
  634. domP* P = primitive->getP_array()[iStrip];
  635. // Ignore invalid P arrays
  636. if (!P || !P->getValue().getCount())
  637. continue;
  638. domUint* pSrcData = &(P->getValue()[0]);
  639. size_t numTriangles = (P->getValue().getCount() / stride) - 2;
  640. // Convert the fan back to a triangle list
  641. domUint* v0 = pSrcData + stride;
  642. for (S32 iTri = 0; iTri < numTriangles; iTri++, v0 += stride) {
  643. pTriangleData->appendArray(stride, pSrcData); // shared vertex
  644. pTriangleData->appendArray(stride, v0); // previous vertex
  645. pTriangleData->appendArray(stride, v0+stride); // current vertex
  646. }
  647. }
  648. }
  649. return pTriangleData;
  650. }
  651. //-----------------------------------------------------------------------------
  652. // <polygons>
  653. template<> inline const domListOfUInts *ColladaPrimitive<domPolygons>::getTriangleData()
  654. {
  655. if (!pTriangleData)
  656. {
  657. // Convert polygons to triangles
  658. pTriangleData = new domListOfUInts();
  659. for (S32 iPoly = 0; iPoly < primitive->getCount(); iPoly++) {
  660. domP* P = primitive->getP_array()[iPoly];
  661. // Ignore invalid P arrays
  662. if (!P || !P->getValue().getCount())
  663. continue;
  664. domUint* pSrcData = &(P->getValue()[0]);
  665. size_t numPoints = P->getValue().getCount() / stride;
  666. // Use a simple tri-fan (centered at the first point) method of
  667. // converting the polygon to triangles.
  668. domUint* v0 = pSrcData;
  669. pSrcData += stride;
  670. for (S32 iTri = 0; iTri < numPoints-2; iTri++) {
  671. pTriangleData->appendArray(stride, v0);
  672. pTriangleData->appendArray(stride*2, pSrcData);
  673. pSrcData += stride;
  674. }
  675. }
  676. }
  677. return pTriangleData;
  678. }
  679. //-----------------------------------------------------------------------------
  680. // <polylist>
  681. template<> inline const domListOfUInts *ColladaPrimitive<domPolylist>::getTriangleData()
  682. {
  683. if (!pTriangleData)
  684. {
  685. // Convert polygons to triangles
  686. pTriangleData = new domListOfUInts();
  687. // Check that the P element has the right number of values (this
  688. // has been seen with certain models exported using COLLADAMax)
  689. const domListOfUInts& vcount = primitive->getVcount()->getValue();
  690. U32 expectedCount = 0;
  691. for (S32 iPoly = 0; iPoly < vcount.getCount(); iPoly++)
  692. expectedCount += vcount[iPoly];
  693. expectedCount *= stride;
  694. if (!primitive->getP() || !primitive->getP()->getValue().getCount() ||
  695. (primitive->getP()->getValue().getCount() != expectedCount) )
  696. {
  697. Con::warnf("<polylist> element found with invalid <p> array. This primitive will be ignored.");
  698. return pTriangleData;
  699. }
  700. domUint* pSrcData = &(primitive->getP()->getValue()[0]);
  701. for (S32 iPoly = 0; iPoly < vcount.getCount(); iPoly++) {
  702. // Use a simple tri-fan (centered at the first point) method of
  703. // converting the polygon to triangles.
  704. domUint* v0 = pSrcData;
  705. pSrcData += stride;
  706. for (S32 iTri = 0; iTri < vcount[iPoly]-2; iTri++) {
  707. pTriangleData->appendArray(stride, v0);
  708. pTriangleData->appendArray(stride*2, pSrcData);
  709. pSrcData += stride;
  710. }
  711. pSrcData += stride;
  712. }
  713. }
  714. return pTriangleData;
  715. }
  716. //-----------------------------------------------------------------------------
  717. /// Convert a custom parameter string to a particular type
  718. template<typename T> inline T convert(const char* value) { return value; }
  719. template<> inline bool convert(const char* value) { return dAtob(value); }
  720. template<> inline S32 convert(const char* value) { return dAtoi(value); }
  721. template<> inline F64 convert(const char* value) { return dAtof(value); }
  722. template<> inline F32 convert(const char* value) { return convert<double>(value); }
  723. //-----------------------------------------------------------------------------
  724. /// Collada animation data
  725. struct AnimChannels : public Vector<struct AnimData*>
  726. {
  727. daeElement *element;
  728. AnimChannels(daeElement* el) : element(el)
  729. {
  730. element->setUserData(this);
  731. }
  732. ~AnimChannels()
  733. {
  734. if (element)
  735. element->setUserData(0);
  736. }
  737. };
  738. struct AnimData
  739. {
  740. bool enabled; ///!< Used to select animation channels for the current clip
  741. _SourceReader input;
  742. _SourceReader output;
  743. _SourceReader inTangent;
  744. _SourceReader outTangent;
  745. _SourceReader interpolation;
  746. U32 targetValueOffset; ///< Offset into the target element (for arrays of values)
  747. U32 targetValueCount; ///< Number of values animated (from OUTPUT source array)
  748. /// Get the animation channels for the Collada element (if any)
  749. static AnimChannels* getAnimChannels(const daeElement* element)
  750. {
  751. return element ? (AnimChannels*)const_cast<daeElement*>(element)->getUserData() : 0;
  752. }
  753. AnimData() : enabled(false) { }
  754. void parseTargetString(const char* target, S32 fullCount, const char* elements[]);
  755. F32 invertParamCubic(F32 param, F32 x0, F32 x1, F32 x2, F32 x3) const;
  756. void interpValue(F32 t, U32 offset, double* value) const;
  757. void interpValue(F32 t, U32 offset, const char** value) const;
  758. };
  759. //-----------------------------------------------------------------------------
  760. // Collada allows any element with an SID or ID attribute to be the target of
  761. // an animation channel, which is very flexible, but awkward to work with. Some
  762. // examples of animated values are:
  763. // - single float
  764. // - single int
  765. // - single bool
  766. // - single string
  767. // - list of floats (transform elements or morph weights)
  768. //
  769. // This class provides a generic way to check if an element is animated, and
  770. // to get the value of the element at a given time.
  771. template<class T>
  772. struct AnimatedElement
  773. {
  774. const daeElement* element; ///< The Collada element (can be NULL)
  775. T defaultVal; ///< Default value (used when element is NULL)
  776. AnimatedElement(const daeElement* e=0) : element(e) { }
  777. /// Check if the element has any animations channels
  778. bool isAnimated() { return (AnimData::getAnimChannels(element) != 0); }
  779. bool isAnimated(F32 start, F32 end) { return isAnimated(); }
  780. /// Get the value of the element at the specified time
  781. T getValue(F32 time)
  782. {
  783. // If the element is NULL, just use the default (handy for <extra> profiles which
  784. // may or may not be present in the document)
  785. T value(defaultVal);
  786. if (const domAny* param = daeSafeCast<domAny>(const_cast<daeElement*>(element))) {
  787. // If the element is not animated, just use its current value
  788. value = convert<T>(param->getValue());
  789. // Animate the value
  790. const AnimChannels* channels = AnimData::getAnimChannels(element);
  791. if (channels && (time >= 0)) {
  792. for (S32 iChannel = 0; iChannel < channels->size(); iChannel++) {
  793. const AnimData* animData = (*channels)[iChannel];
  794. if (animData->enabled)
  795. animData->interpValue(time, 0, &value);
  796. }
  797. }
  798. }
  799. return value;
  800. }
  801. };
  802. template<class T> struct AnimatedElementList : public AnimatedElement<T>
  803. {
  804. AnimatedElementList(const daeElement* e=0) : AnimatedElement<T>(e) { }
  805. // @todo: Disable morph animations for now since they are not supported by T3D
  806. bool isAnimated() { return false; }
  807. bool isAnimated(F32 start, F32 end) { return false; }
  808. // Get the value of the element list at the specified time
  809. T getValue(F32 time)
  810. {
  811. T vec(this->defaultVal);
  812. if (this->element) {
  813. // Get a copy of the vector
  814. vec = *(T*)const_cast<daeElement*>(this->element)->getValuePointer();
  815. // Animate the vector
  816. const AnimChannels* channels = AnimData::getAnimChannels(this->element);
  817. if (channels && (time >= 0)) {
  818. for (S32 iChannel = 0; iChannel < channels->size(); iChannel++) {
  819. const AnimData* animData = (*channels)[iChannel];
  820. if (animData->enabled) {
  821. for (S32 iValue = 0; iValue < animData->targetValueCount; iValue++)
  822. animData->interpValue(time, iValue, &vec[animData->targetValueOffset + iValue]);
  823. }
  824. }
  825. }
  826. }
  827. return vec;
  828. }
  829. };
  830. // Strongly typed animated values
  831. typedef AnimatedElement<double> AnimatedFloat;
  832. typedef AnimatedElement<bool> AnimatedBool;
  833. typedef AnimatedElement<S32> AnimatedInt;
  834. typedef AnimatedElement<const char*> AnimatedString;
  835. typedef AnimatedElementList<domListOfFloats> AnimatedFloatList;
  836. #endif // _COLLADA_UTILS_H_