tsShapeOldRead.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  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 "core/strings/stringFunctions.h"
  23. #include "core/util/endian.h"
  24. #include "ts/tsShapeInstance.h"
  25. //-------------------------------------------------
  26. // put old skins into object list
  27. //-------------------------------------------------
  28. void TSShape::fixupOldSkins(S32 numMeshes, S32 numSkins, S32 numDetails, S32 * detFirstSkin, S32 * detailNumSkins)
  29. {
  30. #if !defined(TORQUE_MAX_LIB)
  31. // this method not necessary in exporter, and a couple lines won't compile for exporter
  32. if (!objects.address() || !meshes.address() || !numSkins)
  33. // not ready for this yet, will catch it on the next pass
  34. return;
  35. S32 numObjects = objects.size();
  36. TSObject * newObjects = objects.address() + objects.size();
  37. TSSkinMesh ** skins = (TSSkinMesh**)&meshes[numMeshes];
  38. Vector<TSSkinMesh*> skinsCopy;
  39. // Note: newObjects has as much free space as we need, so we just need to keep track of the
  40. // number of objects we use and then update objects.size
  41. S32 numSkinObjects = 0;
  42. S32 skinsUsed = 0;
  43. S32 emptySkins = 0;
  44. S32 i;
  45. for (i=0; i<numSkins; i++)
  46. if (skins[i]==NULL)
  47. emptySkins++; // probably never, but just in case
  48. while (skinsUsed<numSkins-emptySkins)
  49. {
  50. TSObject & object = newObjects[numSkinObjects++];
  51. objects.increment();
  52. object.nameIndex = 0; // no name
  53. object.numMeshes = 0;
  54. object.startMeshIndex = numMeshes + skinsCopy.size();
  55. object.nodeIndex = -1;
  56. object.nextSibling = -1;
  57. for (S32 dl=0; dl<numDetails; dl++)
  58. {
  59. // find one mesh per detail to add to this object
  60. // don't really need to be versions of the same object
  61. i = 0;
  62. while (i<detFirstSkin[dl] || detFirstSkin[dl]<0)
  63. i++;
  64. for (; i<numSkins && i<detFirstSkin[dl]+detailNumSkins[dl]; i++)
  65. {
  66. if (skins[i])
  67. {
  68. // found an unused skin... copy it to skinsCopy and set to NULL
  69. skinsCopy.push_back(skins[i]);
  70. skins[i]=NULL;
  71. object.numMeshes++;
  72. skinsUsed++;
  73. break;
  74. }
  75. }
  76. if (i==numSkins || i==detFirstSkin[dl]+detailNumSkins[dl])
  77. {
  78. skinsCopy.push_back(NULL);
  79. object.numMeshes++;
  80. }
  81. }
  82. // exit above loop with one skin per detail...despose of trailing null meshes
  83. while (!skinsCopy.empty() && skinsCopy.last()==NULL)
  84. {
  85. skinsCopy.decrement();
  86. object.numMeshes--;
  87. }
  88. // if no meshes, don't need object
  89. if (!object.numMeshes)
  90. {
  91. objects.decrement();
  92. numSkinObjects--;
  93. }
  94. }
  95. dMemcpy(skins,skinsCopy.address(),skinsCopy.size()*sizeof(TSSkinMesh*));
  96. if (subShapeFirstObject.size()==1)
  97. // as long as only one subshape, we'll now be rendered
  98. subShapeNumObjects[0] += numSkinObjects;
  99. // now for something ugly -- we've added somoe objects to hold the skins...
  100. // now we have to add default states for those objects
  101. // we also have to increment base states on all the sequences that are loaded
  102. dMemmove(objectStates.address()+numObjects+numSkinObjects,objectStates.address()+numObjects,(objectStates.size()-numObjects)*sizeof(ObjectState));
  103. for (i=numObjects; i<numObjects+numSkinObjects; i++)
  104. {
  105. objectStates[i].vis=1.0f;
  106. objectStates[i].frameIndex=0;
  107. objectStates[i].matFrameIndex=0;
  108. }
  109. for (i=0;i<sequences.size();i++)
  110. {
  111. sequences[i].baseObjectState += numSkinObjects;
  112. }
  113. #endif
  114. }
  115. //-------------------------------------------------
  116. // some macros used for read/write
  117. //-------------------------------------------------
  118. // write a vector of structs (minus the first 'm')
  119. #define writeVectorStructMinus(a,m) \
  120. {\
  121. s->write(a.size() - m); \
  122. for (S32 i=m;i<a.size();i++) \
  123. a[i].write(s); \
  124. }
  125. // write a vector of simple types (minus the first 'm')
  126. #define writeVectorSimpleMinus(a,m) \
  127. {\
  128. s->write(a.size() - m); \
  129. for (S32 i=m;i<a.size();i++) \
  130. s->write(a[i]); \
  131. }
  132. // same as above with m=0
  133. #define writeVectorStruct(a) writeVectorStructMinus(a,0)
  134. #define writeVectorSimple(a) writeVectorSimpleMinus(a,0)
  135. // read a vector of structs -- over-writing any existing data
  136. #define readVectorStruct(a) \
  137. { \
  138. S32 sz; \
  139. s->read(&sz); \
  140. a.setSize(sz); \
  141. for (S32 i=0;i<sz;i++) \
  142. a[i].read(s); \
  143. }
  144. // read a vector of simple types -- over-writing any existing data
  145. #define readVectorSimple(a) \
  146. { \
  147. S32 sz; \
  148. s->read(&sz); \
  149. a.setSize(sz); \
  150. for (S32 i=0;i<sz;i++) \
  151. s->read(&a[i]); \
  152. }
  153. // read a vector of structs -- append to any existing data
  154. #define appendVectorStruct(a) \
  155. { \
  156. S32 sz; \
  157. S32 oldSz = a.size(); \
  158. s->read(&sz); \
  159. a.setSize(oldSz + sz); \
  160. for (S32 i=0;i<sz;i++) \
  161. a[i + oldSz].read(s); \
  162. }
  163. // read a vector of simple types -- append to any existing data
  164. #define appendVectorSimple(a) \
  165. { \
  166. S32 sz; \
  167. S32 oldSz = a.size(); \
  168. s->read(&sz); \
  169. a.setSize(oldSz + sz); \
  170. for (S32 i=0;i<sz;i++) \
  171. s->read(&a[i + oldSz]); \
  172. }
  173. //-------------------------------------------------
  174. // export all sequences
  175. //-------------------------------------------------
  176. void TSShape::exportSequences(Stream * s)
  177. {
  178. // write version
  179. s->write(smVersion);
  180. S32 i,sz;
  181. // write node names
  182. // -- this is how we will map imported sequence nodes to shape nodes
  183. sz = nodes.size();
  184. s->write(sz);
  185. for (i=0;i<nodes.size();i++)
  186. writeName(s,nodes[i].nameIndex);
  187. // legacy write -- write zero objects, don't pretend to support object export anymore
  188. s->write(0);
  189. // on import, we will need to adjust keyframe data based on number of
  190. // nodes/objects in this shape...number of nodes can be inferred from
  191. // above, but number of objects cannot be. Write that quantity here:
  192. s->write(objects.size());
  193. // write node states -- skip default node states
  194. s->write(nodeRotations.size());
  195. for (i=0;i<nodeRotations.size();i++)
  196. {
  197. s->write(nodeRotations[i].x);
  198. s->write(nodeRotations[i].y);
  199. s->write(nodeRotations[i].z);
  200. s->write(nodeRotations[i].w);
  201. }
  202. s->write(nodeTranslations.size());
  203. for (i=0;i<nodeTranslations.size(); i++)
  204. {
  205. s->write(nodeTranslations[i].x);
  206. s->write(nodeTranslations[i].y);
  207. s->write(nodeTranslations[i].z);
  208. }
  209. s->write(nodeUniformScales.size());
  210. for (i=0;i<nodeUniformScales.size();i++)
  211. s->write(nodeUniformScales[i]);
  212. s->write(nodeAlignedScales.size());
  213. for (i=0;i<nodeAlignedScales.size();i++)
  214. {
  215. s->write(nodeAlignedScales[i].x);
  216. s->write(nodeAlignedScales[i].y);
  217. s->write(nodeAlignedScales[i].z);
  218. }
  219. s->write(nodeArbitraryScaleRots.size());
  220. for (i=0;i<nodeArbitraryScaleRots.size();i++)
  221. {
  222. s->write(nodeArbitraryScaleRots[i].x);
  223. s->write(nodeArbitraryScaleRots[i].y);
  224. s->write(nodeArbitraryScaleRots[i].z);
  225. s->write(nodeArbitraryScaleRots[i].w);
  226. }
  227. for (i=0;i<nodeArbitraryScaleFactors.size();i++)
  228. {
  229. s->write(nodeArbitraryScaleFactors[i].x);
  230. s->write(nodeArbitraryScaleFactors[i].y);
  231. s->write(nodeArbitraryScaleFactors[i].z);
  232. }
  233. s->write(groundTranslations.size());
  234. for (i=0;i<groundTranslations.size();i++)
  235. {
  236. s->write(groundTranslations[i].x);
  237. s->write(groundTranslations[i].y);
  238. s->write(groundTranslations[i].z);
  239. }
  240. for (i=0;i<groundRotations.size();i++)
  241. {
  242. s->write(groundRotations[i].x);
  243. s->write(groundRotations[i].y);
  244. s->write(groundRotations[i].z);
  245. s->write(groundRotations[i].w);
  246. }
  247. // write object states -- legacy..no object states
  248. s->write((S32)0);
  249. // write sequences
  250. s->write(sequences.size());
  251. for (i=0;i<sequences.size();i++)
  252. {
  253. Sequence & seq = sequences[i];
  254. // first write sequence name
  255. writeName(s,seq.nameIndex);
  256. // now write the sequence itself
  257. seq.write(s,false); // false --> don't write name index
  258. }
  259. // write out all the triggers...
  260. s->write(triggers.size());
  261. for (i=0; i<triggers.size(); i++)
  262. {
  263. s->write(triggers[i].state);
  264. s->write(triggers[i].pos);
  265. }
  266. }
  267. //-------------------------------------------------
  268. // export a single sequence
  269. //-------------------------------------------------
  270. void TSShape::exportSequence(Stream * s, const TSShape::Sequence& seq, bool saveOldFormat)
  271. {
  272. S32 currentVersion = smVersion;
  273. if ( saveOldFormat )
  274. smVersion = 24;
  275. // write version
  276. s->write(smVersion);
  277. // write node names
  278. s->write( nodes.size() );
  279. for ( S32 i = 0; i < nodes.size(); i++ )
  280. writeName( s, nodes[i].nameIndex );
  281. // legacy write -- write zero objects, don't pretend to support object export anymore
  282. s->write( (S32)0 );
  283. // on import, we will need to adjust keyframe data based on number of
  284. // nodes/objects in this shape...number of nodes can be inferred from
  285. // above, but number of objects cannot be. Write that quantity here:
  286. s->write( objects.size() );
  287. // write node states -- skip default node states
  288. S32 count = seq.rotationMatters.count() * seq.numKeyframes;
  289. s->write( count );
  290. for ( S32 i = seq.baseRotation; i < seq.baseRotation + count; i++ )
  291. {
  292. s->write( nodeRotations[i].x );
  293. s->write( nodeRotations[i].y );
  294. s->write( nodeRotations[i].z );
  295. s->write( nodeRotations[i].w );
  296. }
  297. count = seq.translationMatters.count() * seq.numKeyframes;
  298. s->write( count );
  299. for ( S32 i = seq.baseTranslation; i < seq.baseTranslation + count; i++ )
  300. {
  301. s->write( nodeTranslations[i].x );
  302. s->write( nodeTranslations[i].y );
  303. s->write( nodeTranslations[i].z );
  304. }
  305. count = seq.scaleMatters.count() * seq.numKeyframes;
  306. if ( seq.animatesUniformScale() )
  307. {
  308. s->write( count );
  309. for ( S32 i = seq.baseScale; i < seq.baseScale + count; i++ )
  310. s->write( nodeUniformScales[i] );
  311. }
  312. else
  313. s->write( (S32)0 );
  314. if ( seq.animatesAlignedScale() )
  315. {
  316. s->write( count );
  317. for ( S32 i = seq.baseScale; i < seq.baseScale + count; i++ )
  318. {
  319. s->write( nodeAlignedScales[i].x );
  320. s->write( nodeAlignedScales[i].y );
  321. s->write( nodeAlignedScales[i].z );
  322. }
  323. }
  324. else
  325. s->write( (S32)0 );
  326. if ( seq.animatesArbitraryScale() )
  327. {
  328. s->write( count );
  329. for ( S32 i = seq.baseScale; i < seq.baseScale + count; i++ )
  330. {
  331. s->write( nodeArbitraryScaleRots[i].x );
  332. s->write( nodeArbitraryScaleRots[i].y );
  333. s->write( nodeArbitraryScaleRots[i].z );
  334. s->write( nodeArbitraryScaleRots[i].w );
  335. }
  336. for ( S32 i = seq.baseScale; i < seq.baseScale + count; i++ )
  337. {
  338. s->write( nodeArbitraryScaleFactors[i].x );
  339. s->write( nodeArbitraryScaleFactors[i].y );
  340. s->write( nodeArbitraryScaleFactors[i].z );
  341. }
  342. }
  343. else
  344. s->write( (S32)0 );
  345. s->write( seq.numGroundFrames );
  346. for ( S32 i = seq.firstGroundFrame; i < seq.firstGroundFrame + seq.numGroundFrames; i++ )
  347. {
  348. s->write( groundTranslations[i].x );
  349. s->write( groundTranslations[i].y );
  350. s->write( groundTranslations[i].z );
  351. }
  352. for ( S32 i = seq.firstGroundFrame; i < seq.firstGroundFrame + seq.numGroundFrames; i++ )
  353. {
  354. s->write( groundRotations[i].x );
  355. s->write( groundRotations[i].y );
  356. s->write( groundRotations[i].z );
  357. s->write( groundRotations[i].w );
  358. }
  359. // write object states -- legacy..no object states
  360. s->write( (S32)0 );
  361. // write the sequence
  362. s->write( (S32)1 );
  363. writeName( s, seq.nameIndex );
  364. {
  365. // Write a copy of the sequence with all offsets set to 0
  366. TSShape::Sequence tmpSeq(seq);
  367. tmpSeq.baseDecalState = 0;
  368. tmpSeq.baseObjectState = 0;
  369. tmpSeq.baseTranslation = 0;
  370. tmpSeq.baseRotation = 0;
  371. tmpSeq.baseScale = 0;
  372. tmpSeq.firstGroundFrame = 0;
  373. tmpSeq.firstTrigger = 0;
  374. tmpSeq.write( s, false );
  375. }
  376. // write the sequence triggers
  377. s->write( seq.numTriggers );
  378. for ( S32 i = seq.firstTrigger; i < seq.firstTrigger + seq.numTriggers; i++ )
  379. {
  380. s->write( triggers[i].state );
  381. s->write( triggers[i].pos );
  382. }
  383. smVersion = currentVersion;
  384. }
  385. //-------------------------------------------------
  386. // import sequences into existing shape
  387. //-------------------------------------------------
  388. bool TSShape::importSequences(Stream * s, const String& sequencePath)
  389. {
  390. // write version
  391. s->read(&smReadVersion);
  392. if (smReadVersion>smVersion)
  393. {
  394. // error -- don't support future version yet :>
  395. Con::errorf(ConsoleLogEntry::General,
  396. "Sequence import failed: shape exporter newer than running executable.");
  397. return false;
  398. }
  399. if (smReadVersion<19)
  400. {
  401. // error -- don't support future version yet :>
  402. Con::errorf(ConsoleLogEntry::General,
  403. "Sequence import failed: deprecated version (%i).",smReadVersion);
  404. return false;
  405. }
  406. Vector<S32> nodeMap; // node index of each node from imported sequences
  407. Vector<S32> objectMap; // object index of objects from imported sequences
  408. VECTOR_SET_ASSOCIATION(nodeMap);
  409. VECTOR_SET_ASSOCIATION(objectMap);
  410. S32 i,sz;
  411. // read node names
  412. // -- this is how we will map imported sequence nodes to our nodes
  413. s->read(&sz);
  414. nodeMap.setSize(sz);
  415. for (i=0;i<sz;i++)
  416. {
  417. U32 startSize = names.size();
  418. S32 nameIndex = readName(s,true);
  419. nodeMap[i] = findNode(nameIndex);
  420. if (nodeMap[i] < 0)
  421. {
  422. // node found in sequence but not shape => remove the added node name
  423. if (names.size() != startSize)
  424. {
  425. names.decrement();
  426. if (names.size() != startSize)
  427. Con::errorf(ConsoleLogEntry::General, "TSShape::importSequence: failed to remove unused node correctly for dsq %s.", names[nameIndex].c_str(), sequencePath.c_str());
  428. }
  429. }
  430. }
  431. // read the following size, but won't do anything with it...legacy: was going to support
  432. // import of sequences that animate objects...we don't...
  433. s->read(&sz);
  434. // before reading keyframes, take note of a couple numbers
  435. S32 oldShapeNumObjects;
  436. s->read(&oldShapeNumObjects);
  437. // adjust all the new keyframes
  438. S32 adjNodeRots = smReadVersion<22 ? nodeRotations.size() - nodeMap.size() : nodeRotations.size();
  439. S32 adjNodeTrans = smReadVersion<22 ? nodeTranslations.size() - nodeMap.size() : nodeTranslations.size();
  440. S32 adjGroundStates = smReadVersion<22 ? 0 : groundTranslations.size(); // groundTrans==groundRot
  441. // Read the node states into temporary vectors, then use the
  442. // nodeMap to discard unused transforms and map others to our nodes
  443. Vector<Quat16> seqRotations;
  444. Vector<Point3F> seqTranslations;
  445. Vector<F32> seqUniformScales;
  446. Vector<Point3F> seqAlignedScales;
  447. Vector<Quat16> seqArbitraryScaleRots;
  448. Vector<Point3F> seqArbitraryScaleFactors;
  449. if (smReadVersion>21)
  450. {
  451. s->read(&sz);
  452. seqRotations.setSize(sz);
  453. for (i=0; i < sz; i++)
  454. {
  455. s->read(&seqRotations[i].x);
  456. s->read(&seqRotations[i].y);
  457. s->read(&seqRotations[i].z);
  458. s->read(&seqRotations[i].w);
  459. }
  460. s->read(&sz);
  461. seqTranslations.setSize(sz);
  462. for (i=0; i <sz; i++)
  463. {
  464. s->read(&seqTranslations[i].x);
  465. s->read(&seqTranslations[i].y);
  466. s->read(&seqTranslations[i].z);
  467. }
  468. s->read(&sz);
  469. seqUniformScales.setSize(sz);
  470. for (i = 0; i < sz; i++)
  471. s->read(&seqUniformScales[i]);
  472. s->read(&sz);
  473. seqAlignedScales.setSize(sz);
  474. for (i = 0; i < sz; i++)
  475. {
  476. s->read(&seqAlignedScales[i].x);
  477. s->read(&seqAlignedScales[i].y);
  478. s->read(&seqAlignedScales[i].z);
  479. }
  480. s->read(&sz);
  481. seqArbitraryScaleRots.setSize(sz);
  482. for (i = 0; i <sz; i++)
  483. {
  484. s->read(&seqArbitraryScaleRots[i].x);
  485. s->read(&seqArbitraryScaleRots[i].y);
  486. s->read(&seqArbitraryScaleRots[i].z);
  487. s->read(&seqArbitraryScaleRots[i].w);
  488. }
  489. seqArbitraryScaleFactors.setSize(sz);
  490. for (i = 0; i < sz; i++)
  491. {
  492. s->read(&seqArbitraryScaleFactors[i].x);
  493. s->read(&seqArbitraryScaleFactors[i].y);
  494. s->read(&seqArbitraryScaleFactors[i].z);
  495. }
  496. // ground transforms can be read directly into the shape (none will be
  497. // discarded)
  498. s->read(&sz);
  499. S32 oldSz = groundTranslations.size();
  500. groundTranslations.setSize(sz+oldSz);
  501. for (i=oldSz;i<sz+oldSz;i++)
  502. {
  503. s->read(&groundTranslations[i].x);
  504. s->read(&groundTranslations[i].y);
  505. s->read(&groundTranslations[i].z);
  506. }
  507. groundRotations.setSize(sz+oldSz);
  508. for (i=oldSz;i<sz+oldSz;i++)
  509. {
  510. s->read(&groundRotations[i].x);
  511. s->read(&groundRotations[i].y);
  512. s->read(&groundRotations[i].z);
  513. s->read(&groundRotations[i].w);
  514. }
  515. }
  516. else
  517. {
  518. s->read(&sz);
  519. seqRotations.setSize(sz);
  520. seqTranslations.setSize(sz);
  521. for (i = 0; i < sz; i++)
  522. {
  523. s->read(&seqRotations[i].x);
  524. s->read(&seqRotations[i].y);
  525. s->read(&seqRotations[i].z);
  526. s->read(&seqRotations[i].w);
  527. s->read(&seqTranslations[i].x);
  528. s->read(&seqTranslations[i].y);
  529. s->read(&seqTranslations[i].z);
  530. }
  531. }
  532. // add these object states to our own -- shouldn't be any...assume it
  533. s->read(&sz);
  534. // read sequences
  535. s->read(&sz);
  536. S32 startSeqNum = sequences.size();
  537. for (i=0;i<sz;i++)
  538. {
  539. sequences.increment();
  540. Sequence & seq = sequences.last();
  541. // read name
  542. seq.nameIndex = readName(s,true);
  543. // read the rest of the sequence
  544. seq.read(s,false);
  545. seq.baseRotation = nodeRotations.size();
  546. seq.baseTranslation = nodeTranslations.size();
  547. if (smReadVersion > 21)
  548. {
  549. if (seq.animatesUniformScale())
  550. seq.baseScale = nodeUniformScales.size();
  551. else if (seq.animatesAlignedScale())
  552. seq.baseScale = nodeAlignedScales.size();
  553. else if (seq.animatesArbitraryScale())
  554. seq.baseScale = nodeArbitraryScaleFactors.size();
  555. }
  556. // remap the node matters arrays
  557. S32 j;
  558. TSIntegerSet newTransMembership;
  559. TSIntegerSet newRotMembership;
  560. TSIntegerSet newScaleMembership;
  561. for (j = 0; j < (S32)nodeMap.size(); j++)
  562. {
  563. if (nodeMap[j] < 0)
  564. continue;
  565. if (seq.translationMatters.test(j))
  566. newTransMembership.set(nodeMap[j]);
  567. if (seq.rotationMatters.test(j))
  568. newRotMembership.set(nodeMap[j]);
  569. if (seq.scaleMatters.test(j))
  570. newScaleMembership.set(nodeMap[j]);
  571. }
  572. // resize node transform arrays
  573. nodeTranslations.increment(newTransMembership.count() * seq.numKeyframes);
  574. nodeRotations.increment(newRotMembership.count() * seq.numKeyframes);
  575. if (seq.flags & TSShape::ArbitraryScale)
  576. {
  577. S32 scaleCount = newScaleMembership.count() * seq.numKeyframes;
  578. nodeArbitraryScaleRots.increment(scaleCount);
  579. nodeArbitraryScaleFactors.increment(scaleCount);
  580. }
  581. else if (seq.flags & TSShape::AlignedScale)
  582. nodeAlignedScales.increment(newScaleMembership.count() * seq.numKeyframes);
  583. else
  584. nodeUniformScales.increment(newScaleMembership.count() * seq.numKeyframes);
  585. // remap node transforms from temporary arrays
  586. for (S32 nodeID = 0; nodeID < nodeMap.size(); nodeID++)
  587. {
  588. if (nodeMap[nodeID] < 0)
  589. continue;
  590. if (newTransMembership.test(nodeMap[nodeID]))
  591. {
  592. S32 src = seq.numKeyframes * seq.translationMatters.count(nodeID);
  593. S32 dest = seq.baseTranslation + seq.numKeyframes * newTransMembership.count(nodeMap[nodeID]);
  594. dCopyArray(&nodeTranslations[dest], &seqTranslations[src], seq.numKeyframes);
  595. }
  596. if (newRotMembership.test(nodeMap[nodeID]))
  597. {
  598. S32 src = seq.numKeyframes * seq.rotationMatters.count(nodeID);
  599. S32 dest = seq.baseRotation + seq.numKeyframes * newRotMembership.count(nodeMap[nodeID]);
  600. dCopyArray(&nodeRotations[dest], &seqRotations[src], seq.numKeyframes);
  601. }
  602. if (newScaleMembership.test(nodeMap[nodeID]))
  603. {
  604. S32 src = seq.numKeyframes * seq.scaleMatters.count(nodeID);
  605. S32 dest = seq.baseScale + seq.numKeyframes * newScaleMembership.count(nodeMap[nodeID]);
  606. if (seq.flags & TSShape::ArbitraryScale)
  607. {
  608. dCopyArray(&nodeArbitraryScaleRots[dest], &seqArbitraryScaleRots[src], seq.numKeyframes);
  609. dCopyArray(&nodeArbitraryScaleFactors[dest], &seqArbitraryScaleFactors[src], seq.numKeyframes);
  610. }
  611. else if (seq.flags & TSShape::AlignedScale)
  612. dCopyArray(&nodeAlignedScales[dest], &seqAlignedScales[src], seq.numKeyframes);
  613. else
  614. dCopyArray(&nodeUniformScales[dest], &seqUniformScales[src], seq.numKeyframes);
  615. }
  616. }
  617. seq.translationMatters = newTransMembership;
  618. seq.rotationMatters = newRotMembership;
  619. seq.scaleMatters = newScaleMembership;
  620. // adjust trigger numbers...we'll read triggers after sequences...
  621. seq.firstTrigger += triggers.size();
  622. // finally, adjust ground transform's nodes states
  623. seq.firstGroundFrame += adjGroundStates;
  624. }
  625. if (smReadVersion<22)
  626. {
  627. for (i=startSeqNum; i<sequences.size(); i++)
  628. {
  629. // move ground transform data to ground vectors
  630. Sequence & seq = sequences[i];
  631. S32 oldSz = groundTranslations.size();
  632. groundTranslations.setSize(oldSz+seq.numGroundFrames);
  633. groundRotations.setSize(oldSz+seq.numGroundFrames);
  634. for (S32 j=0;j<seq.numGroundFrames;j++)
  635. {
  636. groundTranslations[j+oldSz] = nodeTranslations[seq.firstGroundFrame+adjNodeTrans+j];
  637. groundRotations[j+oldSz] = nodeRotations[seq.firstGroundFrame+adjNodeRots+j];
  638. }
  639. seq.firstGroundFrame = oldSz;
  640. }
  641. }
  642. // add the new triggers
  643. S32 oldSz = triggers.size();
  644. s->read(&sz);
  645. triggers.setSize(oldSz+sz);
  646. for (S32 triggerID=0; triggerID<sz; triggerID++)
  647. {
  648. s->read(&triggers[triggerID +oldSz].state);
  649. s->read(&triggers[triggerID +oldSz].pos);
  650. }
  651. if (smInitOnRead)
  652. init();
  653. return true;
  654. }
  655. //-------------------------------------------------
  656. // read/write sequence
  657. //-------------------------------------------------
  658. void TSShape::Sequence::read(Stream * s, bool readNameIndex)
  659. {
  660. AssertISV(smReadVersion>=19,"Reading old sequence");
  661. if (readNameIndex)
  662. s->read(&nameIndex);
  663. flags = 0;
  664. if (TSShape::smReadVersion>21)
  665. s->read(&flags);
  666. else
  667. flags=0;
  668. s->read(&numKeyframes);
  669. s->read(&duration);
  670. if (TSShape::smReadVersion<22)
  671. {
  672. bool tmp = false;
  673. s->read(&tmp);
  674. if (tmp)
  675. flags |= Blend;
  676. s->read(&tmp);
  677. if (tmp)
  678. flags |= Cyclic;
  679. s->read(&tmp);
  680. if (tmp)
  681. flags |= MakePath;
  682. }
  683. s->read(&priority);
  684. s->read(&firstGroundFrame);
  685. s->read(&numGroundFrames);
  686. if (TSShape::smReadVersion>21)
  687. {
  688. s->read(&baseRotation);
  689. s->read(&baseTranslation);
  690. s->read(&baseScale);
  691. s->read(&baseObjectState);
  692. s->read(&baseDecalState);
  693. }
  694. else
  695. {
  696. s->read(&baseRotation);
  697. baseTranslation=baseRotation;
  698. s->read(&baseObjectState);
  699. s->read(&baseDecalState);
  700. }
  701. s->read(&firstTrigger);
  702. s->read(&numTriggers);
  703. s->read(&toolBegin);
  704. // now the membership sets:
  705. rotationMatters.read(s);
  706. if (TSShape::smReadVersion<22)
  707. translationMatters=rotationMatters;
  708. else
  709. {
  710. translationMatters.read(s);
  711. scaleMatters.read(s);
  712. }
  713. TSIntegerSet dummy;
  714. dummy.read(s); // DEPRECIATED: Decals
  715. dummy.read(s); // DEPRECIATED: Ifl materials
  716. visMatters.read(s);
  717. frameMatters.read(s);
  718. matFrameMatters.read(s);
  719. dirtyFlags = 0;
  720. if (rotationMatters.testAll() || translationMatters.testAll() || scaleMatters.testAll())
  721. dirtyFlags |= TSShapeInstance::TransformDirty;
  722. if (visMatters.testAll())
  723. dirtyFlags |= TSShapeInstance::VisDirty;
  724. if (frameMatters.testAll())
  725. dirtyFlags |= TSShapeInstance::FrameDirty;
  726. if (matFrameMatters.testAll())
  727. dirtyFlags |= TSShapeInstance::MatFrameDirty;
  728. }
  729. void TSShape::Sequence::write(Stream * s, bool writeNameIndex) const
  730. {
  731. if (writeNameIndex)
  732. s->write(nameIndex);
  733. s->write(flags);
  734. s->write(numKeyframes);
  735. s->write(duration);
  736. s->write(priority);
  737. s->write(firstGroundFrame);
  738. s->write(numGroundFrames);
  739. s->write(baseRotation);
  740. s->write(baseTranslation);
  741. s->write(baseScale);
  742. s->write(baseObjectState);
  743. s->write(baseDecalState);
  744. s->write(firstTrigger);
  745. s->write(numTriggers);
  746. s->write(toolBegin);
  747. // now the membership sets:
  748. rotationMatters.write(s);
  749. translationMatters.write(s);
  750. scaleMatters.write(s);
  751. TSIntegerSet dummy;
  752. dummy.write(s); // DEPRECIATED: Decals
  753. dummy.write(s); // DEPRECIATED: Ifl materials
  754. visMatters.write(s);
  755. frameMatters.write(s);
  756. matFrameMatters.write(s);
  757. }
  758. void TSShape::writeName(Stream * s, S32 nameIndex)
  759. {
  760. const char * name = "";
  761. if (nameIndex>=0)
  762. name = names[nameIndex];
  763. S32 sz = (S32)dStrlen(name);
  764. s->write(sz);
  765. if (sz)
  766. s->write(sz*sizeof(char),name);
  767. }
  768. S32 TSShape::readName(Stream * s, bool addName)
  769. {
  770. static char buffer[256];
  771. U32 sz;
  772. S32 nameIndex = -1;
  773. s->read(&sz);
  774. if (sz>0 && sz<255)
  775. {
  776. s->read(sz,buffer);
  777. buffer[sz] = '\0';
  778. nameIndex = findName(buffer);
  779. // Many modeling apps don't support spaces in names, so if the lookup
  780. // failed, try the name again with spaces replaced by underscores
  781. if (nameIndex < 0)
  782. {
  783. while (char *p = dStrchr(buffer, ' '))
  784. *p = '_';
  785. nameIndex = findName(buffer);
  786. }
  787. if (nameIndex<0 && addName)
  788. {
  789. nameIndex = names.size();
  790. names.increment();
  791. names.last() = buffer;
  792. }
  793. }
  794. else
  795. Con::errorf("invalid TSShape::readName length!");
  796. return nameIndex;
  797. }