tsShape.cpp 78 KB

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