tsShape.cpp 74 KB

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