tsShape.cpp 78 KB

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