tsShapeLoader.cpp 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339
  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. #include "console/engineAPI.h"
  24. #include "ts/loader/tsShapeLoader.h"
  25. #include "core/volume.h"
  26. #include "materials/materialList.h"
  27. #include "materials/matInstance.h"
  28. #include "materials/materialManager.h"
  29. #include "ts/tsShapeInstance.h"
  30. #include "ts/tsMaterialList.h"
  31. MODULE_BEGIN( ShapeLoader )
  32. MODULE_INIT_AFTER( GFX )
  33. MODULE_INIT
  34. {
  35. TSShapeLoader::addFormat("Torque DTS", "dts");
  36. TSShapeLoader::addFormat("Torque DSQ", "dsq");
  37. }
  38. MODULE_END;
  39. const F32 TSShapeLoader::DefaultTime = -1.0f;
  40. const F64 TSShapeLoader::MinFrameRate = 15.0f;
  41. const F64 TSShapeLoader::MaxFrameRate = 60.0f;
  42. const F64 TSShapeLoader::AppGroundFrameRate = 10.0f;
  43. Torque::Path TSShapeLoader::shapePath;
  44. Vector<TSShapeLoader::ShapeFormat> TSShapeLoader::smFormats;
  45. //------------------------------------------------------------------------------
  46. // Utility functions
  47. void TSShapeLoader::zapScale(MatrixF& mat)
  48. {
  49. Point3F invScale = mat.getScale();
  50. invScale.x = invScale.x ? (1.0f / invScale.x) : 0;
  51. invScale.y = invScale.y ? (1.0f / invScale.y) : 0;
  52. invScale.z = invScale.z ? (1.0f / invScale.z) : 0;
  53. mat.scale(invScale);
  54. }
  55. //------------------------------------------------------------------------------
  56. // Shape utility functions
  57. MatrixF TSShapeLoader::getLocalNodeMatrix(AppNode* node, F32 t)
  58. {
  59. MatrixF m1 = node->getNodeTransform(t);
  60. // multiply by inverse scale at t=0
  61. MatrixF m10 = node->getNodeTransform(DefaultTime);
  62. m1.scale(Point3F(1.0f/m10.getScale().x, 1.0f/m10.getScale().y, 1.0f/m10.getScale().z));
  63. if (node->mParentIndex >= 0)
  64. {
  65. AppNode *parent = appNodes[node->mParentIndex];
  66. MatrixF m2 = parent->getNodeTransform(t);
  67. // multiply by inverse scale at t=0
  68. MatrixF m20 = parent->getNodeTransform(DefaultTime);
  69. m2.scale(Point3F(1.0f/m20.getScale().x, 1.0f/m20.getScale().y, 1.0f/m20.getScale().z));
  70. // get local transform by pre-multiplying by inverted parent transform
  71. m1 = m2.inverse() * m1;
  72. }
  73. else if (boundsNode && node != boundsNode)
  74. {
  75. // make transform relative to bounds node transform at time=t
  76. MatrixF mb = boundsNode->getNodeTransform(t);
  77. zapScale(mb);
  78. m1 = mb.inverse() * m1;
  79. }
  80. return m1;
  81. }
  82. void TSShapeLoader::generateNodeTransform(AppNode* node, F32 t, bool blend, F32 referenceTime,
  83. QuatF& rot, Point3F& trans, QuatF& srot, Point3F& scale)
  84. {
  85. MatrixF m1 = getLocalNodeMatrix(node, t);
  86. if (blend)
  87. {
  88. MatrixF m0 = getLocalNodeMatrix(node, referenceTime);
  89. m1 = m0.inverse() * m1;
  90. }
  91. rot.set(m1);
  92. trans = m1.getPosition();
  93. srot.identity(); //@todo: srot not supported yet
  94. scale = m1.getScale();
  95. }
  96. //-----------------------------------------------------------------------------
  97. void TSShapeLoader::updateProgress(S32 major, const char* msg, S32 numMinor, S32 minor)
  98. {
  99. // Calculate progress value
  100. F32 progress = (F32)major / NumLoadPhases;
  101. const char *progressMsg = msg;
  102. if (numMinor)
  103. {
  104. progress += (minor * (1.0f / NumLoadPhases) / numMinor);
  105. progressMsg = avar("%s (%d of %d)", msg, minor + 1, numMinor);
  106. }
  107. Con::executef("updateTSShapeLoadProgress", Con::getFloatArg(progress), progressMsg);
  108. }
  109. //-----------------------------------------------------------------------------
  110. // Shape creation entry point
  111. TSShape* TSShapeLoader::generateShape(const Torque::Path& path)
  112. {
  113. shapePath = path;
  114. shape = new TSShape();
  115. shape->mExporterVersion = 124;
  116. shape->mSmallestVisibleSize = 999999;
  117. shape->mSmallestVisibleDL = 0;
  118. shape->mReadVersion = 24;
  119. shape->mFlags = 0;
  120. shape->mSequencesConstructed = 0;
  121. // Get all nodes, objects and sequences in the shape
  122. updateProgress(Load_EnumerateScene, "Enumerating scene...");
  123. enumerateScene();
  124. if (!subshapes.size())
  125. {
  126. delete shape;
  127. Con::errorf("Failed to load shape \"%s\", no subshapes found", path.getFullPath().c_str());
  128. return NULL;
  129. }
  130. // Create the TSShape::Node hierarchy
  131. generateSubshapes();
  132. // Create objects (meshes and details)
  133. generateObjects();
  134. // Generate initial object states and node transforms
  135. generateDefaultStates();
  136. // Generate skins
  137. generateSkins();
  138. // Generate material list
  139. generateMaterialList();
  140. // Generate animation sequences
  141. generateSequences();
  142. // Sort detail levels and meshes
  143. updateProgress(Load_InitShape, "Initialising shape...");
  144. sortDetails();
  145. // Install the TS memory helper into a TSShape object.
  146. install();
  147. return shape;
  148. }
  149. bool TSShapeLoader::processNode(AppNode* node)
  150. {
  151. // Detect bounds node
  152. if ( node->isBounds() )
  153. {
  154. if ( boundsNode )
  155. {
  156. Con::warnf( "More than one bounds node found" );
  157. return false;
  158. }
  159. boundsNode = node;
  160. // Process bounds geometry
  161. MatrixF boundsMat(boundsNode->getNodeTransform(DefaultTime));
  162. boundsMat.inverse();
  163. zapScale(boundsMat);
  164. for (S32 iMesh = 0; iMesh < boundsNode->getNumMesh(); iMesh++)
  165. {
  166. AppMesh* mesh = boundsNode->getMesh(iMesh);
  167. MatrixF transform = mesh->getMeshTransform(DefaultTime);
  168. transform.mulL(boundsMat);
  169. mesh->lockMesh(DefaultTime, transform);
  170. }
  171. return true;
  172. }
  173. // Detect sequence markers
  174. if ( node->isSequence() )
  175. {
  176. //appSequences.push_back(new AppSequence(node));
  177. return false;
  178. }
  179. // Add this node to the subshape (create one if needed)
  180. if ( subshapes.size() == 0 )
  181. subshapes.push_back( new TSShapeLoader::Subshape );
  182. subshapes.last()->branches.push_back( node );
  183. return true;
  184. }
  185. //-----------------------------------------------------------------------------
  186. // Nodes, meshes and skins
  187. typedef bool (*NameCmpFunc)(const String&, const Vector<String>&, void*, void*);
  188. bool cmpShapeName(const String& key, const Vector<String>& names, void* arg1, void* arg2)
  189. {
  190. for (S32 i = 0; i < names.size(); i++)
  191. {
  192. if (names[i].compare(key, 0, String::NoCase) == 0)
  193. return false;
  194. }
  195. return true;
  196. }
  197. String getUniqueName(const char* name, NameCmpFunc isNameUnique, const Vector<String>& names, void* arg1=0, void* arg2=0)
  198. {
  199. const S32 MAX_ITERATIONS = 0x10000; // maximum of 4 characters (A-P) will be appended
  200. String suffix;
  201. for (S32 i = 0; i < MAX_ITERATIONS; i++)
  202. {
  203. // Generate a suffix using the first 16 characters of the alphabet
  204. suffix.clear();
  205. for (S32 value = i; value != 0; value >>= 4)
  206. suffix = suffix + (char)('A' + (value & 0xF));
  207. String uname = name + suffix;
  208. if (isNameUnique(uname, names, arg1, arg2))
  209. return uname;
  210. }
  211. return name;
  212. }
  213. void TSShapeLoader::recurseSubshape(AppNode* appNode, S32 parentIndex, bool recurseChildren)
  214. {
  215. // Ignore local bounds nodes
  216. if (appNode->isBounds())
  217. return;
  218. S32 subShapeNum = shape->subShapeFirstNode.size()-1;
  219. Subshape* subshape = subshapes[subShapeNum];
  220. // Check if we should collapse this node
  221. S32 myIndex;
  222. if (ignoreNode(appNode->getName()))
  223. {
  224. myIndex = parentIndex;
  225. }
  226. else
  227. {
  228. // Check that adding this node will not exceed the maximum node count
  229. if (shape->nodes.size() >= MAX_TS_SET_SIZE)
  230. return;
  231. myIndex = shape->nodes.size();
  232. String nodeName = getUniqueName(appNode->getName(), cmpShapeName, shape->names);
  233. // Create the 3space node
  234. shape->nodes.increment();
  235. TSShape::Node& lastNode = shape->nodes.last();
  236. lastNode.nameIndex = shape->addName(nodeName);
  237. lastNode.parentIndex = parentIndex;
  238. lastNode.firstObject = -1;
  239. lastNode.firstChild = -1;
  240. lastNode.nextSibling = -1;
  241. // Add the AppNode to a matching list (so AppNodes can be accessed using 3space
  242. // node indices)
  243. appNodes.push_back(appNode);
  244. appNodes.last()->mParentIndex = parentIndex;
  245. // Check for NULL detail or AutoBillboard nodes (no children or geometry)
  246. if ((appNode->getNumChildNodes() == 0) &&
  247. (appNode->getNumMesh() == 0))
  248. {
  249. S32 size = 0x7FFFFFFF;
  250. String dname(String::GetTrailingNumber(appNode->getName(), size));
  251. if (dStrEqual(dname, "nulldetail") && (size != 0x7FFFFFFF))
  252. {
  253. shape->addDetail("detail", size, subShapeNum);
  254. }
  255. else if (appNode->isBillboard() && (size != 0x7FFFFFFF))
  256. {
  257. // AutoBillboard detail
  258. S32 numEquatorSteps = 4;
  259. S32 numPolarSteps = 0;
  260. F32 polarAngle = 0.0f;
  261. S32 dl = 0;
  262. S32 dim = 64;
  263. bool includePoles = true;
  264. appNode->getInt("BB::EQUATOR_STEPS", numEquatorSteps);
  265. appNode->getInt("BB::POLAR_STEPS", numPolarSteps);
  266. appNode->getFloat("BB::POLAR_ANGLE", polarAngle);
  267. appNode->getInt("BB::DL", dl);
  268. appNode->getInt("BB::DIM", dim);
  269. appNode->getBool("BB::INCLUDE_POLES", includePoles);
  270. S32 detIndex = shape->addDetail( "bbDetail", size, -1 );
  271. TSShape::Detail& detIndexDetail = shape->details[detIndex];
  272. detIndexDetail.bbEquatorSteps = numEquatorSteps;
  273. detIndexDetail.bbPolarSteps = numPolarSteps;
  274. detIndexDetail.bbDetailLevel = dl;
  275. detIndexDetail.bbDimension = dim;
  276. detIndexDetail.bbIncludePoles = includePoles;
  277. detIndexDetail.bbPolarAngle = polarAngle;
  278. }
  279. }
  280. }
  281. // Collect geometry
  282. for (U32 iMesh = 0; iMesh < appNode->getNumMesh(); iMesh++)
  283. {
  284. AppMesh* mesh = appNode->getMesh(iMesh);
  285. if (!ignoreMesh(mesh->getName()))
  286. {
  287. subshape->objMeshes.push_back(mesh);
  288. subshape->objNodes.push_back(mesh->isSkin() ? -1 : myIndex);
  289. }
  290. }
  291. // Create children
  292. if (recurseChildren)
  293. {
  294. for (S32 iChild = 0; iChild < appNode->getNumChildNodes(); iChild++)
  295. recurseSubshape(appNode->getChildNode(iChild), myIndex, true);
  296. }
  297. }
  298. void TSShapeLoader::generateSubshapes()
  299. {
  300. for (U32 iSub = 0; iSub < subshapes.size(); iSub++)
  301. {
  302. updateProgress(Load_GenerateSubshapes, "Generating subshapes...", subshapes.size(), iSub);
  303. Subshape* subshape = subshapes[iSub];
  304. // Recurse through the node hierarchy, adding 3space nodes and
  305. // collecting geometry
  306. S32 firstNode = shape->nodes.size();
  307. shape->subShapeFirstNode.push_back(firstNode);
  308. for (U32 iBranch = 0; iBranch < subshape->branches.size(); iBranch++)
  309. recurseSubshape(subshape->branches[iBranch], -1, true);
  310. shape->subShapeNumNodes.push_back(shape->nodes.size() - firstNode);
  311. if (shape->nodes.size() >= MAX_TS_SET_SIZE)
  312. {
  313. Con::warnf("Shape exceeds the maximum node count (%d). Ignoring additional nodes.",
  314. MAX_TS_SET_SIZE);
  315. }
  316. }
  317. }
  318. // Custom name comparison function to compare mesh name and detail size
  319. bool cmpMeshNameAndSize(const String& key, const Vector<String>& names, void* arg1, void* arg2)
  320. {
  321. const Vector<AppMesh*>& meshes = *(Vector<AppMesh*>*)arg1;
  322. S32 meshSize = (intptr_t)arg2;
  323. for (S32 i = 0; i < names.size(); i++)
  324. {
  325. if (names[i].compare(key, 0, String::NoCase) == 0)
  326. {
  327. if (meshes[i]->detailSize == meshSize)
  328. return false;
  329. }
  330. }
  331. return true;
  332. }
  333. void TSShapeLoader::generateObjects()
  334. {
  335. for (S32 iSub = 0; iSub < subshapes.size(); iSub++)
  336. {
  337. Subshape* subshape = subshapes[iSub];
  338. shape->subShapeFirstObject.push_back(shape->objects.size());
  339. // Get the names and sizes of the meshes for this subshape
  340. Vector<String> meshNames;
  341. for (S32 iMesh = 0; iMesh < subshape->objMeshes.size(); iMesh++)
  342. {
  343. AppMesh* mesh = subshape->objMeshes[iMesh];
  344. mesh->detailSize = 2;
  345. String name = String::GetTrailingNumber( mesh->getName(), mesh->detailSize );
  346. name = getUniqueName( name, cmpMeshNameAndSize, meshNames, &(subshape->objMeshes), (void*)mesh->detailSize );
  347. meshNames.push_back( name );
  348. // Fix up any collision details that don't have a negative detail level.
  349. if ( dStrStartsWith(meshNames[iMesh], "Collision") ||
  350. dStrStartsWith(meshNames[iMesh], "LOSCol") )
  351. {
  352. if (mesh->detailSize > 0)
  353. mesh->detailSize = -mesh->detailSize;
  354. }
  355. }
  356. // An 'object' is a collection of meshes with the same base name and
  357. // different detail sizes. The object is attached to the node of the
  358. // highest detail mesh.
  359. // Sort the 3 arrays (objMeshes, objNodes, meshNames) by name and size
  360. for (S32 i = 0; i < subshape->objMeshes.size()-1; i++)
  361. {
  362. for (S32 j = i+1; j < subshape->objMeshes.size(); j++)
  363. {
  364. if ((meshNames[i].compare(meshNames[j]) < 0) ||
  365. ((meshNames[i].compare(meshNames[j]) == 0) &&
  366. (subshape->objMeshes[i]->detailSize < subshape->objMeshes[j]->detailSize)))
  367. {
  368. {
  369. AppMesh* tmp = subshape->objMeshes[i];
  370. subshape->objMeshes[i] = subshape->objMeshes[j];
  371. subshape->objMeshes[j] = tmp;
  372. }
  373. {
  374. S32 tmp = subshape->objNodes[i];
  375. subshape->objNodes[i] = subshape->objNodes[j];
  376. subshape->objNodes[j] = tmp;
  377. }
  378. {
  379. String tmp = meshNames[i];
  380. meshNames[i] = meshNames[j];
  381. meshNames[j] = tmp;
  382. }
  383. }
  384. }
  385. }
  386. // Now create objects
  387. const String* lastName = 0;
  388. for (S32 iMesh = 0; iMesh < subshape->objMeshes.size(); iMesh++)
  389. {
  390. AppMesh* mesh = subshape->objMeshes[iMesh];
  391. if (!lastName || (meshNames[iMesh] != *lastName))
  392. {
  393. shape->objects.increment();
  394. TSShape::Object& lastObject = shape->objects.last();
  395. lastObject.nameIndex = shape->addName(meshNames[iMesh]);
  396. lastObject.nodeIndex = subshape->objNodes[iMesh];
  397. lastObject.startMeshIndex = appMeshes.size();
  398. lastObject.numMeshes = 0;
  399. lastName = &meshNames[iMesh];
  400. }
  401. // Add this mesh to the object
  402. appMeshes.push_back(mesh);
  403. shape->objects.last().numMeshes++;
  404. // Set mesh flags
  405. mesh->flags = 0;
  406. if (mesh->isBillboard())
  407. {
  408. mesh->flags |= TSMesh::Billboard;
  409. if (mesh->isBillboardZAxis())
  410. mesh->flags |= TSMesh::BillboardZAxis;
  411. }
  412. // Set the detail name... do fixups for collision details.
  413. const char* detailName = "detail";
  414. if ( mesh->detailSize < 0 )
  415. {
  416. if ( dStrStartsWith(meshNames[iMesh], "Collision") ||
  417. dStrStartsWith(meshNames[iMesh], "Col") )
  418. detailName = "Collision";
  419. else if (dStrStartsWith(meshNames[iMesh], "LOSCol"))
  420. detailName = "LOS";
  421. }
  422. // Attempt to add the detail (will fail if it already exists)
  423. S32 oldNumDetails = shape->details.size();
  424. shape->addDetail(detailName, mesh->detailSize, iSub);
  425. if (shape->details.size() > oldNumDetails)
  426. {
  427. Con::warnf("Object mesh \"%s\" has no matching detail (\"%s%d\" has"
  428. " been added automatically)", mesh->getName(false), detailName, mesh->detailSize);
  429. }
  430. }
  431. // Get object count for this subshape
  432. shape->subShapeNumObjects.push_back(shape->objects.size() - shape->subShapeFirstObject.last());
  433. }
  434. }
  435. void TSShapeLoader::generateSkins()
  436. {
  437. Vector<AppMesh*> skins;
  438. for (S32 iObject = 0; iObject < shape->objects.size(); iObject++)
  439. {
  440. for (S32 iMesh = 0; iMesh < shape->objects[iObject].numMeshes; iMesh++)
  441. {
  442. AppMesh* mesh = appMeshes[shape->objects[iObject].startMeshIndex + iMesh];
  443. if (mesh->isSkin())
  444. skins.push_back(mesh);
  445. }
  446. }
  447. for (S32 iSkin = 0; iSkin < skins.size(); iSkin++)
  448. {
  449. updateProgress(Load_GenerateSkins, "Generating skins...", skins.size(), iSkin);
  450. // Get skin data (bones, vertex weights etc)
  451. AppMesh* skin = skins[iSkin];
  452. skin->lookupSkinData();
  453. // Just copy initial verts and norms for now
  454. skin->initialVerts.set(skin->points.address(), skin->vertsPerFrame);
  455. skin->initialNorms.set(skin->normals.address(), skin->vertsPerFrame);
  456. // Map bones to nodes
  457. skin->nodeIndex.setSize(skin->bones.size());
  458. for (S32 iBone = 0; iBone < skin->bones.size(); iBone++)
  459. {
  460. // Find the node that matches this bone
  461. skin->nodeIndex[iBone] = -1;
  462. for (S32 iNode = 0; iNode < appNodes.size(); iNode++)
  463. {
  464. if (appNodes[iNode]->isEqual(skin->bones[iBone]))
  465. {
  466. delete skin->bones[iBone];
  467. skin->bones[iBone] = appNodes[iNode];
  468. skin->nodeIndex[iBone] = iNode;
  469. break;
  470. }
  471. }
  472. if (skin->nodeIndex[iBone] == -1)
  473. {
  474. Con::warnf("Could not find bone %d. Defaulting to first node", iBone);
  475. skin->nodeIndex[iBone] = 0;
  476. }
  477. }
  478. }
  479. }
  480. void TSShapeLoader::generateDefaultStates()
  481. {
  482. // Generate default object states (includes initial geometry)
  483. for (S32 iObject = 0; iObject < shape->objects.size(); iObject++)
  484. {
  485. updateProgress(Load_GenerateDefaultStates, "Generating initial mesh and node states...",
  486. shape->objects.size(), iObject);
  487. TSShape::Object& obj = shape->objects[iObject];
  488. // Calculate the objectOffset for each mesh at T=0
  489. for (S32 iMesh = 0; iMesh < obj.numMeshes; iMesh++)
  490. {
  491. AppMesh* appMesh = appMeshes[obj.startMeshIndex + iMesh];
  492. AppNode* appNode = obj.nodeIndex >= 0 ? appNodes[obj.nodeIndex] : boundsNode;
  493. MatrixF meshMat(appMesh->getMeshTransform(DefaultTime));
  494. MatrixF nodeMat(appMesh->isSkin() ? meshMat : appNode->getNodeTransform(DefaultTime));
  495. zapScale(nodeMat);
  496. appMesh->objectOffset = nodeMat.inverse() * meshMat;
  497. }
  498. generateObjectState(shape->objects[iObject], DefaultTime, true, true);
  499. }
  500. // Generate default node transforms
  501. for (S32 iNode = 0; iNode < appNodes.size(); iNode++)
  502. {
  503. // Determine the default translation and rotation for the node
  504. QuatF rot, srot;
  505. Point3F trans, scale;
  506. generateNodeTransform(appNodes[iNode], DefaultTime, false, 0, rot, trans, srot, scale);
  507. // Add default node translation and rotation
  508. addNodeRotation(rot, true);
  509. addNodeTranslation(trans, true);
  510. }
  511. }
  512. void TSShapeLoader::generateObjectState(TSShape::Object& obj, F32 t, bool addFrame, bool addMatFrame)
  513. {
  514. shape->objectStates.increment();
  515. TSShape::ObjectState& state = shape->objectStates.last();
  516. state.frameIndex = 0;
  517. state.matFrameIndex = 0;
  518. state.vis = mClampF(appMeshes[obj.startMeshIndex]->getVisValue(t), 0.0f, 1.0f);
  519. if (addFrame || addMatFrame)
  520. {
  521. generateFrame(obj, t, addFrame, addMatFrame);
  522. // set the frame number for the object state
  523. state.frameIndex = appMeshes[obj.startMeshIndex]->numFrames - 1;
  524. state.matFrameIndex = appMeshes[obj.startMeshIndex]->numMatFrames - 1;
  525. }
  526. }
  527. void TSShapeLoader::generateFrame(TSShape::Object& obj, F32 t, bool addFrame, bool addMatFrame)
  528. {
  529. for (S32 iMesh = 0; iMesh < obj.numMeshes; iMesh++)
  530. {
  531. AppMesh* appMesh = appMeshes[obj.startMeshIndex + iMesh];
  532. U32 oldNumPoints = appMesh->points.size();
  533. U32 oldNumUvs = appMesh->uvs.size();
  534. // Get the mesh geometry at time, 't'
  535. // Geometry verts, normals and tverts can be animated (different set for
  536. // each frame), but the TSDrawPrimitives stay the same, so the way lockMesh
  537. // works is that it will only generate the primitives once, then after that
  538. // will just append verts, normals and tverts each time it is called.
  539. appMesh->lockMesh(t, appMesh->objectOffset);
  540. // Calculate vertex normals if required
  541. if (appMesh->normals.size() != appMesh->points.size())
  542. appMesh->computeNormals();
  543. // If this is the first call, set the number of points per frame
  544. if (appMesh->numFrames == 0)
  545. {
  546. appMesh->vertsPerFrame = appMesh->points.size();
  547. }
  548. else
  549. {
  550. // Check frame topology => ie. that the right number of points, normals
  551. // and tverts was added
  552. if ((appMesh->points.size() - oldNumPoints) != appMesh->vertsPerFrame)
  553. {
  554. Con::warnf("Wrong number of points (%d) added at time=%f (expected %d)",
  555. appMesh->points.size() - oldNumPoints, t, appMesh->vertsPerFrame);
  556. addFrame = false;
  557. }
  558. if ((appMesh->normals.size() - oldNumPoints) != appMesh->vertsPerFrame)
  559. {
  560. Con::warnf("Wrong number of normals (%d) added at time=%f (expected %d)",
  561. appMesh->normals.size() - oldNumPoints, t, appMesh->vertsPerFrame);
  562. addFrame = false;
  563. }
  564. if ((appMesh->uvs.size() - oldNumUvs) != appMesh->vertsPerFrame)
  565. {
  566. Con::warnf("Wrong number of tverts (%d) added at time=%f (expected %d)",
  567. appMesh->uvs.size() - oldNumUvs, t, appMesh->vertsPerFrame);
  568. addMatFrame = false;
  569. }
  570. }
  571. // Because lockMesh adds points, normals AND tverts each call, if we didn't
  572. // actually want another frame or matFrame, we need to remove them afterwards.
  573. // In the common case (we DO want the frame), we can do nothing => the
  574. // points/normals/tverts are already in place!
  575. if (addFrame)
  576. {
  577. appMesh->numFrames++;
  578. }
  579. else
  580. {
  581. appMesh->points.setSize(oldNumPoints);
  582. appMesh->normals.setSize(oldNumPoints);
  583. }
  584. if (addMatFrame)
  585. {
  586. appMesh->numMatFrames++;
  587. }
  588. else
  589. {
  590. appMesh->uvs.setSize(oldNumPoints);
  591. }
  592. }
  593. }
  594. //-----------------------------------------------------------------------------
  595. // Materials
  596. /// Convert all Collada materials into a single TSMaterialList
  597. void TSShapeLoader::generateMaterialList()
  598. {
  599. // Install the materials into the material list
  600. shape->materialList = new TSMaterialList;
  601. for (S32 iMat = 0; iMat < AppMesh::appMaterials.size(); iMat++)
  602. {
  603. updateProgress(Load_GenerateMaterials, "Generating materials...", AppMesh::appMaterials.size(), iMat);
  604. AppMaterial* appMat = AppMesh::appMaterials[iMat];
  605. shape->materialList->push_back(appMat->getName(), appMat->getFlags(), U32(-1), U32(-1), U32(-1), 1.0f, appMat->getReflectance());
  606. }
  607. }
  608. //-----------------------------------------------------------------------------
  609. // Animation Sequences
  610. void TSShapeLoader::generateSequences()
  611. {
  612. for (S32 iSeq = 0; iSeq < appSequences.size(); iSeq++)
  613. {
  614. updateProgress(Load_GenerateSequences, "Generating sequences...", appSequences.size(), iSeq);
  615. // Initialize the sequence
  616. appSequences[iSeq]->setActive(true);
  617. shape->sequences.increment();
  618. TSShape::Sequence& seq = shape->sequences.last();
  619. seq.nameIndex = shape->addName(appSequences[iSeq]->getName());
  620. seq.toolBegin = appSequences[iSeq]->getStart();
  621. seq.priority = appSequences[iSeq]->getPriority();
  622. seq.flags = appSequences[iSeq]->getFlags();
  623. // Compute duration and number of keyframes (then adjust time between frames to match)
  624. seq.duration = appSequences[iSeq]->getEnd() - appSequences[iSeq]->getStart();
  625. seq.numKeyframes = (S32)(seq.duration * appSequences[iSeq]->fps + 0.5f) + 1;
  626. seq.sourceData.start = 0;
  627. seq.sourceData.end = seq.numKeyframes-1;
  628. seq.sourceData.total = seq.numKeyframes;
  629. // Set membership arrays (ie. which nodes and objects are affected by this sequence)
  630. setNodeMembership(seq, appSequences[iSeq]);
  631. setObjectMembership(seq, appSequences[iSeq]);
  632. // Generate keyframes
  633. generateNodeAnimation(seq);
  634. generateObjectAnimation(seq, appSequences[iSeq]);
  635. generateGroundAnimation(seq, appSequences[iSeq]);
  636. generateFrameTriggers(seq, appSequences[iSeq]);
  637. // Set sequence flags
  638. seq.dirtyFlags = 0;
  639. if (seq.rotationMatters.testAll() || seq.translationMatters.testAll() || seq.scaleMatters.testAll())
  640. seq.dirtyFlags |= TSShapeInstance::TransformDirty;
  641. if (seq.visMatters.testAll())
  642. seq.dirtyFlags |= TSShapeInstance::VisDirty;
  643. if (seq.frameMatters.testAll())
  644. seq.dirtyFlags |= TSShapeInstance::FrameDirty;
  645. if (seq.matFrameMatters.testAll())
  646. seq.dirtyFlags |= TSShapeInstance::MatFrameDirty;
  647. // Set shape flags (only the most significant scale type)
  648. U32 curVal = shape->mFlags & TSShape::AnyScale;
  649. shape->mFlags &= ~(TSShape::AnyScale);
  650. shape->mFlags |= getMax(curVal, seq.flags & TSShape::AnyScale); // take the larger value (can only convert upwards)
  651. appSequences[iSeq]->setActive(false);
  652. }
  653. }
  654. void TSShapeLoader::setNodeMembership(TSShape::Sequence& seq, const AppSequence* appSeq)
  655. {
  656. seq.rotationMatters.clearAll(); // node rotation (size = nodes.size())
  657. seq.translationMatters.clearAll(); // node translation (size = nodes.size())
  658. seq.scaleMatters.clearAll(); // node scale (size = nodes.size())
  659. // This shouldn't be allowed, but check anyway...
  660. if (seq.numKeyframes < 2)
  661. return;
  662. // Note: this fills the cache with current sequence data. Methods that get
  663. // called later (e.g. generateNodeAnimation) use this info (and assume it's set).
  664. fillNodeTransformCache(seq, appSeq);
  665. // Test to see if the transform changes over the interval in order to decide
  666. // whether to animate the transform in 3space. We don't use app's mechanism
  667. // for doing this because it functions different in different apps and we do
  668. // some special stuff with scale.
  669. setRotationMembership(seq);
  670. setTranslationMembership(seq);
  671. setScaleMembership(seq);
  672. }
  673. void TSShapeLoader::setRotationMembership(TSShape::Sequence& seq)
  674. {
  675. for (S32 iNode = 0; iNode < appNodes.size(); iNode++)
  676. {
  677. // Check if any of the node rotations are different to
  678. // the default rotation
  679. QuatF defaultRot;
  680. shape->defaultRotations[iNode].getQuatF(&defaultRot);
  681. for (S32 iFrame = 0; iFrame < seq.numKeyframes; iFrame++)
  682. {
  683. if (nodeRotCache[iNode][iFrame] != defaultRot)
  684. {
  685. seq.rotationMatters.set(iNode);
  686. break;
  687. }
  688. }
  689. }
  690. }
  691. void TSShapeLoader::setTranslationMembership(TSShape::Sequence& seq)
  692. {
  693. for (S32 iNode = 0; iNode < appNodes.size(); iNode++)
  694. {
  695. // Check if any of the node translations are different to
  696. // the default translation
  697. Point3F& defaultTrans = shape->defaultTranslations[iNode];
  698. for (S32 iFrame = 0; iFrame < seq.numKeyframes; iFrame++)
  699. {
  700. if (!nodeTransCache[iNode][iFrame].equal(defaultTrans))
  701. {
  702. seq.translationMatters.set(iNode);
  703. break;
  704. }
  705. }
  706. }
  707. }
  708. void TSShapeLoader::setScaleMembership(TSShape::Sequence& seq)
  709. {
  710. Point3F unitScale(1,1,1);
  711. U32 arbitraryScaleCount = 0;
  712. U32 alignedScaleCount = 0;
  713. U32 uniformScaleCount = 0;
  714. for (S32 iNode = 0; iNode < appNodes.size(); iNode++)
  715. {
  716. // Check if any of the node scales are not the unit scale
  717. for (S32 iFrame = 0; iFrame < seq.numKeyframes; iFrame++)
  718. {
  719. Point3F& scale = nodeScaleCache[iNode][iFrame];
  720. if (!unitScale.equal(scale))
  721. {
  722. // Determine what type of scale this is
  723. if (!nodeScaleRotCache[iNode][iFrame].isIdentity())
  724. arbitraryScaleCount++;
  725. else if (scale.x != scale.y || scale.y != scale.z)
  726. alignedScaleCount++;
  727. else
  728. uniformScaleCount++;
  729. seq.scaleMatters.set(iNode);
  730. break;
  731. }
  732. }
  733. }
  734. // Only one type of scale is animated
  735. if (arbitraryScaleCount)
  736. seq.flags |= TSShape::ArbitraryScale;
  737. else if (alignedScaleCount)
  738. seq.flags |= TSShape::AlignedScale;
  739. else if (uniformScaleCount)
  740. seq.flags |= TSShape::UniformScale;
  741. }
  742. void TSShapeLoader::setObjectMembership(TSShape::Sequence& seq, const AppSequence* appSeq)
  743. {
  744. seq.visMatters.clearAll(); // object visibility (size = objects.size())
  745. seq.frameMatters.clearAll(); // vert animation (morph) (size = objects.size())
  746. seq.matFrameMatters.clearAll(); // UV animation (size = objects.size())
  747. for (S32 iObject = 0; iObject < shape->objects.size(); iObject++)
  748. {
  749. if (!appMeshes[shape->objects[iObject].startMeshIndex])
  750. continue;
  751. if (appMeshes[shape->objects[iObject].startMeshIndex]->animatesVis(appSeq))
  752. seq.visMatters.set(iObject);
  753. // Morph and UV animation has been deprecated
  754. //if (appMeshes[shape->objects[iObject].startMeshIndex]->animatesFrame(appSeq))
  755. //seq.frameMatters.set(iObject);
  756. //if (appMeshes[shape->objects[iObject].startMeshIndex]->animatesMatFrame(appSeq))
  757. //seq.matFrameMatters.set(iObject);
  758. }
  759. }
  760. void TSShapeLoader::clearNodeTransformCache()
  761. {
  762. // clear out the transform caches
  763. for (S32 i = 0; i < nodeRotCache.size(); i++)
  764. delete [] nodeRotCache[i];
  765. nodeRotCache.clear();
  766. for (S32 i = 0; i < nodeTransCache.size(); i++)
  767. delete [] nodeTransCache[i];
  768. nodeTransCache.clear();
  769. for (S32 i = 0; i < nodeScaleRotCache.size(); i++)
  770. delete [] nodeScaleRotCache[i];
  771. nodeScaleRotCache.clear();
  772. for (S32 i = 0; i < nodeScaleCache.size(); i++)
  773. delete [] nodeScaleCache[i];
  774. nodeScaleCache.clear();
  775. }
  776. void TSShapeLoader::fillNodeTransformCache(TSShape::Sequence& seq, const AppSequence* appSeq)
  777. {
  778. // clear out the transform caches and set it up for this sequence
  779. clearNodeTransformCache();
  780. nodeRotCache.setSize(appNodes.size());
  781. for (S32 i = 0; i < nodeRotCache.size(); i++)
  782. nodeRotCache[i] = new QuatF[seq.numKeyframes];
  783. nodeTransCache.setSize(appNodes.size());
  784. for (S32 i = 0; i < nodeTransCache.size(); i++)
  785. nodeTransCache[i] = new Point3F[seq.numKeyframes];
  786. nodeScaleRotCache.setSize(appNodes.size());
  787. for (S32 i = 0; i < nodeScaleRotCache.size(); i++)
  788. nodeScaleRotCache[i] = new QuatF[seq.numKeyframes];
  789. nodeScaleCache.setSize(appNodes.size());
  790. for (S32 i = 0; i < nodeScaleCache.size(); i++)
  791. nodeScaleCache[i] = new Point3F[seq.numKeyframes];
  792. // get the node transforms for every frame
  793. for (S32 iFrame = 0; iFrame < seq.numKeyframes; iFrame++)
  794. {
  795. F32 time = appSeq->getStart() + seq.duration * iFrame / getMax(1, seq.numKeyframes - 1);
  796. for (S32 iNode = 0; iNode < appNodes.size(); iNode++)
  797. {
  798. generateNodeTransform(appNodes[iNode], time, seq.isBlend(), appSeq->getBlendRefTime(),
  799. nodeRotCache[iNode][iFrame], nodeTransCache[iNode][iFrame],
  800. nodeScaleRotCache[iNode][iFrame], nodeScaleCache[iNode][iFrame]);
  801. }
  802. }
  803. }
  804. void TSShapeLoader::addNodeRotation(QuatF& rot, bool defaultVal)
  805. {
  806. Quat16 rot16;
  807. rot16.set(rot);
  808. if (!defaultVal)
  809. shape->nodeRotations.push_back(rot16);
  810. else
  811. shape->defaultRotations.push_back(rot16);
  812. }
  813. void TSShapeLoader::addNodeTranslation(Point3F& trans, bool defaultVal)
  814. {
  815. if (!defaultVal)
  816. shape->nodeTranslations.push_back(trans);
  817. else
  818. shape->defaultTranslations.push_back(trans);
  819. }
  820. void TSShapeLoader::addNodeUniformScale(F32 scale)
  821. {
  822. shape->nodeUniformScales.push_back(scale);
  823. }
  824. void TSShapeLoader::addNodeAlignedScale(Point3F& scale)
  825. {
  826. shape->nodeAlignedScales.push_back(scale);
  827. }
  828. void TSShapeLoader::addNodeArbitraryScale(QuatF& qrot, Point3F& scale)
  829. {
  830. Quat16 rot16;
  831. rot16.set(qrot);
  832. shape->nodeArbitraryScaleRots.push_back(rot16);
  833. shape->nodeArbitraryScaleFactors.push_back(scale);
  834. }
  835. void TSShapeLoader::generateNodeAnimation(TSShape::Sequence& seq)
  836. {
  837. seq.baseRotation = shape->nodeRotations.size();
  838. seq.baseTranslation = shape->nodeTranslations.size();
  839. seq.baseScale = (seq.flags & TSShape::ArbitraryScale) ? shape->nodeArbitraryScaleRots.size() :
  840. (seq.flags & TSShape::AlignedScale) ? shape->nodeAlignedScales.size() :
  841. shape->nodeUniformScales.size();
  842. for (S32 iNode = 0; iNode < appNodes.size(); iNode++)
  843. {
  844. for (S32 iFrame = 0; iFrame < seq.numKeyframes; iFrame++)
  845. {
  846. if (seq.rotationMatters.test(iNode))
  847. addNodeRotation(nodeRotCache[iNode][iFrame], false);
  848. if (seq.translationMatters.test(iNode))
  849. addNodeTranslation(nodeTransCache[iNode][iFrame], false);
  850. if (seq.scaleMatters.test(iNode))
  851. {
  852. QuatF& rot = nodeScaleRotCache[iNode][iFrame];
  853. Point3F scale = nodeScaleCache[iNode][iFrame];
  854. if (seq.flags & TSShape::ArbitraryScale)
  855. addNodeArbitraryScale(rot, scale);
  856. else if (seq.flags & TSShape::AlignedScale)
  857. addNodeAlignedScale(scale);
  858. else if (seq.flags & TSShape::UniformScale)
  859. addNodeUniformScale((scale.x+scale.y+scale.z)/3.0f);
  860. }
  861. }
  862. }
  863. }
  864. void TSShapeLoader::generateObjectAnimation(TSShape::Sequence& seq, const AppSequence* appSeq)
  865. {
  866. seq.baseObjectState = shape->objectStates.size();
  867. for (S32 iObject = 0; iObject < shape->objects.size(); iObject++)
  868. {
  869. bool visMatters = seq.visMatters.test(iObject);
  870. bool frameMatters = seq.frameMatters.test(iObject);
  871. bool matFrameMatters = seq.matFrameMatters.test(iObject);
  872. if (visMatters || frameMatters || matFrameMatters)
  873. {
  874. for (S32 iFrame = 0; iFrame < seq.numKeyframes; iFrame++)
  875. {
  876. F32 time = appSeq->getStart() + seq.duration * iFrame / getMax(1, seq.numKeyframes - 1);
  877. generateObjectState(shape->objects[iObject], time, frameMatters, matFrameMatters);
  878. }
  879. }
  880. }
  881. }
  882. void TSShapeLoader::generateGroundAnimation(TSShape::Sequence& seq, const AppSequence* appSeq)
  883. {
  884. seq.firstGroundFrame = shape->groundTranslations.size();
  885. seq.numGroundFrames = 0;
  886. if (!boundsNode)
  887. return;
  888. // Check if the bounds node is animated by this sequence
  889. seq.numGroundFrames = (S32)((seq.duration + 0.25f/AppGroundFrameRate) * AppGroundFrameRate);
  890. seq.flags |= TSShape::MakePath;
  891. // Get ground transform at the start of the sequence
  892. MatrixF invStartMat = boundsNode->getNodeTransform(appSeq->getStart());
  893. zapScale(invStartMat);
  894. invStartMat.inverse();
  895. for (S32 iFrame = 0; iFrame < seq.numGroundFrames; iFrame++)
  896. {
  897. F32 time = appSeq->getStart() + seq.duration * iFrame / getMax(1, seq.numGroundFrames - 1);
  898. // Determine delta bounds node transform at 't'
  899. MatrixF mat = boundsNode->getNodeTransform(time);
  900. zapScale(mat);
  901. mat = invStartMat * mat;
  902. // Add ground transform
  903. Quat16 rotation;
  904. rotation.set(QuatF(mat));
  905. shape->groundTranslations.push_back(mat.getPosition());
  906. shape->groundRotations.push_back(rotation);
  907. }
  908. }
  909. void TSShapeLoader::generateFrameTriggers(TSShape::Sequence& seq, const AppSequence* appSeq)
  910. {
  911. // Initialize triggers
  912. seq.firstTrigger = shape->triggers.size();
  913. seq.numTriggers = appSeq->getNumTriggers();
  914. if (!seq.numTriggers)
  915. return;
  916. seq.flags |= TSShape::MakePath;
  917. // Add triggers
  918. for (S32 iTrigger = 0; iTrigger < seq.numTriggers; iTrigger++)
  919. {
  920. shape->triggers.increment();
  921. appSeq->getTrigger(iTrigger, shape->triggers.last());
  922. }
  923. // Track the triggers that get turned off by this shape...normally, triggers
  924. // aren't turned on/off, just on...if we are a trigger that does both then we
  925. // need to mark ourselves as such so that on/off can become off/on when sequence
  926. // is played in reverse...
  927. U32 offTriggers = 0;
  928. for (S32 iTrigger = 0; iTrigger < seq.numTriggers; iTrigger++)
  929. {
  930. U32 state = shape->triggers[seq.firstTrigger+iTrigger].state;
  931. if ((state & TSShape::Trigger::StateOn) == 0)
  932. offTriggers |= (state & TSShape::Trigger::StateMask);
  933. }
  934. // We now know which states are turned off, set invert on all those (including when turned on)
  935. for (int iTrigger = 0; iTrigger < seq.numTriggers; iTrigger++)
  936. {
  937. if (shape->triggers[seq.firstTrigger + iTrigger].state & offTriggers)
  938. shape->triggers[seq.firstTrigger + iTrigger].state |= TSShape::Trigger::InvertOnReverse;
  939. }
  940. }
  941. //-----------------------------------------------------------------------------
  942. void TSShapeLoader::sortDetails()
  943. {
  944. // Sort objects by: transparency, material index and node index
  945. // Insert NULL meshes where required
  946. for (S32 iSub = 0; iSub < subshapes.size(); iSub++)
  947. {
  948. Vector<S32> validDetails;
  949. shape->getSubShapeDetails(iSub, validDetails);
  950. for (S32 iDet = 0; iDet < validDetails.size(); iDet++)
  951. {
  952. TSShape::Detail &detail = shape->details[validDetails[iDet]];
  953. if (detail.subShapeNum >= 0)
  954. detail.objectDetailNum = iDet;
  955. for (S32 iObj = shape->subShapeFirstObject[iSub];
  956. iObj < (shape->subShapeFirstObject[iSub] + shape->subShapeNumObjects[iSub]);
  957. iObj++)
  958. {
  959. TSShape::Object &object = shape->objects[iObj];
  960. // Insert a NULL mesh for this detail level if required (ie. if the
  961. // object does not already have a mesh with an equal or higher detail)
  962. S32 meshIndex = (iDet < object.numMeshes) ? iDet : object.numMeshes-1;
  963. if (appMeshes[object.startMeshIndex + meshIndex]->detailSize < shape->details[iDet].size)
  964. {
  965. // Add a NULL mesh
  966. appMeshes.insert(object.startMeshIndex + iDet, NULL);
  967. object.numMeshes++;
  968. // Fixup the start index for the other objects
  969. for (S32 k = iObj+1; k < shape->objects.size(); k++)
  970. shape->objects[k].startMeshIndex++;
  971. }
  972. }
  973. }
  974. }
  975. }
  976. // Install into the TSShape, the shape is expected to be empty.
  977. // Data is not copied, the TSShape is modified to point to memory
  978. // managed by this object. This object is also bound to the TSShape
  979. // object and will be deleted when it's deleted.
  980. void TSShapeLoader::install()
  981. {
  982. // Arrays that are filled in by ts shape init, but need
  983. // to be allocated beforehand.
  984. shape->subShapeFirstTranslucentObject.setSize(shape->subShapeFirstObject.size());
  985. // Construct TS sub-meshes
  986. shape->meshes.setSize(appMeshes.size());
  987. for (U32 m = 0; m < appMeshes.size(); m++)
  988. shape->meshes[m] = appMeshes[m] ? appMeshes[m]->constructTSMesh() : NULL;
  989. // Remove empty meshes and objects
  990. for (S32 iObj = shape->objects.size()-1; iObj >= 0; iObj--)
  991. {
  992. TSShape::Object& obj = shape->objects[iObj];
  993. for (S32 iMesh = obj.numMeshes-1; iMesh >= 0; iMesh--)
  994. {
  995. TSMesh *mesh = shape->meshes[obj.startMeshIndex + iMesh];
  996. if (mesh && !mesh->primitives.size())
  997. {
  998. S32 oldMeshCount = obj.numMeshes;
  999. destructInPlace(mesh);
  1000. shape->removeMeshFromObject(iObj, iMesh);
  1001. iMesh -= (oldMeshCount - obj.numMeshes - 1); // handle when more than one mesh is removed
  1002. }
  1003. }
  1004. if (!obj.numMeshes)
  1005. shape->removeObject(shape->getName(obj.nameIndex));
  1006. }
  1007. // Add a dummy object if needed so the shape loads and renders ok
  1008. if (!shape->details.size())
  1009. {
  1010. shape->addDetail("detail", 2, 0);
  1011. shape->subShapeNumObjects.last() = 1;
  1012. shape->meshes.push_back(NULL);
  1013. shape->objects.increment();
  1014. TSShape::Object& lastObject = shape->objects.last();
  1015. lastObject.nameIndex = shape->addName("dummy");
  1016. lastObject.nodeIndex = 0;
  1017. lastObject.startMeshIndex = 0;
  1018. lastObject.numMeshes = 1;
  1019. shape->objectStates.increment();
  1020. shape->objectStates.last().frameIndex = 0;
  1021. shape->objectStates.last().matFrameIndex = 0;
  1022. shape->objectStates.last().vis = 1.0f;
  1023. }
  1024. // Update smallest visible detail
  1025. shape->mSmallestVisibleDL = -1;
  1026. shape->mSmallestVisibleSize = 999999;
  1027. for (S32 i = 0; i < shape->details.size(); i++)
  1028. {
  1029. if ((shape->details[i].size >= 0) &&
  1030. (shape->details[i].size < shape->mSmallestVisibleSize))
  1031. {
  1032. shape->mSmallestVisibleDL = i;
  1033. shape->mSmallestVisibleSize = shape->details[i].size;
  1034. }
  1035. }
  1036. computeBounds(shape->bounds);
  1037. if (!shape->bounds.isValidBox())
  1038. shape->bounds = Box3F(1.0f);
  1039. shape->bounds.getCenter(&shape->center);
  1040. shape->radius = (shape->bounds.maxExtents - shape->center).len();
  1041. shape->tubeRadius = shape->radius;
  1042. shape->init();
  1043. }
  1044. void TSShapeLoader::computeBounds(Box3F& bounds)
  1045. {
  1046. // Compute the box that encloses the model geometry
  1047. bounds = Box3F::Invalid;
  1048. // Use bounds node geometry if present
  1049. if ( boundsNode && boundsNode->getNumMesh() )
  1050. {
  1051. for (S32 iMesh = 0; iMesh < boundsNode->getNumMesh(); iMesh++)
  1052. {
  1053. AppMesh* mesh = boundsNode->getMesh( iMesh );
  1054. if ( !mesh )
  1055. continue;
  1056. Box3F meshBounds;
  1057. mesh->computeBounds( meshBounds );
  1058. if ( meshBounds.isValidBox() )
  1059. bounds.intersect( meshBounds );
  1060. }
  1061. }
  1062. else
  1063. {
  1064. // Compute bounds based on all geometry in the model
  1065. for (S32 iMesh = 0; iMesh < appMeshes.size(); iMesh++)
  1066. {
  1067. AppMesh* mesh = appMeshes[iMesh];
  1068. if ( !mesh )
  1069. continue;
  1070. Box3F meshBounds;
  1071. mesh->computeBounds( meshBounds );
  1072. if ( meshBounds.isValidBox() )
  1073. bounds.intersect( meshBounds );
  1074. }
  1075. }
  1076. }
  1077. TSShapeLoader::~TSShapeLoader()
  1078. {
  1079. clearNodeTransformCache();
  1080. // Clear shared AppMaterial list
  1081. for (S32 iMat = 0; iMat < AppMesh::appMaterials.size(); iMat++)
  1082. delete AppMesh::appMaterials[iMat];
  1083. AppMesh::appMaterials.clear();
  1084. // Delete Subshapes
  1085. delete boundsNode;
  1086. for (S32 iSub = 0; iSub < subshapes.size(); iSub++)
  1087. delete subshapes[iSub];
  1088. // Delete AppSequences
  1089. for (S32 iSeq = 0; iSeq < appSequences.size(); iSeq++)
  1090. delete appSequences[iSeq];
  1091. appSequences.clear();
  1092. }
  1093. // Static functions to handle supported formats for shape loader.
  1094. void TSShapeLoader::addFormat(String name, String extension)
  1095. {
  1096. ShapeFormat newFormat;
  1097. newFormat.mName = name;
  1098. newFormat.mExtension = extension;
  1099. smFormats.push_back(newFormat);
  1100. }
  1101. String TSShapeLoader::getFormatExtensions()
  1102. {
  1103. // "*.dsq TAB *.dae TAB
  1104. StringBuilder output;
  1105. for(U32 n = 0; n < smFormats.size(); ++n)
  1106. {
  1107. output.append("*.");
  1108. output.append(smFormats[n].mExtension);
  1109. output.append("\t");
  1110. }
  1111. return output.end();
  1112. }
  1113. String TSShapeLoader::getFormatFilters()
  1114. {
  1115. // "DSQ Files|*.dsq|COLLADA Files|*.dae|"
  1116. StringBuilder output;
  1117. for(U32 n = 0; n < smFormats.size(); ++n)
  1118. {
  1119. output.append(smFormats[n].mName);
  1120. output.append("|*.");
  1121. output.append(smFormats[n].mExtension);
  1122. output.append("|");
  1123. }
  1124. return output.end();
  1125. }
  1126. DefineConsoleFunction( getFormatExtensions, const char*, ( ),,
  1127. "Returns a list of supported shape format extensions separated by tabs."
  1128. "Example output: *.dsq TAB *.dae TAB")
  1129. {
  1130. return Con::getReturnBuffer(TSShapeLoader::getFormatExtensions());
  1131. }
  1132. DefineConsoleFunction( getFormatFilters, const char*, ( ),,
  1133. "Returns a list of supported shape formats in filter form.\n"
  1134. "Example output: DSQ Files|*.dsq|COLLADA Files|*.dae|")
  1135. {
  1136. return Con::getReturnBuffer(TSShapeLoader::getFormatFilters());
  1137. }