tsShapeLoader.cpp 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340
  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. shape->finalizeEditable();
  1044. }
  1045. void TSShapeLoader::computeBounds(Box3F& bounds)
  1046. {
  1047. // Compute the box that encloses the model geometry
  1048. bounds = Box3F::Invalid;
  1049. // Use bounds node geometry if present
  1050. if ( boundsNode && boundsNode->getNumMesh() )
  1051. {
  1052. for (S32 iMesh = 0; iMesh < boundsNode->getNumMesh(); iMesh++)
  1053. {
  1054. AppMesh* mesh = boundsNode->getMesh( iMesh );
  1055. if ( !mesh )
  1056. continue;
  1057. Box3F meshBounds;
  1058. mesh->computeBounds( meshBounds );
  1059. if ( meshBounds.isValidBox() )
  1060. bounds.intersect( meshBounds );
  1061. }
  1062. }
  1063. else
  1064. {
  1065. // Compute bounds based on all geometry in the model
  1066. for (S32 iMesh = 0; iMesh < appMeshes.size(); iMesh++)
  1067. {
  1068. AppMesh* mesh = appMeshes[iMesh];
  1069. if ( !mesh )
  1070. continue;
  1071. Box3F meshBounds;
  1072. mesh->computeBounds( meshBounds );
  1073. if ( meshBounds.isValidBox() )
  1074. bounds.intersect( meshBounds );
  1075. }
  1076. }
  1077. }
  1078. TSShapeLoader::~TSShapeLoader()
  1079. {
  1080. clearNodeTransformCache();
  1081. // Clear shared AppMaterial list
  1082. for (S32 iMat = 0; iMat < AppMesh::appMaterials.size(); iMat++)
  1083. delete AppMesh::appMaterials[iMat];
  1084. AppMesh::appMaterials.clear();
  1085. // Delete Subshapes
  1086. delete boundsNode;
  1087. for (S32 iSub = 0; iSub < subshapes.size(); iSub++)
  1088. delete subshapes[iSub];
  1089. // Delete AppSequences
  1090. for (S32 iSeq = 0; iSeq < appSequences.size(); iSeq++)
  1091. delete appSequences[iSeq];
  1092. appSequences.clear();
  1093. }
  1094. // Static functions to handle supported formats for shape loader.
  1095. void TSShapeLoader::addFormat(String name, String extension)
  1096. {
  1097. ShapeFormat newFormat;
  1098. newFormat.mName = name;
  1099. newFormat.mExtension = extension;
  1100. smFormats.push_back(newFormat);
  1101. }
  1102. String TSShapeLoader::getFormatExtensions()
  1103. {
  1104. // "*.dsq TAB *.dae TAB
  1105. StringBuilder output;
  1106. for(U32 n = 0; n < smFormats.size(); ++n)
  1107. {
  1108. output.append("*.");
  1109. output.append(smFormats[n].mExtension);
  1110. output.append("\t");
  1111. }
  1112. return output.end();
  1113. }
  1114. String TSShapeLoader::getFormatFilters()
  1115. {
  1116. // "DSQ Files|*.dsq|COLLADA Files|*.dae|"
  1117. StringBuilder output;
  1118. for(U32 n = 0; n < smFormats.size(); ++n)
  1119. {
  1120. output.append(smFormats[n].mName);
  1121. output.append("|*.");
  1122. output.append(smFormats[n].mExtension);
  1123. output.append("|");
  1124. }
  1125. return output.end();
  1126. }
  1127. DefineConsoleFunction( getFormatExtensions, const char*, ( ),,
  1128. "Returns a list of supported shape format extensions separated by tabs."
  1129. "Example output: *.dsq TAB *.dae TAB")
  1130. {
  1131. return Con::getReturnBuffer(TSShapeLoader::getFormatExtensions());
  1132. }
  1133. DefineConsoleFunction( getFormatFilters, const char*, ( ),,
  1134. "Returns a list of supported shape formats in filter form.\n"
  1135. "Example output: DSQ Files|*.dsq|COLLADA Files|*.dae|")
  1136. {
  1137. return Con::getReturnBuffer(TSShapeLoader::getFormatFilters());
  1138. }