tsShape.cpp 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289
  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 "ts/tsShape.h"
  24. #include "ts/tsLastDetail.h"
  25. #include "ts/tsMaterialList.h"
  26. #include "core/stringTable.h"
  27. #include "console/console.h"
  28. #include "ts/tsShapeInstance.h"
  29. #include "collision/convex.h"
  30. #include "materials/matInstance.h"
  31. #include "materials/materialManager.h"
  32. #include "math/mathIO.h"
  33. #include "core/util/endian.h"
  34. #include "core/stream/fileStream.h"
  35. #include "console/compiler.h"
  36. #include "core/fileObject.h"
  37. #ifdef TORQUE_COLLADA
  38. extern TSShape* loadColladaShape(const Torque::Path &path);
  39. #endif
  40. /// most recent version -- this is the version we write
  41. S32 TSShape::smVersion = 26;
  42. /// the version currently being read...valid only during a read
  43. S32 TSShape::smReadVersion = -1;
  44. const U32 TSShape::smMostRecentExporterVersion = DTS_EXPORTER_CURRENT_VERSION;
  45. F32 TSShape::smAlphaOutLastDetail = -1.0f;
  46. F32 TSShape::smAlphaInBillboard = 0.15f;
  47. F32 TSShape::smAlphaOutBillboard = 0.15f;
  48. F32 TSShape::smAlphaInDefault = -1.0f;
  49. F32 TSShape::smAlphaOutDefault = -1.0f;
  50. // don't bother even loading this many of the highest detail levels (but
  51. // always load last renderable detail)
  52. S32 TSShape::smNumSkipLoadDetails = 0;
  53. bool TSShape::smInitOnRead = true;
  54. TSShape::TSShape()
  55. {
  56. materialList = NULL;
  57. mReadVersion = -1; // -1 means constructed from scratch (e.g., in exporter or no read yet)
  58. mHasSkinMesh = false;
  59. mSequencesConstructed = false;
  60. mShapeData = NULL;
  61. mShapeDataSize = 0;
  62. mUseDetailFromScreenError = false;
  63. mDetailLevelLookup.setSize( 1 );
  64. mDetailLevelLookup[0].set( -1, 0 );
  65. VECTOR_SET_ASSOCIATION(sequences);
  66. VECTOR_SET_ASSOCIATION(nodeRotations);
  67. VECTOR_SET_ASSOCIATION(nodeTranslations);
  68. VECTOR_SET_ASSOCIATION(nodeUniformScales);
  69. VECTOR_SET_ASSOCIATION(nodeAlignedScales);
  70. VECTOR_SET_ASSOCIATION(nodeArbitraryScaleRots);
  71. VECTOR_SET_ASSOCIATION(nodeArbitraryScaleFactors);
  72. VECTOR_SET_ASSOCIATION(groundRotations);
  73. VECTOR_SET_ASSOCIATION(groundTranslations);
  74. VECTOR_SET_ASSOCIATION(triggers);
  75. VECTOR_SET_ASSOCIATION(billboardDetails);
  76. VECTOR_SET_ASSOCIATION(detailCollisionAccelerators);
  77. VECTOR_SET_ASSOCIATION(names);
  78. VECTOR_SET_ASSOCIATION( nodes );
  79. VECTOR_SET_ASSOCIATION( objects );
  80. VECTOR_SET_ASSOCIATION( objectStates );
  81. VECTOR_SET_ASSOCIATION( subShapeFirstNode );
  82. VECTOR_SET_ASSOCIATION( subShapeFirstObject );
  83. VECTOR_SET_ASSOCIATION( detailFirstSkin );
  84. VECTOR_SET_ASSOCIATION( subShapeNumNodes );
  85. VECTOR_SET_ASSOCIATION( subShapeNumObjects );
  86. VECTOR_SET_ASSOCIATION( details );
  87. VECTOR_SET_ASSOCIATION( defaultRotations );
  88. VECTOR_SET_ASSOCIATION( defaultTranslations );
  89. VECTOR_SET_ASSOCIATION( subShapeFirstTranslucentObject );
  90. VECTOR_SET_ASSOCIATION( meshes );
  91. VECTOR_SET_ASSOCIATION( alphaIn );
  92. VECTOR_SET_ASSOCIATION( alphaOut );
  93. }
  94. TSShape::~TSShape()
  95. {
  96. delete materialList;
  97. S32 i;
  98. // everything left over here is a legit mesh
  99. for (i=0; i<meshes.size(); i++)
  100. {
  101. if (!meshes[i])
  102. continue;
  103. // Handle meshes that were either assembled with the shape or added later
  104. if (((S8*)meshes[i] >= mShapeData) && ((S8*)meshes[i] < (mShapeData + mShapeDataSize)))
  105. destructInPlace(meshes[i]);
  106. else
  107. delete meshes[i];
  108. }
  109. for (i=0; i<billboardDetails.size(); i++)
  110. {
  111. delete billboardDetails[i];
  112. billboardDetails[i] = NULL;
  113. }
  114. billboardDetails.clear();
  115. // Delete any generated accelerators
  116. S32 dca;
  117. for (dca = 0; dca < detailCollisionAccelerators.size(); dca++)
  118. {
  119. ConvexHullAccelerator* accel = detailCollisionAccelerators[dca];
  120. if (accel != NULL) {
  121. delete [] accel->vertexList;
  122. delete [] accel->normalList;
  123. for (S32 j = 0; j < accel->numVerts; j++)
  124. delete [] accel->emitStrings[j];
  125. delete [] accel->emitStrings;
  126. delete accel;
  127. }
  128. }
  129. for (dca = 0; dca < detailCollisionAccelerators.size(); dca++)
  130. detailCollisionAccelerators[dca] = NULL;
  131. if( mShapeData )
  132. delete[] mShapeData;
  133. }
  134. const String& TSShape::getName( S32 nameIndex ) const
  135. {
  136. AssertFatal(nameIndex>=0 && nameIndex<names.size(),"TSShape::getName");
  137. return names[nameIndex];
  138. }
  139. const String& TSShape::getMeshName( S32 meshIndex ) const
  140. {
  141. S32 nameIndex = objects[meshIndex].nameIndex;
  142. if ( nameIndex < 0 )
  143. return String::EmptyString;
  144. return names[nameIndex];
  145. }
  146. const String& TSShape::getNodeName( S32 nodeIndex ) const
  147. {
  148. S32 nameIdx = nodes[nodeIndex].nameIndex;
  149. if ( nameIdx < 0 )
  150. return String::EmptyString;
  151. return names[nameIdx];
  152. }
  153. const String& TSShape::getSequenceName( S32 seqIndex ) const
  154. {
  155. AssertFatal(seqIndex >= 0 && seqIndex<sequences.size(),"TSShape::getSequenceName index beyond range");
  156. S32 nameIdx = sequences[seqIndex].nameIndex;
  157. if ( nameIdx < 0 )
  158. return String::EmptyString;
  159. return names[nameIdx];
  160. }
  161. S32 TSShape::findName(const String &name) const
  162. {
  163. for (S32 i=0; i<names.size(); i++)
  164. {
  165. if (names[i].equal( name, String::NoCase ))
  166. return i;
  167. }
  168. return -1;
  169. }
  170. const String& TSShape::getTargetName( S32 mapToNameIndex ) const
  171. {
  172. S32 targetCount = materialList->getMaterialNameList().size();
  173. if(mapToNameIndex < 0 || mapToNameIndex >= targetCount)
  174. return String::EmptyString;
  175. return materialList->getMaterialNameList()[mapToNameIndex];
  176. }
  177. S32 TSShape::getTargetCount() const
  178. {
  179. if(!this)
  180. return -1;
  181. return materialList->getMaterialNameList().size();
  182. }
  183. S32 TSShape::findNode(S32 nameIndex) const
  184. {
  185. for (S32 i=0; i<nodes.size(); i++)
  186. if (nodes[i].nameIndex==nameIndex)
  187. return i;
  188. return -1;
  189. }
  190. S32 TSShape::findObject(S32 nameIndex) const
  191. {
  192. for (S32 i=0; i<objects.size(); i++)
  193. if (objects[i].nameIndex==nameIndex)
  194. return i;
  195. return -1;
  196. }
  197. S32 TSShape::findDetail(S32 nameIndex) const
  198. {
  199. for (S32 i=0; i<details.size(); i++)
  200. if (details[i].nameIndex==nameIndex)
  201. return i;
  202. return -1;
  203. }
  204. S32 TSShape::findDetailBySize(S32 size) const
  205. {
  206. for (S32 i=0; i<details.size(); i++)
  207. if (details[i].size==size)
  208. return i;
  209. return -1;
  210. }
  211. S32 TSShape::findSequence(S32 nameIndex) const
  212. {
  213. for (S32 i=0; i<sequences.size(); i++)
  214. if (sequences[i].nameIndex==nameIndex)
  215. return i;
  216. return -1;
  217. }
  218. bool TSShape::findMeshIndex(const String& meshName, S32& objIndex, S32& meshIndex)
  219. {
  220. // Determine the object name and detail size from the mesh name
  221. S32 detailSize = 999;
  222. objIndex = findObject(String::GetTrailingNumber(meshName, detailSize));
  223. if (objIndex < 0)
  224. return false;
  225. // Determine the subshape this object belongs to
  226. S32 subShapeIndex = getSubShapeForObject(objIndex);
  227. AssertFatal(subShapeIndex < subShapeFirstObject.size(), "Could not find subshape for object!");
  228. // Get the detail levels for the subshape
  229. Vector<S32> validDetails;
  230. getSubShapeDetails(subShapeIndex, validDetails);
  231. // Find the detail with the correct size
  232. for (meshIndex = 0; meshIndex < validDetails.size(); meshIndex++)
  233. {
  234. const TSShape::Detail& det = details[validDetails[meshIndex]];
  235. if (detailSize == det.size)
  236. return true;
  237. }
  238. return false;
  239. }
  240. TSMesh* TSShape::findMesh(const String& meshName)
  241. {
  242. S32 objIndex, meshIndex;
  243. if (!findMeshIndex(meshName, objIndex, meshIndex))
  244. return 0;
  245. return meshes[objects[objIndex].startMeshIndex + meshIndex];
  246. }
  247. S32 TSShape::getSubShapeForNode(S32 nodeIndex)
  248. {
  249. for (S32 i = 0; i < subShapeFirstNode.size(); i++)
  250. {
  251. S32 start = subShapeFirstNode[i];
  252. S32 end = start + subShapeNumNodes[i];
  253. if ((nodeIndex >= start) && (nodeIndex < end))
  254. return i;;
  255. }
  256. return -1;
  257. }
  258. S32 TSShape::getSubShapeForObject(S32 objIndex)
  259. {
  260. for (S32 i = 0; i < subShapeFirstObject.size(); i++)
  261. {
  262. S32 start = subShapeFirstObject[i];
  263. S32 end = start + subShapeNumObjects[i];
  264. if ((objIndex >= start) && (objIndex < end))
  265. return i;
  266. }
  267. return -1;
  268. }
  269. void TSShape::getSubShapeDetails(S32 subShapeIndex, Vector<S32>& validDetails)
  270. {
  271. validDetails.clear();
  272. for (S32 i = 0; i < details.size(); i++)
  273. {
  274. if ((details[i].subShapeNum == subShapeIndex) ||
  275. (details[i].subShapeNum < 0))
  276. validDetails.push_back(i);
  277. }
  278. }
  279. void TSShape::getNodeWorldTransform(S32 nodeIndex, MatrixF* mat) const
  280. {
  281. if ( nodeIndex == -1 )
  282. {
  283. mat->identity();
  284. }
  285. else
  286. {
  287. // Calculate the world transform of the given node
  288. defaultRotations[nodeIndex].getQuatF().setMatrix(mat);
  289. mat->setPosition(defaultTranslations[nodeIndex]);
  290. S32 parentIndex = nodes[nodeIndex].parentIndex;
  291. while (parentIndex != -1)
  292. {
  293. MatrixF mat2(*mat);
  294. defaultRotations[parentIndex].getQuatF().setMatrix(mat);
  295. mat->setPosition(defaultTranslations[parentIndex]);
  296. mat->mul(mat2);
  297. parentIndex = nodes[parentIndex].parentIndex;
  298. }
  299. }
  300. }
  301. void TSShape::getNodeObjects(S32 nodeIndex, Vector<S32>& nodeObjects)
  302. {
  303. for (S32 i = 0; i < objects.size(); i++)
  304. {
  305. if ((nodeIndex == -1) || (objects[i].nodeIndex == nodeIndex))
  306. nodeObjects.push_back(i);
  307. }
  308. }
  309. void TSShape::getNodeChildren(S32 nodeIndex, Vector<S32>& nodeChildren)
  310. {
  311. for (S32 i = 0; i < nodes.size(); i++)
  312. {
  313. if (nodes[i].parentIndex == nodeIndex)
  314. nodeChildren.push_back(i);
  315. }
  316. }
  317. void TSShape::getObjectDetails(S32 objIndex, Vector<S32>& objDetails)
  318. {
  319. // Get the detail levels for this subshape
  320. Vector<S32> validDetails;
  321. getSubShapeDetails(getSubShapeForObject(objIndex), validDetails);
  322. // Get the non-null details for this object
  323. const TSShape::Object& obj = objects[objIndex];
  324. for (S32 i = 0; i < obj.numMeshes; i++)
  325. {
  326. if (meshes[obj.startMeshIndex + i])
  327. objDetails.push_back(validDetails[i]);
  328. }
  329. }
  330. void TSShape::init()
  331. {
  332. S32 numSubShapes = subShapeFirstNode.size();
  333. AssertFatal(numSubShapes==subShapeFirstObject.size(),"TSShape::init");
  334. S32 i,j;
  335. // set up parent/child relationships on nodes and objects
  336. for (i=0; i<nodes.size(); i++)
  337. nodes[i].firstObject = nodes[i].firstChild = nodes[i].nextSibling = -1;
  338. for (i=0; i<nodes.size(); i++)
  339. {
  340. S32 parentIndex = nodes[i].parentIndex;
  341. if (parentIndex>=0)
  342. {
  343. if (nodes[parentIndex].firstChild<0)
  344. nodes[parentIndex].firstChild=i;
  345. else
  346. {
  347. S32 child = nodes[parentIndex].firstChild;
  348. while (nodes[child].nextSibling>=0)
  349. child = nodes[child].nextSibling;
  350. nodes[child].nextSibling = i;
  351. }
  352. }
  353. }
  354. for (i=0; i<objects.size(); i++)
  355. {
  356. objects[i].nextSibling = -1;
  357. S32 nodeIndex = objects[i].nodeIndex;
  358. if (nodeIndex>=0)
  359. {
  360. if (nodes[nodeIndex].firstObject<0)
  361. nodes[nodeIndex].firstObject = i;
  362. else
  363. {
  364. S32 objectIndex = nodes[nodeIndex].firstObject;
  365. while (objects[objectIndex].nextSibling>=0)
  366. objectIndex = objects[objectIndex].nextSibling;
  367. objects[objectIndex].nextSibling = i;
  368. }
  369. }
  370. }
  371. mFlags = 0;
  372. for (i=0; i<sequences.size(); i++)
  373. {
  374. if (!sequences[i].animatesScale())
  375. continue;
  376. U32 curVal = mFlags & AnyScale;
  377. U32 newVal = sequences[i].flags & AnyScale;
  378. mFlags &= ~(AnyScale);
  379. mFlags |= getMax(curVal,newVal); // take the larger value (can only convert upwards)
  380. }
  381. // set up alphaIn and alphaOut vectors...
  382. alphaIn.setSize(details.size());
  383. alphaOut.setSize(details.size());
  384. for (i=0; i<details.size(); i++)
  385. {
  386. if (details[i].size<0)
  387. {
  388. // we don't care...
  389. alphaIn[i] = 0.0f;
  390. alphaOut[i] = 0.0f;
  391. }
  392. else if (i+1==details.size() || details[i+1].size<0)
  393. {
  394. alphaIn[i] = 0.0f;
  395. alphaOut[i] = smAlphaOutLastDetail;
  396. }
  397. else
  398. {
  399. if (details[i+1].subShapeNum<0)
  400. {
  401. // following detail is a billboard detail...treat special...
  402. alphaIn[i] = smAlphaInBillboard;
  403. alphaOut[i] = smAlphaOutBillboard;
  404. }
  405. else
  406. {
  407. // next detail is normal detail
  408. alphaIn[i] = smAlphaInDefault;
  409. alphaOut[i] = smAlphaOutDefault;
  410. }
  411. }
  412. }
  413. for (i=mSmallestVisibleDL-1; i>=0; i--)
  414. {
  415. if (i<smNumSkipLoadDetails)
  416. {
  417. // this detail level renders when pixel size
  418. // is larger than our cap...zap all the meshes and decals
  419. // associated with it and use the next detail level
  420. // instead...
  421. S32 ss = details[i].subShapeNum;
  422. S32 od = details[i].objectDetailNum;
  423. if (ss==details[i+1].subShapeNum && od==details[i+1].objectDetailNum)
  424. // doh! already done this one (init can be called multiple times on same shape due
  425. // to sequence importing).
  426. continue;
  427. details[i].subShapeNum = details[i+1].subShapeNum;
  428. details[i].objectDetailNum = details[i+1].objectDetailNum;
  429. }
  430. }
  431. for (i=0; i<details.size(); i++)
  432. {
  433. S32 count = 0;
  434. S32 ss = details[i].subShapeNum;
  435. S32 od = details[i].objectDetailNum;
  436. if (ss<0)
  437. {
  438. // billboard detail...
  439. details[i].polyCount = 2;
  440. continue;
  441. }
  442. S32 start = subShapeFirstObject[ss];
  443. S32 end = start + subShapeNumObjects[ss];
  444. for (j=start; j<end; j++)
  445. {
  446. Object & obj = objects[j];
  447. if (od<obj.numMeshes)
  448. {
  449. TSMesh * mesh = meshes[obj.startMeshIndex+od];
  450. count += mesh ? mesh->getNumPolys() : 0;
  451. }
  452. }
  453. details[i].polyCount = count;
  454. }
  455. // Init the collision accelerator array. Note that we don't compute the
  456. // accelerators until the app requests them
  457. {
  458. S32 dca;
  459. for (dca = 0; dca < detailCollisionAccelerators.size(); dca++)
  460. {
  461. ConvexHullAccelerator* accel = detailCollisionAccelerators[dca];
  462. if (accel != NULL) {
  463. delete [] accel->vertexList;
  464. delete [] accel->normalList;
  465. for (S32 j = 0; j < accel->numVerts; j++)
  466. delete [] accel->emitStrings[j];
  467. delete [] accel->emitStrings;
  468. delete accel;
  469. }
  470. }
  471. detailCollisionAccelerators.setSize(details.size());
  472. for (dca = 0; dca < detailCollisionAccelerators.size(); dca++)
  473. detailCollisionAccelerators[dca] = NULL;
  474. }
  475. initVertexFeatures();
  476. initMaterialList();
  477. }
  478. void TSShape::initVertexFeatures()
  479. {
  480. bool hasColors = false;
  481. bool hasTexcoord2 = false;
  482. Vector<TSMesh*>::iterator iter = meshes.begin();
  483. for ( ; iter != meshes.end(); iter++ )
  484. {
  485. TSMesh *mesh = *iter;
  486. if ( mesh &&
  487. ( mesh->getMeshType() == TSMesh::StandardMeshType ||
  488. mesh->getMeshType() == TSMesh::SkinMeshType ) )
  489. {
  490. if ( mesh->mVertexData.isReady() )
  491. {
  492. hasColors |= mesh->mHasColor;
  493. hasTexcoord2 |= mesh->mHasTVert2;
  494. }
  495. else
  496. {
  497. hasColors |= !mesh->colors.empty();
  498. hasTexcoord2 |= !mesh->tverts2.empty();
  499. }
  500. }
  501. }
  502. mVertSize = ( hasTexcoord2 || hasColors ) ? sizeof(TSMesh::__TSMeshVertex_3xUVColor) : sizeof(TSMesh::__TSMeshVertexBase);
  503. mVertexFormat.clear();
  504. mVertexFormat.addElement( GFXSemantic::POSITION, GFXDeclType_Float3 );
  505. mVertexFormat.addElement( GFXSemantic::TANGENTW, GFXDeclType_Float, 3 );
  506. mVertexFormat.addElement( GFXSemantic::NORMAL, GFXDeclType_Float3 );
  507. mVertexFormat.addElement( GFXSemantic::TANGENT, GFXDeclType_Float3 );
  508. mVertexFormat.addElement( GFXSemantic::TEXCOORD, GFXDeclType_Float2, 0 );
  509. if(hasTexcoord2 || hasColors)
  510. {
  511. mVertexFormat.addElement( GFXSemantic::TEXCOORD, GFXDeclType_Float2, 1 );
  512. mVertexFormat.addElement( GFXSemantic::COLOR, GFXDeclType_Color );
  513. mVertexFormat.addElement( GFXSemantic::TEXCOORD, GFXDeclType_Float, 2 );
  514. }
  515. // Go fix up meshes to include defaults for optional features
  516. // and initialize them if they're not a skin mesh.
  517. iter = meshes.begin();
  518. for ( ; iter != meshes.end(); iter++ )
  519. {
  520. TSMesh *mesh = *iter;
  521. if ( !mesh ||
  522. ( mesh->getMeshType() != TSMesh::StandardMeshType &&
  523. mesh->getMeshType() != TSMesh::SkinMeshType ) )
  524. continue;
  525. // Set the flags.
  526. mesh->mVertexFormat = &mVertexFormat;
  527. mesh->mVertSize = mVertSize;
  528. // Create and fill aligned data structure
  529. mesh->convertToAlignedMeshData();
  530. // Init the vertex buffer.
  531. if ( mesh->getMeshType() == TSMesh::StandardMeshType )
  532. mesh->createVBIB();
  533. }
  534. }
  535. void TSShape::setupBillboardDetails( const String &cachePath )
  536. {
  537. // set up billboard details -- only do this once, meaning that
  538. // if we add a sequence to the shape we don't redo the billboard
  539. // details...
  540. if ( !billboardDetails.empty() )
  541. return;
  542. for ( U32 i=0; i < details.size(); i++ )
  543. {
  544. const Detail &det = details[i];
  545. if ( det.subShapeNum >= 0 )
  546. continue; // not a billboard detail
  547. while (billboardDetails.size() <= i )
  548. billboardDetails.push_back(NULL);
  549. billboardDetails[i] = new TSLastDetail( this,
  550. cachePath,
  551. det.bbEquatorSteps,
  552. det.bbPolarSteps,
  553. det.bbPolarAngle,
  554. det.bbIncludePoles,
  555. det.bbDetailLevel,
  556. det.bbDimension );
  557. billboardDetails[i]->update();
  558. }
  559. }
  560. void TSShape::initMaterialList()
  561. {
  562. S32 numSubShapes = subShapeFirstObject.size();
  563. #if defined(TORQUE_MAX_LIB)
  564. subShapeFirstTranslucentObject.setSize(numSubShapes);
  565. #endif
  566. mHasSkinMesh = false;
  567. S32 i,j,k;
  568. // for each subshape, find the first translucent object
  569. // also, while we're at it, set mHasTranslucency
  570. for (S32 ss = 0; ss<numSubShapes; ss++)
  571. {
  572. S32 start = subShapeFirstObject[ss];
  573. S32 end = subShapeNumObjects[ss];
  574. subShapeFirstTranslucentObject[ss] = end;
  575. for (i=start; i<end; i++)
  576. {
  577. // check to see if this object has translucency
  578. Object & obj = objects[i];
  579. for (j=0; j<obj.numMeshes; j++)
  580. {
  581. TSMesh * mesh = meshes[obj.startMeshIndex+j];
  582. if (!mesh)
  583. continue;
  584. mHasSkinMesh |= mesh->getMeshType() == TSMesh::SkinMeshType;
  585. for (k=0; k<mesh->primitives.size(); k++)
  586. {
  587. if (mesh->primitives[k].matIndex & TSDrawPrimitive::NoMaterial)
  588. continue;
  589. S32 flags = materialList->getFlags(mesh->primitives[k].matIndex & TSDrawPrimitive::MaterialMask);
  590. if (flags & TSMaterialList::AuxiliaryMap)
  591. continue;
  592. if (flags & TSMaterialList::Translucent)
  593. {
  594. mFlags |= HasTranslucency;
  595. subShapeFirstTranslucentObject[ss] = i;
  596. break;
  597. }
  598. }
  599. if (k!=mesh->primitives.size())
  600. break;
  601. }
  602. if (j!=obj.numMeshes)
  603. break;
  604. }
  605. if (i!=end)
  606. break;
  607. }
  608. }
  609. bool TSShape::preloadMaterialList(const Torque::Path &path)
  610. {
  611. if (materialList)
  612. materialList->setTextureLookupPath(path.getPath());
  613. return true;
  614. }
  615. bool TSShape::buildConvexHull(S32 dl) const
  616. {
  617. AssertFatal(dl>=0 && dl<details.size(),"TSShape::buildConvexHull: detail out of range");
  618. bool ok = true;
  619. const Detail & detail = details[dl];
  620. S32 ss = detail.subShapeNum;
  621. S32 od = detail.objectDetailNum;
  622. S32 start = subShapeFirstObject[ss];
  623. S32 end = subShapeNumObjects[ss];
  624. for (S32 i=start; i<end; i++)
  625. {
  626. TSMesh * mesh = meshes[objects[i].startMeshIndex+od];
  627. if (!mesh)
  628. continue;
  629. ok &= mesh->buildConvexHull();
  630. }
  631. return ok;
  632. }
  633. Vector<MatrixF> gTempNodeTransforms(__FILE__, __LINE__);
  634. void TSShape::computeBounds(S32 dl, Box3F & bounds) const
  635. {
  636. // if dl==-1, nothing to do
  637. if (dl==-1)
  638. return;
  639. AssertFatal(dl>=0 && dl<details.size(),"TSShapeInstance::computeBounds");
  640. // get subshape and object detail
  641. const TSDetail * detail = &details[dl];
  642. S32 ss = detail->subShapeNum;
  643. S32 od = detail->objectDetailNum;
  644. // If we have no subshapes then there is
  645. // no valid bounds for this detail level.
  646. if ( ss < 0 )
  647. return;
  648. // set up temporary storage for non-local transforms...
  649. S32 i;
  650. S32 start = subShapeFirstNode[ss];
  651. S32 end = subShapeNumNodes[ss] + start;
  652. gTempNodeTransforms.setSize(end-start);
  653. for (i=start; i<end; i++)
  654. {
  655. MatrixF mat;
  656. QuatF q;
  657. TSTransform::setMatrix(defaultRotations[i].getQuatF(&q),defaultTranslations[i],&mat);
  658. if (nodes[i].parentIndex>=0)
  659. gTempNodeTransforms[i-start].mul(gTempNodeTransforms[nodes[i].parentIndex-start],mat);
  660. else
  661. gTempNodeTransforms[i-start] = mat;
  662. }
  663. // run through objects and updating bounds as we go
  664. bounds.minExtents.set( 10E30f, 10E30f, 10E30f);
  665. bounds.maxExtents.set(-10E30f,-10E30f,-10E30f);
  666. Box3F box;
  667. start = subShapeFirstObject[ss];
  668. end = subShapeNumObjects[ss] + start;
  669. for (i=start; i<end; i++)
  670. {
  671. const Object * object = &objects[i];
  672. TSMesh * mesh = od<object->numMeshes ? meshes[object->startMeshIndex+od] : NULL;
  673. if (mesh)
  674. {
  675. static MatrixF idMat(true);
  676. if (object->nodeIndex<0)
  677. mesh->computeBounds(idMat,box);
  678. else
  679. mesh->computeBounds(gTempNodeTransforms[object->nodeIndex-start],box);
  680. bounds.minExtents.setMin(box.minExtents);
  681. bounds.maxExtents.setMax(box.maxExtents);
  682. }
  683. }
  684. }
  685. TSShapeAlloc TSShape::smTSAlloc;
  686. #define tsalloc TSShape::smTSAlloc
  687. // messy stuff: check to see if we should "skip" meshNum
  688. // this assumes that meshes for a given object are in a row
  689. // skipDL is the lowest detail number we keep (i.e., the # of details we skip)
  690. bool TSShape::checkSkip(S32 meshNum, S32 & curObject, S32 skipDL)
  691. {
  692. if (skipDL==0)
  693. // easy out...
  694. return false;
  695. // skip detail level exists on this subShape
  696. S32 skipSS = details[skipDL].subShapeNum;
  697. if (curObject<objects.size())
  698. {
  699. S32 start = objects[curObject].startMeshIndex;
  700. if (meshNum>=start)
  701. {
  702. // we are either from this object, the next object, or a decal
  703. if (meshNum < start + objects[curObject].numMeshes)
  704. {
  705. // this object...
  706. if (subShapeFirstObject[skipSS]>curObject)
  707. // haven't reached this subshape yet
  708. return true;
  709. if (skipSS+1==subShapeFirstObject.size() || curObject<subShapeFirstObject[skipSS+1])
  710. // curObject is on subshape of skip detail...make sure it's after skipDL
  711. return (meshNum-start<details[skipDL].objectDetailNum);
  712. // if we get here, then curObject occurs on subShape after skip detail (so keep it)
  713. return false;
  714. }
  715. else
  716. // advance object, try again
  717. return checkSkip(meshNum,++curObject,skipDL);
  718. }
  719. }
  720. AssertFatal(0,"TSShape::checkSkip: assertion failed");
  721. return false;
  722. }
  723. void TSShape::assembleShape()
  724. {
  725. S32 i,j;
  726. // get counts...
  727. S32 numNodes = tsalloc.get32();
  728. S32 numObjects = tsalloc.get32();
  729. S32 numDecals = tsalloc.get32();
  730. S32 numSubShapes = tsalloc.get32();
  731. S32 numIflMaterials = tsalloc.get32();
  732. S32 numNodeRots;
  733. S32 numNodeTrans;
  734. S32 numNodeUniformScales;
  735. S32 numNodeAlignedScales;
  736. S32 numNodeArbitraryScales;
  737. if (smReadVersion<22)
  738. {
  739. numNodeRots = numNodeTrans = tsalloc.get32() - numNodes;
  740. numNodeUniformScales = numNodeAlignedScales = numNodeArbitraryScales = 0;
  741. }
  742. else
  743. {
  744. numNodeRots = tsalloc.get32();
  745. numNodeTrans = tsalloc.get32();
  746. numNodeUniformScales = tsalloc.get32();
  747. numNodeAlignedScales = tsalloc.get32();
  748. numNodeArbitraryScales = tsalloc.get32();
  749. }
  750. S32 numGroundFrames = 0;
  751. if (smReadVersion>23)
  752. numGroundFrames = tsalloc.get32();
  753. S32 numObjectStates = tsalloc.get32();
  754. S32 numDecalStates = tsalloc.get32();
  755. S32 numTriggers = tsalloc.get32();
  756. S32 numDetails = tsalloc.get32();
  757. S32 numMeshes = tsalloc.get32();
  758. S32 numSkins = 0;
  759. if (smReadVersion<23)
  760. // in later versions, skins are kept with other meshes
  761. numSkins = tsalloc.get32();
  762. S32 numNames = tsalloc.get32();
  763. // Note that we are recalculating these values later on for safety.
  764. mSmallestVisibleSize = (F32)tsalloc.get32();
  765. mSmallestVisibleDL = tsalloc.get32();
  766. tsalloc.checkGuard();
  767. // get bounds...
  768. tsalloc.get32((S32*)&radius,1);
  769. tsalloc.get32((S32*)&tubeRadius,1);
  770. tsalloc.get32((S32*)&center,3);
  771. tsalloc.get32((S32*)&bounds,6);
  772. tsalloc.checkGuard();
  773. // copy various vectors...
  774. S32 * ptr32 = tsalloc.copyToShape32(numNodes*5);
  775. nodes.set(ptr32,numNodes);
  776. tsalloc.checkGuard();
  777. ptr32 = tsalloc.copyToShape32(numObjects*6,true);
  778. if (!ptr32)
  779. ptr32 = tsalloc.allocShape32(numSkins*6); // pre v23 shapes store skins and meshes separately...no longer
  780. else
  781. tsalloc.allocShape32(numSkins*6);
  782. objects.set(ptr32,numObjects);
  783. tsalloc.checkGuard();
  784. // DEPRECATED decals
  785. ptr32 = tsalloc.getPointer32(numDecals*5);
  786. tsalloc.checkGuard();
  787. // DEPRECATED ifl materials
  788. ptr32 = tsalloc.copyToShape32(numIflMaterials*5);
  789. tsalloc.checkGuard();
  790. ptr32 = tsalloc.copyToShape32(numSubShapes,true);
  791. subShapeFirstNode.set(ptr32,numSubShapes);
  792. ptr32 = tsalloc.copyToShape32(numSubShapes,true);
  793. subShapeFirstObject.set(ptr32,numSubShapes);
  794. // DEPRECATED subShapeFirstDecal
  795. ptr32 = tsalloc.getPointer32(numSubShapes);
  796. tsalloc.checkGuard();
  797. ptr32 = tsalloc.copyToShape32(numSubShapes);
  798. subShapeNumNodes.set(ptr32,numSubShapes);
  799. ptr32 = tsalloc.copyToShape32(numSubShapes);
  800. subShapeNumObjects.set(ptr32,numSubShapes);
  801. // DEPRECATED subShapeNumDecals
  802. ptr32 = tsalloc.getPointer32(numSubShapes);
  803. tsalloc.checkGuard();
  804. ptr32 = tsalloc.allocShape32(numSubShapes);
  805. subShapeFirstTranslucentObject.set(ptr32,numSubShapes);
  806. // get default translation and rotation
  807. S16 * ptr16 = tsalloc.allocShape16(0);
  808. for (i=0;i<numNodes;i++)
  809. tsalloc.copyToShape16(4);
  810. defaultRotations.set(ptr16,numNodes);
  811. tsalloc.align32();
  812. ptr32 = tsalloc.allocShape32(0);
  813. for (i=0;i<numNodes;i++)
  814. {
  815. tsalloc.copyToShape32(3);
  816. tsalloc.copyToShape32(sizeof(Point3F)-12); // handle alignment issues w/ point3f
  817. }
  818. defaultTranslations.set(ptr32,numNodes);
  819. // get any node sequence data stored in shape
  820. nodeTranslations.setSize(numNodeTrans);
  821. for (i=0;i<numNodeTrans;i++)
  822. tsalloc.get32((S32*)&nodeTranslations[i],3);
  823. nodeRotations.setSize(numNodeRots);
  824. for (i=0;i<numNodeRots;i++)
  825. tsalloc.get16((S16*)&nodeRotations[i],4);
  826. tsalloc.align32();
  827. tsalloc.checkGuard();
  828. if (smReadVersion>21)
  829. {
  830. // more node sequence data...scale
  831. nodeUniformScales.setSize(numNodeUniformScales);
  832. for (i=0;i<numNodeUniformScales;i++)
  833. tsalloc.get32((S32*)&nodeUniformScales[i],1);
  834. nodeAlignedScales.setSize(numNodeAlignedScales);
  835. for (i=0;i<numNodeAlignedScales;i++)
  836. tsalloc.get32((S32*)&nodeAlignedScales[i],3);
  837. nodeArbitraryScaleFactors.setSize(numNodeArbitraryScales);
  838. for (i=0;i<numNodeArbitraryScales;i++)
  839. tsalloc.get32((S32*)&nodeArbitraryScaleFactors[i],3);
  840. nodeArbitraryScaleRots.setSize(numNodeArbitraryScales);
  841. for (i=0;i<numNodeArbitraryScales;i++)
  842. tsalloc.get16((S16*)&nodeArbitraryScaleRots[i],4);
  843. tsalloc.align32();
  844. tsalloc.checkGuard();
  845. }
  846. // old shapes need ground transforms moved to ground arrays...but only do it once
  847. if (smReadVersion<22 && tsalloc.allocShape32(0))
  848. {
  849. for (i=0; i<sequences.size(); i++)
  850. {
  851. // move ground transform data to ground vectors
  852. Sequence & seq = sequences[i];
  853. S32 oldSz = groundTranslations.size();
  854. groundTranslations.setSize(oldSz+seq.numGroundFrames);
  855. groundRotations.setSize(oldSz+seq.numGroundFrames);
  856. for (S32 j=0;j<seq.numGroundFrames;j++)
  857. {
  858. groundTranslations[j+oldSz] = nodeTranslations[seq.firstGroundFrame+j-numNodes];
  859. groundRotations[j+oldSz] = nodeRotations[seq.firstGroundFrame+j-numNodes];
  860. }
  861. seq.firstGroundFrame = oldSz;
  862. seq.baseTranslation -= numNodes;
  863. seq.baseRotation -= numNodes;
  864. seq.baseScale = 0; // not used on older shapes...but keep it clean
  865. }
  866. }
  867. // version 22 & 23 shapes accidentally had no ground transforms, and ground for
  868. // earlier shapes is handled just above, so...
  869. if (smReadVersion>23)
  870. {
  871. groundTranslations.setSize(numGroundFrames);
  872. for (i=0;i<numGroundFrames;i++)
  873. tsalloc.get32((S32*)&groundTranslations[i],3);
  874. groundRotations.setSize(numGroundFrames);
  875. for (i=0;i<numGroundFrames;i++)
  876. tsalloc.get16((S16*)&groundRotations[i],4);
  877. tsalloc.align32();
  878. tsalloc.checkGuard();
  879. }
  880. // object states
  881. ptr32 = tsalloc.copyToShape32(numObjectStates*3);
  882. objectStates.set(ptr32,numObjectStates);
  883. tsalloc.allocShape32(numSkins*3); // provide buffer after objectStates for older shapes
  884. tsalloc.checkGuard();
  885. // DEPRECATED decal states
  886. ptr32 = tsalloc.getPointer32(numDecalStates);
  887. tsalloc.checkGuard();
  888. // frame triggers
  889. ptr32 = tsalloc.getPointer32(numTriggers*2);
  890. triggers.setSize(numTriggers);
  891. dMemcpy(triggers.address(),ptr32,sizeof(S32)*numTriggers*2);
  892. tsalloc.checkGuard();
  893. // details
  894. if ( smReadVersion >= 26 )
  895. {
  896. U32 alignedSize32 = sizeof( Detail ) / 4;
  897. ptr32 = tsalloc.copyToShape32( numDetails * alignedSize32, true );
  898. details.set( ptr32, numDetails );
  899. }
  900. else
  901. {
  902. // Previous to version 26 the Detail structure
  903. // only contained the first 7 values...
  904. //
  905. // struct Detail
  906. // {
  907. // S32 nameIndex;
  908. // S32 subShapeNum;
  909. // S32 objectDetailNum;
  910. // F32 size;
  911. // F32 averageError;
  912. // F32 maxError;
  913. // S32 polyCount;
  914. // };
  915. //
  916. // In the code below we're reading just these 7 values and
  917. // copying them to the new larger structure.
  918. ptr32 = tsalloc.copyToShape32( numDetails * 7, true );
  919. details.setSize( numDetails );
  920. for ( U32 i = 0; i < details.size(); i++, ptr32 += 7 )
  921. {
  922. Detail *det = &(details[i]);
  923. // Clear the struct... we don't want to leave
  924. // garbage in the parts that are unfilled.
  925. U32 alignedSize32 = sizeof( Detail );
  926. dMemset( det, 0, alignedSize32 );
  927. // Copy the old struct values over.
  928. dMemcpy( det, ptr32, 7 * 4 );
  929. // If this is an autobillboard then we need to
  930. // fill in the new part of the struct.
  931. if ( det->subShapeNum >= 0 )
  932. continue;
  933. S32 lastDetailOpts = det->objectDetailNum;
  934. det->bbEquatorSteps = lastDetailOpts & 0x7F; // bits 0..6
  935. det->bbPolarSteps = (lastDetailOpts >> 7) & 0x3F; // bits 7..12
  936. det->bbPolarAngle = 0.5f * M_PI_F * (1.0f/64.0f) * (F32) (( lastDetailOpts >>13 ) & 0x3F); // bits 13..18
  937. det->bbDetailLevel = (lastDetailOpts >> 19) & 0x0F; // 19..22
  938. det->bbDimension = (lastDetailOpts >> 23) & 0xFF; // 23..30
  939. det->bbIncludePoles = (lastDetailOpts & 0x80000000)!=0; // bit 31
  940. }
  941. }
  942. // Some DTS exporters (MAX - I'm looking at you!) write garbage into the
  943. // averageError and maxError values which stops LOD from working correctly.
  944. // Try to detect and fix it
  945. for ( U32 i = 0; i < details.size(); i++ )
  946. {
  947. if ( ( details[i].averageError == 0 ) || ( details[i].averageError > 10000 ) ||
  948. ( details[i].maxError == 0 ) || ( details[i].maxError > 10000 ) )
  949. {
  950. details[i].averageError = details[i].maxError = -1.0f;
  951. }
  952. }
  953. // We don't trust the value of mSmallestVisibleDL loaded from the dts
  954. // since some legacy meshes seem to have the wrong value. Recalculate it
  955. // now that we have the details loaded.
  956. updateSmallestVisibleDL();
  957. S32 skipDL = getMin(mSmallestVisibleDL,smNumSkipLoadDetails);
  958. if (skipDL < 0)
  959. skipDL = 0;
  960. tsalloc.checkGuard();
  961. // about to read in the meshes...first must allocate some scratch space
  962. S32 scratchSize = getMax(numSkins,numMeshes);
  963. TSMesh::smVertsList.setSize(scratchSize);
  964. TSMesh::smTVertsList.setSize(scratchSize);
  965. if ( smReadVersion >= 26 )
  966. {
  967. TSMesh::smTVerts2List.setSize(scratchSize);
  968. TSMesh::smColorsList.setSize(scratchSize);
  969. }
  970. TSMesh::smNormsList.setSize(scratchSize);
  971. TSMesh::smEncodedNormsList.setSize(scratchSize);
  972. TSMesh::smDataCopied.setSize(scratchSize);
  973. TSSkinMesh::smInitTransformList.setSize(scratchSize);
  974. TSSkinMesh::smVertexIndexList.setSize(scratchSize);
  975. TSSkinMesh::smBoneIndexList.setSize(scratchSize);
  976. TSSkinMesh::smWeightList.setSize(scratchSize);
  977. TSSkinMesh::smNodeIndexList.setSize(scratchSize);
  978. for (i=0; i<numMeshes; i++)
  979. {
  980. TSMesh::smVertsList[i]=NULL;
  981. TSMesh::smTVertsList[i]=NULL;
  982. if ( smReadVersion >= 26 )
  983. {
  984. TSMesh::smTVerts2List[i] = NULL;
  985. TSMesh::smColorsList[i] = NULL;
  986. }
  987. TSMesh::smNormsList[i]=NULL;
  988. TSMesh::smEncodedNormsList[i]=NULL;
  989. TSMesh::smDataCopied[i]=false;
  990. TSSkinMesh::smInitTransformList[i] = NULL;
  991. TSSkinMesh::smVertexIndexList[i] = NULL;
  992. TSSkinMesh::smBoneIndexList[i] = NULL;
  993. TSSkinMesh::smWeightList[i] = NULL;
  994. TSSkinMesh::smNodeIndexList[i] = NULL;
  995. }
  996. // read in the meshes (sans skins)...straightforward read one at a time
  997. ptr32 = tsalloc.allocShape32(numMeshes + numSkins*numDetails); // leave room for skins on old shapes
  998. S32 curObject = 0; // for tracking skipped meshes
  999. for (i=0; i<numMeshes; i++)
  1000. {
  1001. bool skip = checkSkip(i,curObject,skipDL); // skip this mesh?
  1002. S32 meshType = tsalloc.get32();
  1003. if (meshType == TSMesh::DecalMeshType)
  1004. // decal mesh deprecated
  1005. skip = true;
  1006. TSMesh * mesh = TSMesh::assembleMesh(meshType,skip);
  1007. if (ptr32)
  1008. ptr32[i] = skip ? 0 : (S32)mesh;
  1009. // fill in location of verts, tverts, and normals for detail levels
  1010. if (mesh && meshType!=TSMesh::DecalMeshType)
  1011. {
  1012. TSMesh::smVertsList[i] = mesh->verts.address();
  1013. TSMesh::smTVertsList[i] = mesh->tverts.address();
  1014. TSMesh::smNormsList[i] = mesh->norms.address();
  1015. TSMesh::smEncodedNormsList[i] = mesh->encodedNorms.address();
  1016. TSMesh::smDataCopied[i] = !skip; // as long as we didn't skip this mesh, the data should be in shape now
  1017. if (meshType==TSMesh::SkinMeshType)
  1018. {
  1019. TSSkinMesh * skin = (TSSkinMesh*)mesh;
  1020. TSMesh::smVertsList[i] = skin->batchData.initialVerts.address();
  1021. TSMesh::smNormsList[i] = skin->batchData.initialNorms.address();
  1022. TSSkinMesh::smInitTransformList[i] = skin->batchData.initialTransforms.address();
  1023. TSSkinMesh::smVertexIndexList[i] = skin->vertexIndex.address();
  1024. TSSkinMesh::smBoneIndexList[i] = skin->boneIndex.address();
  1025. TSSkinMesh::smWeightList[i] = skin->weight.address();
  1026. TSSkinMesh::smNodeIndexList[i] = skin->batchData.nodeIndex.address();
  1027. }
  1028. }
  1029. }
  1030. meshes.set(ptr32,numMeshes);
  1031. tsalloc.checkGuard();
  1032. // names
  1033. char * nameBufferStart = (char*)tsalloc.getPointer8(0);
  1034. char * name = nameBufferStart;
  1035. S32 nameBufferSize = 0;
  1036. names.setSize(numNames);
  1037. for (i=0; i<numNames; i++)
  1038. {
  1039. for (j=0; name[j]; j++)
  1040. ;
  1041. names[i] = name;
  1042. nameBufferSize += j + 1;
  1043. name += j + 1;
  1044. }
  1045. tsalloc.getPointer8(nameBufferSize);
  1046. tsalloc.align32();
  1047. tsalloc.checkGuard();
  1048. if (smReadVersion<23)
  1049. {
  1050. // get detail information about skins...
  1051. S32 * detailFirstSkin = tsalloc.getPointer32(numDetails);
  1052. S32 * detailNumSkins = tsalloc.getPointer32(numDetails);
  1053. tsalloc.checkGuard();
  1054. // about to read in skins...clear out scratch space...
  1055. if (numSkins)
  1056. {
  1057. TSSkinMesh::smInitTransformList.setSize(numSkins);
  1058. TSSkinMesh::smVertexIndexList.setSize(numSkins);
  1059. TSSkinMesh::smBoneIndexList.setSize(numSkins);
  1060. TSSkinMesh::smWeightList.setSize(numSkins);
  1061. TSSkinMesh::smNodeIndexList.setSize(numSkins);
  1062. }
  1063. for (i=0; i<numSkins; i++)
  1064. {
  1065. TSMesh::smVertsList[i]=NULL;
  1066. TSMesh::smTVertsList[i]=NULL;
  1067. TSMesh::smNormsList[i]=NULL;
  1068. TSMesh::smEncodedNormsList[i]=NULL;
  1069. TSMesh::smDataCopied[i]=false;
  1070. TSSkinMesh::smInitTransformList[i] = NULL;
  1071. TSSkinMesh::smVertexIndexList[i] = NULL;
  1072. TSSkinMesh::smBoneIndexList[i] = NULL;
  1073. TSSkinMesh::smWeightList[i] = NULL;
  1074. TSSkinMesh::smNodeIndexList[i] = NULL;
  1075. }
  1076. // skins
  1077. ptr32 = tsalloc.allocShape32(numSkins);
  1078. for (i=0; i<numSkins; i++)
  1079. {
  1080. bool skip = i<detailFirstSkin[skipDL];
  1081. TSSkinMesh * skin = (TSSkinMesh*)TSMesh::assembleMesh(TSMesh::SkinMeshType,skip);
  1082. if (meshes.address())
  1083. {
  1084. // add pointer to skin in shapes list of meshes
  1085. // we reserved room for this above...
  1086. meshes.set(meshes.address(),meshes.size()+1);
  1087. meshes[meshes.size()-1] = skip ? NULL : skin;
  1088. }
  1089. // fill in location of verts, tverts, and normals for shared detail levels
  1090. if (skin)
  1091. {
  1092. TSMesh::smVertsList[i] = skin->batchData.initialVerts.address();
  1093. TSMesh::smTVertsList[i] = skin->tverts.address();
  1094. TSMesh::smNormsList[i] = skin->batchData.initialNorms.address();
  1095. TSMesh::smEncodedNormsList[i] = skin->encodedNorms.address();
  1096. TSMesh::smDataCopied[i] = !skip; // as long as we didn't skip this mesh, the data should be in shape now
  1097. TSSkinMesh::smInitTransformList[i] = skin->batchData.initialTransforms.address();
  1098. TSSkinMesh::smVertexIndexList[i] = skin->vertexIndex.address();
  1099. TSSkinMesh::smBoneIndexList[i] = skin->boneIndex.address();
  1100. TSSkinMesh::smWeightList[i] = skin->weight.address();
  1101. TSSkinMesh::smNodeIndexList[i] = skin->batchData.nodeIndex.address();
  1102. }
  1103. }
  1104. tsalloc.checkGuard();
  1105. // we now have skins in mesh list...add skin objects to object list and patch things up
  1106. fixupOldSkins(numMeshes,numSkins,numDetails,detailFirstSkin,detailNumSkins);
  1107. }
  1108. // allocate storage space for some arrays (filled in during Shape::init)...
  1109. ptr32 = tsalloc.allocShape32(numDetails);
  1110. alphaIn.set(ptr32,numDetails);
  1111. ptr32 = tsalloc.allocShape32(numDetails);
  1112. alphaOut.set(ptr32,numDetails);
  1113. }
  1114. void TSShape::disassembleShape()
  1115. {
  1116. S32 i;
  1117. // set counts...
  1118. S32 numNodes = tsalloc.set32(nodes.size());
  1119. S32 numObjects = tsalloc.set32(objects.size());
  1120. tsalloc.set32(0); // DEPRECATED decals
  1121. S32 numSubShapes = tsalloc.set32(subShapeFirstNode.size());
  1122. tsalloc.set32(0); // DEPRECATED ifl materials
  1123. S32 numNodeRotations = tsalloc.set32(nodeRotations.size());
  1124. S32 numNodeTranslations = tsalloc.set32(nodeTranslations.size());
  1125. S32 numNodeUniformScales = tsalloc.set32(nodeUniformScales.size());
  1126. S32 numNodeAlignedScales = tsalloc.set32(nodeAlignedScales.size());
  1127. S32 numNodeArbitraryScales = tsalloc.set32(nodeArbitraryScaleFactors.size());
  1128. S32 numGroundFrames = tsalloc.set32(groundTranslations.size());
  1129. S32 numObjectStates = tsalloc.set32(objectStates.size());
  1130. tsalloc.set32(0); // DEPRECATED decals
  1131. S32 numTriggers = tsalloc.set32(triggers.size());
  1132. S32 numDetails = tsalloc.set32(details.size());
  1133. S32 numMeshes = tsalloc.set32(meshes.size());
  1134. S32 numNames = tsalloc.set32(names.size());
  1135. tsalloc.set32((S32)mSmallestVisibleSize);
  1136. tsalloc.set32(mSmallestVisibleDL);
  1137. tsalloc.setGuard();
  1138. // get bounds...
  1139. tsalloc.copyToBuffer32((S32*)&radius,1);
  1140. tsalloc.copyToBuffer32((S32*)&tubeRadius,1);
  1141. tsalloc.copyToBuffer32((S32*)&center,3);
  1142. tsalloc.copyToBuffer32((S32*)&bounds,6);
  1143. tsalloc.setGuard();
  1144. // copy various vectors...
  1145. tsalloc.copyToBuffer32((S32*)nodes.address(),numNodes*5);
  1146. tsalloc.setGuard();
  1147. tsalloc.copyToBuffer32((S32*)objects.address(),numObjects*6);
  1148. tsalloc.setGuard();
  1149. // DEPRECATED: no copy decals
  1150. tsalloc.setGuard();
  1151. tsalloc.copyToBuffer32(0,0); // DEPRECATED: ifl materials!
  1152. tsalloc.setGuard();
  1153. tsalloc.copyToBuffer32((S32*)subShapeFirstNode.address(),numSubShapes);
  1154. tsalloc.copyToBuffer32((S32*)subShapeFirstObject.address(),numSubShapes);
  1155. tsalloc.copyToBuffer32(0, numSubShapes); // DEPRECATED: no copy subShapeFirstDecal
  1156. tsalloc.setGuard();
  1157. tsalloc.copyToBuffer32((S32*)subShapeNumNodes.address(),numSubShapes);
  1158. tsalloc.copyToBuffer32((S32*)subShapeNumObjects.address(),numSubShapes);
  1159. tsalloc.copyToBuffer32(0, numSubShapes); // DEPRECATED: no copy subShapeNumDecals
  1160. tsalloc.setGuard();
  1161. // default transforms...
  1162. tsalloc.copyToBuffer16((S16*)defaultRotations.address(),numNodes*4);
  1163. tsalloc.copyToBuffer32((S32*)defaultTranslations.address(),numNodes*3);
  1164. // animated transforms...
  1165. tsalloc.copyToBuffer16((S16*)nodeRotations.address(),numNodeRotations*4);
  1166. tsalloc.copyToBuffer32((S32*)nodeTranslations.address(),numNodeTranslations*3);
  1167. tsalloc.setGuard();
  1168. // ...with scale
  1169. tsalloc.copyToBuffer32((S32*)nodeUniformScales.address(),numNodeUniformScales);
  1170. tsalloc.copyToBuffer32((S32*)nodeAlignedScales.address(),numNodeAlignedScales*3);
  1171. tsalloc.copyToBuffer32((S32*)nodeArbitraryScaleFactors.address(),numNodeArbitraryScales*3);
  1172. tsalloc.copyToBuffer16((S16*)nodeArbitraryScaleRots.address(),numNodeArbitraryScales*4);
  1173. tsalloc.setGuard();
  1174. tsalloc.copyToBuffer32((S32*)groundTranslations.address(),3*numGroundFrames);
  1175. tsalloc.copyToBuffer16((S16*)groundRotations.address(),4*numGroundFrames);
  1176. tsalloc.setGuard();
  1177. // object states..
  1178. tsalloc.copyToBuffer32((S32*)objectStates.address(),numObjectStates*3);
  1179. tsalloc.setGuard();
  1180. // decal states...
  1181. // DEPRECATED (numDecalStates = 0)
  1182. tsalloc.setGuard();
  1183. // frame triggers
  1184. tsalloc.copyToBuffer32((S32*)triggers.address(),numTriggers*2);
  1185. tsalloc.setGuard();
  1186. // details
  1187. if (TSShape::smVersion > 25)
  1188. {
  1189. U32 alignedSize32 = sizeof( Detail ) / 4;
  1190. tsalloc.copyToBuffer32((S32*)details.address(),numDetails * alignedSize32 );
  1191. }
  1192. else
  1193. {
  1194. // Legacy details => no explicit autobillboard parameters
  1195. U32 legacyDetailSize32 = 7; // only store the first 7 4-byte values of each detail
  1196. for ( S32 i = 0; i < details.size(); i++ )
  1197. tsalloc.copyToBuffer32( (S32*)&details[i], legacyDetailSize32 );
  1198. }
  1199. tsalloc.setGuard();
  1200. // read in the meshes (sans skins)...
  1201. bool * isMesh = new bool[numMeshes]; // funny business because decals are pretend meshes (legacy issue)
  1202. for (i=0;i<numMeshes;i++)
  1203. isMesh[i]=false;
  1204. for (i=0; i<objects.size(); i++)
  1205. {
  1206. for (S32 j=0; j<objects[i].numMeshes; j++)
  1207. // even if an empty mesh, it's a mesh...
  1208. isMesh[objects[i].startMeshIndex+j]=true;
  1209. }
  1210. for (i=0; i<numMeshes; i++)
  1211. {
  1212. TSMesh * mesh = NULL;
  1213. // decal mesh deprecated
  1214. if (isMesh[i])
  1215. mesh = meshes[i];
  1216. tsalloc.set32( (mesh && mesh->getMeshType() != TSMesh::DecalMeshType) ? mesh->getMeshType() : TSMesh::NullMeshType);
  1217. if (mesh)
  1218. mesh->disassemble();
  1219. }
  1220. delete [] isMesh;
  1221. tsalloc.setGuard();
  1222. // names
  1223. for (i=0; i<numNames; i++)
  1224. tsalloc.copyToBuffer8((S8 *)(names[i].c_str()),names[i].length()+1);
  1225. tsalloc.setGuard();
  1226. }
  1227. //-------------------------------------------------
  1228. // write whole shape
  1229. //-------------------------------------------------
  1230. /** Determine whether we can write this shape in TSTPRO compatible format */
  1231. bool TSShape::canWriteOldFormat() const
  1232. {
  1233. // Cannot use old format if using autobillboard details
  1234. for (S32 i = 0; i < details.size(); i++)
  1235. {
  1236. if (details[i].subShapeNum < 0)
  1237. return false;
  1238. }
  1239. for (S32 i = 0; i < meshes.size(); i++)
  1240. {
  1241. if (!meshes[i])
  1242. continue;
  1243. // Cannot use old format if using the new functionality (COLORs, 2nd UV set)
  1244. if (meshes[i]->tverts2.size() || meshes[i]->colors.size())
  1245. return false;
  1246. // Cannot use old format if any primitive has too many triangles
  1247. // (ie. cannot fit in a S16)
  1248. for (S32 j = 0; j < meshes[i]->primitives.size(); j++)
  1249. {
  1250. if ((meshes[i]->primitives[j].start +
  1251. meshes[i]->primitives[j].numElements) >= (1 << 15))
  1252. {
  1253. return false;
  1254. }
  1255. }
  1256. }
  1257. return true;
  1258. }
  1259. void TSShape::write(Stream * s, bool saveOldFormat)
  1260. {
  1261. S32 currentVersion = smVersion;
  1262. if (saveOldFormat)
  1263. smVersion = 24;
  1264. // write version
  1265. s->write(smVersion | (mExporterVersion<<16));
  1266. tsalloc.setWrite();
  1267. disassembleShape();
  1268. S32 * buffer32 = tsalloc.getBuffer32();
  1269. S16 * buffer16 = tsalloc.getBuffer16();
  1270. S8 * buffer8 = tsalloc.getBuffer8();
  1271. S32 size32 = tsalloc.getBufferSize32();
  1272. S32 size16 = tsalloc.getBufferSize16();
  1273. S32 size8 = tsalloc.getBufferSize8();
  1274. // convert sizes to dwords...
  1275. if (size16 & 1)
  1276. size16 += 2;
  1277. size16 >>= 1;
  1278. if (size8 & 3)
  1279. size8 += 4;
  1280. size8 >>= 2;
  1281. S32 sizeMemBuffer, start16, start8;
  1282. sizeMemBuffer = size32 + size16 + size8;
  1283. start16 = size32;
  1284. start8 = start16+size16;
  1285. // in dwords -- write will properly endian-flip.
  1286. s->write(sizeMemBuffer);
  1287. s->write(start16);
  1288. s->write(start8);
  1289. // endian-flip the entire write buffers.
  1290. fixEndian(buffer32,buffer16,buffer8,size32,size16,size8);
  1291. // now write buffers
  1292. s->write(size32*4,buffer32);
  1293. s->write(size16*4,buffer16);
  1294. s->write(size8 *4,buffer8);
  1295. // write sequences - write will properly endian-flip.
  1296. s->write(sequences.size());
  1297. for (S32 i=0; i<sequences.size(); i++)
  1298. sequences[i].write(s);
  1299. // write material list - write will properly endian-flip.
  1300. materialList->write(*s);
  1301. delete [] buffer32;
  1302. delete [] buffer16;
  1303. delete [] buffer8;
  1304. smVersion = currentVersion;
  1305. }
  1306. //-------------------------------------------------
  1307. // read whole shape
  1308. //-------------------------------------------------
  1309. bool TSShape::read(Stream * s)
  1310. {
  1311. // read version - read handles endian-flip
  1312. s->read(&smReadVersion);
  1313. mExporterVersion = smReadVersion >> 16;
  1314. smReadVersion &= 0xFF;
  1315. if (smReadVersion>smVersion)
  1316. {
  1317. // error -- don't support future versions yet :>
  1318. Con::errorf(ConsoleLogEntry::General,
  1319. "Error: attempt to load a version %i dts-shape, can currently only load version %i and before.",
  1320. smReadVersion,smVersion);
  1321. return false;
  1322. }
  1323. mReadVersion = smReadVersion;
  1324. S32 * memBuffer32;
  1325. S16 * memBuffer16;
  1326. S8 * memBuffer8;
  1327. S32 count32, count16, count8;
  1328. if (mReadVersion<19)
  1329. {
  1330. Con::errorf("... Shape with old version.");
  1331. return false;
  1332. }
  1333. else
  1334. {
  1335. S32 i;
  1336. U32 sizeMemBuffer, startU16, startU8;
  1337. // in dwords. - read handles endian-flip
  1338. s->read(&sizeMemBuffer);
  1339. s->read(&startU16);
  1340. s->read(&startU8);
  1341. if (s->getStatus()!=Stream::Ok)
  1342. {
  1343. Con::errorf(ConsoleLogEntry::General, "Error: bad shape file.");
  1344. return false;
  1345. }
  1346. S32 * tmp = new S32[sizeMemBuffer];
  1347. s->read(sizeof(S32)*sizeMemBuffer,(U8*)tmp);
  1348. memBuffer32 = tmp;
  1349. memBuffer16 = (S16*)(tmp+startU16);
  1350. memBuffer8 = (S8*)(tmp+startU8);
  1351. count32 = startU16;
  1352. count16 = startU8-startU16;
  1353. count8 = sizeMemBuffer-startU8;
  1354. // read sequences
  1355. S32 numSequences;
  1356. s->read(&numSequences);
  1357. sequences.setSize(numSequences);
  1358. for (i=0; i<numSequences; i++)
  1359. {
  1360. sequences[i].read(s);
  1361. // Store initial (empty) source data
  1362. sequences[i].sourceData.total = sequences[i].numKeyframes;
  1363. sequences[i].sourceData.end = sequences[i].sourceData.total - 1;
  1364. }
  1365. // read material list
  1366. delete materialList; // just in case...
  1367. materialList = new TSMaterialList;
  1368. materialList->read(*s);
  1369. }
  1370. // since we read in the buffers, we need to endian-flip their entire contents...
  1371. fixEndian(memBuffer32,memBuffer16,memBuffer8,count32,count16,count8);
  1372. tsalloc.setRead(memBuffer32,memBuffer16,memBuffer8,true);
  1373. assembleShape(); // determine size of buffer needed
  1374. mShapeDataSize = tsalloc.getSize();
  1375. tsalloc.doAlloc();
  1376. mShapeData = tsalloc.getBuffer();
  1377. tsalloc.setRead(memBuffer32,memBuffer16,memBuffer8,false);
  1378. assembleShape(); // copy to buffer
  1379. AssertFatal(tsalloc.getSize()==mShapeDataSize,"TSShape::read: shape data buffer size mis-calculated");
  1380. delete [] memBuffer32;
  1381. if (smInitOnRead)
  1382. init();
  1383. //if (names.size() == 3 && dStricmp(names[2], "Box") == 0)
  1384. //{
  1385. // Con::errorf("\nnodes.set(dMalloc(%d * sizeof(Node)), %d);", nodes.size(), nodes.size());
  1386. // for (U32 i = 0; i < nodes.size(); i++)
  1387. // {
  1388. // Node& obj = nodes[i];
  1389. // Con::errorf(" nodes[%d].nameIndex = %d;", i, obj.nameIndex);
  1390. // Con::errorf(" nodes[%d].parentIndex = %d;", i, obj.parentIndex);
  1391. // Con::errorf(" nodes[%d].firstObject = %d;", i, obj.firstObject);
  1392. // Con::errorf(" nodes[%d].firstChild = %d;", i, obj.firstChild);
  1393. // Con::errorf(" nodes[%d].nextSibling = %d;", i, obj.nextSibling);
  1394. // }
  1395. // Con::errorf("\nobjects.set(dMalloc(%d * sizeof(Object)), %d);", objects.size(), objects.size());
  1396. // for (U32 i = 0; i < objects.size(); i++)
  1397. // {
  1398. // Object& obj = objects[i];
  1399. // Con::errorf(" objects[%d].nameIndex = %d;", i, obj.nameIndex);
  1400. // Con::errorf(" objects[%d].numMeshes = %d;", i, obj.numMeshes);
  1401. // Con::errorf(" objects[%d].startMeshIndex = %d;", i, obj.startMeshIndex);
  1402. // Con::errorf(" objects[%d].nodeIndex = %d;", i, obj.nodeIndex);
  1403. // Con::errorf(" objects[%d].nextSibling = %d;", i, obj.nextSibling);
  1404. // Con::errorf(" objects[%d].firstDecal = %d;", i, obj.firstDecal);
  1405. // }
  1406. // Con::errorf("\nobjectStates.set(dMalloc(%d * sizeof(ObjectState)), %d);", objectStates.size(), objectStates.size());
  1407. // for (U32 i = 0; i < objectStates.size(); i++)
  1408. // {
  1409. // ObjectState& obj = objectStates[i];
  1410. // Con::errorf(" objectStates[%d].vis = %g;", i, obj.vis);
  1411. // Con::errorf(" objectStates[%d].frameIndex = %d;", i, obj.frameIndex);
  1412. // Con::errorf(" objectStates[%d].matFrameIndex = %d;", i, obj.matFrameIndex);
  1413. // }
  1414. // Con::errorf("\nsubShapeFirstNode.set(dMalloc(%d * sizeof(S32)), %d);", subShapeFirstNode.size(), subShapeFirstNode.size());
  1415. // for (U32 i = 0; i < subShapeFirstNode.size(); i++)
  1416. // Con::errorf(" subShapeFirstNode[%d] = %d;", i, subShapeFirstNode[i]);
  1417. // Con::errorf("\nsubShapeFirstObject.set(dMalloc(%d * sizeof(S32)), %d);", subShapeFirstObject.size(), subShapeFirstObject.size());
  1418. // for (U32 i = 0; i < subShapeFirstObject.size(); i++)
  1419. // Con::errorf(" subShapeFirstObject[%d] = %d;", i, subShapeFirstObject[i]);
  1420. // //Con::errorf("numDetailFirstSkins = %d", detailFirstSkin.size());
  1421. // Con::errorf("\nsubShapeNumNodes.set(dMalloc(%d * sizeof(S32)), %d);", subShapeNumNodes.size(), subShapeNumNodes.size());
  1422. // for (U32 i = 0; i < subShapeNumNodes.size(); i++)
  1423. // Con::errorf(" subShapeNumNodes[%d] = %d;", i, subShapeNumNodes[i]);
  1424. // Con::errorf("\nsubShapeNumObjects.set(dMalloc(%d * sizeof(S32)), %d);", subShapeNumObjects.size(), subShapeNumObjects.size());
  1425. // for (U32 i = 0; i < subShapeNumObjects.size(); i++)
  1426. // Con::errorf(" subShapeNumObjects[%d] = %d;", i, subShapeNumObjects[i]);
  1427. // Con::errorf("\ndetails.set(dMalloc(%d * sizeof(Detail)), %d);", details.size(), details.size());
  1428. // for (U32 i = 0; i < details.size(); i++)
  1429. // {
  1430. // Detail& obj = details[i];
  1431. // Con::errorf(" details[%d].nameIndex = %d;", i, obj.nameIndex);
  1432. // Con::errorf(" details[%d].subShapeNum = %d;", i, obj.subShapeNum);
  1433. // Con::errorf(" details[%d].objectDetailNum = %d;", i, obj.objectDetailNum);
  1434. // Con::errorf(" details[%d].size = %g;", i, obj.size);
  1435. // Con::errorf(" details[%d].averageError = %g;", i, obj.averageError);
  1436. // Con::errorf(" details[%d].maxError = %g;", i, obj.maxError);
  1437. // Con::errorf(" details[%d].polyCount = %d;", i, obj.polyCount);
  1438. // }
  1439. // Con::errorf("\ndefaultRotations.set(dMalloc(%d * sizeof(Quat16)), %d);", defaultRotations.size(), defaultRotations.size());
  1440. // for (U32 i = 0; i < defaultRotations.size(); i++)
  1441. // {
  1442. // Con::errorf(" defaultRotations[%d].x = %g;", i, defaultRotations[i].x);
  1443. // Con::errorf(" defaultRotations[%d].y = %g;", i, defaultRotations[i].y);
  1444. // Con::errorf(" defaultRotations[%d].z = %g;", i, defaultRotations[i].z);
  1445. // Con::errorf(" defaultRotations[%d].w = %g;", i, defaultRotations[i].w);
  1446. // }
  1447. // Con::errorf("\ndefaultTranslations.set(dMalloc(%d * sizeof(Point3F)), %d);", defaultTranslations.size(), defaultTranslations.size());
  1448. // for (U32 i = 0; i < defaultTranslations.size(); i++)
  1449. // Con::errorf(" defaultTranslations[%d].set(%g, %g, %g);", i, defaultTranslations[i].x, defaultTranslations[i].y, defaultTranslations[i].z);
  1450. // Con::errorf("\nsubShapeFirstTranslucentObject.set(dMalloc(%d * sizeof(S32)), %d);", subShapeFirstTranslucentObject.size(), subShapeFirstTranslucentObject.size());
  1451. // for (U32 i = 0; i < subShapeFirstTranslucentObject.size(); i++)
  1452. // Con::errorf(" subShapeFirstTranslucentObject[%d] = %d;", i, subShapeFirstTranslucentObject[i]);
  1453. // Con::errorf("\nmeshes.set(dMalloc(%d * sizeof(TSMesh)), %d);", meshes.size(), meshes.size());
  1454. // for (U32 i = 0; i < meshes.size(); i++)
  1455. // {
  1456. // TSMesh* obj = meshes[i];
  1457. // if (obj)
  1458. // {
  1459. // Con::errorf(" meshes[%d]->meshType = %d;", i, obj->meshType);
  1460. // Con::errorf(" meshes[%d]->mBounds.minExtents.set(%g, %g, %g);", i, obj->mBounds.minExtents.x, obj->mBounds.minExtents.y, obj->mBounds.minExtents.z);
  1461. // Con::errorf(" meshes[%d]->mBounds.maxExtents.set(%g, %g, %g);", i, obj->mBounds.maxExtents.x, obj->mBounds.maxExtents.y, obj->mBounds.maxExtents.z);
  1462. // Con::errorf(" meshes[%d]->mCenter.set(%g, %g, %g);", i, obj->mCenter.x, obj->mCenter.y, obj->mCenter.z);
  1463. // Con::errorf(" meshes[%d]->mRadius = %g;", i, obj->mRadius);
  1464. // Con::errorf(" meshes[%d]->mVisibility = %g;", i, obj->mVisibility);
  1465. // Con::errorf(" meshes[%d]->mDynamic = %d;", i, obj->mDynamic);
  1466. // Con::errorf(" meshes[%d]->parentMesh = %d;", i, obj->parentMesh);
  1467. // Con::errorf(" meshes[%d]->numFrames = %d;", i, obj->numFrames);
  1468. // Con::errorf(" meshes[%d]->numMatFrames = %d;", i, obj->numMatFrames);
  1469. // Con::errorf(" meshes[%d]->vertsPerFrame = %d;", i, obj->vertsPerFrame);
  1470. // Con::errorf("\n meshes[%d]->verts.set(dMalloc(%d * sizeof(Point3F)), %d);", obj->verts.size(), obj->verts.size());
  1471. // for (U32 j = 0; j < obj->verts.size(); j++)
  1472. // Con::errorf(" meshes[%d]->verts[%d].set(%g, %g, %g);", i, j, obj->verts[j].x, obj->verts[j].y, obj->verts[j].z);
  1473. // Con::errorf("\n meshes[%d]->norms.set(dMalloc(%d * sizeof(Point3F)), %d);", obj->norms.size(), obj->norms.size());
  1474. // for (U32 j = 0; j < obj->norms.size(); j++)
  1475. // Con::errorf(" meshes[%d]->norms[%d].set(%g, %g, %g);", i, j, obj->norms[j].x, obj->norms[j].y, obj->norms[j].z);
  1476. // Con::errorf("\n meshes[%d]->tverts.set(dMalloc(%d * sizeof(Point2F)), %d);", obj->tverts.size(), obj->tverts.size());
  1477. // for (U32 j = 0; j < obj->tverts.size(); j++)
  1478. // Con::errorf(" meshes[%d]->tverts[%d].set(%g, %g);", i, j, obj->tverts[j].x, obj->tverts[j].y);
  1479. // Con::errorf("\n meshes[%d]->primitives.set(dMalloc(%d * sizeof(TSDrawPrimitive)), %d);", obj->primitives.size(), obj->primitives.size());
  1480. // for (U32 j = 0; j < obj->primitives.size(); j++)
  1481. // {
  1482. // TSDrawPrimitive& prim = obj->primitives[j];
  1483. // Con::errorf(" meshes[%d]->primitives[%d].start = %d;", i, j, prim.start);
  1484. // Con::errorf(" meshes[%d]->primitives[%d].numElements = %d;", i, j, prim.numElements);
  1485. // Con::errorf(" meshes[%d]->primitives[%d].matIndex = %d;", i, j, prim.matIndex);
  1486. // }
  1487. // Con::errorf("\n meshes[%d]->encodedNorms.set(dMalloc(%d * sizeof(U8)), %d);", obj->encodedNorms.size(), obj->encodedNorms.size());
  1488. // for (U32 j = 0; j < obj->encodedNorms.size(); j++)
  1489. // Con::errorf(" meshes[%d]->encodedNorms[%d] = %c;", i, j, obj->encodedNorms[j]);
  1490. // Con::errorf("\n meshes[%d]->indices.set(dMalloc(%d * sizeof(U16)), %d);", obj->indices.size(), obj->indices.size());
  1491. // for (U32 j = 0; j < obj->indices.size(); j++)
  1492. // Con::errorf(" meshes[%d]->indices[%d] = %d;", i, j, obj->indices[j]);
  1493. // Con::errorf("\n meshes[%d]->initialTangents.set(dMalloc(%d * sizeof(Point3F)), %d);", obj->initialTangents.size(), obj->initialTangents.size());
  1494. // for (U32 j = 0; j < obj->initialTangents.size(); j++)
  1495. // Con::errorf(" meshes[%d]->initialTangents[%d].set(%g, %g, %g);", i, j, obj->initialTangents[j].x, obj->initialTangents[j].y, obj->initialTangents[j].z);
  1496. // Con::errorf("\n meshes[%d]->tangents.set(dMalloc(%d * sizeof(Point4F)), %d);", obj->tangents.size(), obj->tangents.size());
  1497. // for (U32 j = 0; j < obj->tangents.size(); j++)
  1498. // Con::errorf(" meshes[%d]->tangents[%d].set(%g, %g, %g, %g);", i, j, obj->tangents[j].x, obj->tangents[j].y, obj->tangents[j].z, obj->tangents[j].w);
  1499. // Con::errorf(" meshes[%d]->billboardAxis.set(%g, %g, %g);", i, obj->billboardAxis.x, obj->billboardAxis.y, obj->billboardAxis.z);
  1500. // Con::errorf("\n meshes[%d]->planeNormals.set(dMalloc(%d * sizeof(Point3F)), %d);", obj->planeNormals.size(), obj->planeNormals.size());
  1501. // for (U32 j = 0; j < obj->planeNormals.size(); j++)
  1502. // Con::errorf(" meshes[%d]->planeNormals[%d].set(%g, %g, %g);", i, j, obj->planeNormals[j].x, obj->planeNormals[j].y, obj->planeNormals[j].z);
  1503. // Con::errorf("\n meshes[%d]->planeConstants.set(dMalloc(%d * sizeof(F32)), %d);", obj->planeConstants.size(), obj->planeConstants.size());
  1504. // for (U32 j = 0; j < obj->planeConstants.size(); j++)
  1505. // Con::errorf(" meshes[%d]->planeConstants[%d] = %g;", i, j, obj->planeConstants[j]);
  1506. // Con::errorf("\n meshes[%d]->planeMaterials.set(dMalloc(%d * sizeof(U32)), %d);", obj->planeMaterials.size(), obj->planeMaterials.size());
  1507. // for (U32 j = 0; j < obj->planeMaterials.size(); j++)
  1508. // Con::errorf(" meshes[%d]->planeMaterials[%d] = %d;", i, j, obj->planeMaterials[j]);
  1509. // Con::errorf(" meshes[%d]->planesPerFrame = %d;", i, obj->planesPerFrame);
  1510. // Con::errorf(" meshes[%d]->mergeBufferStart = %d;", i, obj->mergeBufferStart);
  1511. // }
  1512. // }
  1513. // Con::errorf("\nalphaIn.set(dMalloc(%d * sizeof(F32)), %d);", alphaIn.size(), alphaIn.size());
  1514. // for (U32 i = 0; i < alphaIn.size(); i++)
  1515. // Con::errorf(" alphaIn[%d] = %g;", i, alphaIn[i]);
  1516. // Con::errorf("\nalphaOut.set(dMalloc(%d * sizeof(F32)), %d);", alphaOut.size(), alphaOut.size());
  1517. // for (U32 i = 0; i < alphaOut.size(); i++)
  1518. // Con::errorf(" alphaOut[%d] = %g;", i, alphaOut[i]);
  1519. // //Con::errorf("numSequences = %d", sequences.size());
  1520. // //Con::errorf("numNodeRotations = %d", nodeRotations.size());
  1521. // //Con::errorf("numNodeTranslations = %d", nodeTranslations.size());
  1522. // //Con::errorf("numNodeUniformScales = %d", nodeUniformScales.size());
  1523. // //Con::errorf("numNodeAlignedScales = %d", nodeAlignedScales.size());
  1524. // //Con::errorf("numNodeArbitraryScaleRots = %d", nodeArbitraryScaleRots.size());
  1525. // //Con::errorf("numNodeArbitraryScaleFactors = %d", nodeArbitraryScaleFactors.size());
  1526. // //Con::errorf("numGroundRotations = %d", groundRotations.size());
  1527. // //Con::errorf("numGroundTranslations = %d", groundTranslations.size());
  1528. // //Con::errorf("numTriggers = %d", triggers.size());
  1529. // //Con::errorf("numBillboardDetails = %d", billboardDetails.size());
  1530. // //Con::errorf("\nnumDetailCollisionAccelerators = %d", detailCollisionAccelerators.size());
  1531. // //for (U32 i = 0; i < detailCollisionAccelerators.size(); i++)
  1532. // //{
  1533. // // ConvexHullAccelerator* obj = detailCollisionAccelerators[i];
  1534. // // if (obj)
  1535. // // {
  1536. // // Con::errorf(" detailCollisionAccelerators[%d].numVerts = %d", i, obj->numVerts);
  1537. // // for (U32 j = 0; j < obj->numVerts; j++)
  1538. // // {
  1539. // // Con::errorf(" verts[%d](%g, %g, %g)", j, obj->vertexList[j].x, obj->vertexList[j].y, obj->vertexList[j].z);
  1540. // // Con::errorf(" norms[%d](%g, %g, %g)", j, obj->normalList[j].x, obj->normalList[j].y, obj->normalList[j].z);
  1541. // // //U8** emitStrings;
  1542. // // }
  1543. // // }
  1544. // //}
  1545. // Con::errorf("\nnames.setSize(%d);", names.size());
  1546. // for (U32 i = 0; i < names.size(); i++)
  1547. // Con::errorf(" names[%d] = StringTable->insert(\"%s\");", i, names[i]);
  1548. // //TSMaterialList * materialList;
  1549. // Con::errorf("\nradius = %g;", radius);
  1550. // Con::errorf("tubeRadius = %g;", tubeRadius);
  1551. // Con::errorf("center.set(%g, %g, %g);", center.x, center.y, center.z);
  1552. // Con::errorf("bounds.minExtents.set(%g, %g, %g);", bounds.minExtents.x, bounds.minExtents.y, bounds.minExtents.z);
  1553. // Con::errorf("bounds.maxExtents.set(%g, %g, %g);", bounds.maxExtents.x, bounds.maxExtents.y, bounds.maxExtents.z);
  1554. // Con::errorf("\nmExporterVersion = %d;", mExporterVersion);
  1555. // Con::errorf("mSmallestVisibleSize = %g;", mSmallestVisibleSize);
  1556. // Con::errorf("mSmallestVisibleDL = %d;", mSmallestVisibleDL);
  1557. // Con::errorf("mReadVersion = %d;", mReadVersion);
  1558. // Con::errorf("mFlags = %d;", mFlags);
  1559. // //Con::errorf("data = %d", data);
  1560. // Con::errorf("mSequencesConstructed = %d;", mSequencesConstructed);
  1561. //}
  1562. return true;
  1563. }
  1564. void TSShape::createEmptyShape()
  1565. {
  1566. nodes.set(dMalloc(1 * sizeof(Node)), 1);
  1567. nodes[0].nameIndex = 1;
  1568. nodes[0].parentIndex = -1;
  1569. nodes[0].firstObject = 0;
  1570. nodes[0].firstChild = -1;
  1571. nodes[0].nextSibling = -1;
  1572. objects.set(dMalloc(1 * sizeof(Object)), 1);
  1573. objects[0].nameIndex = 2;
  1574. objects[0].numMeshes = 1;
  1575. objects[0].startMeshIndex = 0;
  1576. objects[0].nodeIndex = 0;
  1577. objects[0].nextSibling = -1;
  1578. objects[0].firstDecal = -1;
  1579. objectStates.set(dMalloc(1 * sizeof(ObjectState)), 1);
  1580. objectStates[0].vis = 1;
  1581. objectStates[0].frameIndex = 0;
  1582. objectStates[0].matFrameIndex = 0;
  1583. subShapeFirstNode.set(dMalloc(1 * sizeof(S32)), 1);
  1584. subShapeFirstNode[0] = 0;
  1585. subShapeFirstObject.set(dMalloc(1 * sizeof(S32)), 1);
  1586. subShapeFirstObject[0] = 0;
  1587. detailFirstSkin.set(NULL, 0);
  1588. subShapeNumNodes.set(dMalloc(1 * sizeof(S32)), 1);
  1589. subShapeNumNodes[0] = 1;
  1590. subShapeNumObjects.set(dMalloc(1 * sizeof(S32)), 1);
  1591. subShapeNumObjects[0] = 1;
  1592. details.set(dMalloc(1 * sizeof(Detail)), 1);
  1593. details[0].nameIndex = 0;
  1594. details[0].subShapeNum = 0;
  1595. details[0].objectDetailNum = 0;
  1596. details[0].size = 2.0f;
  1597. details[0].averageError = -1.0f;
  1598. details[0].maxError = -1.0f;
  1599. details[0].polyCount = 0;
  1600. defaultRotations.set(dMalloc(1 * sizeof(Quat16)), 1);
  1601. defaultRotations[0].x = 0.0f;
  1602. defaultRotations[0].y = 0.0f;
  1603. defaultRotations[0].z = 0.0f;
  1604. defaultRotations[0].w = 0.0f;
  1605. defaultTranslations.set(dMalloc(1 * sizeof(Point3F)), 1);
  1606. defaultTranslations[0].set(0.0f, 0.0f, 0.0f);
  1607. subShapeFirstTranslucentObject.set(dMalloc(1 * sizeof(S32)), 1);
  1608. subShapeFirstTranslucentObject[0] = 1;
  1609. alphaIn.set(dMalloc(1 * sizeof(F32)), 1);
  1610. alphaIn[0] = 0;
  1611. alphaOut.set(dMalloc(1 * sizeof(F32)), 1);
  1612. alphaOut[0] = -1;
  1613. sequences.set(NULL, 0);
  1614. nodeRotations.set(NULL, 0);
  1615. nodeTranslations.set(NULL, 0);
  1616. nodeUniformScales.set(NULL, 0);
  1617. nodeAlignedScales.set(NULL, 0);
  1618. nodeArbitraryScaleRots.set(NULL, 0);
  1619. nodeArbitraryScaleFactors.set(NULL, 0);
  1620. groundRotations.set(NULL, 0);
  1621. groundTranslations.set(NULL, 0);
  1622. triggers.set(NULL, 0);
  1623. billboardDetails.set(NULL, 0);
  1624. names.setSize(3);
  1625. names[0] = StringTable->insert("Detail2");
  1626. names[1] = StringTable->insert("Mesh2");
  1627. names[2] = StringTable->insert("Mesh");
  1628. radius = 0.866025f;
  1629. tubeRadius = 0.707107f;
  1630. center.set(0.0f, 0.5f, 0.0f);
  1631. bounds.minExtents.set(-0.5f, 0.0f, -0.5f);
  1632. bounds.maxExtents.set(0.5f, 1.0f, 0.5f);
  1633. mExporterVersion = 124;
  1634. mSmallestVisibleSize = 2;
  1635. mSmallestVisibleDL = 0;
  1636. mReadVersion = 24;
  1637. mFlags = 0;
  1638. mSequencesConstructed = 0;
  1639. mUseDetailFromScreenError = false;
  1640. mDetailLevelLookup.setSize( 1 );
  1641. mDetailLevelLookup[0].set( -1, 0 );
  1642. // Init the collision accelerator array. Note that we don't compute the
  1643. // accelerators until the app requests them
  1644. detailCollisionAccelerators.setSize(details.size());
  1645. for (U32 i = 0; i < detailCollisionAccelerators.size(); i++)
  1646. detailCollisionAccelerators[i] = NULL;
  1647. }
  1648. void TSShape::fixEndian(S32 * buff32, S16 * buff16, S8 *, S32 count32, S32 count16, S32)
  1649. {
  1650. // if endian-ness isn't the same, need to flip the buffer contents.
  1651. if (0x12345678!=convertLEndianToHost(0x12345678))
  1652. {
  1653. for (S32 i=0; i<count32; i++)
  1654. buff32[i]=convertLEndianToHost(buff32[i]);
  1655. for (S32 i=0; i<count16*2; i++)
  1656. buff16[i]=convertLEndianToHost(buff16[i]);
  1657. }
  1658. }
  1659. template<> void *Resource<TSShape>::create(const Torque::Path &path)
  1660. {
  1661. // Execute the shape script if it exists
  1662. Torque::Path scriptPath(path);
  1663. scriptPath.setExtension("cs");
  1664. // Don't execute the script if we're already doing so!
  1665. StringTableEntry currentScript = Platform::stripBasePath(CodeBlock::getCurrentCodeBlockFullPath());
  1666. if (!scriptPath.getFullPath().equal(currentScript))
  1667. {
  1668. Torque::Path scriptPathDSO(scriptPath);
  1669. scriptPathDSO.setExtension("cs.dso");
  1670. if (Torque::FS::IsFile(scriptPathDSO) || Torque::FS::IsFile(scriptPath))
  1671. {
  1672. String evalCmd = "exec(\"" + scriptPath + "\");";
  1673. String instantGroup = Con::getVariable("InstantGroup");
  1674. Con::setIntVariable("InstantGroup", RootGroupId);
  1675. Con::evaluate((const char*)evalCmd.c_str(), false, scriptPath.getFullPath());
  1676. Con::setVariable("InstantGroup", instantGroup.c_str());
  1677. }
  1678. }
  1679. // Attempt to load the shape
  1680. TSShape * ret = 0;
  1681. bool readSuccess = false;
  1682. const String extension = path.getExtension();
  1683. if ( extension.equal( "dts", String::NoCase ) )
  1684. {
  1685. FileStream stream;
  1686. stream.open( path.getFullPath(), Torque::FS::File::Read );
  1687. if ( stream.getStatus() != Stream::Ok )
  1688. {
  1689. Con::errorf( "Resource<TSShape>::create - Could not open '%s'", path.getFullPath().c_str() );
  1690. return NULL;
  1691. }
  1692. ret = new TSShape;
  1693. readSuccess = ret->read(&stream);
  1694. }
  1695. else if ( extension.equal( "dae", String::NoCase ) || extension.equal( "kmz", String::NoCase ) )
  1696. {
  1697. #ifdef TORQUE_COLLADA
  1698. // Attempt to load the DAE file
  1699. ret = loadColladaShape(path);
  1700. readSuccess = (ret != NULL);
  1701. #else
  1702. // No COLLADA support => attempt to load the cached DTS file instead
  1703. Torque::Path cachedPath = path;
  1704. cachedPath.setExtension("cached.dts");
  1705. FileStream stream;
  1706. stream.open( cachedPath.getFullPath(), Torque::FS::File::Read );
  1707. if ( stream.getStatus() != Stream::Ok )
  1708. {
  1709. Con::errorf( "Resource<TSShape>::create - Could not open '%s'", cachedPath.getFullPath().c_str() );
  1710. return NULL;
  1711. }
  1712. ret = new TSShape;
  1713. readSuccess = ret->read(&stream);
  1714. #endif
  1715. }
  1716. else
  1717. {
  1718. Con::errorf( "Resource<TSShape>::create - '%s' has an unknown file format", path.getFullPath().c_str() );
  1719. delete ret;
  1720. return NULL;
  1721. }
  1722. if( !readSuccess )
  1723. {
  1724. Con::errorf( "Resource<TSShape>::create - Error reading '%s'", path.getFullPath().c_str() );
  1725. delete ret;
  1726. ret = NULL;
  1727. }
  1728. return ret;
  1729. }
  1730. template<> ResourceBase::Signature Resource<TSShape>::signature()
  1731. {
  1732. return MakeFourCC('t','s','s','h');
  1733. }
  1734. TSShape::ConvexHullAccelerator* TSShape::getAccelerator(S32 dl)
  1735. {
  1736. AssertFatal(dl < details.size(), "Error, bad detail level!");
  1737. if (dl == -1)
  1738. return NULL;
  1739. AssertFatal( detailCollisionAccelerators.size() == details.size(),
  1740. "TSShape::getAccelerator() - mismatched array sizes!" );
  1741. if (detailCollisionAccelerators[dl] == NULL)
  1742. computeAccelerator(dl);
  1743. AssertFatal(detailCollisionAccelerators[dl] != NULL, "This should be non-null after computing it!");
  1744. return detailCollisionAccelerators[dl];
  1745. }
  1746. void TSShape::computeAccelerator(S32 dl)
  1747. {
  1748. AssertFatal(dl < details.size(), "Error, bad detail level!");
  1749. // Have we already computed this?
  1750. if (detailCollisionAccelerators[dl] != NULL)
  1751. return;
  1752. // Create a bogus features list...
  1753. ConvexFeature cf;
  1754. MatrixF mat(true);
  1755. Point3F n(0, 0, 1);
  1756. const TSDetail* detail = &details[dl];
  1757. S32 ss = detail->subShapeNum;
  1758. S32 od = detail->objectDetailNum;
  1759. S32 start = subShapeFirstObject[ss];
  1760. S32 end = subShapeNumObjects[ss] + start;
  1761. if (start < end)
  1762. {
  1763. // run through objects and collide
  1764. // DMMNOTE: This assumes that the transform of the collision hulls is
  1765. // identity...
  1766. U32 surfaceKey = 0;
  1767. for (S32 i = start; i < end; i++)
  1768. {
  1769. const TSObject* obj = &objects[i];
  1770. if (obj->numMeshes && od < obj->numMeshes) {
  1771. TSMesh* mesh = meshes[obj->startMeshIndex + od];
  1772. if (mesh)
  1773. mesh->getFeatures(0, mat, n, &cf, surfaceKey);
  1774. }
  1775. }
  1776. }
  1777. Vector<Point3F> fixedVerts;
  1778. VECTOR_SET_ASSOCIATION(fixedVerts);
  1779. S32 i;
  1780. for (i = 0; i < cf.mVertexList.size(); i++) {
  1781. S32 j;
  1782. bool found = false;
  1783. for (j = 0; j < cf.mFaceList.size(); j++) {
  1784. if (cf.mFaceList[j].vertex[0] == i ||
  1785. cf.mFaceList[j].vertex[1] == i ||
  1786. cf.mFaceList[j].vertex[2] == i) {
  1787. found = true;
  1788. break;
  1789. }
  1790. }
  1791. if (!found)
  1792. continue;
  1793. found = false;
  1794. for (j = 0; j < fixedVerts.size(); j++) {
  1795. if (fixedVerts[j] == cf.mVertexList[i]) {
  1796. found = true;
  1797. break;
  1798. }
  1799. }
  1800. if (found == true) {
  1801. // Ok, need to replace any references to vertex i in the facelists with
  1802. // a reference to vertex j in the fixed list
  1803. for (S32 k = 0; k < cf.mFaceList.size(); k++) {
  1804. for (S32 l = 0; l < 3; l++) {
  1805. if (cf.mFaceList[k].vertex[l] == i)
  1806. cf.mFaceList[k].vertex[l] = j;
  1807. }
  1808. }
  1809. } else {
  1810. for (S32 k = 0; k < cf.mFaceList.size(); k++) {
  1811. for (S32 l = 0; l < 3; l++) {
  1812. if (cf.mFaceList[k].vertex[l] == i)
  1813. cf.mFaceList[k].vertex[l] = fixedVerts.size();
  1814. }
  1815. }
  1816. fixedVerts.push_back(cf.mVertexList[i]);
  1817. }
  1818. }
  1819. cf.mVertexList.setSize(0);
  1820. cf.mVertexList = fixedVerts;
  1821. // Ok, so now we have a vertex list. Lets copy that out...
  1822. ConvexHullAccelerator* accel = new ConvexHullAccelerator;
  1823. detailCollisionAccelerators[dl] = accel;
  1824. accel->numVerts = cf.mVertexList.size();
  1825. accel->vertexList = new Point3F[accel->numVerts];
  1826. dMemcpy(accel->vertexList, cf.mVertexList.address(), sizeof(Point3F) * accel->numVerts);
  1827. accel->normalList = new Point3F[cf.mFaceList.size()];
  1828. for (i = 0; i < cf.mFaceList.size(); i++)
  1829. accel->normalList[i] = cf.mFaceList[i].normal;
  1830. accel->emitStrings = new U8*[accel->numVerts];
  1831. dMemset(accel->emitStrings, 0, sizeof(U8*) * accel->numVerts);
  1832. for (i = 0; i < accel->numVerts; i++) {
  1833. S32 j;
  1834. Vector<U32> faces;
  1835. VECTOR_SET_ASSOCIATION(faces);
  1836. for (j = 0; j < cf.mFaceList.size(); j++) {
  1837. if (cf.mFaceList[j].vertex[0] == i ||
  1838. cf.mFaceList[j].vertex[1] == i ||
  1839. cf.mFaceList[j].vertex[2] == i) {
  1840. faces.push_back(j);
  1841. }
  1842. }
  1843. AssertFatal(faces.size() != 0, "Huh? Vertex unreferenced by any faces");
  1844. // Insert all faces that didn't make the first cut, but share a plane with
  1845. // a face that's on the short list.
  1846. for (j = 0; j < cf.mFaceList.size(); j++) {
  1847. bool found = false;
  1848. S32 k;
  1849. for (k = 0; k < faces.size(); k++) {
  1850. if (faces[k] == j)
  1851. found = true;
  1852. }
  1853. if (found)
  1854. continue;
  1855. found = false;
  1856. for (k = 0; k < faces.size(); k++) {
  1857. if (mDot(accel->normalList[faces[k]], accel->normalList[j]) > 0.999) {
  1858. found = true;
  1859. break;
  1860. }
  1861. }
  1862. if (found)
  1863. faces.push_back(j);
  1864. }
  1865. Vector<U32> vertRemaps;
  1866. VECTOR_SET_ASSOCIATION(vertRemaps);
  1867. for (j = 0; j < faces.size(); j++) {
  1868. for (U32 k = 0; k < 3; k++) {
  1869. U32 insert = cf.mFaceList[faces[j]].vertex[k];
  1870. bool found = false;
  1871. for (S32 l = 0; l < vertRemaps.size(); l++) {
  1872. if (insert == vertRemaps[l]) {
  1873. found = true;
  1874. break;
  1875. }
  1876. }
  1877. if (!found)
  1878. vertRemaps.push_back(insert);
  1879. }
  1880. }
  1881. Vector<Point2I> edges;
  1882. VECTOR_SET_ASSOCIATION(edges);
  1883. for (j = 0; j < faces.size(); j++) {
  1884. for (U32 k = 0; k < 3; k++) {
  1885. U32 edgeStart = cf.mFaceList[faces[j]].vertex[(k + 0) % 3];
  1886. U32 edgeEnd = cf.mFaceList[faces[j]].vertex[(k + 1) % 3];
  1887. U32 e0 = getMin(edgeStart, edgeEnd);
  1888. U32 e1 = getMax(edgeStart, edgeEnd);
  1889. bool found = false;
  1890. for (S32 l = 0; l < edges.size(); l++) {
  1891. if (edges[l].x == e0 && edges[l].y == e1) {
  1892. found = true;
  1893. break;
  1894. }
  1895. }
  1896. if (!found)
  1897. edges.push_back(Point2I(e0, e1));
  1898. }
  1899. }
  1900. //AssertFatal(vertRemaps.size() < 256 && faces.size() < 256 && edges.size() < 256,
  1901. // "Error, ran over the shapebase assumptions about convex hulls.");
  1902. U32 emitStringLen = 1 + vertRemaps.size() +
  1903. 1 + (edges.size() * 2) +
  1904. 1 + (faces.size() * 4);
  1905. accel->emitStrings[i] = new U8[emitStringLen];
  1906. U32 currPos = 0;
  1907. accel->emitStrings[i][currPos++] = vertRemaps.size();
  1908. for (j = 0; j < vertRemaps.size(); j++)
  1909. accel->emitStrings[i][currPos++] = vertRemaps[j];
  1910. accel->emitStrings[i][currPos++] = edges.size();
  1911. for (j = 0; j < edges.size(); j++) {
  1912. S32 l;
  1913. U32 old = edges[j].x;
  1914. bool found = false;
  1915. for (l = 0; l < vertRemaps.size(); l++) {
  1916. if (vertRemaps[l] == old) {
  1917. found = true;
  1918. accel->emitStrings[i][currPos++] = l;
  1919. break;
  1920. }
  1921. }
  1922. AssertFatal(found, "Error, couldn't find the remap!");
  1923. old = edges[j].y;
  1924. found = false;
  1925. for (l = 0; l < vertRemaps.size(); l++) {
  1926. if (vertRemaps[l] == old) {
  1927. found = true;
  1928. accel->emitStrings[i][currPos++] = l;
  1929. break;
  1930. }
  1931. }
  1932. AssertFatal(found, "Error, couldn't find the remap!");
  1933. }
  1934. accel->emitStrings[i][currPos++] = faces.size();
  1935. for (j = 0; j < faces.size(); j++) {
  1936. accel->emitStrings[i][currPos++] = faces[j];
  1937. for (U32 k = 0; k < 3; k++) {
  1938. U32 old = cf.mFaceList[faces[j]].vertex[k];
  1939. bool found = false;
  1940. for (S32 l = 0; l < vertRemaps.size(); l++) {
  1941. if (vertRemaps[l] == old) {
  1942. found = true;
  1943. accel->emitStrings[i][currPos++] = l;
  1944. break;
  1945. }
  1946. }
  1947. AssertFatal(found, "Error, couldn't find the remap!");
  1948. }
  1949. }
  1950. AssertFatal(currPos == emitStringLen, "Error, over/underflowed the emission string!");
  1951. }
  1952. }