tsShapeEdit.cpp 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095
  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/consoleTypes.h"
  24. #include "core/resourceManager.h"
  25. #include "ts/tsShape.h"
  26. #include "ts/tsShapeInstance.h"
  27. #include "ts/tsLastDetail.h"
  28. #include "ts/tsMaterialList.h"
  29. #include "core/stream/fileStream.h"
  30. #include "core/volume.h"
  31. //-----------------------------------------------------------------------------
  32. S32 TSShape::addName(const String& name)
  33. {
  34. // Check for empty names
  35. if (name.isEmpty())
  36. return -1;
  37. // Return the index of the new name (add if it is unique)
  38. S32 index = findName(name);
  39. if (index >= 0)
  40. return index;
  41. names.push_back(StringTable->insert(name));
  42. return names.size()-1;
  43. }
  44. void TSShape::updateSmallestVisibleDL()
  45. {
  46. // Update smallest visible detail
  47. mSmallestVisibleDL = -1;
  48. mSmallestVisibleSize = F32_MAX;
  49. F32 maxSize = 0.0f;
  50. for (S32 i = 0; i < details.size(); i++)
  51. {
  52. maxSize = getMax( maxSize, details[i].size );
  53. if ((details[i].size >= 0) && (details[i].size < mSmallestVisibleSize))
  54. {
  55. mSmallestVisibleDL = i;
  56. mSmallestVisibleSize = details[i].size;
  57. }
  58. }
  59. // Initialize the detail level lod lookup table.
  60. mDetailLevelLookup.setSize( (U32)( maxSize * 2.0f ) + 2 );
  61. for ( U32 l=0; l < mDetailLevelLookup.size(); l++ )
  62. {
  63. F32 pixelSize = (F32)l;
  64. S32 dl = -1;
  65. for ( U32 d=0; d < details.size(); d++ )
  66. {
  67. // Break when we get to hidden detail
  68. // levels like collision shapes.
  69. if ( details[d].size < 0 )
  70. break;
  71. if ( pixelSize > details[d].size )
  72. {
  73. dl = d;
  74. break;
  75. }
  76. if ( d + 1 >= details.size() || details[d+1].size < 0 )
  77. {
  78. // We've run out of details and haven't found anything?
  79. // Let's just grab this one.
  80. dl = d;
  81. break;
  82. }
  83. }
  84. // Calculate the intra detail level.
  85. F32 intraDL = 0;
  86. if ( dl > -1 )
  87. {
  88. F32 curSize = details[dl].size;
  89. F32 nextSize = dl == 0 ? 2.0f * curSize : details[dl - 1].size;
  90. intraDL = mClampF( nextSize - curSize > 0.01f ? (pixelSize - curSize) / (nextSize - curSize) : 1.0f, 0, 1 );
  91. }
  92. mDetailLevelLookup[l].set( dl, intraDL );
  93. }
  94. // Test for using the legacy screen error
  95. // lod method here instead of runtime.
  96. //
  97. // See setDetailFromDistance().
  98. //
  99. mUseDetailFromScreenError = mSmallestVisibleDL >= 0 &&
  100. details.first().maxError >= 0;
  101. }
  102. S32 TSShape::addDetail(const String& dname, S32 size, S32 subShapeNum)
  103. {
  104. S32 nameIndex = addName(avar("%s%d", dname.c_str(), size));
  105. // Check if this detail size has already been added
  106. S32 index;
  107. for (index = 0; index < details.size(); index++)
  108. {
  109. if ((details[index].size == size) &&
  110. (details[index].subShapeNum == subShapeNum) &&
  111. (details[index].nameIndex == nameIndex))
  112. return index;
  113. if (details[index].size < size)
  114. break;
  115. }
  116. // Create a new detail level at the right index, so array
  117. // remains sorted by detail size (from largest to smallest)
  118. details.insert(index);
  119. TSShape::Detail &detail = details[index];
  120. // Clear the detail to ensure no garbage values
  121. // are left in any vars we don't set.
  122. dMemset( &detail, 0, sizeof( Detail ) );
  123. // Setup the detail.
  124. detail.nameIndex = nameIndex;
  125. detail.size = size;
  126. detail.subShapeNum = subShapeNum;
  127. detail.objectDetailNum = 0;
  128. detail.averageError = -1;
  129. detail.maxError = -1;
  130. detail.polyCount = 0;
  131. // Resize alpha vectors
  132. alphaIn.increment();
  133. alphaOut.increment();
  134. // Fixup objectDetailNum in other detail levels
  135. for (S32 i = index+1; i < details.size(); i++)
  136. {
  137. if ((details[i].subShapeNum >= 0) &&
  138. ((subShapeNum == -1) || (details[i].subShapeNum == subShapeNum)))
  139. details[i].objectDetailNum++;
  140. }
  141. // Update smallest visible detail
  142. updateSmallestVisibleDL();
  143. return index;
  144. }
  145. S32 TSShape::addImposter(const String& cachePath, S32 size, S32 numEquatorSteps,
  146. S32 numPolarSteps, S32 dl, S32 dim, bool includePoles, F32 polarAngle)
  147. {
  148. // Check if the desired size is already in use
  149. bool isNewDetail = false;
  150. S32 detIndex = findDetailBySize( size );
  151. if ( detIndex >= 0 )
  152. {
  153. // Size is in use. If the detail is already an imposter, we can just change
  154. // the settings, otherwise quit
  155. if ( details[detIndex].subShapeNum >= 0 )
  156. {
  157. Con::errorf( "TSShape::addImposter: A non-billboard detail already "
  158. "exists at size %d", size );
  159. return -1;
  160. }
  161. }
  162. else
  163. {
  164. // Size is not in use. If an imposter already exists, change its size, otherwise
  165. // create a new detail
  166. for ( detIndex = 0; detIndex < details.size(); ++detIndex )
  167. {
  168. if ( details[detIndex].subShapeNum < 0 )
  169. {
  170. // Change the imposter detail size
  171. setDetailSize( details[detIndex].size, size );
  172. break;
  173. }
  174. }
  175. if ( detIndex == details.size() )
  176. {
  177. isNewDetail = true;
  178. detIndex = addDetail( "bbDetail", size, -1 );
  179. }
  180. }
  181. // Now set the billboard properties.
  182. Detail &detail = details[detIndex];
  183. // In prior to DTS version 26 we would pack the autobillboard
  184. // into this single 32bit value. This was prone to overflows
  185. // of parameters caused random bugs.
  186. //
  187. // Set the old autobillboard properties var to zero.
  188. detail.objectDetailNum = 0;
  189. // We now use the new vars.
  190. detail.bbEquatorSteps = numEquatorSteps;
  191. detail.bbPolarSteps = numPolarSteps;
  192. detail.bbPolarAngle = polarAngle;
  193. detail.bbDetailLevel = dl;
  194. detail.bbDimension = dim;
  195. detail.bbIncludePoles = includePoles;
  196. // Rebuild billboard details or force an update of the modified detail
  197. if ( isNewDetail )
  198. {
  199. // Add NULL meshes for this detail
  200. for ( S32 iObj = 0; iObj < objects.size(); ++iObj )
  201. {
  202. if ( detIndex < objects[iObj].numMeshes )
  203. {
  204. objects[iObj].numMeshes++;
  205. meshes.insert( objects[iObj].startMeshIndex + detIndex, NULL );
  206. for (S32 j = iObj + 1; j < objects.size(); ++j )
  207. objects[j].startMeshIndex++;
  208. }
  209. }
  210. // Could be dedicated server.
  211. if ( GFXDevice::devicePresent() )
  212. setupBillboardDetails( cachePath );
  213. while ( detailCollisionAccelerators.size() < details.size() )
  214. detailCollisionAccelerators.push_back( NULL );
  215. }
  216. else
  217. {
  218. if ( billboardDetails.size() && GFXDevice::devicePresent() )
  219. {
  220. delete billboardDetails[detIndex];
  221. billboardDetails[detIndex] = new TSLastDetail(
  222. this,
  223. cachePath,
  224. detail.bbEquatorSteps,
  225. detail.bbPolarSteps,
  226. detail.bbPolarAngle,
  227. detail.bbIncludePoles,
  228. detail.bbDetailLevel,
  229. detail.bbDimension );
  230. billboardDetails[detIndex]->update( true );
  231. }
  232. }
  233. return detIndex;
  234. }
  235. bool TSShape::removeImposter()
  236. {
  237. // Find the imposter detail level
  238. S32 detIndex;
  239. for ( detIndex = 0; detIndex < details.size(); ++detIndex )
  240. {
  241. if ( details[detIndex].subShapeNum < 0 )
  242. break;
  243. }
  244. if ( detIndex == details.size() )
  245. {
  246. Con::errorf( "TSShape::removeImposter: No imposter detail level found in shape" );
  247. return false;
  248. }
  249. // Remove the detail level
  250. details.erase( detIndex );
  251. if ( detIndex < billboardDetails.size() )
  252. {
  253. // Delete old textures
  254. TSLastDetail* bb = billboardDetails[detIndex];
  255. bb->deleteImposterCacheTextures();
  256. delete billboardDetails[detIndex];
  257. }
  258. billboardDetails.clear();
  259. detailCollisionAccelerators.erase( detIndex );
  260. // Remove the (NULL) meshes from each object
  261. for ( S32 iObj = 0; iObj < objects.size(); ++iObj )
  262. {
  263. if ( detIndex < objects[iObj].numMeshes )
  264. {
  265. objects[iObj].numMeshes--;
  266. meshes.erase( objects[iObj].startMeshIndex + detIndex );
  267. for (S32 j = iObj + 1; j < objects.size(); ++j )
  268. objects[j].startMeshIndex--;
  269. }
  270. }
  271. // Update smallest visible size
  272. updateSmallestVisibleDL();
  273. return true;
  274. }
  275. //-----------------------------------------------------------------------------
  276. /// Get the index of the element in the group with a given name
  277. template<class T> S32 findByName(Vector<T>& group, S32 nameIndex)
  278. {
  279. for (S32 i = 0; i < group.size(); i++)
  280. if (group[i].nameIndex == nameIndex)
  281. return i;
  282. return -1;
  283. }
  284. /// Adjust the nameIndex for elements in the group
  285. template<class T> void adjustForNameRemoval(Vector<T>& group, S32 nameIndex)
  286. {
  287. for (S32 i = 0; i < group.size(); i++)
  288. if (group[i].nameIndex > nameIndex)
  289. group[i].nameIndex--;
  290. }
  291. bool TSShape::removeName(const String& name)
  292. {
  293. // Check if the name is still in use
  294. S32 nameIndex = findName(name);
  295. if ((findByName(nodes, nameIndex) >= 0) ||
  296. (findByName(objects, nameIndex) >= 0) ||
  297. (findByName(sequences, nameIndex) >= 0) ||
  298. (findByName(details, nameIndex) >= 0))
  299. return false;
  300. // Remove the name, then update nameIndex for affected elements
  301. names.erase(nameIndex);
  302. adjustForNameRemoval(nodes, nameIndex);
  303. adjustForNameRemoval(objects, nameIndex);
  304. adjustForNameRemoval(sequences, nameIndex);
  305. adjustForNameRemoval(details, nameIndex);
  306. return true;
  307. }
  308. //-----------------------------------------------------------------------------
  309. template<class T> bool doRename(TSShape* shape, Vector<T>& group, const String& oldName, const String& newName)
  310. {
  311. // Find the element in the group with the oldName
  312. S32 index = findByName(group, shape->findName(oldName));
  313. if (index < 0)
  314. {
  315. Con::errorf("TSShape::rename: Could not find '%s'", oldName.c_str());
  316. return false;
  317. }
  318. // Ignore trivial renames
  319. if (oldName.equal(newName, String::NoCase))
  320. return true;
  321. // Check that this name is not already in use
  322. if (findByName(group, shape->findName(newName)) >= 0)
  323. {
  324. Con::errorf("TSShape::rename: '%s' is already in use", newName.c_str());
  325. return false;
  326. }
  327. // Do the rename (the old name will be removed if it is no longer in use)
  328. group[index].nameIndex = shape->addName(newName);
  329. shape->removeName(oldName);
  330. return true;
  331. }
  332. bool TSShape::renameNode(const String& oldName, const String& newName)
  333. {
  334. return doRename(this, nodes, oldName, newName);
  335. }
  336. bool TSShape::renameObject(const String& oldName, const String& newName)
  337. {
  338. return doRename(this, objects, oldName, newName);
  339. }
  340. bool TSShape::renameDetail(const String& oldName, const String& newName)
  341. {
  342. return doRename(this, details, oldName, newName);
  343. }
  344. bool TSShape::renameSequence(const String& oldName, const String& newName)
  345. {
  346. return doRename(this, sequences, oldName, newName);
  347. }
  348. //-----------------------------------------------------------------------------
  349. bool TSShape::addNode(const String& name, const String& parentName, const Point3F& pos, const QuatF& rot)
  350. {
  351. // Check that adding this node would not exceed the maximum count
  352. if (nodes.size() >= MAX_TS_SET_SIZE)
  353. {
  354. Con::errorf("TSShape::addNode: Cannot add node, shape already has maximum (%d) nodes", MAX_TS_SET_SIZE);
  355. return false;
  356. }
  357. // Check that there is not already a node with this name
  358. if (findNode(name) >= 0)
  359. {
  360. Con::errorf("TSShape::addNode: %s already exists!", name.c_str());
  361. return false;
  362. }
  363. // Find the parent node (OK for name to be empty => node is at root level)
  364. S32 parentIndex = -1;
  365. if (dStrcmp(parentName, ""))
  366. {
  367. parentIndex = findNode(parentName);
  368. if (parentIndex < 0)
  369. {
  370. Con::errorf("TSShape::addNode: Could not find parent node '%s'", parentName.c_str());
  371. return false;
  372. }
  373. }
  374. // Insert node at the end of the subshape
  375. S32 subShapeIndex = (parentIndex >= 0) ? getSubShapeForNode(parentIndex) : 0;
  376. S32 nodeIndex = subShapeNumNodes[subShapeIndex];
  377. // Adjust subshape node indices
  378. subShapeNumNodes[subShapeIndex]++;
  379. for (S32 i = subShapeIndex + 1; i < subShapeFirstNode.size(); i++)
  380. subShapeFirstNode[i]++;
  381. // Update animation sequences
  382. for (S32 iSeq = 0; iSeq < sequences.size(); iSeq++)
  383. {
  384. // Update animation matters arrays (new node is not animated)
  385. TSShape::Sequence& seq = sequences[iSeq];
  386. seq.translationMatters.insert(nodeIndex, false);
  387. seq.rotationMatters.insert(nodeIndex, false);
  388. seq.scaleMatters.insert(nodeIndex, false);
  389. }
  390. // Insert the new node
  391. TSShape::Node node;
  392. node.nameIndex = addName(name);
  393. node.parentIndex = parentIndex;
  394. node.firstChild = -1;
  395. node.firstObject = -1;
  396. node.nextSibling = -1;
  397. nodes.insert(nodeIndex, node);
  398. // Insert node default translation and rotation
  399. Quat16 rot16;
  400. rot16.set(rot);
  401. defaultTranslations.insert(nodeIndex, pos);
  402. defaultRotations.insert(nodeIndex, rot16);
  403. // Fixup node indices
  404. for (S32 i = 0; i < nodes.size(); i++)
  405. {
  406. if (nodes[i].parentIndex >= nodeIndex)
  407. nodes[i].parentIndex++;
  408. }
  409. for (S32 i = 0; i < objects.size(); i++)
  410. {
  411. if (objects[i].nodeIndex >= nodeIndex)
  412. objects[i].nodeIndex++;
  413. }
  414. for (S32 i = 0; i < meshes.size(); i++)
  415. {
  416. if (meshes[i] && (meshes[i]->getMeshType() == TSMesh::SkinMeshType))
  417. {
  418. TSSkinMesh* skin = dynamic_cast<TSSkinMesh*>(meshes[i]);
  419. for (S32 j = 0; j < skin->batchData.nodeIndex.size(); j++)
  420. {
  421. if (skin->batchData.nodeIndex[j] >= nodeIndex)
  422. skin->batchData.nodeIndex[j]++;
  423. }
  424. }
  425. }
  426. // Re-initialise the shape
  427. init();
  428. return true;
  429. }
  430. /// Erase animation keyframes (translation, rotation etc)
  431. template<class T> S32 eraseStates(Vector<T>& vec, const TSIntegerSet& matters, S32 base, S32 numKeyframes, S32 index=-1)
  432. {
  433. S32 dest, count;
  434. if (index == -1)
  435. {
  436. // Erase for all nodes/objects
  437. dest = base;
  438. count = numKeyframes * matters.count();
  439. }
  440. else
  441. {
  442. // Erase for the indexed node/object only
  443. dest = base + matters.count(index)*numKeyframes;
  444. count = numKeyframes;
  445. }
  446. // Erase the values
  447. if (count)
  448. {
  449. if ((dest + count) < vec.size())
  450. dCopyArray(&vec[dest], &vec[dest + count], vec.size() - (dest + count));
  451. vec.decrement(count);
  452. }
  453. return count;
  454. }
  455. bool TSShape::removeNode(const String& name)
  456. {
  457. // Find the node to be removed
  458. S32 nodeIndex = findNode(name);
  459. if (nodeIndex < 0)
  460. {
  461. Con::errorf("TSShape::removeNode: Could not find node '%s'", name.c_str());
  462. return false;
  463. }
  464. S32 nodeParentIndex = nodes[nodeIndex].parentIndex;
  465. // Warn if there are objects attached to this node
  466. Vector<S32> nodeObjects;
  467. getNodeObjects(nodeIndex, nodeObjects);
  468. if (nodeObjects.size())
  469. {
  470. Con::warnf("TSShape::removeNode: Node '%s' has %d objects attached, these "
  471. "will be reassigned to the node's parent ('%s')", name.c_str(), nodeObjects.size(),
  472. ((nodeParentIndex >= 0) ? getName(nodes[nodeParentIndex].nameIndex).c_str() : "null"));
  473. }
  474. // Update animation sequences
  475. for (S32 iSeq = 0; iSeq < sequences.size(); iSeq++)
  476. {
  477. TSShape::Sequence& seq = sequences[iSeq];
  478. // Remove animated node transforms
  479. if (seq.translationMatters.test(nodeIndex))
  480. eraseStates(nodeTranslations, seq.translationMatters, seq.baseTranslation, seq.numKeyframes, nodeIndex);
  481. if (seq.rotationMatters.test(nodeIndex))
  482. eraseStates(nodeRotations, seq.rotationMatters, seq.baseRotation, seq.numKeyframes, nodeIndex);
  483. if (seq.scaleMatters.test(nodeIndex))
  484. {
  485. if (seq.flags & TSShape::ArbitraryScale)
  486. {
  487. eraseStates(nodeArbitraryScaleRots, seq.scaleMatters, seq.baseScale, seq.numKeyframes, nodeIndex);
  488. eraseStates(nodeArbitraryScaleFactors, seq.scaleMatters, seq.baseScale, seq.numKeyframes, nodeIndex);
  489. }
  490. else if (seq.flags & TSShape::AlignedScale)
  491. eraseStates(nodeAlignedScales, seq.scaleMatters, seq.baseScale, seq.numKeyframes, nodeIndex);
  492. else
  493. eraseStates(nodeUniformScales, seq.scaleMatters, seq.baseScale, seq.numKeyframes, nodeIndex);
  494. }
  495. seq.translationMatters.erase(nodeIndex);
  496. seq.rotationMatters.erase(nodeIndex);
  497. seq.scaleMatters.erase(nodeIndex);
  498. }
  499. // Remove the node
  500. nodes.erase(nodeIndex);
  501. defaultTranslations.erase(nodeIndex);
  502. defaultRotations.erase(nodeIndex);
  503. // Adjust subshape node indices
  504. S32 subShapeIndex = getSubShapeForNode(nodeIndex);
  505. subShapeNumNodes[subShapeIndex]--;
  506. for (S32 i = subShapeIndex + 1; i < subShapeFirstNode.size(); i++)
  507. subShapeFirstNode[i]--;
  508. // Fixup node parent indices
  509. for (S32 i = 0; i < nodes.size(); i++)
  510. {
  511. if (nodes[i].parentIndex == nodeIndex)
  512. nodes[i].parentIndex = -1;
  513. else if (nodes[i].parentIndex > nodeIndex)
  514. nodes[i].parentIndex--;
  515. }
  516. if (nodeParentIndex > nodeIndex)
  517. nodeParentIndex--;
  518. // Fixup object node indices, and re-assign attached objects to node's parent
  519. for (S32 i = 0; i < objects.size(); i++)
  520. {
  521. if (objects[i].nodeIndex == nodeIndex)
  522. objects[i].nodeIndex = nodeParentIndex;
  523. if (objects[i].nodeIndex > nodeIndex)
  524. objects[i].nodeIndex--;
  525. }
  526. // Fixup skin weight node indices, and re-assign weights for deleted node to its parent
  527. for (S32 i = 0; i < meshes.size(); i++)
  528. {
  529. if (meshes[i] && (meshes[i]->getMeshType() == TSMesh::SkinMeshType))
  530. {
  531. TSSkinMesh* skin = dynamic_cast<TSSkinMesh*>(meshes[i]);
  532. for (S32 j = 0; j < skin->batchData.nodeIndex.size(); j++)
  533. {
  534. if (skin->batchData.nodeIndex[j] == nodeIndex)
  535. skin->batchData.nodeIndex[j] = nodeParentIndex;
  536. if (skin->batchData.nodeIndex[j] > nodeIndex)
  537. skin->batchData.nodeIndex[j]--;
  538. }
  539. }
  540. }
  541. // Remove the sequence name if it is no longer in use
  542. removeName(name);
  543. // Re-initialise the shape
  544. init();
  545. return true;
  546. }
  547. //-----------------------------------------------------------------------------
  548. bool TSShape::setNodeTransform(const String& name, const Point3F& pos, const QuatF& rot)
  549. {
  550. // Find the node to be transformed
  551. S32 nodeIndex = findNode(name);
  552. if (nodeIndex < 0)
  553. {
  554. Con::errorf("TSShape::setNodeTransform: Could not find node '%s'", name.c_str());
  555. return false;
  556. }
  557. // Update initial node position and rotation
  558. defaultTranslations[nodeIndex] = pos;
  559. defaultRotations[nodeIndex].set(rot);
  560. return true;
  561. }
  562. //-----------------------------------------------------------------------------
  563. S32 TSShape::addObject(const String& objName, S32 subShapeIndex)
  564. {
  565. S32 objIndex = subShapeNumObjects[subShapeIndex];
  566. // Add object to subshape
  567. subShapeNumObjects[subShapeIndex]++;
  568. for (S32 i = subShapeIndex + 1; i < subShapeFirstObject.size(); i++)
  569. subShapeFirstObject[i]++;
  570. TSShape::Object obj;
  571. obj.nameIndex = addName(objName);
  572. obj.nodeIndex = 0;
  573. obj.numMeshes = 0;
  574. obj.startMeshIndex = (objIndex == 0) ? 0 : objects[objIndex-1].startMeshIndex + objects[objIndex-1].numMeshes;
  575. obj.firstDecal = 0;
  576. obj.nextSibling = 0;
  577. objects.insert(objIndex, obj);
  578. // Add default object state
  579. TSShape::ObjectState state;
  580. state.frameIndex = 0;
  581. state.matFrameIndex = 0;
  582. state.vis = 1.0f;
  583. objectStates.insert(objIndex, state);
  584. // Fixup sequences
  585. for (S32 i = 0; i < sequences.size(); i++)
  586. sequences[i].baseObjectState++;
  587. return objIndex;
  588. }
  589. void TSShape::addMeshToObject(S32 objIndex, S32 meshIndex, TSMesh* mesh)
  590. {
  591. TSShape::Object& obj = objects[objIndex];
  592. // Pad with NULLs if required
  593. S32 oldNumMeshes = obj.numMeshes;
  594. if (mesh)
  595. {
  596. for (S32 i = obj.numMeshes; i < meshIndex; i++)
  597. {
  598. meshes.insert(obj.startMeshIndex + i, NULL);
  599. obj.numMeshes++;
  600. }
  601. }
  602. // Insert the new mesh
  603. meshes.insert(obj.startMeshIndex + meshIndex, mesh);
  604. obj.numMeshes++;
  605. // Skinned meshes are not attached to any node
  606. if (mesh && (mesh->getMeshType() == TSMesh::SkinMeshType))
  607. obj.nodeIndex = -1;
  608. // Fixup mesh indices for other objects
  609. for (S32 i = 0; i < objects.size(); i++)
  610. {
  611. if ((i != objIndex) && (objects[i].startMeshIndex >= obj.startMeshIndex))
  612. objects[i].startMeshIndex += (obj.numMeshes - oldNumMeshes);
  613. }
  614. }
  615. void TSShape::removeMeshFromObject(S32 objIndex, S32 meshIndex)
  616. {
  617. TSShape::Object& obj = objects[objIndex];
  618. // Remove the mesh, but do not destroy it (this must be done by the caller)
  619. meshes[obj.startMeshIndex + meshIndex] = NULL;
  620. // Check if there are any objects remaining that have a valid mesh at this
  621. // detail size
  622. bool removeDetail = true;
  623. for (S32 i = 0; i < objects.size(); i++)
  624. {
  625. if ((meshIndex < objects[i].numMeshes) && meshes[objects[i].startMeshIndex + meshIndex])
  626. {
  627. removeDetail = false;
  628. break;
  629. }
  630. }
  631. // Remove detail level if possible
  632. if (removeDetail)
  633. {
  634. for (S32 i = 0; i < objects.size(); i++)
  635. {
  636. if (meshIndex < objects[i].numMeshes)
  637. {
  638. meshes.erase(objects[i].startMeshIndex + meshIndex);
  639. objects[i].numMeshes--;
  640. for (S32 j = 0; j < objects.size(); j++)
  641. {
  642. if (objects[j].startMeshIndex > objects[i].startMeshIndex)
  643. objects[j].startMeshIndex--;
  644. }
  645. }
  646. }
  647. Vector<S32> validDetails;
  648. getSubShapeDetails(getSubShapeForObject(objIndex), validDetails);
  649. for (S32 i = 0; i < validDetails.size(); i++)
  650. {
  651. TSShape::Detail& detail = details[validDetails[i]];
  652. if (detail.objectDetailNum > meshIndex)
  653. detail.objectDetailNum--;
  654. }
  655. details.erase(validDetails[meshIndex]);
  656. }
  657. // Remove trailing NULL meshes from the object
  658. S32 oldNumMeshes = obj.numMeshes;
  659. while (obj.numMeshes && !meshes[obj.startMeshIndex + obj.numMeshes - 1])
  660. {
  661. meshes.erase(obj.startMeshIndex + obj.numMeshes - 1);
  662. obj.numMeshes--;
  663. }
  664. // Fixup mesh indices for other objects
  665. for (S32 i = 0; i < objects.size(); i++)
  666. {
  667. if (objects[i].startMeshIndex > obj.startMeshIndex)
  668. objects[i].startMeshIndex -= (oldNumMeshes - obj.numMeshes);
  669. }
  670. }
  671. bool TSShape::setObjectNode(const String& objName, const String& nodeName)
  672. {
  673. // Find the object and node
  674. S32 objIndex = findObject(objName);
  675. if (objIndex < 0)
  676. {
  677. Con::errorf("TSShape::setObjectNode: Could not find object '%s'", objName.c_str());
  678. return false;
  679. }
  680. S32 nodeIndex;
  681. if (nodeName.isEmpty())
  682. nodeIndex = -1;
  683. else
  684. {
  685. nodeIndex = findNode(nodeName);
  686. if (nodeIndex < 0)
  687. {
  688. Con::errorf("TSShape::setObjectNode: Could not find node '%s'", nodeName.c_str());
  689. return false;
  690. }
  691. }
  692. objects[objIndex].nodeIndex = nodeIndex;
  693. return true;
  694. }
  695. bool TSShape::removeObject(const String& name)
  696. {
  697. // Find the object
  698. S32 objIndex = findObject(name);
  699. if (objIndex < 0)
  700. {
  701. Con::errorf("TSShape::removeObject: Could not find object '%s'", name.c_str());
  702. return false;
  703. }
  704. // Destroy all meshes in the object
  705. TSShape::Object& obj = objects[objIndex];
  706. while ( obj.numMeshes )
  707. {
  708. destructInPlace(meshes[obj.startMeshIndex + obj.numMeshes - 1]);
  709. removeMeshFromObject(objIndex, obj.numMeshes - 1);
  710. }
  711. // Remove the object from the shape
  712. objects.erase(objIndex);
  713. S32 subShapeIndex = getSubShapeForObject(objIndex);
  714. subShapeNumObjects[subShapeIndex]--;
  715. for (S32 i = subShapeIndex + 1; i < subShapeFirstObject.size(); i++)
  716. subShapeFirstObject[i]--;
  717. // Remove the object from all sequences
  718. for (S32 i = 0; i < sequences.size(); i++)
  719. {
  720. TSShape::Sequence& seq = sequences[i];
  721. TSIntegerSet objMatters(seq.frameMatters);
  722. objMatters.overlap(seq.matFrameMatters);
  723. objMatters.overlap(seq.visMatters);
  724. if (objMatters.test(objIndex))
  725. eraseStates(objectStates, objMatters, seq.baseObjectState, seq.numKeyframes, objIndex);
  726. seq.frameMatters.erase(objIndex);
  727. seq.matFrameMatters.erase(objIndex);
  728. seq.visMatters.erase(objIndex);
  729. }
  730. // Remove the object name if it is no longer in use
  731. removeName(name);
  732. // Update smallest visible detail
  733. updateSmallestVisibleDL();
  734. // Re-initialise the shape
  735. init();
  736. return true;
  737. }
  738. //-----------------------------------------------------------------------------
  739. // Helper to copy a TSMesh ready for adding to this TSShape (with the right vertex format etc)
  740. TSMesh* TSShape::copyMesh( const TSMesh* srcMesh ) const
  741. {
  742. TSMesh * mesh = 0;
  743. if ( srcMesh && ( srcMesh->getMeshType() == TSMesh::SkinMeshType ) )
  744. {
  745. TSSkinMesh* skin = new TSSkinMesh;
  746. // Copy skin elements
  747. const TSSkinMesh *srcSkin = dynamic_cast<const TSSkinMesh*>(srcMesh);
  748. skin->weight = srcSkin->weight;
  749. skin->vertexIndex = srcSkin->vertexIndex;
  750. skin->boneIndex = srcSkin->boneIndex;
  751. skin->batchData.nodeIndex = srcSkin->batchData.nodeIndex;
  752. skin->batchData.initialTransforms = srcSkin->batchData.initialTransforms;
  753. skin->batchData.initialVerts = srcSkin->batchData.initialVerts;
  754. skin->batchData.initialNorms = srcSkin->batchData.initialNorms;
  755. mesh = static_cast<TSMesh*>(skin);
  756. }
  757. else
  758. {
  759. mesh = new TSMesh;
  760. }
  761. if ( !srcMesh )
  762. return mesh; // return an empty mesh
  763. // Copy mesh elements
  764. mesh->indices = srcMesh->indices;
  765. mesh->primitives = srcMesh->primitives;
  766. mesh->numFrames = srcMesh->numFrames;
  767. mesh->numMatFrames = srcMesh->numMatFrames;
  768. mesh->vertsPerFrame = srcMesh->vertsPerFrame;
  769. mesh->setFlags(srcMesh->getFlags());
  770. mesh->mNumVerts = srcMesh->mNumVerts;
  771. // Copy vertex data in an *unpacked* form
  772. mesh->copySourceVertexDataFrom(srcMesh);
  773. mesh->createTangents(mesh->verts, mesh->norms);
  774. mesh->encodedNorms.set(NULL, 0);
  775. mesh->computeBounds();
  776. return mesh;
  777. }
  778. bool TSShape::addMesh(TSMesh* mesh, const String& meshName)
  779. {
  780. // Ensure mesh is in editable state
  781. mesh->makeEditable();
  782. // Determine the object name and detail size from the mesh name
  783. S32 detailSize = 999;
  784. String objName(String::GetTrailingNumber(meshName, detailSize));
  785. // Find the destination object (create one if it does not exist)
  786. S32 objIndex = findObject(objName);
  787. if (objIndex < 0)
  788. objIndex = addObject(objName, 0);
  789. AssertFatal(objIndex >= 0 && objIndex < objects.size(), "Invalid object index!");
  790. // Determine the subshape this object belongs to
  791. S32 subShapeIndex = getSubShapeForObject(objIndex);
  792. AssertFatal(subShapeIndex < subShapeFirstObject.size(), "Could not find subshape for object!");
  793. // Get the existing detail levels for the subshape
  794. Vector<S32> validDetails;
  795. getSubShapeDetails(subShapeIndex, validDetails);
  796. // Determine where to add the new mesh, and whether this is a new detail
  797. S32 detIndex;
  798. bool newDetail = true;
  799. for (detIndex = 0; detIndex < validDetails.size(); detIndex++)
  800. {
  801. const TSShape::Detail& det = details[validDetails[detIndex]];
  802. if (detailSize >= det.size)
  803. {
  804. newDetail = (det.size != detailSize);
  805. break;
  806. }
  807. }
  808. // Insert the new detail level if required
  809. if (newDetail)
  810. {
  811. // Determine a name for the detail level
  812. const char* detailName;
  813. if (dStrStartsWith(objName, "Col"))
  814. detailName = "collision";
  815. else if (dStrStartsWith(objName, "loscol"))
  816. detailName = "los";
  817. else
  818. detailName = "detail";
  819. S32 index = addDetail(detailName, detailSize, subShapeIndex);
  820. details[index].objectDetailNum = detIndex;
  821. }
  822. // Adding a new mesh or detail level is a bit tricky, since each
  823. // object potentially stores a different number of meshes, including
  824. // NULL meshes for higher detail levels where required.
  825. // For example, the following table shows 3 objects. Note how NULLs
  826. // must be inserted for detail levels higher than the first valid
  827. // mesh, but details after the the last valid mesh are left empty.
  828. //
  829. // Detail | Object1 | Object2 | Object3
  830. // ---------+-----------+-----------+---------
  831. // 128 | 128 | NULL | NULL
  832. // 64 | | NULL | 64
  833. // 32 | | 32 | NULL
  834. // 2 | | | 2
  835. // Add meshes as required for each object
  836. for (S32 i = 0; i < subShapeNumObjects[subShapeIndex]; i++)
  837. {
  838. S32 index = subShapeFirstObject[subShapeIndex] + i;
  839. const TSShape::Object& obj = objects[index];
  840. if (index == objIndex)
  841. {
  842. // The target object: replace the existing mesh (if any) or add a new one
  843. // if required.
  844. if (!newDetail && (detIndex < obj.numMeshes))
  845. {
  846. if ( meshes[obj.startMeshIndex + detIndex] )
  847. destructInPlace(meshes[obj.startMeshIndex + detIndex]);
  848. meshes[obj.startMeshIndex + detIndex] = mesh;
  849. }
  850. else
  851. addMeshToObject(index, detIndex, mesh);
  852. }
  853. else
  854. {
  855. // Other objects: add a NULL mesh only if inserting before a valid mesh
  856. if (newDetail && (detIndex < obj.numMeshes))
  857. addMeshToObject(index, detIndex, NULL);
  858. }
  859. }
  860. // Re-initialise the shape
  861. init();
  862. return true;
  863. }
  864. bool TSShape::addMesh(TSShape* srcShape, const String& srcMeshName, const String& meshName)
  865. {
  866. // Find the mesh in the source shape
  867. TSMesh* srcMesh = srcShape->findMesh(srcMeshName);
  868. if (!srcMesh)
  869. {
  870. Con::errorf("TSShape::addMesh: Could not find mesh '%s' in shape", srcMeshName.c_str());
  871. return false;
  872. }
  873. // Copy the source mesh
  874. TSMesh *mesh = copyMesh( srcMesh );
  875. if (srcMesh->getMeshType() == TSMesh::SkinMeshType)
  876. {
  877. TSSkinMesh *srcSkin = dynamic_cast<TSSkinMesh*>(srcMesh);
  878. // Check that the source skin is compatible with our skeleton
  879. Vector<S32> nodeMap(srcShape->nodes.size());
  880. for (S32 i = 0; i < srcShape->nodes.size(); i++)
  881. nodeMap.push_back( findNode( srcShape->getName(srcShape->nodes[i].nameIndex) ) );
  882. for (S32 i = 0; i < srcSkin->boneIndex.size(); i++)
  883. {
  884. S32 srcNode = srcSkin->boneIndex[i];
  885. if (nodeMap[srcNode] == -1)
  886. {
  887. const char* name = srcShape->getName(srcShape->nodes[srcNode].nameIndex).c_str();
  888. Con::errorf("TSShape::addMesh: Skin is weighted to node (%s) that "
  889. "does not exist in this shape", name);
  890. return false;
  891. }
  892. }
  893. TSSkinMesh *skin = dynamic_cast<TSSkinMesh*>(mesh);
  894. // Remap node indices
  895. skin->batchData.nodeIndex = srcSkin->batchData.nodeIndex;
  896. for (S32 i = 0; i < skin->batchData.nodeIndex.size(); i++)
  897. skin->batchData.nodeIndex[i] = nodeMap[skin->batchData.nodeIndex[i]];
  898. }
  899. // Add the copied mesh to the shape
  900. if (!addMesh(mesh, meshName))
  901. {
  902. delete mesh;
  903. return false;
  904. }
  905. // Copy materials used by the source mesh (only if from a different shape)
  906. if (srcShape != this)
  907. {
  908. for (S32 i = 0; i < mesh->primitives.size(); i++)
  909. {
  910. if (!(mesh->primitives[i].matIndex & TSDrawPrimitive::NoMaterial))
  911. {
  912. S32 drawType = (mesh->primitives[i].matIndex & (~TSDrawPrimitive::MaterialMask));
  913. S32 srcMatIndex = mesh->primitives[i].matIndex & TSDrawPrimitive::MaterialMask;
  914. const String& matName = srcShape->materialList->getMaterialName(srcMatIndex);
  915. // Add the material if it does not already exist
  916. S32 destMatIndex = materialList->getMaterialNameList().find_next(matName);
  917. if (destMatIndex < 0)
  918. {
  919. destMatIndex = materialList->size();
  920. materialList->push_back(matName, srcShape->materialList->getFlags(srcMatIndex));
  921. }
  922. mesh->primitives[i].matIndex = drawType | destMatIndex;
  923. }
  924. }
  925. }
  926. return true;
  927. }
  928. bool TSShape::setMeshSize(const String& meshName, S32 size)
  929. {
  930. S32 objIndex, meshIndex;
  931. if (!findMeshIndex(meshName, objIndex, meshIndex) ||
  932. !meshes[objects[objIndex].startMeshIndex + meshIndex])
  933. {
  934. Con::errorf("TSShape::setMeshSize: Could not find mesh '%s'", meshName.c_str());
  935. return false;
  936. }
  937. // Remove the mesh from the object, but don't destroy it
  938. TSShape::Object& obj = objects[objIndex];
  939. TSMesh* mesh = meshes[obj.startMeshIndex + meshIndex];
  940. removeMeshFromObject(objIndex, meshIndex);
  941. // Add the mesh back at the new position
  942. addMesh(mesh, avar("%s %d", getName(obj.nameIndex).c_str(), size));
  943. // Update smallest visible detail
  944. updateSmallestVisibleDL();
  945. // Re-initialise the shape
  946. init();
  947. return true;
  948. }
  949. bool TSShape::removeMesh(const String& meshName)
  950. {
  951. S32 objIndex, meshIndex;
  952. if (!findMeshIndex(meshName, objIndex, meshIndex) ||
  953. !meshes[objects[objIndex].startMeshIndex + meshIndex])
  954. {
  955. Con::errorf("TSShape::removeMesh: Could not find mesh '%s'", meshName.c_str());
  956. return false;
  957. }
  958. // Destroy and remove the mesh
  959. TSShape::Object& obj = objects[objIndex];
  960. destructInPlace(meshes[obj.startMeshIndex + meshIndex]);
  961. removeMeshFromObject(objIndex, meshIndex);
  962. // Remove the object if there are no meshes left
  963. if (!obj.numMeshes)
  964. removeObject(getName(obj.nameIndex));
  965. // Update smallest visible detail
  966. updateSmallestVisibleDL();
  967. // Re-initialise the shape
  968. init();
  969. return true;
  970. }
  971. //-----------------------------------------------------------------------------
  972. // Helper function for dealing with some of the Vectors used in a TSShape. 'meshes'
  973. // for example contains a TSMesh* per-object, per-detail-level, with NULLs for
  974. // undefined details for each object. Trailing NULLs are not added to the Vector,
  975. // so you end up with a different number of pointers for each object, depending
  976. // on which detail levels it defines. This makes it tricky to move meshes around
  977. // since you have to know how many elements belong to each object.
  978. // To simplify things, this function pads the Vector up to a certain length (so
  979. // all objects can appear to have the same number of meshes), the moves a single
  980. // element to a new index, then trims trailing NULLs again.
  981. template<class T>
  982. static void _PadMoveAndTrim(Vector<T*>& vec, S32 offset, S32 count,
  983. S32 padLength, S32 oldIndex, S32 newIndex)
  984. {
  985. // Pad the array with NULLs
  986. for ( S32 i = count; i < padLength; ++i )
  987. vec.insert( offset + count, NULL );
  988. // Move the element from the old to the new index
  989. T* tmp = vec[offset + oldIndex];
  990. vec.erase( offset + oldIndex );
  991. vec.insert( offset + newIndex, tmp );
  992. // Trim trailing NULLs from the vector
  993. for ( S32 i = padLength - 1; i >= 0; --i )
  994. {
  995. if ( vec[offset + i] )
  996. break;
  997. else
  998. vec.erase( offset + i );
  999. }
  1000. }
  1001. S32 TSShape::setDetailSize(S32 oldSize, S32 newSize)
  1002. {
  1003. S32 oldIndex = findDetailBySize( oldSize );
  1004. if ( oldIndex < 0 )
  1005. {
  1006. Con::errorf( "TSShape::setDetailSize: Cannot find detail with size %d", oldSize );
  1007. return -1;
  1008. }
  1009. // Remove this detail from the list
  1010. TSShape::Detail tmpDetail = details[oldIndex];
  1011. tmpDetail.size = newSize;
  1012. details.erase(oldIndex);
  1013. // Determine the new position for the detail (details are sorted by size)
  1014. S32 newIndex = 0;
  1015. for ( newIndex = 0; newIndex < details.size(); ++newIndex )
  1016. {
  1017. if ( newSize > details[newIndex].size )
  1018. break;
  1019. }
  1020. // Add the detail at its new position
  1021. details.insert( newIndex, tmpDetail );
  1022. // Rename the detail so its trailing size value is correct
  1023. {
  1024. S32 tmp;
  1025. String oldName( getName( tmpDetail.nameIndex ) );
  1026. String newName( String::GetTrailingNumber( oldName, tmp ) );
  1027. newName += String::ToString( "%d", newSize );
  1028. renameDetail(oldName, newName);
  1029. }
  1030. if ( newIndex != oldIndex )
  1031. {
  1032. // Fixup details
  1033. for ( S32 iDet = 0; iDet < details.size(); iDet++ )
  1034. {
  1035. if ( details[iDet].subShapeNum < 0 )
  1036. {
  1037. if ( details[iDet].bbDetailLevel == oldIndex )
  1038. details[iDet].bbDetailLevel = newIndex;
  1039. }
  1040. else
  1041. {
  1042. details[iDet].objectDetailNum = iDet;
  1043. }
  1044. }
  1045. // Fixup Billboard details
  1046. _PadMoveAndTrim( billboardDetails, 0, billboardDetails.size(),
  1047. details.size(), oldIndex, newIndex );
  1048. // Now move the mesh for each object in the subshape (adding and removing
  1049. // NULLs as appropriate)
  1050. for ( S32 iObj = 0; iObj < objects.size(); ++iObj )
  1051. {
  1052. TSShape::Object& obj = objects[iObj];
  1053. S32 oldMeshCount = meshes.size();
  1054. _PadMoveAndTrim( meshes, obj.startMeshIndex, obj.numMeshes,
  1055. details.size(), oldIndex, newIndex );
  1056. obj.numMeshes += ( meshes.size() - oldMeshCount );
  1057. // Fixup startMeshIndex for remaining objects
  1058. for ( S32 j = iObj + 1; j < objects.size(); ++j )
  1059. objects[j].startMeshIndex += ( meshes.size() - oldMeshCount );
  1060. }
  1061. }
  1062. // Update smallest visible detail
  1063. updateSmallestVisibleDL();
  1064. // Re-initialise the shape
  1065. init();
  1066. return newIndex;
  1067. }
  1068. bool TSShape::removeDetail( S32 size )
  1069. {
  1070. S32 dl = findDetailBySize( size );
  1071. if ( ( dl < 0 ) || ( dl >= details.size() ) )
  1072. {
  1073. Con::errorf( "TSShape::removeDetail: Invalid detail index (%d)", dl );
  1074. return false;
  1075. }
  1076. // Destroy and remove each mesh in the detail level
  1077. for ( S32 objIndex = objects.size()-1; objIndex >= 0; objIndex-- )
  1078. {
  1079. TSShape::Object& obj = objects[objIndex];
  1080. if ( dl < obj.numMeshes )
  1081. {
  1082. if ( meshes[obj.startMeshIndex + dl] )
  1083. destructInPlace( meshes[obj.startMeshIndex + dl] );
  1084. removeMeshFromObject(objIndex, dl);
  1085. // Remove the object if there are no meshes left
  1086. if (!obj.numMeshes)
  1087. removeObject( getName( obj.nameIndex ) );
  1088. }
  1089. }
  1090. // Destroy billboard detail level
  1091. if ( dl < billboardDetails.size() )
  1092. {
  1093. if ( billboardDetails[dl] )
  1094. {
  1095. // Delete old textures
  1096. billboardDetails[dl]->deleteImposterCacheTextures();
  1097. delete billboardDetails[dl];
  1098. }
  1099. billboardDetails.erase( dl );
  1100. }
  1101. // Update smallest visible detail
  1102. updateSmallestVisibleDL();
  1103. // Re-initialise the shape
  1104. init();
  1105. return true;
  1106. }
  1107. //-----------------------------------------------------------------------------
  1108. bool TSShape::addSequence(const Torque::Path& path, const String& fromSeq,
  1109. const String& name, S32 startFrame, S32 endFrame,
  1110. bool padRotKeys, bool padTransKeys)
  1111. {
  1112. String oldName(fromSeq);
  1113. if (path.getExtension().equal("dsq", String::NoCase))
  1114. {
  1115. S32 oldSeqCount = sequences.size();
  1116. // DSQ source file
  1117. char filenameBuf[1024];
  1118. Con::expandScriptFilename(filenameBuf, sizeof(filenameBuf), path.getFullPath().c_str());
  1119. FileStream *f;
  1120. if((f = FileStream::createAndOpen( filenameBuf, Torque::FS::File::Read )) == NULL)
  1121. {
  1122. Con::errorf("TSShape::addSequence: Could not load DSQ file '%s'", filenameBuf);
  1123. return false;
  1124. }
  1125. if (!importSequences(f, filenameBuf) || (f->getStatus() != Stream::Ok))
  1126. {
  1127. delete f;
  1128. Con::errorf("TSShape::addSequence: Load sequence file '%s' failed", filenameBuf);
  1129. return false;
  1130. }
  1131. delete f;
  1132. // Rename the new sequence if required (avoid rename if name is not
  1133. // unique (this will be fixed up later, and we don't need 2 errors about it!)
  1134. if (oldName.isEmpty())
  1135. oldName = getName(sequences.last().nameIndex);
  1136. if (!oldName.equal(name))
  1137. {
  1138. if (findSequence(name) == -1)
  1139. {
  1140. // Use a dummy intermediate name since we might be renaming from an
  1141. // existing name (and we want to rename the right sequence!)
  1142. sequences.last().nameIndex = addName("__dummy__");
  1143. renameSequence("__dummy__", name);
  1144. }
  1145. }
  1146. // Check that sequences have unique names
  1147. bool lastSequenceRejected = false;
  1148. for (S32 i = sequences.size()-1; i >= oldSeqCount; i--)
  1149. {
  1150. S32 nameIndex = (i == sequences.size()-1) ? findName(name) : sequences[i].nameIndex;
  1151. S32 seqIndex = findSequence(nameIndex);
  1152. if ((seqIndex != -1) && (seqIndex != i))
  1153. {
  1154. Con::errorf("TSShape::addSequence: Failed to add sequence '%s' "
  1155. "(name already exists)", getName(nameIndex).c_str());
  1156. sequences[i].nameIndex = addName("__dummy__");
  1157. removeSequence("__dummy__");
  1158. if (i == sequences.size())
  1159. lastSequenceRejected = true;
  1160. }
  1161. }
  1162. // @todo:Need to remove keyframes if start!=0 and end!=-1
  1163. TSShape::Sequence& seq = sequences.last();
  1164. // Store information about how this sequence was created
  1165. seq.sourceData.from = String::ToString("%s\t%s", filenameBuf, name.c_str());
  1166. seq.sourceData.total = seq.numKeyframes;
  1167. seq.sourceData.start = ((startFrame < 0) || (startFrame >= seq.numKeyframes)) ? 0 : startFrame;
  1168. seq.sourceData.end = ((endFrame < 0) || (endFrame >= seq.numKeyframes)) ? seq.numKeyframes-1 : endFrame;
  1169. return (sequences.size() != oldSeqCount);
  1170. }
  1171. /* Check that sequence to be added does not already exist */
  1172. if (findSequence(name) != -1)
  1173. {
  1174. Con::errorf("TSShape::addSequence: Cannot add sequence '%s' (name already exists)", name.c_str());
  1175. return false;
  1176. }
  1177. Resource<TSShape> hSrcShape;
  1178. TSShape* srcShape = this; // Assume we are copying an existing sequence
  1179. if (path.getExtension().equal("dts", String::NoCase) ||
  1180. path.getExtension().equal("dae", String::NoCase))
  1181. {
  1182. // DTS or DAE source file
  1183. char filenameBuf[1024];
  1184. Con::expandScriptFilename(filenameBuf, sizeof(filenameBuf), path.getFullPath().c_str());
  1185. hSrcShape = ResourceManager::get().load(filenameBuf);
  1186. if (!bool(hSrcShape))
  1187. {
  1188. Con::errorf("TSShape::addSequence: Could not load source shape '%s'", path.getFullPath().c_str());
  1189. return false;
  1190. }
  1191. srcShape = const_cast<TSShape*>((const TSShape*)hSrcShape);
  1192. if (!srcShape->sequences.size())
  1193. {
  1194. Con::errorf("TSShape::addSequence: Source shape '%s' does not contain any sequences", path.getFullPath().c_str());
  1195. return false;
  1196. }
  1197. // If no sequence name is specified, just use the first one
  1198. if (oldName.isEmpty())
  1199. oldName = srcShape->getName(srcShape->sequences[0].nameIndex);
  1200. }
  1201. else
  1202. {
  1203. // Source is an existing sequence
  1204. oldName = path.getFullPath();
  1205. }
  1206. // Find the sequence
  1207. S32 seqIndex = srcShape->findSequence(oldName);
  1208. if (seqIndex < 0)
  1209. {
  1210. Con::errorf("TSShape::addSequence: Could not find sequence named '%s'", oldName.c_str());
  1211. return false;
  1212. }
  1213. // Check keyframe range
  1214. const TSShape::Sequence* srcSeq = &srcShape->sequences[seqIndex];
  1215. if ((startFrame < 0) || (startFrame >= srcSeq->numKeyframes))
  1216. {
  1217. Con::warnf("TSShape::addSequence: Start keyframe (%d) out of range (0-%d) for sequence '%s'",
  1218. startFrame, srcSeq->numKeyframes-1, oldName.c_str());
  1219. startFrame = 0;
  1220. }
  1221. if (endFrame < 0)
  1222. endFrame = srcSeq->numKeyframes - 1;
  1223. else if (endFrame >= srcSeq->numKeyframes)
  1224. {
  1225. Con::warnf("TSShape::addSequence: End keyframe (%d) out of range (0-%d) for sequence '%s'",
  1226. endFrame, srcSeq->numKeyframes-1, oldName.c_str());
  1227. endFrame = srcSeq->numKeyframes - 1;
  1228. }
  1229. // Create array to map source nodes to our nodes
  1230. Vector<S32> nodeMap(srcShape->nodes.size());
  1231. for (S32 i = 0; i < srcShape->nodes.size(); i++)
  1232. nodeMap.push_back(findNode(srcShape->getName(srcShape->nodes[i].nameIndex)));
  1233. // Create array to map source objects to our objects
  1234. Vector<S32> objectMap(srcShape->objects.size());
  1235. for (S32 i = 0; i < srcShape->objects.size(); i++)
  1236. objectMap.push_back(findObject(srcShape->getName(srcShape->objects[i].nameIndex)));
  1237. // Copy the source sequence (need to do it this ugly way instead of just
  1238. // using push_back since srcSeq pointer may change if copying a sequence
  1239. // from inside the shape itself
  1240. sequences.increment();
  1241. TSShape::Sequence& seq = sequences.last();
  1242. srcSeq = &srcShape->sequences[seqIndex]; // update pointer as it may have changed!
  1243. seq = *srcSeq;
  1244. seq.nameIndex = addName(name);
  1245. seq.numKeyframes = endFrame - startFrame + 1;
  1246. if (seq.duration > 0)
  1247. seq.duration *= ((F32)seq.numKeyframes / srcSeq->numKeyframes);
  1248. // Add object states
  1249. // Note: only visibility animation is supported
  1250. seq.frameMatters.clearAll();
  1251. seq.matFrameMatters.clearAll();
  1252. seq.visMatters.clearAll();
  1253. for (S32 i = 0; i < objectMap.size(); i++)
  1254. {
  1255. if (objectMap[i] < 0)
  1256. continue;
  1257. if (srcSeq->visMatters.test(i))
  1258. {
  1259. // Check if visibility is animated within the frames to be copied
  1260. const F32 defaultVis = srcShape->objectStates[i].vis;
  1261. S32 objNum = srcSeq->visMatters.count(i);
  1262. for (S32 iFrame = startFrame; iFrame <= endFrame; iFrame++)
  1263. {
  1264. if (srcShape->getObjectState(*srcSeq, iFrame, objNum).vis != defaultVis)
  1265. {
  1266. seq.visMatters.set(objectMap[i]);
  1267. break;
  1268. }
  1269. }
  1270. }
  1271. }
  1272. TSIntegerSet srcObjectStateSet(srcSeq->frameMatters);
  1273. srcObjectStateSet.overlap(srcSeq->matFrameMatters);
  1274. srcObjectStateSet.overlap(srcSeq->visMatters);
  1275. TSIntegerSet objectStateSet(seq.frameMatters);
  1276. objectStateSet.overlap(seq.matFrameMatters);
  1277. objectStateSet.overlap(seq.visMatters);
  1278. seq.baseObjectState = objectStates.size();
  1279. objectStates.increment(objectStateSet.count()*seq.numKeyframes);
  1280. for (S32 i = 0; i < objectMap.size(); i++)
  1281. {
  1282. if (objectMap[i] < 0)
  1283. continue;
  1284. // Note: only visibility animation is supported
  1285. if (objectStateSet.test(objectMap[i]))
  1286. {
  1287. S32 src = srcSeq->baseObjectState + srcSeq->numKeyframes * srcObjectStateSet.count(i) + startFrame;
  1288. S32 dest = seq.baseObjectState + seq.numKeyframes * objectStateSet.count(objectMap[i]);
  1289. dCopyArray(&objectStates[dest], &srcShape->objectStates[src], seq.numKeyframes);
  1290. }
  1291. }
  1292. // Add ground frames
  1293. F32 ratio = (F32)seq.numKeyframes / srcSeq->numKeyframes;
  1294. S32 groundBase = srcSeq->firstGroundFrame + startFrame*ratio;
  1295. seq.numGroundFrames *= ratio;
  1296. seq.firstGroundFrame = groundTranslations.size();
  1297. groundTranslations.reserve(groundTranslations.size() + seq.numGroundFrames);
  1298. groundRotations.reserve(groundRotations.size() + seq.numGroundFrames);
  1299. for (S32 i = 0; i < seq.numGroundFrames; i++)
  1300. {
  1301. groundTranslations.push_back(srcShape->groundTranslations[groundBase + i]);
  1302. groundRotations.push_back(srcShape->groundRotations[groundBase + i]);
  1303. }
  1304. // Add triggers
  1305. seq.numTriggers = 0;
  1306. seq.firstTrigger = triggers.size();
  1307. F32 seqStartPos = (F32)startFrame / seq.numKeyframes;
  1308. F32 seqEndPos = (F32)endFrame / seq.numKeyframes;
  1309. for (S32 i = 0; i < srcSeq->numTriggers; i++)
  1310. {
  1311. const TSShape::Trigger& srcTrig = srcShape->triggers[srcSeq->firstTrigger + i];
  1312. if ((srcTrig.pos >= seqStartPos) && (srcTrig.pos <= seqEndPos))
  1313. {
  1314. triggers.push_back(srcTrig);
  1315. triggers.last().pos -= seqStartPos;
  1316. seq.numTriggers++;
  1317. }
  1318. }
  1319. // Fixup node matters arrays
  1320. seq.translationMatters.clearAll();
  1321. seq.rotationMatters.clearAll();
  1322. seq.scaleMatters.clearAll();
  1323. for (S32 i = 0; i < nodeMap.size(); i++)
  1324. {
  1325. if (nodeMap[i] < 0)
  1326. continue;
  1327. if (srcSeq->translationMatters.test(i))
  1328. {
  1329. // Check if node position is animated within the frames to be copied
  1330. const Point3F& defaultTrans = srcShape->defaultTranslations[i];
  1331. S32 tranNum = srcSeq->translationMatters.count(i);
  1332. for (S32 iFrame = startFrame; iFrame <= endFrame; iFrame++)
  1333. {
  1334. if (srcShape->getTranslation(*srcSeq, iFrame, tranNum) != defaultTrans)
  1335. {
  1336. seq.translationMatters.set(nodeMap[i]);
  1337. break;
  1338. }
  1339. }
  1340. }
  1341. if (srcSeq->rotationMatters.test(i))
  1342. {
  1343. // Check if node rotation is animated within the frames to be copied
  1344. const QuatF defaultRot = srcShape->defaultRotations[i].getQuatF();
  1345. S32 rotNum = srcSeq->rotationMatters.count(i);
  1346. for (S32 iFrame = startFrame; iFrame <= endFrame; iFrame++)
  1347. {
  1348. QuatF temp;
  1349. if (srcShape->getRotation(*srcSeq, iFrame, rotNum, &temp) != defaultRot)
  1350. {
  1351. seq.rotationMatters.set(nodeMap[i]);
  1352. break;
  1353. }
  1354. }
  1355. }
  1356. if (srcSeq->scaleMatters.test(i))
  1357. {
  1358. S32 scaleNum = srcSeq->scaleMatters.count(i);
  1359. // Check if node scale is animated within the frames to be copied
  1360. if (srcSeq->animatesArbitraryScale())
  1361. {
  1362. TSScale defaultScale;
  1363. defaultScale.identity();
  1364. for (S32 iFrame = startFrame; iFrame <= endFrame; iFrame++)
  1365. {
  1366. TSScale temp;
  1367. if (!(srcShape->getArbitraryScale(*srcSeq, iFrame, scaleNum, &temp) == defaultScale))
  1368. {
  1369. seq.scaleMatters.set(nodeMap[i]);
  1370. break;
  1371. }
  1372. }
  1373. }
  1374. else if (srcSeq->animatesAlignedScale())
  1375. {
  1376. const Point3F defaultScale(Point3F::One);
  1377. for (S32 iFrame = startFrame; iFrame <= endFrame; iFrame++)
  1378. {
  1379. if (srcShape->getAlignedScale(*srcSeq, iFrame, scaleNum) != defaultScale)
  1380. {
  1381. seq.scaleMatters.set(nodeMap[i]);
  1382. break;
  1383. }
  1384. }
  1385. }
  1386. else if (srcSeq->animatesUniformScale())
  1387. {
  1388. const F32 defaultScale = 1.0f;
  1389. for (S32 iFrame = startFrame; iFrame <= endFrame; iFrame++)
  1390. {
  1391. if (srcShape->getUniformScale(*srcSeq, iFrame, scaleNum) != defaultScale)
  1392. {
  1393. seq.scaleMatters.set(nodeMap[i]);
  1394. break;
  1395. }
  1396. }
  1397. }
  1398. }
  1399. }
  1400. // Resize the node transform arrays
  1401. seq.baseTranslation = nodeTranslations.size();
  1402. nodeTranslations.increment(seq.translationMatters.count()*seq.numKeyframes);
  1403. seq.baseRotation = nodeRotations.size();
  1404. nodeRotations.increment(seq.rotationMatters.count()*seq.numKeyframes);
  1405. if (seq.flags & TSShape::ArbitraryScale)
  1406. {
  1407. S32 scaleCount = seq.scaleMatters.count();
  1408. seq.baseScale = nodeArbitraryScaleRots.size();
  1409. nodeArbitraryScaleRots.increment(scaleCount*seq.numKeyframes);
  1410. nodeArbitraryScaleFactors.increment(scaleCount*seq.numKeyframes);
  1411. }
  1412. else if (seq.flags & TSShape::AlignedScale)
  1413. {
  1414. seq.baseScale = nodeAlignedScales.size();
  1415. nodeAlignedScales.increment(seq.scaleMatters.count()*seq.numKeyframes);
  1416. }
  1417. else
  1418. {
  1419. seq.baseScale = nodeUniformScales.size();
  1420. nodeUniformScales.increment(seq.scaleMatters.count()*seq.numKeyframes);
  1421. }
  1422. // Add node transforms (remap from source node indices to our node indices). As
  1423. // well as copying animated node translations and rotations, also handle when the
  1424. // default translation and rotation are different between the source and
  1425. // destination shapes.
  1426. for (S32 i = 0; i < nodeMap.size(); i++)
  1427. {
  1428. if (nodeMap[i] < 0)
  1429. continue;
  1430. if (seq.translationMatters.test(nodeMap[i]))
  1431. {
  1432. S32 src = srcSeq->baseTranslation + srcSeq->numKeyframes * srcSeq->translationMatters.count(i) + startFrame;
  1433. S32 dest = seq.baseTranslation + seq.numKeyframes * seq.translationMatters.count(nodeMap[i]);
  1434. dCopyArray(&nodeTranslations[dest], &srcShape->nodeTranslations[src], seq.numKeyframes);
  1435. }
  1436. else if (padTransKeys && (defaultTranslations[nodeMap[i]] != srcShape->defaultTranslations[i]))
  1437. {
  1438. seq.translationMatters.set(nodeMap[i]);
  1439. S32 dest = seq.baseTranslation + seq.numKeyframes * seq.translationMatters.count(nodeMap[i]);
  1440. for (S32 j = 0; j < seq.numKeyframes; j++)
  1441. nodeTranslations.insert(dest, srcShape->defaultTranslations[i]);
  1442. }
  1443. if (seq.rotationMatters.test(nodeMap[i]))
  1444. {
  1445. S32 src = srcSeq->baseRotation + srcSeq->numKeyframes * srcSeq->rotationMatters.count(i) + startFrame;
  1446. S32 dest = seq.baseRotation + seq.numKeyframes * seq.rotationMatters.count(nodeMap[i]);
  1447. dCopyArray(&nodeRotations[dest], &srcShape->nodeRotations[src], seq.numKeyframes);
  1448. }
  1449. else if (padRotKeys && (defaultRotations[nodeMap[i]] != srcShape->defaultRotations[i]))
  1450. {
  1451. seq.rotationMatters.set(nodeMap[i]);
  1452. S32 dest = seq.baseRotation + seq.numKeyframes * seq.rotationMatters.count(nodeMap[i]);
  1453. for (S32 j = 0; j < seq.numKeyframes; j++)
  1454. nodeRotations.insert(dest, srcShape->defaultRotations[i]);
  1455. }
  1456. if (seq.scaleMatters.test(nodeMap[i]))
  1457. {
  1458. S32 src = srcSeq->baseScale + srcSeq->numKeyframes * srcSeq->scaleMatters.count(i)+ startFrame;
  1459. S32 dest = seq.baseScale + seq.numKeyframes * seq.scaleMatters.count(nodeMap[i]);
  1460. if (seq.flags & TSShape::ArbitraryScale)
  1461. {
  1462. dCopyArray(&nodeArbitraryScaleRots[dest], &srcShape->nodeArbitraryScaleRots[src], seq.numKeyframes);
  1463. dCopyArray(&nodeArbitraryScaleFactors[dest], &srcShape->nodeArbitraryScaleFactors[src], seq.numKeyframes);
  1464. }
  1465. else if (seq.flags & TSShape::AlignedScale)
  1466. dCopyArray(&nodeAlignedScales[dest], &srcShape->nodeAlignedScales[src], seq.numKeyframes);
  1467. else
  1468. dCopyArray(&nodeUniformScales[dest], &srcShape->nodeUniformScales[src], seq.numKeyframes);
  1469. }
  1470. }
  1471. // Set shape flags (only the most significant scale type)
  1472. U32 curVal = mFlags & AnyScale;
  1473. mFlags &= ~(AnyScale);
  1474. mFlags |= getMax(curVal, seq.flags & AnyScale); // take the larger value (can only convert upwards)
  1475. // Set sequence flags
  1476. seq.dirtyFlags = 0;
  1477. if (seq.rotationMatters.testAll() || seq.translationMatters.testAll() || seq.scaleMatters.testAll())
  1478. seq.dirtyFlags |= TSShapeInstance::TransformDirty;
  1479. if (seq.visMatters.testAll())
  1480. seq.dirtyFlags |= TSShapeInstance::VisDirty;
  1481. if (seq.frameMatters.testAll())
  1482. seq.dirtyFlags |= TSShapeInstance::FrameDirty;
  1483. if (seq.matFrameMatters.testAll())
  1484. seq.dirtyFlags |= TSShapeInstance::MatFrameDirty;
  1485. // Store information about how this sequence was created
  1486. seq.sourceData.from = String::ToString("%s\t%s", path.getFullPath().c_str(), oldName.c_str());
  1487. seq.sourceData.total = srcSeq->numKeyframes;
  1488. seq.sourceData.start = startFrame;
  1489. seq.sourceData.end = endFrame;
  1490. return true;
  1491. }
  1492. bool TSShape::removeSequence(const String& name)
  1493. {
  1494. // Find the sequence to be removed
  1495. S32 seqIndex = findSequence(name);
  1496. if (seqIndex < 0)
  1497. {
  1498. Con::errorf("TSShape::removeSequence: Could not find sequence '%s'", name.c_str());
  1499. return false;
  1500. }
  1501. TSShape::Sequence& seq = sequences[seqIndex];
  1502. // Remove the node transforms for this sequence
  1503. S32 transCount = eraseStates(nodeTranslations, seq.translationMatters, seq.baseTranslation, seq.numKeyframes);
  1504. S32 rotCount = eraseStates(nodeRotations, seq.rotationMatters, seq.baseRotation, seq.numKeyframes);
  1505. S32 scaleCount = 0;
  1506. if (seq.flags & TSShape::ArbitraryScale)
  1507. {
  1508. scaleCount = eraseStates(nodeArbitraryScaleRots, seq.scaleMatters, seq.baseScale, seq.numKeyframes);
  1509. eraseStates(nodeArbitraryScaleFactors, seq.scaleMatters, seq.baseScale, seq.numKeyframes);
  1510. }
  1511. else if (seq.flags & TSShape::AlignedScale)
  1512. scaleCount = eraseStates(nodeAlignedScales, seq.scaleMatters, seq.baseScale, seq.numKeyframes);
  1513. else
  1514. scaleCount = eraseStates(nodeUniformScales, seq.scaleMatters, seq.baseScale, seq.numKeyframes);
  1515. // Remove the object states for this sequence
  1516. TSIntegerSet objMatters(seq.frameMatters);
  1517. objMatters.overlap(seq.matFrameMatters);
  1518. objMatters.overlap(seq.visMatters);
  1519. S32 objCount = eraseStates(objectStates, objMatters, seq.baseObjectState, seq.numKeyframes);
  1520. // Remove groundframes and triggers
  1521. TSIntegerSet dummy;
  1522. eraseStates(groundTranslations, dummy, seq.firstGroundFrame, seq.numGroundFrames, 0);
  1523. eraseStates(groundRotations, dummy, seq.firstGroundFrame, seq.numGroundFrames, 0);
  1524. eraseStates(triggers, dummy, seq.firstTrigger, seq.numTriggers, 0);
  1525. // Fixup the base indices of the other sequences
  1526. for (S32 i = seqIndex + 1; i < sequences.size(); i++)
  1527. {
  1528. sequences[i].baseTranslation -= transCount;
  1529. sequences[i].baseRotation -= rotCount;
  1530. sequences[i].baseScale -= scaleCount;
  1531. sequences[i].baseObjectState -= objCount;
  1532. sequences[i].firstGroundFrame -= seq.numGroundFrames;
  1533. sequences[i].firstTrigger -= seq.numTriggers;
  1534. }
  1535. // Remove the sequence itself
  1536. sequences.erase(seqIndex);
  1537. // Remove the sequence name if it is no longer in use
  1538. removeName(name);
  1539. return true;
  1540. }
  1541. //-----------------------------------------------------------------------------
  1542. bool TSShape::addTrigger(const String& seqName, S32 keyframe, S32 state)
  1543. {
  1544. // Find the sequence
  1545. S32 seqIndex = findSequence(seqName);
  1546. if (seqIndex < 0)
  1547. {
  1548. Con::errorf("TSShape::addTrigger: Could not find sequence '%s'", seqName.c_str());
  1549. return false;
  1550. }
  1551. TSShape::Sequence& seq = sequences[seqIndex];
  1552. if (keyframe >= seq.numKeyframes)
  1553. {
  1554. Con::errorf("TSShape::addTrigger: Keyframe out of range (0-%d for sequence '%s')",
  1555. seq.numKeyframes-1, seqName.c_str());
  1556. return false;
  1557. }
  1558. // Encode the trigger state
  1559. if (state < 0)
  1560. state = 1 << (-state-1);
  1561. else if (state > 0)
  1562. state = (1 << (state-1)) | TSShape::Trigger::StateOn;
  1563. // Fixup seq.firstTrigger if this sequence does not have any triggers yet
  1564. if (seq.numTriggers == 0)
  1565. {
  1566. seq.firstTrigger = 0;
  1567. for (S32 i = 0; i < seqIndex; i++)
  1568. seq.firstTrigger += sequences[i].numTriggers;
  1569. }
  1570. // Find where to insert the trigger (sorted by keyframe)
  1571. S32 trigIndex;
  1572. for (trigIndex = seq.firstTrigger; trigIndex < (seq.firstTrigger + seq.numTriggers); trigIndex++)
  1573. {
  1574. const TSShape::Trigger& trig = triggers[trigIndex];
  1575. if ((S32)(trig.pos * seq.numKeyframes) > keyframe)
  1576. break;
  1577. }
  1578. // Create the new trigger
  1579. TSShape::Trigger trig;
  1580. trig.pos = (F32)keyframe / getMax(1, seq.numKeyframes-1);
  1581. trig.state = state;
  1582. triggers.insert(trigIndex, trig);
  1583. seq.numTriggers++;
  1584. // set invert for other triggers if needed
  1585. if ((trig.state & TSShape::Trigger::StateOn) == 0)
  1586. {
  1587. U32 offTrigger = (trig.state & TSShape::Trigger::StateMask);
  1588. for (S32 i = 0; i < seq.numTriggers; i++)
  1589. {
  1590. if (triggers[seq.firstTrigger + i].state & offTrigger)
  1591. triggers[seq.firstTrigger + i].state |= TSShape::Trigger::InvertOnReverse;
  1592. }
  1593. }
  1594. // fixup firstTrigger index for other sequences
  1595. for (S32 i = seqIndex + 1; i < sequences.size(); i++)
  1596. {
  1597. if (sequences[i].numTriggers > 0)
  1598. sequences[i].firstTrigger++;
  1599. }
  1600. // set MakePath flag so triggers will be animated
  1601. seq.flags |= TSShape::MakePath;
  1602. return true;
  1603. }
  1604. bool TSShape::removeTrigger(const String& seqName, S32 keyframe, S32 state)
  1605. {
  1606. // Find the sequence
  1607. S32 seqIndex = findSequence(seqName);
  1608. if (seqIndex < 0)
  1609. {
  1610. Con::errorf("TSShape::removeTrigger: Could not find sequence '%s'", seqName.c_str());
  1611. return false;
  1612. }
  1613. TSShape::Sequence& seq = sequences[seqIndex];
  1614. if (keyframe >= seq.numKeyframes)
  1615. {
  1616. Con::errorf("TSShape::removeTrigger: Keyframe out of range (0-%d for sequence '%s')",
  1617. seq.numKeyframes-1, seqName.c_str());
  1618. return false;
  1619. }
  1620. // Encode the trigger state
  1621. if (state < 0)
  1622. state = 1 << (-state-1);
  1623. else if (state > 0)
  1624. state = (1 << (state-1)) | TSShape::Trigger::StateOn;
  1625. // Find and remove the trigger
  1626. for (S32 trigIndex = seq.firstTrigger; trigIndex < (seq.firstTrigger + seq.numTriggers); trigIndex++)
  1627. {
  1628. TSShape::Trigger& trig = triggers[trigIndex];
  1629. S32 cmpFrame = (S32)(trig.pos * (seq.numKeyframes-1) + 0.5f);
  1630. S32 cmpState = trig.state & (~TSShape::Trigger::InvertOnReverse);
  1631. if ((cmpFrame == keyframe) && (cmpState == state))
  1632. {
  1633. triggers.erase(trigIndex);
  1634. seq.numTriggers--;
  1635. // Fix up firstTrigger for other sequences
  1636. for (S32 i = seqIndex + 1; i < sequences.size(); i++)
  1637. {
  1638. if (sequences[i].numTriggers > 0)
  1639. sequences[i].firstTrigger--;
  1640. }
  1641. // Clear MakePath flag if no more triggers
  1642. if ( seq.numTriggers == 0 )
  1643. seq.flags &= (~TSShape::MakePath);
  1644. return true;
  1645. }
  1646. }
  1647. Con::errorf("TSShape::removeTrigger: Could not find trigger (%d, %d) for sequence '%s'",
  1648. keyframe, state, seqName.c_str());
  1649. return false;
  1650. }
  1651. void TSShape::getNodeKeyframe(S32 nodeIndex, const TSShape::Sequence& seq, S32 keyframe, MatrixF* mat) const
  1652. {
  1653. // Get the node rotation and translation
  1654. QuatF rot;
  1655. if (seq.rotationMatters.test(nodeIndex))
  1656. {
  1657. S32 index = seq.rotationMatters.count(nodeIndex) * seq.numKeyframes + keyframe;
  1658. nodeRotations[seq.baseRotation + index].getQuatF(&rot);
  1659. }
  1660. else
  1661. defaultRotations[nodeIndex].getQuatF(&rot);
  1662. Point3F trans;
  1663. if (seq.translationMatters.test(nodeIndex))
  1664. {
  1665. S32 index = seq.translationMatters.count(nodeIndex) * seq.numKeyframes + keyframe;
  1666. trans = nodeTranslations[seq.baseTranslation + index];
  1667. }
  1668. else
  1669. trans = defaultTranslations[nodeIndex];
  1670. // Set the keyframe matrix
  1671. rot.setMatrix(mat);
  1672. mat->setPosition(trans);
  1673. }
  1674. bool TSShape::setSequenceBlend(const String& seqName, bool blend, const String& blendRefSeqName, S32 blendRefFrame)
  1675. {
  1676. // Find the target sequence
  1677. S32 seqIndex = findSequence(seqName);
  1678. if (seqIndex < 0)
  1679. {
  1680. Con::errorf("TSShape::setSequenceBlend: Could not find sequence named '%s'", seqName.c_str());
  1681. return false;
  1682. }
  1683. TSShape::Sequence& seq = sequences[seqIndex];
  1684. // Ignore if blend flag is already correct
  1685. if (seq.isBlend() == blend)
  1686. return true;
  1687. // Find the sequence containing the reference frame
  1688. S32 blendRefSeqIndex = findSequence(blendRefSeqName);
  1689. if (blendRefSeqIndex < 0)
  1690. {
  1691. Con::errorf("TSShape::setSequenceBlend: Could not find reference sequence named '%s'", blendRefSeqName.c_str());
  1692. return false;
  1693. }
  1694. TSShape::Sequence& blendRefSeq = sequences[blendRefSeqIndex];
  1695. if ((blendRefFrame < 0) || (blendRefFrame >= blendRefSeq.numKeyframes))
  1696. {
  1697. Con::errorf("TSShape::setSequenceBlend: Reference frame out of range (0-%d)", blendRefSeq.numKeyframes-1);
  1698. return false;
  1699. }
  1700. // Set the new flag
  1701. if (blend)
  1702. seq.flags |= TSShape::Blend;
  1703. else
  1704. seq.flags &= (~TSShape::Blend);
  1705. // For each animated node in the target sequence, need to add or subtract the
  1706. // reference keyframe from each frame
  1707. TSIntegerSet nodeMatters(seq.rotationMatters);
  1708. nodeMatters.overlap(seq.translationMatters);
  1709. S32 end = nodeMatters.end();
  1710. for (S32 nodeIndex = nodeMatters.start(); nodeIndex < end; nodeMatters.next(nodeIndex))
  1711. {
  1712. MatrixF refMat;
  1713. getNodeKeyframe(nodeIndex, blendRefSeq, blendRefFrame, &refMat);
  1714. // Add or subtract the reference?
  1715. if (blend)
  1716. refMat.inverse();
  1717. bool updateRot(false), updateTrans(false);
  1718. S32 rotOffset(0), transOffset(0);
  1719. if (seq.rotationMatters.test(nodeIndex))
  1720. {
  1721. updateRot = true;
  1722. rotOffset = seq.baseRotation + seq.rotationMatters.count(nodeIndex) * seq.numKeyframes;
  1723. }
  1724. if (seq.translationMatters.test(nodeIndex))
  1725. {
  1726. updateTrans = true;
  1727. transOffset = seq.baseTranslation + seq.translationMatters.count(nodeIndex) * seq.numKeyframes;
  1728. }
  1729. for (S32 frame = 0; frame < seq.numKeyframes; frame++)
  1730. {
  1731. MatrixF oldMat;
  1732. getNodeKeyframe(nodeIndex, seq, frame, &oldMat);
  1733. MatrixF newMat;
  1734. newMat.mul(refMat, oldMat);
  1735. if (updateRot)
  1736. nodeRotations[rotOffset + frame].set(QuatF(newMat));
  1737. if (updateTrans)
  1738. nodeTranslations[transOffset + frame] = newMat.getPosition();
  1739. }
  1740. }
  1741. // Update sequence blend information
  1742. seq.sourceData.blendSeq = blendRefSeqName;
  1743. seq.sourceData.blendFrame = blendRefFrame;
  1744. return true;
  1745. }
  1746. bool TSShape::setSequenceGroundSpeed(const String& seqName, const Point3F& trans, const Point3F& rot)
  1747. {
  1748. // Find the sequence
  1749. S32 seqIndex = findSequence(seqName);
  1750. if (seqIndex < 0)
  1751. {
  1752. Con::errorf("setSequenceGroundSpeed: Could not find sequence named '%s'", seqName.c_str());
  1753. return false;
  1754. }
  1755. TSShape::Sequence& seq = sequences[seqIndex];
  1756. // Determine how many ground-frames to generate (FPS=10, at least 1 frame)
  1757. const F32 groundFrameRate = 10.0f;
  1758. S32 numFrames = getMax(1, (S32)(seq.duration * groundFrameRate));
  1759. // Allocate space for the frames (add/delete frames as required)
  1760. S32 frameAdjust = numFrames - seq.numGroundFrames;
  1761. for (S32 i = 0; i < mAbs(frameAdjust); i++)
  1762. {
  1763. if (frameAdjust > 0)
  1764. {
  1765. groundTranslations.insert(seq.firstGroundFrame);
  1766. groundRotations.insert(seq.firstGroundFrame);
  1767. }
  1768. else
  1769. {
  1770. groundTranslations.erase(seq.firstGroundFrame);
  1771. groundRotations.erase(seq.firstGroundFrame);
  1772. }
  1773. }
  1774. // Fixup ground frame indices
  1775. seq.numGroundFrames += frameAdjust;
  1776. for (S32 i = seqIndex+1; i < sequences.size(); i++)
  1777. sequences[i].firstGroundFrame += frameAdjust;
  1778. // Generate the ground-frames
  1779. Point3F adjTrans = trans;
  1780. Point3F adjRot = rot;
  1781. if (seq.numGroundFrames > 0)
  1782. {
  1783. adjTrans /= seq.numGroundFrames;
  1784. adjRot /= seq.numGroundFrames;
  1785. }
  1786. QuatF rotSpeed(adjRot);
  1787. QuatF groundRot(rotSpeed);
  1788. for (S32 i = 0; i < seq.numGroundFrames; i++)
  1789. {
  1790. groundTranslations[seq.firstGroundFrame + i] = adjTrans * (i + 1);
  1791. groundRotations[seq.firstGroundFrame + i].set(groundRot);
  1792. groundRot *= rotSpeed;
  1793. }
  1794. // set MakePath flag so ground frames will be animated
  1795. seq.flags |= TSShape::MakePath;
  1796. return true;
  1797. }