colladaAppMesh.cpp 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099
  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. #include "platform/platform.h"
  23. // Make GCC happy. Needs to have seen this before processing the
  24. // hash table template.
  25. struct VertTuple;
  26. namespace DictHash
  27. {
  28. inline U32 hash( const VertTuple& data );
  29. }
  30. #include "ts/collada/colladaExtensions.h"
  31. #include "ts/collada/colladaAppMesh.h"
  32. #include "ts/collada/colladaAppNode.h"
  33. #include "ts/collada/colladaAppMaterial.h"
  34. #include "core/util/tDictionary.h"
  35. #include "core/stringTable.h"
  36. using namespace ColladaUtils;
  37. bool ColladaAppMesh::fixedSizeEnabled = false;
  38. S32 ColladaAppMesh::fixedSize = 2;
  39. //-----------------------------------------------------------------------------
  40. // Define a VertTuple dictionary to allow fast tuple lookups
  41. namespace DictHash
  42. {
  43. inline U32 hash(const VertTuple& data)
  44. {
  45. return (U32)data.vertex;
  46. }
  47. }
  48. typedef Map<VertTuple, S32> VertTupleMap;
  49. //-----------------------------------------------------------------------------
  50. // Find a source with matching ID. Cannot use the DOM .getElement method since
  51. // some lame Collada exporters generate <source>s with non-unique IDs.
  52. daeElement* findInputSource(const daeElement* input)
  53. {
  54. // Try using the DOM .getElement method => the resolved element's parent
  55. // should be the input's grandparent
  56. daeElement* parent = ((daeElement*)input)->getParentElement();
  57. daeElement* grandparent = parent ? parent->getParentElement() : 0;
  58. if (!grandparent)
  59. return NULL;
  60. const domURIFragmentType* uri = 0;
  61. if (input->getElementType() == COLLADA_TYPE::INPUTLOCAL)
  62. uri = &daeSafeCast<domInputLocal>((daeElement*)input)->getSource();
  63. else if (input->getElementType() == COLLADA_TYPE::INPUTLOCALOFFSET)
  64. uri = &daeSafeCast<domInputLocalOffset>((daeElement*)input)->getSource();
  65. if (!uri)
  66. return NULL;
  67. daeElement* element = uri->getElement();
  68. if (element && element->getParentElement() == grandparent)
  69. return element;
  70. else
  71. {
  72. // Probably a non-unique ID => search for the matching element manually
  73. // Skip the leading '#' on source IDs
  74. const char* id = uri->originalStr().c_str();
  75. if (id && (id[0] == '#'))
  76. id++;
  77. for (S32 iChild = 0; iChild < grandparent->getChildren().getCount(); iChild++)
  78. {
  79. element = grandparent->getChildren()[iChild];
  80. if ((element->getElementType() != COLLADA_TYPE::SOURCE) &&
  81. (element->getElementType() != COLLADA_TYPE::VERTICES))
  82. continue;
  83. if (dStrEqual(id, element->getAttribute("id").c_str()))
  84. return element;
  85. }
  86. }
  87. return NULL;
  88. }
  89. //-----------------------------------------------------------------------------
  90. // Collada scatters the data required for geometry all over the place; this class
  91. // helps to group it all together.
  92. class MeshStreams
  93. {
  94. public:
  95. // The sources we want to read from the mesh stream. Can be any order, but
  96. // sources of the same type (eg. UVs and UV2s) must be sequential (to allow
  97. // ordering by set index)
  98. enum eSourceType {
  99. Points,
  100. Normals,
  101. Colors,
  102. UVs,
  103. UV2s,
  104. Joints,
  105. Weights,
  106. InvBindMatrices,
  107. NumStreams
  108. };
  109. const char* SourceTypeToSemantic(eSourceType type)
  110. {
  111. switch ( type )
  112. {
  113. case Points: return "POSITION";
  114. case Normals: return "NORMAL";
  115. case Colors: return "COLOR";
  116. case UVs:
  117. case UV2s: return "TEXCOORD";
  118. case Joints: return "JOINT";
  119. case Weights: return "WEIGHT";
  120. case InvBindMatrices: return "INV_BIND_MATRIX";
  121. default: return "";
  122. }
  123. }
  124. private:
  125. /// Classify a single input
  126. template<class T>
  127. static void selectInput(T input, T sortedInputs[], S32 start, S32 end=-1)
  128. {
  129. if (end == -1)
  130. end = start;
  131. // Get the set for this input
  132. const domInputLocalOffset* localOffset = daeSafeCast<domInputLocalOffset>(input);
  133. domUint newSet = localOffset ? localOffset->getSet() : 0;
  134. // Add the input to the right place in the list (somewhere between start and end)
  135. for (S32 i = start; i <= end; i++) {
  136. const domInputLocalOffset* localOffset = daeSafeCast<domInputLocalOffset>(sortedInputs[i]);
  137. domUint set = localOffset ? localOffset->getSet() : 0xFFFFFFFF;
  138. if (newSet < set) {
  139. for (S32 j = i + 1; j <= end; j++)
  140. sortedInputs[j] = sortedInputs[j-1];
  141. sortedInputs[i] = input;
  142. return;
  143. }
  144. }
  145. }
  146. /// Attempt to initialise a _SourceReader
  147. template<class T>
  148. bool initSourceReader(T input, eSourceType type, _SourceReader& reader, const char* params[])
  149. {
  150. if (!input)
  151. return false;
  152. // Try to get the source element
  153. const domSource* source = 0;
  154. daeElement *element = findInputSource(input);
  155. if (element->getElementType() == COLLADA_TYPE::SOURCE)
  156. source = daeSafeCast<domSource>(element);
  157. else if (element->getElementType() == COLLADA_TYPE::VERTICES) {
  158. const domVertices* vertices = daeSafeCast<domVertices>(element);
  159. // Search for the input with the desired semantic
  160. const char* semantic = SourceTypeToSemantic( type );
  161. for (int iInput = 0; iInput < vertices->getInput_array().getCount(); iInput++)
  162. {
  163. domInputLocal* input = vertices->getInput_array().get(iInput);
  164. if (dStrEqual(input->getSemantic(), semantic))
  165. {
  166. source = daeSafeCast<domSource>(findInputSource(input));
  167. break;
  168. }
  169. }
  170. }
  171. if (!source)
  172. return false;
  173. return reader.initFromSource(source, params);
  174. }
  175. public:
  176. _SourceReader points;
  177. _SourceReader normals;
  178. _SourceReader colors;
  179. _SourceReader uvs;
  180. _SourceReader uv2s;
  181. _SourceReader joints;
  182. _SourceReader weights;
  183. _SourceReader invBindMatrices;
  184. /// Clear the mesh streams
  185. void reset()
  186. {
  187. points.reset();
  188. normals.reset();
  189. colors.reset();
  190. uvs.reset();
  191. uv2s.reset();
  192. joints.reset();
  193. weights.reset();
  194. invBindMatrices.reset();
  195. }
  196. /// Classify a set of inputs by type and set number (needs to be a template
  197. /// because Collada has two forms of input arrays that may be accessed in
  198. /// an identical fashion, but the classes are unrelated. Sigh.
  199. template<class T>
  200. static void classifyInputs(const daeTArray<T>& inputs, T sortedInputs[], U32 *maxOffset=0)
  201. {
  202. if (maxOffset)
  203. *maxOffset = 0;
  204. // Clear output array
  205. for (int i = 0; i < NumStreams; i++)
  206. sortedInputs[i] = 0;
  207. // Separate inputs by type, and sort by set (ie. lowest TEXCOORD set becomes UV,
  208. // next TEXCOORD set becomes UV2 etc)
  209. for (int iInput = 0; iInput < inputs.getCount(); iInput++) {
  210. const T& input = inputs[iInput];
  211. const daeString semantic = input->getSemantic();
  212. if (dStrEqual(semantic, "VERTEX"))
  213. {
  214. domVertices* vertices = daeSafeCast<domVertices>(findInputSource(input));
  215. // The <vertices> element may contain multiple inputs (eg. POSITION, NORMAL etc)
  216. domInputLocalRef verticesInputs[NumStreams];
  217. classifyInputs(vertices->getInput_array(), verticesInputs);
  218. for (int iStream = 0; iStream < NumStreams; iStream++)
  219. {
  220. if (verticesInputs[iStream] != 0)
  221. sortedInputs[iStream] = input;
  222. }
  223. }
  224. else if (dStrEqual(semantic, "POSITION")) selectInput(input, sortedInputs, Points);
  225. else if (dStrEqual(semantic, "NORMAL")) selectInput(input, sortedInputs, Normals);
  226. else if (dStrEqual(semantic, "COLOR")) selectInput(input, sortedInputs, Colors);
  227. else if (dStrEqual(semantic, "TEXCOORD")) selectInput(input, sortedInputs, UVs, UV2s);
  228. else if (dStrEqual(semantic, "JOINT")) selectInput(input, sortedInputs, Joints);
  229. else if (dStrEqual(semantic, "WEIGHT")) selectInput(input, sortedInputs, Weights);
  230. else if (dStrEqual(semantic, "INV_BIND_MATRIX")) selectInput(input, sortedInputs, InvBindMatrices);
  231. if (maxOffset)
  232. {
  233. const domInputLocalOffset* localOffset = daeSafeCast<domInputLocalOffset>(input);
  234. domUint offset = localOffset ? localOffset->getOffset() : 0;
  235. if (offset > (*maxOffset))
  236. *maxOffset = offset;
  237. }
  238. }
  239. }
  240. /// Read a set of inputs into the named sources. There may be multiple 'sets'
  241. /// of COLOR or TEXCOORD (uvs) streams, but we are only interested in the
  242. /// first COLOR set (ie. smallest set value), and the first 2 TEXCOORDS sets.
  243. template<class T>
  244. bool readInputs(const daeTArray<T>& inputs)
  245. {
  246. // Sort inputs by type and set to find the ones we are interested in
  247. T sortedInputs[NumStreams];
  248. classifyInputs(inputs, sortedInputs);
  249. // Attempt to initialise the SourceReaders
  250. const char* vertex_params[] = { "X", "Y", "Z", "" };
  251. initSourceReader(sortedInputs[Points], Points, points, vertex_params);
  252. const char* normal_params[] = { "X", "Y", "Z", "" };
  253. initSourceReader(sortedInputs[Normals], Normals, normals, normal_params);
  254. const char* color_params[] = { "R", "G", "B", "A", "" };
  255. initSourceReader(sortedInputs[Colors], Colors, colors, color_params);
  256. const char* uv_params[] = { "S", "T", "" };
  257. const char* uv_params2[] = { "U", "V", "" }; // some files use the nonstandard U,V or X,Y param names
  258. const char* uv_params3[] = { "X", "Y", "" };
  259. if (!initSourceReader(sortedInputs[UVs], UVs, uvs, uv_params))
  260. if (!initSourceReader(sortedInputs[UVs], UVs, uvs, uv_params2))
  261. initSourceReader(sortedInputs[UVs], UVs, uvs, uv_params3);
  262. if (!initSourceReader(sortedInputs[UV2s], UV2s, uv2s, uv_params))
  263. if (!initSourceReader(sortedInputs[UV2s], UV2s, uv2s, uv_params2))
  264. initSourceReader(sortedInputs[UV2s], UV2s, uv2s, uv_params3);
  265. const char* joint_params[] = { "JOINT", "" };
  266. initSourceReader(sortedInputs[Joints], Joints, joints, joint_params);
  267. const char* weight_params[] = { "WEIGHT", "" };
  268. initSourceReader(sortedInputs[Weights], Weights, weights, weight_params);
  269. const char* matrix_params[] = { "TRANSFORM", "" };
  270. initSourceReader(sortedInputs[InvBindMatrices], InvBindMatrices, invBindMatrices, matrix_params);
  271. return true;
  272. }
  273. };
  274. //------------------------------------------------------------------------------
  275. ColladaAppMesh::ColladaAppMesh(const domInstance_geometry* instance, ColladaAppNode* node)
  276. : instanceGeom(instance), instanceCtrl(0), appNode(node), geomExt(0)
  277. {
  278. flags = 0;
  279. numFrames = 0;
  280. numMatFrames = 0;
  281. }
  282. ColladaAppMesh::ColladaAppMesh(const domInstance_controller* instance, ColladaAppNode* node)
  283. : instanceGeom(0), instanceCtrl(instance), appNode(node), geomExt(0)
  284. {
  285. flags = 0;
  286. numFrames = 0;
  287. numMatFrames = 0;
  288. }
  289. const char* ColladaAppMesh::getName(bool allowFixed)
  290. {
  291. // Some exporters add a 'PIVOT' or unnamed node between the mesh and the
  292. // actual object node. Detect this and return the object node name instead
  293. // of the pivot node.
  294. const char* nodeName = appNode->getName();
  295. if ( dStrEqual(nodeName, "null") || dStrEndsWith(nodeName, "PIVOT") )
  296. nodeName = appNode->getParentName();
  297. // If all geometry is being fixed to the same size, append the size
  298. // to the name
  299. return allowFixed && fixedSizeEnabled ? avar("%s %d", nodeName, fixedSize) : nodeName;
  300. }
  301. MatrixF ColladaAppMesh::getMeshTransform(F32 time)
  302. {
  303. return appNode->getNodeTransform(time);
  304. }
  305. bool ColladaAppMesh::animatesVis(const AppSequence* appSeq)
  306. {
  307. #define IS_VIS_ANIMATED(node) \
  308. (dynamic_cast<const ColladaAppNode*>(node)->nodeExt->visibility.isAnimated(appSeq->getStart(), appSeq->getEnd()))
  309. // Check if the node visibility is animated within the sequence interval
  310. return IS_VIS_ANIMATED(appNode) || (appNode->appParent ? IS_VIS_ANIMATED(appNode->appParent) : false);
  311. }
  312. bool ColladaAppMesh::animatesMatFrame(const AppSequence* appSeq)
  313. {
  314. // Texture coordinates may be animated in two ways:
  315. // - by animating the MAYA profile texture transform (diffuse texture)
  316. // - by animating the morph weights for morph targets with different UVs
  317. // Check if the MAYA profile texture transform is animated
  318. for (int iMat = 0; iMat < appMaterials.size(); iMat++) {
  319. ColladaAppMaterial* appMat = static_cast<ColladaAppMaterial*>(appMaterials[iMat]);
  320. if (appMat->effectExt &&
  321. appMat->effectExt->animatesTextureTransform(appSeq->getStart(), appSeq->getEnd()))
  322. return true;
  323. }
  324. // Check that the morph weights are animated within the sequence interval,
  325. // and that the morph targets have different UVs to the base geometry.
  326. bool animated = false;
  327. bool differentUVs = false;
  328. if (const domMorph* morph = getMorph()) {
  329. for (int iInput = 0; iInput < morph->getTargets()->getInput_array().getCount(); iInput++) {
  330. const domInputLocal* input = morph->getTargets()->getInput_array()[iInput];
  331. if (dStrEqual(input->getSemantic(), "MORPH_TARGET")) {
  332. // @todo: Check if morph targets have different UVs to base geometry
  333. differentUVs = false;
  334. }
  335. if (dStrEqual(input->getSemantic(), "MORPH_WEIGHT")) {
  336. const domSource* source = daeSafeCast<domSource>(findInputSource(input));
  337. AnimatedFloatList weights(source ? source->getFloat_array() : 0);
  338. animated = weights.isAnimated(appSeq->getStart(), appSeq->getEnd());
  339. }
  340. }
  341. }
  342. return (animated && differentUVs);
  343. }
  344. bool ColladaAppMesh::animatesFrame(const AppSequence* appSeq)
  345. {
  346. // Collada <morph>s ALWAYS contain vert positions, so just need to check if
  347. // the morph weights are animated within the sequence interval
  348. bool animated = false;
  349. if (const domMorph* morph = getMorph()) {
  350. for (int iInput = 0; iInput < morph->getTargets()->getInput_array().getCount(); iInput++) {
  351. const domInputLocal* input = morph->getTargets()->getInput_array()[iInput];
  352. if (dStrEqual(input->getSemantic(), "MORPH_WEIGHT")) {
  353. const domSource* source = daeSafeCast<domSource>(findInputSource(input));
  354. AnimatedFloatList weights(source ? source->getFloat_array() : 0);
  355. animated = weights.isAnimated(appSeq->getStart(), appSeq->getEnd());
  356. break;
  357. }
  358. }
  359. }
  360. return animated;
  361. }
  362. F32 ColladaAppMesh::getVisValue(F32 t)
  363. {
  364. #define GET_VIS(node) \
  365. (dynamic_cast<const ColladaAppNode*>(node)->nodeExt->visibility.getValue(t))
  366. // Get the visibility of the mesh's node at time, 't'
  367. return GET_VIS(appNode) * (appNode->appParent ? GET_VIS(appNode->appParent) : 1.0f);
  368. }
  369. S32 ColladaAppMesh::addMaterial(const char* symbol)
  370. {
  371. if (!symbol)
  372. return TSDrawPrimitive::NoMaterial;
  373. // Lookup the symbol in the materials already bound to this geometry/controller
  374. // instance
  375. Map<StringTableEntry,U32>::Iterator itr = boundMaterials.find(symbol);
  376. if (itr != boundMaterials.end())
  377. return itr->value;
  378. // Find the Collada material that this symbol maps to
  379. U32 matIndex = TSDrawPrimitive::NoMaterial;
  380. const domBind_material* binds = instanceGeom ? instanceGeom->getBind_material() :
  381. instanceCtrl->getBind_material();
  382. if (binds) {
  383. const domInstance_material_Array& matArray = binds->getTechnique_common()->getInstance_material_array();
  384. for (int iBind = 0; iBind < matArray.getCount(); iBind++) {
  385. if (dStrEqual(matArray[iBind]->getSymbol(), symbol)) {
  386. // Find the index of the bound material in the shape global list
  387. const domMaterial* mat = daeSafeCast<domMaterial>(matArray[iBind]->getTarget().getElement());
  388. for (matIndex = 0; matIndex < appMaterials.size(); matIndex++) {
  389. if (static_cast<ColladaAppMaterial*>(appMaterials[matIndex])->mat == mat)
  390. break;
  391. }
  392. // Check if this material needs to be added to the shape global list
  393. if (matIndex == appMaterials.size()) {
  394. if (mat)
  395. appMaterials.push_back(new ColladaAppMaterial(mat));
  396. else
  397. appMaterials.push_back(new ColladaAppMaterial(symbol));
  398. }
  399. break;
  400. }
  401. }
  402. }
  403. // Add this symbol to the bound list for the mesh
  404. boundMaterials.insert(StringTable->insert(symbol), matIndex);
  405. return matIndex;
  406. }
  407. void ColladaAppMesh::getPrimitives(const domGeometry* geometry)
  408. {
  409. // Only do this once
  410. if (primitives.size())
  411. return;
  412. // Read the <geometry> extension
  413. if (!geomExt)
  414. geomExt = new ColladaExtension_geometry(geometry);
  415. // Get the supported primitive elements for this geometry, and warn
  416. // about unsupported elements
  417. Vector<BasePrimitive*> meshPrims;
  418. const daeElementRefArray& contents = geometry->getMesh()->getContents();
  419. for (int iElem = 0; iElem < contents.getCount(); iElem++) {
  420. if (BasePrimitive::isPrimitive(contents[iElem])) {
  421. if (BasePrimitive::isSupportedPrimitive(contents[iElem]))
  422. meshPrims.push_back(BasePrimitive::get(contents[iElem]));
  423. else {
  424. daeErrorHandler::get()->handleWarning(avar("Collada <%s> element "
  425. "in %s is not supported.", contents[iElem]->getElementName(),
  426. _GetNameOrId(geometry)));
  427. }
  428. }
  429. }
  430. MeshStreams streams;
  431. VertTupleMap tupleMap;
  432. // Create Torque primitives
  433. for (int iPrim = 0; iPrim < meshPrims.size(); iPrim++) {
  434. // Primitive element must have at least 1 triangle
  435. const domListOfUInts* pTriData = meshPrims[iPrim]->getTriangleData();
  436. if (!pTriData)
  437. continue;
  438. U32 numTriangles = pTriData->getCount() / meshPrims[iPrim]->getStride() / 3;
  439. if (!numTriangles)
  440. continue;
  441. // Create TSMesh primitive
  442. primitives.increment();
  443. TSDrawPrimitive& primitive = primitives.last();
  444. primitive.start = indices.size();
  445. primitive.matIndex = (TSDrawPrimitive::Triangles | TSDrawPrimitive::Indexed) |
  446. addMaterial(meshPrims[iPrim]->getMaterial());
  447. // Get the AppMaterial associated with this primitive
  448. ColladaAppMaterial* appMat = 0;
  449. if (!(primitive.matIndex & TSDrawPrimitive::NoMaterial))
  450. appMat = static_cast<ColladaAppMaterial*>(appMaterials[primitive.matIndex & TSDrawPrimitive::MaterialMask]);
  451. // Force the material to be double-sided if this geometry is double-sided.
  452. if (geomExt->double_sided && appMat && appMat->effectExt)
  453. appMat->effectExt->double_sided = true;
  454. // Pre-allocate triangle indices
  455. primitive.numElements = numTriangles * 3;
  456. indices.setSize(indices.size() + primitive.numElements);
  457. U32* dstIndex = indices.end() - primitive.numElements;
  458. // Determine the offset for each element type in the stream, and also the
  459. // maximum input offset, which will be the number of indices per vertex we
  460. // need to skip.
  461. domInputLocalOffsetRef sortedInputs[MeshStreams::NumStreams];
  462. MeshStreams::classifyInputs(meshPrims[iPrim]->getInputs(), sortedInputs);
  463. S32 offsets[MeshStreams::NumStreams];
  464. for (S32 i = 0; i < MeshStreams::NumStreams; i++)
  465. offsets[i] = sortedInputs[i] ? sortedInputs[i]->getOffset() : -1;
  466. // Loop through indices
  467. const domUint* pSrcData = &(pTriData->get(0));
  468. for (U32 iTri = 0; iTri < numTriangles; iTri++) {
  469. // If the next triangle could cause us to index across a 16-bit
  470. // boundary, split this primitive and clear the tuple map to
  471. // ensure primitives only index verts within a 16-bit range.
  472. if (vertTuples.size() &&
  473. (((vertTuples.size()-1) ^ (vertTuples.size()+2)) & 0x10000))
  474. {
  475. // Pad vertTuples up to the next 16-bit boundary
  476. while (vertTuples.size() & 0xFFFF)
  477. vertTuples.push_back(VertTuple(vertTuples.last()));
  478. // Split the primitive at the current triangle
  479. S32 indicesRemaining = (numTriangles - iTri) * 3;
  480. if (iTri > 0)
  481. {
  482. daeErrorHandler::get()->handleWarning(avar("Splitting primitive "
  483. "in %s: too many verts for 16-bit indices.", _GetNameOrId(geometry)));
  484. primitives.last().numElements -= indicesRemaining;
  485. primitives.push_back(TSDrawPrimitive(primitives.last()));
  486. }
  487. primitives.last().numElements = indicesRemaining;
  488. primitives.last().start = indices.size() - indicesRemaining;
  489. tupleMap.clear();
  490. }
  491. streams.reset();
  492. streams.readInputs(meshPrims[iPrim]->getInputs());
  493. for (U32 v = 0; v < 3; v++) {
  494. // Collect vert tuples into a single array so we can easily grab
  495. // vertex data later.
  496. VertTuple tuple;
  497. tuple.prim = iPrim;
  498. tuple.vertex = offsets[MeshStreams::Points] >= 0 ? pSrcData[offsets[MeshStreams::Points]] : -1;
  499. tuple.normal = offsets[MeshStreams::Normals] >= 0 ? pSrcData[offsets[MeshStreams::Normals]] : -1;
  500. tuple.color = offsets[MeshStreams::Colors] >= 0 ? pSrcData[offsets[MeshStreams::Colors]] : -1;
  501. tuple.uv = offsets[MeshStreams::UVs] >= 0 ? pSrcData[offsets[MeshStreams::UVs]] : -1;
  502. tuple.uv2 = offsets[MeshStreams::UV2s] >= 0 ? pSrcData[offsets[MeshStreams::UV2s]] : -1;
  503. tuple.dataVertex = tuple.vertex > -1 ? streams.points.getPoint3FValue(tuple.vertex) : Point3F::Max;
  504. tuple.dataNormal = tuple.normal > -1 ? streams.normals.getPoint3FValue(tuple.normal) : Point3F::Max;
  505. tuple.dataColor = tuple.color > -1 ? streams.colors.getColorIValue(tuple.color) : ColorI(0,0,0);
  506. tuple.dataUV = tuple.uv > -1 ? streams.uvs.getPoint2FValue(tuple.uv) : Point2F::Max;
  507. tuple.dataUV2 = tuple.uv2 > -1 ? streams.uv2s.getPoint2FValue(tuple.uv2) : Point2F::Max;
  508. VertTupleMap::Iterator itr = tupleMap.find(tuple);
  509. if (itr == tupleMap.end())
  510. {
  511. itr = tupleMap.insert(tuple, vertTuples.size());
  512. vertTuples.push_back(tuple);
  513. }
  514. // Collada uses CCW for front face and Torque uses the opposite, so
  515. // for normal (non-inverted) meshes, the indices are flipped.
  516. if (appNode->invertMeshes)
  517. dstIndex[v] = itr->value;
  518. else
  519. dstIndex[2 - v] = itr->value;
  520. pSrcData += meshPrims[iPrim]->getStride();
  521. }
  522. dstIndex += 3;
  523. }
  524. }
  525. for (int iPrim = 0; iPrim < meshPrims.size(); iPrim++)
  526. delete meshPrims[iPrim];
  527. }
  528. void ColladaAppMesh::getVertexData(const domGeometry* geometry, F32 time, const MatrixF& objectOffset,
  529. Vector<Point3F>& v_points,
  530. Vector<Point3F>& v_norms,
  531. Vector<ColorI>& v_colors,
  532. Vector<Point2F>& v_uvs,
  533. Vector<Point2F>& v_uv2s,
  534. bool appendValues)
  535. {
  536. if (!primitives.size())
  537. return;
  538. MeshStreams streams;
  539. S32 lastPrimitive = -1;
  540. ColladaAppMaterial* appMat = 0;
  541. // Get the supported primitive elements for this geometry
  542. Vector<BasePrimitive*> meshPrims;
  543. const daeElementRefArray& contents = geometry->getMesh()->getContents();
  544. for (int iElem = 0; iElem < contents.getCount(); iElem++) {
  545. if (BasePrimitive::isSupportedPrimitive(contents[iElem]))
  546. meshPrims.push_back(BasePrimitive::get(contents[iElem]));
  547. }
  548. // If appending values, pre-allocate the arrays
  549. if (appendValues) {
  550. v_points.setSize(v_points.size() + vertTuples.size());
  551. v_uvs.setSize(v_uvs.size() + vertTuples.size());
  552. }
  553. // Get pointers to arrays
  554. Point3F* points_array = &v_points[v_points.size() - vertTuples.size()];
  555. Point2F* uvs_array = &v_uvs[v_uvs.size() - vertTuples.size()];
  556. Point3F* norms_array = NULL;
  557. ColorI* colors_array = NULL;
  558. Point2F* uv2s_array = NULL;
  559. for (int iVert = 0; iVert < vertTuples.size(); iVert++) {
  560. const VertTuple& tuple = vertTuples[iVert];
  561. // Change primitives?
  562. if (tuple.prim != lastPrimitive) {
  563. if (meshPrims.size() <= tuple.prim) {
  564. daeErrorHandler::get()->handleError(avar("Failed to get vertex data "
  565. "for %s. Primitives do not match base geometry.", geometry->getID()));
  566. break;
  567. }
  568. // Update vertex/normal/UV streams and get the new material index
  569. streams.reset();
  570. streams.readInputs(meshPrims[tuple.prim]->getInputs());
  571. S32 matIndex = addMaterial(meshPrims[tuple.prim]->getMaterial());
  572. if (matIndex != TSDrawPrimitive::NoMaterial)
  573. appMat = static_cast<ColladaAppMaterial*>(appMaterials[matIndex]);
  574. else
  575. appMat = 0;
  576. lastPrimitive = tuple.prim;
  577. }
  578. // If we are NOT appending values, only set the value if it actually exists
  579. // in the mesh data stream.
  580. if (appendValues || ((tuple.vertex >= 0) && (tuple.vertex < streams.points.size()))) {
  581. points_array[iVert] = streams.points.getPoint3FValue(tuple.vertex);
  582. // Flip verts for inverted meshes
  583. if (appNode->invertMeshes)
  584. points_array[iVert].z = -points_array[iVert].z;
  585. objectOffset.mulP(points_array[iVert]);
  586. }
  587. if (appendValues || ((tuple.uv >= 0) && (tuple.uv < streams.uvs.size()))) {
  588. uvs_array[iVert] = streams.uvs.getPoint2FValue(tuple.uv);
  589. if (appMat && appMat->effectExt)
  590. appMat->effectExt->applyTextureTransform(uvs_array[iVert], time);
  591. uvs_array[iVert].y = 1.0f - uvs_array[iVert].y; // Collada texcoords are upside down compared to TGE
  592. }
  593. // The rest is non-required data... if it doesn't exist then don't append it.
  594. if ( (tuple.normal >= 0) && (tuple.normal < streams.normals.size()) ) {
  595. if ( !norms_array && iVert == 0 )
  596. {
  597. v_norms.setSize(v_norms.size() + vertTuples.size());
  598. norms_array = &v_norms[v_norms.size() - vertTuples.size()];
  599. }
  600. if ( norms_array ) {
  601. norms_array[iVert] = streams.normals.getPoint3FValue(tuple.normal);
  602. // Flip normals for inverted meshes
  603. if (appNode->invertMeshes)
  604. norms_array[iVert].z = -norms_array[iVert].z;
  605. }
  606. }
  607. if ( (tuple.color >= 0) && (tuple.color < streams.colors.size()))
  608. {
  609. if ( !colors_array && iVert == 0 )
  610. {
  611. v_colors.setSize(v_colors.size() + vertTuples.size());
  612. colors_array = &v_colors[v_colors.size() - vertTuples.size()];
  613. }
  614. if ( colors_array )
  615. colors_array[iVert] = streams.colors.getColorIValue(tuple.color);
  616. }
  617. if ( (tuple.uv2 >= 0) && (tuple.uv2 < streams.uv2s.size()) )
  618. {
  619. if ( !uv2s_array && iVert == 0 )
  620. {
  621. v_uv2s.setSize(v_uv2s.size() + vertTuples.size());
  622. uv2s_array = &v_uv2s[v_uv2s.size() - vertTuples.size()];
  623. }
  624. if ( uv2s_array )
  625. {
  626. uv2s_array[iVert] = streams.uv2s.getPoint2FValue(tuple.uv2);
  627. if (appMat && appMat->effectExt)
  628. appMat->effectExt->applyTextureTransform(uv2s_array[iVert], time);
  629. uv2s_array[iVert].y = 1.0f - uv2s_array[iVert].y; // Collada texcoords are upside down compared to TGE
  630. }
  631. }
  632. }
  633. for (int iPrim = 0; iPrim < meshPrims.size(); iPrim++)
  634. delete meshPrims[iPrim];
  635. }
  636. void ColladaAppMesh::getMorphVertexData(const domMorph* morph, F32 time, const MatrixF& objectOffset,
  637. Vector<Point3F>& v_points,
  638. Vector<Point3F>& v_norms,
  639. Vector<ColorI>& v_colors,
  640. Vector<Point2F>& v_uvs,
  641. Vector<Point2F>& v_uv2s)
  642. {
  643. // @todo: Could the base geometry (or any target geometry) also be a morph?
  644. // Get the target geometries and weights (could be animated)
  645. Vector<const domGeometry*> targetGeoms;
  646. domListOfFloats targetWeights;
  647. for (int iInput = 0; iInput < morph->getTargets()->getInput_array().getCount(); iInput++) {
  648. const domInputLocal* input = morph->getTargets()->getInput_array()[iInput];
  649. const domSource* source = daeSafeCast<domSource>(findInputSource(input));
  650. if (dStrEqual(input->getSemantic(), "MORPH_TARGET")) {
  651. // Get the morph targets
  652. _SourceReader srcTargets;
  653. srcTargets.initFromSource(source);
  654. for (int iTarget = 0; iTarget < srcTargets.size(); iTarget++) {
  655. // Lookup the element and add to the targets list
  656. daeIDRef idref(srcTargets.getStringValue(iTarget));
  657. idref.setContainer(morph->getDocument()->getDomRoot());
  658. targetGeoms.push_back(daeSafeCast<domGeometry>(idref.getElement()));
  659. }
  660. }
  661. else if (dStrEqual(input->getSemantic(), "MORPH_WEIGHT")) {
  662. // Get the (possibly animated) morph weight
  663. targetWeights = AnimatedFloatList(source->getFloat_array()).getValue(time);
  664. }
  665. }
  666. // Check that we have a weight for each target
  667. if (targetGeoms.size() != targetWeights.getCount())
  668. {
  669. domController* ctrl = daeSafeCast<domController>(const_cast<domMorph*>(morph)->getParent());
  670. Con::warnf("Mismatched morph targets and weights in %s.", _GetNameOrId(ctrl));
  671. // Set unused targets to zero weighting (unused weights are ignored)
  672. while (targetGeoms.size() > targetWeights.getCount())
  673. targetWeights.append(0.0f);
  674. }
  675. // Get the base geometry and vertex data
  676. const domGeometry* baseGeometry = daeSafeCast<domGeometry>(morph->getSource().getElement());
  677. if (!baseGeometry)
  678. return;
  679. getPrimitives(baseGeometry);
  680. getVertexData(baseGeometry, time, objectOffset, v_points, v_norms, v_colors, v_uvs, v_uv2s, true);
  681. // Get pointers to the arrays of base geometry data
  682. Point3F* points_array = &v_points[v_points.size() - vertTuples.size()];
  683. Point3F* norms_array = &v_norms[v_norms.size() - vertTuples.size()];
  684. Point2F* uvs_array = &v_uvs[v_uvs.size() - vertTuples.size()];
  685. ColorI* colors_array = v_colors.size() ? &v_colors[v_colors.size() - vertTuples.size()] : 0;
  686. Point2F* uv2s_array = v_uv2s.size() ? &v_uv2s[v_uv2s.size() - vertTuples.size()] : 0;
  687. // Normalize base vertex data?
  688. if (morph->getMethod() == MORPHMETHODTYPE_NORMALIZED) {
  689. F32 weightSum = 0.0f;
  690. for (int iWeight = 0; iWeight < targetWeights.getCount(); iWeight++) {
  691. weightSum += targetWeights[iWeight];
  692. }
  693. // Result = Base*(1.0-w1-w2 ... -wN) + w1*Target1 + w2*Target2 ... + wN*TargetN
  694. weightSum = mClampF(1.0f - weightSum, 0.0f, 1.0f);
  695. for (int iVert = 0; iVert < vertTuples.size(); iVert++) {
  696. points_array[iVert] *= weightSum;
  697. norms_array[iVert] *= weightSum;
  698. uvs_array[iVert] *= weightSum;
  699. }
  700. if (uv2s_array) {
  701. for (int iVert = 0; iVert < vertTuples.size(); iVert++)
  702. uv2s_array[iVert] *= weightSum;
  703. }
  704. }
  705. // Interpolate using the target geometry and weights
  706. for (int iTarget = 0; iTarget < targetGeoms.size(); iTarget++) {
  707. // Ignore empty weights
  708. if (targetWeights[iTarget] == 0.0f)
  709. continue;
  710. // Get target geometry data into temporary arrays
  711. Vector<Point3F> targetPoints;
  712. Vector<Point3F> targetNorms;
  713. Vector<Point2F> targetUvs;
  714. Vector<ColorI> targetColors;
  715. Vector<Point2F> targetUv2s;
  716. // Copy base geometry into target geometry (will be used if target does
  717. // not define normals or uvs)
  718. targetPoints.set(points_array, vertTuples.size());
  719. targetNorms.set(norms_array, vertTuples.size());
  720. targetUvs.set(uvs_array, vertTuples.size());
  721. if (colors_array)
  722. targetColors.set(colors_array, vertTuples.size());
  723. if (uv2s_array)
  724. targetUv2s.set(uv2s_array, vertTuples.size());
  725. getVertexData(targetGeoms[iTarget], time, objectOffset, targetPoints, targetNorms, targetColors, targetUvs, targetUv2s, false);
  726. // Combine with base geometry
  727. for (int iVert = 0; iVert < vertTuples.size(); iVert++) {
  728. points_array[iVert] += targetPoints[iVert] * targetWeights[iTarget];
  729. norms_array[iVert] += targetNorms[iVert] * targetWeights[iTarget];
  730. uvs_array[iVert] += targetUvs[iVert] * targetWeights[iTarget];
  731. }
  732. if (uv2s_array) {
  733. for (int iVert = 0; iVert < vertTuples.size(); iVert++)
  734. uv2s_array[iVert] += targetUv2s[iVert] * targetWeights[iTarget];
  735. }
  736. if (colors_array) {
  737. for (int iVert = 0; iVert < vertTuples.size(); iVert++)
  738. colors_array[iVert] += targetColors[iVert] * (F32)targetWeights[iTarget];
  739. }
  740. }
  741. }
  742. void ColladaAppMesh::lockMesh(F32 t, const MatrixF& objectOffset)
  743. {
  744. // Find the geometry element for this mesh. Could be one of 3 things:
  745. // 1) a simple static mesh (Collada <geometry> element)
  746. // 2) a simple morph (some combination of static meshes)
  747. // 3) a skin (skin geometry could also be a morph!)
  748. daeElement* geometry = 0;
  749. if (instanceGeom) {
  750. // Simple, static mesh
  751. geometry = instanceGeom->getUrl().getElement();
  752. }
  753. else if (instanceCtrl) {
  754. const domController* ctrl = daeSafeCast<domController>(instanceCtrl->getUrl().getElement());
  755. if (!ctrl) {
  756. daeErrorHandler::get()->handleWarning(avar("Failed to find <controller> "
  757. "element for %s", getName()));
  758. return;
  759. }
  760. else if (ctrl->getMorph()) {
  761. // Morph controller
  762. geometry = ctrl->getMorph();
  763. }
  764. else {
  765. // Skinned mesh: source geometry could be static geometry or a morph controller.
  766. geometry = ctrl->getSkin()->getSource().getElement();
  767. if (geometry && geometry->getElementType() == COLLADA_TYPE::CONTROLLER)
  768. geometry = daeSafeCast<domController>(geometry)->getMorph();
  769. }
  770. }
  771. if (!geometry) {
  772. daeErrorHandler::get()->handleWarning(avar("Failed to find source geometry "
  773. "for %s", getName()));
  774. return;
  775. }
  776. // Now get the vertex data at the specified time
  777. if (geometry->getElementType() == COLLADA_TYPE::GEOMETRY) {
  778. getPrimitives(daeSafeCast<domGeometry>(geometry));
  779. getVertexData(daeSafeCast<domGeometry>(geometry), t, objectOffset, points, normals, colors, uvs, uv2s, true);
  780. }
  781. else if (geometry->getElementType() == COLLADA_TYPE::MORPH) {
  782. getMorphVertexData(daeSafeCast<domMorph>(geometry), t, objectOffset, points, normals, colors, uvs, uv2s);
  783. }
  784. else {
  785. daeErrorHandler::get()->handleWarning(avar("Unsupported geometry type "
  786. "'<%s>' for %s", geometry->getElementName(), getName()));
  787. }
  788. }
  789. void ColladaAppMesh::lookupSkinData()
  790. {
  791. // Only lookup skin data once
  792. if (!isSkin() || weight.size())
  793. return;
  794. // Get the skin and vertex weight data
  795. const domSkin* skin = daeSafeCast<domController>(instanceCtrl->getUrl().getElement())->getSkin();
  796. const domSkin::domVertex_weights& weightIndices = *(skin->getVertex_weights());
  797. const domListOfInts& weights_v = weightIndices.getV()->getValue();
  798. const domListOfUInts& weights_vcount = weightIndices.getVcount()->getValue();
  799. MeshStreams streams;
  800. streams.readInputs(skin->getJoints()->getInput_array());
  801. streams.readInputs(weightIndices.getInput_array());
  802. MatrixF invObjOffset(objectOffset);
  803. invObjOffset.inverse();
  804. // Get the bind shape matrix
  805. MatrixF bindShapeMatrix(true);
  806. if (skin->getBind_shape_matrix())
  807. bindShapeMatrix = vecToMatrixF<domMatrix>(skin->getBind_shape_matrix()->getValue());
  808. bindShapeMatrix.mul(invObjOffset);
  809. // Determine the offset into the vindices array for each vertex (since each
  810. // vertex may have multiple [bone, weight] pairs in the array)
  811. Vector<U32> vindicesOffset;
  812. const domInt* vindices = (domInt*)weights_v.getRaw(0);
  813. for (int iWeight = 0; iWeight < weights_vcount.getCount(); iWeight++) {
  814. // Store the offset into the vindices array for this vertex
  815. vindicesOffset.push_back(vindices - (domInt*)weights_v.getRaw(0));
  816. vindices += (weights_vcount[iWeight]*2); // 2 indices [bone, weight] per vert
  817. }
  818. // Set vertex weights
  819. bool tooManyWeightsWarning = false;
  820. for (int iVert = 0; iVert < vertsPerFrame; iVert++) {
  821. const domUint* vcount = (domUint*)weights_vcount.getRaw(0);
  822. const domInt* vindices = (domInt*)weights_v.getRaw(0);
  823. vindices += vindicesOffset[vertTuples[iVert].vertex];
  824. S32 nonZeroWeightCount = 0;
  825. for (int iWeight = 0; iWeight < vcount[vertTuples[iVert].vertex]; iWeight++) {
  826. S32 bIndex = vindices[iWeight*2];
  827. F32 bWeight = streams.weights.getFloatValue( vindices[iWeight*2 + 1] );
  828. // Ignore empty weights
  829. if ( bIndex < 0 || bWeight == 0 )
  830. continue;
  831. // Limit the number of weights per bone (keep the N largest influences)
  832. if ( nonZeroWeightCount >= TSSkinMesh::BatchData::maxBonePerVert )
  833. {
  834. if (vcount[vertTuples[iVert].vertex] > TSSkinMesh::BatchData::maxBonePerVert)
  835. {
  836. if (!tooManyWeightsWarning)
  837. {
  838. tooManyWeightsWarning = true;
  839. daeErrorHandler::get()->handleWarning(avar("At least one vertex has "
  840. "too many bone weights. Limiting to the largest %d influences.",
  841. TSSkinMesh::BatchData::maxBonePerVert));
  842. }
  843. }
  844. // Too many weights => find and replace the smallest one
  845. S32 minIndex = weight.size() - TSSkinMesh::BatchData::maxBonePerVert;
  846. F32 minWeight = weight[minIndex];
  847. for (S32 i = minIndex + 1; i < weight.size(); i++)
  848. {
  849. if (weight[i] < minWeight)
  850. {
  851. minWeight = weight[i];
  852. minIndex = i;
  853. }
  854. }
  855. boneIndex[minIndex] = bIndex;
  856. weight[minIndex] = bWeight;
  857. }
  858. else
  859. {
  860. vertexIndex.push_back( iVert );
  861. boneIndex.push_back( bIndex );
  862. weight.push_back( bWeight );
  863. nonZeroWeightCount++;
  864. }
  865. }
  866. }
  867. // Normalize vertex weights (force weights for each vert to sum to 1)
  868. int iWeight = 0;
  869. while (iWeight < weight.size()) {
  870. // Find the last weight with the same vertex number, and sum all weights for
  871. // that vertex
  872. F32 invTotalWeight = 0;
  873. int iLast;
  874. for (iLast = iWeight; iLast < weight.size(); iLast++) {
  875. if (vertexIndex[iLast] != vertexIndex[iWeight])
  876. break;
  877. invTotalWeight += weight[iLast];
  878. }
  879. // Then normalize the vertex weights
  880. invTotalWeight = 1.0f / invTotalWeight;
  881. for (; iWeight < iLast; iWeight++)
  882. weight[iWeight] *= invTotalWeight;
  883. }
  884. // Add dummy AppNodes to allow Collada joints to be mapped to 3space nodes
  885. bones.setSize(streams.joints.size());
  886. initialTransforms.setSize(streams.joints.size());
  887. for (int iJoint = 0; iJoint < streams.joints.size(); iJoint++)
  888. {
  889. const char* jointName = streams.joints.getStringValue(iJoint);
  890. // Lookup the joint element
  891. const domNode* joint = 0;
  892. if (instanceCtrl->getSkeleton_array().getCount()) {
  893. // Search for the node using the <skeleton> as the base element
  894. for (int iSkel = 0; iSkel < instanceCtrl->getSkeleton_array().getCount(); iSkel++) {
  895. xsAnyURI skeleton = instanceCtrl->getSkeleton_array()[iSkel]->getValue();
  896. daeSIDResolver resolver(skeleton.getElement(), jointName);
  897. joint = daeSafeCast<domNode>(resolver.getElement());
  898. if (joint)
  899. break;
  900. }
  901. }
  902. else {
  903. // Search for the node from the root level
  904. daeSIDResolver resolver(skin->getDocument()->getDomRoot(), jointName);
  905. joint = daeSafeCast<domNode>(resolver.getElement());
  906. }
  907. if (!joint) {
  908. daeErrorHandler::get()->handleWarning(avar("Failed to find bone '%s', "
  909. "defaulting to instance_controller parent node '%s'", jointName, appNode->getName()));
  910. joint = appNode->getDomNode();
  911. }
  912. bones[iJoint] = new ColladaAppNode(joint);
  913. initialTransforms[iJoint] = objectOffset;
  914. // Bone scaling is generally ignored during import, since 3space only
  915. // stores default node transform and rotation. Compensate for this by
  916. // removing the scaling from the inverse bind transform as well
  917. MatrixF invBind = streams.invBindMatrices.getMatrixFValue(iJoint);
  918. if (!ColladaUtils::getOptions().ignoreNodeScale)
  919. {
  920. Point3F invScale = invBind.getScale();
  921. invScale.x = invScale.x ? (1.0f / invScale.x) : 0;
  922. invScale.y = invScale.y ? (1.0f / invScale.y) : 0;
  923. invScale.z = invScale.z ? (1.0f / invScale.z) : 0;
  924. initialTransforms[iJoint].scale(invScale);
  925. }
  926. // Inverted node coordinate spaces (negative scale factor) are corrected
  927. // in ColladaAppNode::getNodeTransform, so need to apply the same operation
  928. // here to match
  929. if (m_matF_determinant(invBind) < 0.0f)
  930. initialTransforms[iJoint].scale(Point3F(1, 1, -1));
  931. initialTransforms[iJoint].mul(invBind);
  932. initialTransforms[iJoint].mul(bindShapeMatrix);
  933. }
  934. }