tsShape.cpp 74 KB

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