tsMesh.cpp 114 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478
  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/tsMesh.h"
  24. #include "ts/tsMeshIntrinsics.h"
  25. #include "ts/tsDecal.h"
  26. #include "ts/tsSortedMesh.h"
  27. #include "ts/tsShape.h"
  28. #include "ts/tsShapeInstance.h"
  29. #include "ts/tsRenderState.h"
  30. #include "ts/tsMaterialList.h"
  31. #include "ts/instancingMatHook.h"
  32. #include "math/mMath.h"
  33. #include "math/mathIO.h"
  34. #include "math/mathUtils.h"
  35. #include "console/console.h"
  36. #include "scene/sceneObject.h"
  37. #include "core/bitRender.h"
  38. #include "collision/convex.h"
  39. #include "collision/optimizedPolyList.h"
  40. #include "core/frameAllocator.h"
  41. #include "platform/profiler.h"
  42. #include "materials/sceneData.h"
  43. #include "materials/materialManager.h"
  44. #include "scene/sceneManager.h"
  45. #include "scene/sceneRenderState.h"
  46. #include "materials/matInstance.h"
  47. #include "materials/materialFeatureTypes.h"
  48. #include "renderInstance/renderPassManager.h"
  49. #include "materials/customMaterialDefinition.h"
  50. #include "gfx/util/triListOpt.h"
  51. #include "util/triRayCheck.h"
  52. #include "opcode/Opcode.h"
  53. #if defined(TORQUE_OS_XENON)
  54. # include "platformXbox/platformXbox.h"
  55. #endif
  56. GFXPrimitiveType drawTypes[] = { GFXTriangleList, GFXTriangleStrip };
  57. #define getDrawType(a) (drawTypes[a])
  58. // structures used to share data between detail levels...
  59. // used (and valid) during load only
  60. Vector<Point3F*> TSMesh::smVertsList;
  61. Vector<Point3F*> TSMesh::smNormsList;
  62. Vector<U8*> TSMesh::smEncodedNormsList;
  63. Vector<Point2F*> TSMesh::smTVertsList;
  64. Vector<Point2F*> TSMesh::smTVerts2List;
  65. Vector<ColorI*> TSMesh::smColorsList;
  66. Vector<bool> TSMesh::smDataCopied;
  67. Vector<MatrixF*> TSSkinMesh::smInitTransformList;
  68. Vector<S32*> TSSkinMesh::smVertexIndexList;
  69. Vector<S32*> TSSkinMesh::smBoneIndexList;
  70. Vector<F32*> TSSkinMesh::smWeightList;
  71. Vector<S32*> TSSkinMesh::smNodeIndexList;
  72. bool TSSkinMesh::smDebugSkinVerts = false;
  73. Vector<Point3F> gNormalStore;
  74. bool TSMesh::smUseTriangles = false; // convert all primitives to triangle lists on load
  75. bool TSMesh::smUseOneStrip = true; // join triangle strips into one long strip on load
  76. S32 TSMesh::smMinStripSize = 1; // smallest number of _faces_ allowed per strip (all else put in tri list)
  77. bool TSMesh::smUseEncodedNormals = false;
  78. const F32 TSMesh::VISIBILITY_EPSILON = 0.0001f;
  79. S32 TSMesh::smMaxInstancingVerts = 200;
  80. MatrixF TSMesh::smDummyNodeTransform(1);
  81. // quick function to force object to face camera -- currently throws out roll :(
  82. void tsForceFaceCamera( MatrixF *mat, const Point3F *objScale )
  83. {
  84. Point4F p;
  85. mat->getColumn( 3, &p );
  86. mat->identity();
  87. mat->setColumn( 3, p );
  88. if ( objScale )
  89. {
  90. MatrixF scale( true );
  91. scale.scale( *objScale );
  92. mat->mul( scale );
  93. }
  94. }
  95. //-----------------------------------------------------
  96. // TSMesh render methods
  97. //-----------------------------------------------------
  98. void TSMesh::render( TSVertexBufferHandle &instanceVB )
  99. {
  100. innerRender(instanceVB, mPB);
  101. }
  102. void TSMesh::innerRender( TSVertexBufferHandle &vb, GFXPrimitiveBufferHandle &pb )
  103. {
  104. if ( !vb.isValid() || !pb.isValid() )
  105. return;
  106. GFX->setVertexBuffer( vb );
  107. GFX->setPrimitiveBuffer( pb );
  108. for( U32 p = 0; p < primitives.size(); p++ )
  109. GFX->drawPrimitive( p );
  110. }
  111. void TSMesh::render( TSMaterialList *materials,
  112. const TSRenderState &rdata,
  113. bool isSkinDirty,
  114. const Vector<MatrixF> &transforms,
  115. TSVertexBufferHandle &vertexBuffer,
  116. const char *meshName)
  117. {
  118. // These are only used by TSSkinMesh.
  119. TORQUE_UNUSED( isSkinDirty );
  120. TORQUE_UNUSED( transforms );
  121. // Pass our shared VB.
  122. innerRender(materials, rdata, vertexBuffer, mPB, meshName);
  123. }
  124. void TSMesh::innerRender( TSMaterialList *materials, const TSRenderState &rdata, TSVertexBufferHandle &vb, GFXPrimitiveBufferHandle &pb, const char *meshName )
  125. {
  126. PROFILE_SCOPE( TSMesh_InnerRender );
  127. if( vertsPerFrame <= 0 )
  128. return;
  129. F32 meshVisibility = rdata.getFadeOverride() * mVisibility;
  130. if ( meshVisibility < VISIBILITY_EPSILON )
  131. return;
  132. const SceneRenderState *state = rdata.getSceneState();
  133. RenderPassManager *renderPass = state->getRenderPass();
  134. MeshRenderInst *coreRI = renderPass->allocInst<MeshRenderInst>();
  135. coreRI->type = RenderPassManager::RIT_Mesh;
  136. #ifdef TORQUE_ENABLE_GFXDEBUGEVENTS
  137. coreRI->meshName = meshName;
  138. #endif
  139. // Pass accumulation texture along.
  140. coreRI->accuTex = rdata.getAccuTex();
  141. const MatrixF &objToWorld = GFX->getWorldMatrix();
  142. // Sort by the center point or the bounds.
  143. if ( rdata.useOriginSort() )
  144. coreRI->sortDistSq = ( objToWorld.getPosition() - state->getCameraPosition() ).lenSquared();
  145. else
  146. {
  147. Box3F rBox = mBounds;
  148. objToWorld.mul( rBox );
  149. coreRI->sortDistSq = rBox.getSqDistanceToPoint( state->getCameraPosition() );
  150. }
  151. if (getFlags(Billboard))
  152. {
  153. Point3F camPos = state->getDiffuseCameraPosition();
  154. Point3F objPos;
  155. objToWorld.getColumn(3, &objPos);
  156. Point3F targetVector = camPos - objPos;
  157. if(getFlags(BillboardZAxis))
  158. targetVector.z = 0.0f;
  159. targetVector.normalize();
  160. MatrixF orient = MathUtils::createOrientFromDir(targetVector);
  161. orient.setPosition(objPos);
  162. orient.scale(objToWorld.getScale());
  163. coreRI->objectToWorld = renderPass->allocUniqueXform( orient );
  164. }
  165. else
  166. coreRI->objectToWorld = renderPass->allocUniqueXform( objToWorld );
  167. coreRI->worldToCamera = renderPass->allocSharedXform(RenderPassManager::View);
  168. coreRI->projection = renderPass->allocSharedXform(RenderPassManager::Projection);
  169. AssertFatal( vb.isValid(), "TSMesh::innerRender() - Got invalid vertex buffer!" );
  170. AssertFatal( pb.isValid(), "TSMesh::innerRender() - Got invalid primitive buffer!" );
  171. coreRI->vertBuff = &vb;
  172. coreRI->primBuff = &pb;
  173. coreRI->defaultKey2 = (uintptr_t) coreRI->vertBuff;
  174. coreRI->materialHint = rdata.getMaterialHint();
  175. coreRI->visibility = meshVisibility;
  176. coreRI->cubemap = rdata.getCubemap();
  177. if ( getMeshType() == TSMesh::SkinMeshType )
  178. {
  179. rdata.getNodeTransforms(&coreRI->mNodeTransforms, &coreRI->mNodeTransformCount);
  180. }
  181. else
  182. {
  183. coreRI->mNodeTransforms = &TSMesh::smDummyNodeTransform;
  184. coreRI->mNodeTransformCount = 1;
  185. }
  186. // NOTICE: SFXBB is removed and refraction is disabled!
  187. //coreRI->backBuffTex = GFX->getSfxBackBuffer();
  188. for ( S32 i = 0; i < primitives.size(); i++ )
  189. {
  190. const TSDrawPrimitive &draw = primitives[i];
  191. // We need to have a material.
  192. if ( draw.matIndex & TSDrawPrimitive::NoMaterial )
  193. continue;
  194. #ifdef TORQUE_DEBUG_BREAK_INSPECT
  195. // for inspection if you happen to be running in a debugger and can't do bit
  196. // operations in your head.
  197. S32 triangles = draw.matIndex & TSDrawPrimitive::Triangles;
  198. S32 strip = draw.matIndex & TSDrawPrimitive::Strip;
  199. S32 fan = draw.matIndex & TSDrawPrimitive::Fan;
  200. S32 indexed = draw.matIndex & TSDrawPrimitive::Indexed;
  201. S32 type = draw.matIndex & TSDrawPrimitive::TypeMask;
  202. TORQUE_UNUSED(triangles);
  203. TORQUE_UNUSED(strip);
  204. TORQUE_UNUSED(fan);
  205. TORQUE_UNUSED(indexed);
  206. TORQUE_UNUSED(type);
  207. //define TORQUE_DEBUG_BREAK_INSPECT, and insert debug break here to inspect the above elements at runtime
  208. #endif
  209. const U32 matIndex = draw.matIndex & TSDrawPrimitive::MaterialMask;
  210. BaseMatInstance *matInst = materials->getMaterialInst( matIndex );
  211. #ifndef TORQUE_OS_MAC
  212. // Get the instancing material if this mesh qualifies.
  213. if ( meshType != SkinMeshType && pb->mPrimitiveArray[i].numVertices < smMaxInstancingVerts )
  214. if (matInst && !matInst->getFeatures().hasFeature(MFT_HardwareSkinning))
  215. matInst = InstancingMaterialHook::getInstancingMat( matInst );
  216. #endif
  217. // If we don't have a material instance after the overload then
  218. // there is nothing to render... skip this primitive.
  219. matInst = state->getOverrideMaterial( matInst );
  220. if ( !matInst || !matInst->isValid())
  221. continue;
  222. // If the material needs lights then gather them
  223. // here once and set them on the core render inst.
  224. if ( matInst->isForwardLit() && !coreRI->lights[0] && rdata.getLightQuery() )
  225. rdata.getLightQuery()->getLights( coreRI->lights, 8 );
  226. MeshRenderInst *ri = renderPass->allocInst<MeshRenderInst>();
  227. *ri = *coreRI;
  228. ri->matInst = matInst;
  229. ri->defaultKey = matInst->getStateHint();
  230. ri->primBuffIndex = mPrimBufferOffset + i;
  231. // Translucent materials need the translucent type.
  232. if ( matInst->getMaterial()->isTranslucent() )
  233. {
  234. ri->type = RenderPassManager::RIT_Translucent;
  235. ri->translucentSort = true;
  236. }
  237. renderPass->addInst( ri );
  238. }
  239. }
  240. const Point3F * TSMesh::getNormals( S32 firstVert )
  241. {
  242. if ( getFlags( UseEncodedNormals ) )
  243. {
  244. gNormalStore.setSize( vertsPerFrame );
  245. for ( S32 i = 0; i < encodedNorms.size(); i++ )
  246. gNormalStore[i] = decodeNormal( encodedNorms[ i + firstVert ] );
  247. return gNormalStore.address();
  248. }
  249. return &norms[firstVert];
  250. }
  251. //-----------------------------------------------------
  252. // TSMesh collision methods
  253. //-----------------------------------------------------
  254. bool TSMesh::buildPolyList( S32 frame, AbstractPolyList *polyList, U32 &surfaceKey, TSMaterialList *materials )
  255. {
  256. S32 firstVert = vertsPerFrame * frame, i, base = 0;
  257. bool hasTVert2 = getHasTVert2();
  258. // add the verts...
  259. if ( vertsPerFrame )
  260. {
  261. if ( mVertexData.isReady() )
  262. {
  263. OptimizedPolyList* opList = dynamic_cast<OptimizedPolyList*>(polyList);
  264. if ( opList )
  265. {
  266. base = opList->mVertexList.size();
  267. for ( i = 0; i < vertsPerFrame; i++ )
  268. {
  269. // Don't use vertex() method as we want to retain the original indices
  270. OptimizedPolyList::VertIndex vert;
  271. vert.vertIdx = opList->insertPoint( mVertexData.getBase( i + firstVert ).vert() );
  272. vert.normalIdx = opList->insertNormal( mVertexData.getBase( i + firstVert ).normal() );
  273. vert.uv0Idx = opList->insertUV0( mVertexData.getBase( i + firstVert ).tvert() );
  274. if ( hasTVert2 )
  275. vert.uv1Idx = opList->insertUV1( mVertexData.getColor( i + firstVert ).tvert2() );
  276. opList->mVertexList.push_back( vert );
  277. }
  278. }
  279. else
  280. {
  281. base = polyList->addPointAndNormal( mVertexData.getBase( firstVert ).vert(), mVertexData.getBase( firstVert ).normal() );
  282. for ( i = 1; i < vertsPerFrame; i++ )
  283. {
  284. polyList->addPointAndNormal( mVertexData.getBase( i + firstVert ).vert(), mVertexData.getBase( i + firstVert ).normal() );
  285. }
  286. }
  287. }
  288. else
  289. {
  290. OptimizedPolyList* opList = dynamic_cast<OptimizedPolyList*>(polyList);
  291. if ( opList )
  292. {
  293. base = opList->mVertexList.size();
  294. for ( i = 0; i < vertsPerFrame; i++ )
  295. {
  296. // Don't use vertex() method as we want to retain the original indices
  297. OptimizedPolyList::VertIndex vert;
  298. vert.vertIdx = opList->insertPoint( verts[ i + firstVert ] );
  299. vert.normalIdx = opList->insertNormal( norms[ i + firstVert ] );
  300. vert.uv0Idx = opList->insertUV0( tverts[ i + firstVert ] );
  301. if ( hasTVert2 )
  302. vert.uv1Idx = opList->insertUV1( tverts2[ i + firstVert ] );
  303. opList->mVertexList.push_back( vert );
  304. }
  305. }
  306. else
  307. {
  308. base = polyList->addPointAndNormal( verts[firstVert], norms[firstVert] );
  309. for ( i = 1; i < vertsPerFrame; i++ )
  310. polyList->addPointAndNormal( verts[ i + firstVert ], norms[ i + firstVert ] );
  311. }
  312. }
  313. }
  314. // add the polys...
  315. for ( i = 0; i < primitives.size(); i++ )
  316. {
  317. TSDrawPrimitive & draw = primitives[i];
  318. U32 start = draw.start;
  319. AssertFatal( draw.matIndex & TSDrawPrimitive::Indexed,"TSMesh::buildPolyList (1)" );
  320. U32 matIndex = draw.matIndex & TSDrawPrimitive::MaterialMask;
  321. BaseMatInstance* material = ( materials ? materials->getMaterialInst( matIndex ) : 0 );
  322. // gonna depend on what kind of primitive it is...
  323. if ( (draw.matIndex & TSDrawPrimitive::TypeMask) == TSDrawPrimitive::Triangles )
  324. {
  325. for ( S32 j = 0; j < draw.numElements; )
  326. {
  327. U32 idx0 = base + indices[start + j + 0];
  328. U32 idx1 = base + indices[start + j + 1];
  329. U32 idx2 = base + indices[start + j + 2];
  330. polyList->begin(material,surfaceKey++);
  331. polyList->vertex( idx0 );
  332. polyList->vertex( idx1 );
  333. polyList->vertex( idx2 );
  334. polyList->plane( idx0, idx1, idx2 );
  335. polyList->end();
  336. j += 3;
  337. }
  338. }
  339. else
  340. {
  341. AssertFatal( (draw.matIndex & TSDrawPrimitive::TypeMask) == TSDrawPrimitive::Strip,"TSMesh::buildPolyList (2)" );
  342. U32 idx0 = base + indices[start + 0];
  343. U32 idx1;
  344. U32 idx2 = base + indices[start + 1];
  345. U32 * nextIdx = &idx1;
  346. for ( S32 j = 2; j < draw.numElements; j++ )
  347. {
  348. *nextIdx = idx2;
  349. // nextIdx = (j%2)==0 ? &idx0 : &idx1;
  350. nextIdx = (U32*) ( (dsize_t)nextIdx ^ (dsize_t)&idx0 ^ (dsize_t)&idx1);
  351. idx2 = base + indices[start + j];
  352. if ( idx0 == idx1 || idx0 == idx2 || idx1 == idx2 )
  353. continue;
  354. polyList->begin( material, surfaceKey++ );
  355. polyList->vertex( idx0 );
  356. polyList->vertex( idx1 );
  357. polyList->vertex( idx2 );
  358. polyList->plane( idx0, idx1, idx2 );
  359. polyList->end();
  360. }
  361. }
  362. }
  363. return true;
  364. }
  365. bool TSMesh::getFeatures( S32 frame, const MatrixF& mat, const VectorF&, ConvexFeature* cf, U32& )
  366. {
  367. S32 firstVert = vertsPerFrame * frame;
  368. S32 i;
  369. S32 base = cf->mVertexList.size();
  370. for ( i = 0; i < vertsPerFrame; i++ )
  371. {
  372. cf->mVertexList.increment();
  373. mat.mulP( mVertexData.getBase(firstVert + i).vert(), &cf->mVertexList.last() );
  374. }
  375. // add the polys...
  376. for ( i = 0; i < primitives.size(); i++ )
  377. {
  378. TSDrawPrimitive & draw = primitives[i];
  379. U32 start = draw.start;
  380. AssertFatal( draw.matIndex & TSDrawPrimitive::Indexed,"TSMesh::buildPolyList (1)" );
  381. // gonna depend on what kind of primitive it is...
  382. if ( (draw.matIndex & TSDrawPrimitive::TypeMask) == TSDrawPrimitive::Triangles)
  383. {
  384. for ( S32 j = 0; j < draw.numElements; j += 3 )
  385. {
  386. PlaneF plane( cf->mVertexList[base + indices[start + j + 0]],
  387. cf->mVertexList[base + indices[start + j + 1]],
  388. cf->mVertexList[base + indices[start + j + 2]]);
  389. cf->mFaceList.increment();
  390. ConvexFeature::Face& lastFace = cf->mFaceList.last();
  391. lastFace.normal = plane;
  392. lastFace.vertex[0] = base + indices[start + j + 0];
  393. lastFace.vertex[1] = base + indices[start + j + 1];
  394. lastFace.vertex[2] = base + indices[start + j + 2];
  395. for ( U32 l = 0; l < 3; l++ )
  396. {
  397. U32 newEdge0, newEdge1;
  398. U32 zero = base + indices[start + j + l];
  399. U32 one = base + indices[start + j + ((l+1)%3)];
  400. newEdge0 = getMin( zero, one );
  401. newEdge1 = getMax( zero, one );
  402. bool found = false;
  403. for ( S32 k = 0; k < cf->mEdgeList.size(); k++ )
  404. {
  405. if ( cf->mEdgeList[k].vertex[0] == newEdge0 &&
  406. cf->mEdgeList[k].vertex[1] == newEdge1)
  407. {
  408. found = true;
  409. break;
  410. }
  411. }
  412. if ( !found )
  413. {
  414. cf->mEdgeList.increment();
  415. cf->mEdgeList.last().vertex[0] = newEdge0;
  416. cf->mEdgeList.last().vertex[1] = newEdge1;
  417. }
  418. }
  419. }
  420. }
  421. else
  422. {
  423. AssertFatal( (draw.matIndex & TSDrawPrimitive::TypeMask) == TSDrawPrimitive::Strip,"TSMesh::buildPolyList (2)" );
  424. U32 idx0 = base + indices[start + 0];
  425. U32 idx1;
  426. U32 idx2 = base + indices[start + 1];
  427. U32 * nextIdx = &idx1;
  428. for ( S32 j = 2; j < draw.numElements; j++ )
  429. {
  430. *nextIdx = idx2;
  431. nextIdx = (U32*) ( (dsize_t)nextIdx ^ (dsize_t)&idx0 ^ (dsize_t)&idx1);
  432. idx2 = base + indices[start + j];
  433. if ( idx0 == idx1 || idx0 == idx2 || idx1 == idx2 )
  434. continue;
  435. PlaneF plane( cf->mVertexList[idx0],
  436. cf->mVertexList[idx1],
  437. cf->mVertexList[idx2] );
  438. cf->mFaceList.increment();
  439. cf->mFaceList.last().normal = plane;
  440. cf->mFaceList.last().vertex[0] = idx0;
  441. cf->mFaceList.last().vertex[1] = idx1;
  442. cf->mFaceList.last().vertex[2] = idx2;
  443. U32 newEdge0, newEdge1;
  444. newEdge0 = getMin( idx0, idx1 );
  445. newEdge1 = getMax( idx0, idx1 );
  446. bool found = false;
  447. S32 k;
  448. for ( k = 0; k < cf->mEdgeList.size(); k++ )
  449. {
  450. ConvexFeature::Edge currentEdge = cf->mEdgeList[k];
  451. if (currentEdge.vertex[0] == newEdge0 &&
  452. currentEdge.vertex[1] == newEdge1)
  453. {
  454. found = true;
  455. break;
  456. }
  457. }
  458. if ( !found )
  459. {
  460. cf->mEdgeList.increment();
  461. cf->mEdgeList.last().vertex[0] = newEdge0;
  462. cf->mEdgeList.last().vertex[1] = newEdge1;
  463. }
  464. newEdge0 = getMin( idx1, idx2 );
  465. newEdge1 = getMax( idx1, idx2 );
  466. found = false;
  467. for ( k = 0; k < cf->mEdgeList.size(); k++ )
  468. {
  469. if ( cf->mEdgeList[k].vertex[0] == newEdge0 &&
  470. cf->mEdgeList[k].vertex[1] == newEdge1 )
  471. {
  472. found = true;
  473. break;
  474. }
  475. }
  476. if ( !found )
  477. {
  478. cf->mEdgeList.increment();
  479. cf->mEdgeList.last().vertex[0] = newEdge0;
  480. cf->mEdgeList.last().vertex[1] = newEdge1;
  481. }
  482. newEdge0 = getMin(idx0, idx2);
  483. newEdge1 = getMax(idx0, idx2);
  484. found = false;
  485. for ( k = 0; k < cf->mEdgeList.size(); k++ )
  486. {
  487. if ( cf->mEdgeList[k].vertex[0] == newEdge0 &&
  488. cf->mEdgeList[k].vertex[1] == newEdge1 )
  489. {
  490. found = true;
  491. break;
  492. }
  493. }
  494. if ( !found )
  495. {
  496. cf->mEdgeList.increment();
  497. cf->mEdgeList.last().vertex[0] = newEdge0;
  498. cf->mEdgeList.last().vertex[1] = newEdge1;
  499. }
  500. }
  501. }
  502. }
  503. return false;
  504. }
  505. void TSMesh::support( S32 frame, const Point3F &v, F32 *currMaxDP, Point3F *currSupport )
  506. {
  507. if ( vertsPerFrame == 0 )
  508. return;
  509. U32 waterMark = FrameAllocator::getWaterMark();
  510. F32* pDots = (F32*)FrameAllocator::alloc( sizeof(F32) * vertsPerFrame );
  511. S32 firstVert = vertsPerFrame * frame;
  512. m_point3F_bulk_dot( &v.x,
  513. &mVertexData.getBase(firstVert).vert().x,
  514. vertsPerFrame,
  515. mVertexData.vertSize(),
  516. pDots );
  517. F32 localdp = *currMaxDP;
  518. S32 index = -1;
  519. for ( S32 i = 0; i < vertsPerFrame; i++ )
  520. {
  521. if ( pDots[i] > localdp )
  522. {
  523. localdp = pDots[i];
  524. index = i;
  525. }
  526. }
  527. FrameAllocator::setWaterMark(waterMark);
  528. if ( index != -1 )
  529. {
  530. *currMaxDP = localdp;
  531. *currSupport = mVertexData.getBase(index + firstVert).vert();
  532. }
  533. }
  534. bool TSMesh::castRay( S32 frame, const Point3F & start, const Point3F & end, RayInfo * rayInfo, TSMaterialList* materials )
  535. {
  536. if ( planeNormals.empty() )
  537. buildConvexHull(); // if haven't done it yet...
  538. // Keep track of startTime and endTime. They start out at just under 0 and just over 1, respectively.
  539. // As we check against each plane, prune start and end times back to represent current intersection of
  540. // line with all the planes (or rather with all the half-spaces defined by the planes).
  541. // But, instead of explicitly keeping track of startTime and endTime, keep track as numerator and denominator
  542. // so that we can avoid as many divisions as possible.
  543. // F32 startTime = -0.01f;
  544. F32 startNum = -0.01f;
  545. F32 startDen = 1.00f;
  546. // F32 endTime = 1.01f;
  547. F32 endNum = 1.01f;
  548. F32 endDen = 1.00f;
  549. S32 curPlane = 0;
  550. U32 curMaterial = 0;
  551. bool found = false;
  552. // the following block of code is an optimization...
  553. // it isn't necessary if the longer version of the main loop is used
  554. bool tmpFound;
  555. S32 tmpPlane;
  556. F32 sgn = -1.0f;
  557. F32 * pnum = &startNum;
  558. F32 * pden = &startDen;
  559. S32 * pplane = &curPlane;
  560. bool * pfound = &found;
  561. S32 startPlane = frame * planesPerFrame;
  562. for ( S32 i = startPlane; i < startPlane + planesPerFrame; i++ )
  563. {
  564. // if start & end outside, no collision
  565. // if start & end inside, continue
  566. // if start outside, end inside, or visa versa, find intersection of line with plane
  567. // then update intersection of line with hull (using startTime and endTime)
  568. F32 dot1 = mDot( planeNormals[i], start ) - planeConstants[i];
  569. F32 dot2 = mDot( planeNormals[i], end) - planeConstants[i];
  570. if ( dot1 * dot2 > 0.0f )
  571. {
  572. // same side of the plane...which side -- dot==0 considered inside
  573. if ( dot1 > 0.0f )
  574. return false; // start and end outside of this plane, no collision
  575. // start and end inside plane, continue
  576. continue;
  577. }
  578. //AssertFatal( dot1 / ( dot1 - dot2 ) >= 0.0f && dot1 / ( dot1 - dot2 ) <= 1.0f,"TSMesh::castRay (1)" );
  579. // find intersection (time) with this plane...
  580. // F32 time = dot1 / (dot1-dot2);
  581. F32 num = mFabs( dot1 );
  582. F32 den = mFabs( dot1 - dot2 );
  583. // the following block of code is an optimized version...
  584. // this can be commented out and the following block of code used instead
  585. // if debugging a problem in this code, that should probably be done
  586. // if you want to see how this works, look at the following block of code,
  587. // not this one...
  588. // Note that this does not get optimized appropriately...it is included this way
  589. // as an idea for future optimization.
  590. if ( sgn * dot1 >= 0 )
  591. {
  592. sgn *= -1.0f;
  593. pnum = (F32*) ((dsize_t)pnum ^ (dsize_t)&endNum ^ (dsize_t)&startNum);
  594. pden = (F32*) ((dsize_t)pden ^ (dsize_t)&endDen ^ (dsize_t)&startDen);
  595. pplane = (S32*) ((dsize_t)pplane ^ (dsize_t)&tmpPlane ^ (dsize_t)&curPlane);
  596. pfound = (bool*) ((dsize_t)pfound ^ (dsize_t)&tmpFound ^ (dsize_t)&found);
  597. }
  598. bool noCollision = num * endDen * sgn < endNum * den * sgn && num * startDen * sgn < startNum * den * sgn;
  599. if (num * *pden * sgn < *pnum * den * sgn && !noCollision)
  600. {
  601. *pnum = num;
  602. *pden = den;
  603. *pplane = i;
  604. *pfound = true;
  605. }
  606. else if ( noCollision )
  607. return false;
  608. // if (dot1<=0.0f)
  609. // {
  610. // // start is inside plane, end is outside...chop off end
  611. // if (num*endDen<endNum*den) // if (time<endTime)
  612. // {
  613. // if (num*startDen<startNum*den) //if (time<startTime)
  614. // // no intersection of line and hull
  615. // return false;
  616. // // endTime = time;
  617. // endNum = num;
  618. // endDen = den;
  619. // }
  620. // // else, no need to do anything, just continue (we've been more inside than this)
  621. // }
  622. // else // dot2<=0.0f
  623. // {
  624. // // end is inside poly, start is outside...chop off start
  625. // AssertFatal(dot2<=0.0f,"TSMesh::castRay (2)");
  626. // if (num*startDen>startNum*den) // if (time>startTime)
  627. // {
  628. // if (num*endDen>endNum*den) //if (time>endTime)
  629. // // no intersection of line and hull
  630. // return false;
  631. // // startTime = time;
  632. // startNum = num;
  633. // startDen = den;
  634. // curPlane = i;
  635. // curMaterial = planeMaterials[i-startPlane];
  636. // found = true;
  637. // }
  638. // // else, no need to do anything, just continue (we've been more inside than this)
  639. // }
  640. }
  641. // setup rayInfo
  642. if ( found && rayInfo )
  643. {
  644. curMaterial = planeMaterials[ curPlane - startPlane ];
  645. rayInfo->t = (F32)startNum/(F32)startDen; // finally divide...
  646. rayInfo->normal = planeNormals[curPlane];
  647. if (materials && materials->size() > 0)
  648. rayInfo->material = materials->getMaterialInst( curMaterial );
  649. else
  650. rayInfo->material = NULL;
  651. rayInfo->setContactPoint( start, end );
  652. return true;
  653. }
  654. else if ( found )
  655. return true;
  656. // only way to get here is if start is inside hull...
  657. // we could return null and just plug in garbage for the material and normal...
  658. return false;
  659. }
  660. bool TSMesh::castRayRendered( S32 frame, const Point3F & start, const Point3F & end, RayInfo * rayInfo, TSMaterialList* materials )
  661. {
  662. if( vertsPerFrame <= 0 )
  663. return false;
  664. if( mNumVerts == 0 )
  665. return false;
  666. S32 firstVert = vertsPerFrame * frame;
  667. bool found = false;
  668. F32 best_t = F32_MAX;
  669. U32 bestIdx0 = 0, bestIdx1 = 0, bestIdx2 = 0;
  670. BaseMatInstance* bestMaterial = NULL;
  671. Point3F dir = end - start;
  672. for ( S32 i = 0; i < primitives.size(); i++ )
  673. {
  674. TSDrawPrimitive & draw = primitives[i];
  675. U32 drawStart = draw.start;
  676. AssertFatal( draw.matIndex & TSDrawPrimitive::Indexed,"TSMesh::castRayRendered (1)" );
  677. U32 matIndex = draw.matIndex & TSDrawPrimitive::MaterialMask;
  678. BaseMatInstance* material = ( materials ? materials->getMaterialInst( matIndex ) : 0 );
  679. U32 idx0, idx1, idx2;
  680. // gonna depend on what kind of primitive it is...
  681. if ( (draw.matIndex & TSDrawPrimitive::TypeMask) == TSDrawPrimitive::Triangles )
  682. {
  683. for ( S32 j = 0; j < draw.numElements-2; j += 3 )
  684. {
  685. idx0 = indices[drawStart + j + 0];
  686. idx1 = indices[drawStart + j + 1];
  687. idx2 = indices[drawStart + j + 2];
  688. F32 cur_t = 0;
  689. Point2F b;
  690. if(castRayTriangle(start, dir, mVertexData.getBase(firstVert + idx0).vert(),
  691. mVertexData.getBase(firstVert + idx1).vert(), mVertexData.getBase(firstVert + idx2).vert(), cur_t, b))
  692. {
  693. if(cur_t < best_t)
  694. {
  695. best_t = cur_t;
  696. bestIdx0 = idx0;
  697. bestIdx1 = idx1;
  698. bestIdx2 = idx2;
  699. bestMaterial = material;
  700. found = true;
  701. }
  702. }
  703. }
  704. }
  705. else
  706. {
  707. AssertFatal( (draw.matIndex & TSDrawPrimitive::TypeMask) == TSDrawPrimitive::Strip,"TSMesh::castRayRendered (2)" );
  708. idx0 = indices[drawStart + 0];
  709. idx2 = indices[drawStart + 1];
  710. U32 * nextIdx = &idx1;
  711. for ( S32 j = 2; j < draw.numElements; j++ )
  712. {
  713. *nextIdx = idx2;
  714. // nextIdx = (j%2)==0 ? &idx0 : &idx1;
  715. nextIdx = (U32*) ( (dsize_t)nextIdx ^ (dsize_t)&idx0 ^ (dsize_t)&idx1);
  716. idx2 = indices[drawStart + j];
  717. if ( idx0 == idx1 || idx0 == idx2 || idx1 == idx2 )
  718. continue;
  719. F32 cur_t = 0;
  720. Point2F b;
  721. if(castRayTriangle(start, dir, mVertexData.getBase(firstVert + idx0).vert(),
  722. mVertexData.getBase(firstVert + idx1).vert(), mVertexData.getBase(firstVert + idx2).vert(), cur_t, b))
  723. {
  724. if(cur_t < best_t)
  725. {
  726. best_t = cur_t;
  727. bestIdx0 = firstVert + idx0;
  728. bestIdx1 = firstVert + idx1;
  729. bestIdx2 = firstVert + idx2;
  730. bestMaterial = material;
  731. found = true;
  732. }
  733. }
  734. }
  735. }
  736. }
  737. // setup rayInfo
  738. if ( found && rayInfo )
  739. {
  740. rayInfo->t = best_t;
  741. Point3F normal;
  742. mCross(mVertexData.getBase(bestIdx2).vert()-mVertexData.getBase(bestIdx0).vert(),mVertexData.getBase(bestIdx1).vert()-mVertexData.getBase(bestIdx0).vert(),&normal);
  743. if ( mDot( normal, normal ) < 0.001f )
  744. {
  745. mCross( mVertexData.getBase(bestIdx0).vert() - mVertexData.getBase(bestIdx1).vert(), mVertexData.getBase(bestIdx2).vert() - mVertexData.getBase(bestIdx1).vert(), &normal );
  746. if ( mDot( normal, normal ) < 0.001f )
  747. {
  748. mCross( mVertexData.getBase(bestIdx1).vert() - mVertexData.getBase(bestIdx2).vert(), mVertexData.getBase(bestIdx0).vert() - mVertexData.getBase(bestIdx2).vert(), &normal );
  749. }
  750. }
  751. normal.normalize();
  752. rayInfo->normal = normal;
  753. rayInfo->material = bestMaterial;
  754. rayInfo->setContactPoint( start, end );
  755. return true;
  756. }
  757. else if ( found )
  758. return true;
  759. return false;
  760. }
  761. bool TSMesh::addToHull( U32 idx0, U32 idx1, U32 idx2 )
  762. {
  763. // calculate the normal of this triangle... remember, we lose precision
  764. // when we subtract two large numbers that are very close to each other,
  765. // so depending on how we calculate the normal, we could get a
  766. // different result. so, we will calculate the normal three different
  767. // ways and take the one that gives us the largest vector before we
  768. // normalize.
  769. Point3F normal1, normal2, normal3;
  770. const Point3F& vertex0Data = mVertexData.getBase(idx0).vert();
  771. const Point3F& vertex1Data = mVertexData.getBase(idx1).vert();
  772. const Point3F& vertex2Data = mVertexData.getBase(idx2).vert();
  773. mCross(vertex2Data-vertex0Data,vertex1Data-vertex0Data,&normal1);
  774. mCross(vertex0Data-vertex1Data,vertex2Data-vertex1Data,&normal2);
  775. mCross(vertex1Data-vertex2Data,vertex0Data-vertex2Data,&normal3);
  776. Point3F normal = normal1;
  777. F32 greatestMagSquared = mDot(normal1, normal1);
  778. F32 magSquared = mDot(normal2, normal2);
  779. if (magSquared > greatestMagSquared)
  780. {
  781. normal = normal2;
  782. greatestMagSquared = magSquared;
  783. }
  784. magSquared = mDot(normal3, normal3);
  785. if (magSquared > greatestMagSquared)
  786. {
  787. normal = normal3;
  788. greatestMagSquared = magSquared;
  789. }
  790. if (mDot(normal, normal) < 0.00000001f)
  791. return false;
  792. normal.normalize();
  793. F32 k = mDot( normal, mVertexData.getBase(idx0).vert() );
  794. for ( S32 i = 0; i < planeNormals.size(); i++ )
  795. {
  796. if ( mDot( planeNormals[i], normal ) > 0.99f && mFabs( k-planeConstants[i] ) < 0.01f )
  797. return false; // this is a repeat...
  798. }
  799. // new plane, add it to the list...
  800. planeNormals.push_back( normal );
  801. planeConstants.push_back( k );
  802. return true;
  803. }
  804. bool TSMesh::buildConvexHull()
  805. {
  806. // already done, return without error
  807. if ( planeNormals.size() )
  808. return true;
  809. bool error = false;
  810. // should probably only have 1 frame, but just in case...
  811. planesPerFrame = 0;
  812. S32 frame, i, j;
  813. for ( frame = 0; frame < numFrames; frame++ )
  814. {
  815. S32 firstVert = vertsPerFrame * frame;
  816. S32 firstPlane = planeNormals.size();
  817. for ( i = 0; i < primitives.size(); i++ )
  818. {
  819. TSDrawPrimitive & draw = primitives[i];
  820. U32 start = draw.start;
  821. AssertFatal( draw.matIndex & TSDrawPrimitive::Indexed,"TSMesh::buildConvexHull (1)" );
  822. // gonna depend on what kind of primitive it is...
  823. if ( (draw.matIndex & TSDrawPrimitive::TypeMask) == TSDrawPrimitive::Triangles )
  824. {
  825. for ( j = 0; j < draw.numElements; j += 3 )
  826. if ( addToHull( indices[start + j + 0] + firstVert,
  827. indices[start + j + 1] + firstVert,
  828. indices[start + j + 2] + firstVert ) && frame == 0 )
  829. planeMaterials.push_back( draw.matIndex & TSDrawPrimitive::MaterialMask );
  830. }
  831. else
  832. {
  833. AssertFatal( (draw.matIndex&TSDrawPrimitive::Strip) == TSDrawPrimitive::Strip,"TSMesh::buildConvexHull (2)" );
  834. U32 idx0 = indices[start + 0] + firstVert;
  835. U32 idx1;
  836. U32 idx2 = indices[start + 1] + firstVert;
  837. U32 * nextIdx = &idx1;
  838. for ( j = 2; j < draw.numElements; j++ )
  839. {
  840. *nextIdx = idx2;
  841. // nextIdx = (j%2)==0 ? &idx0 : &idx1;
  842. nextIdx = (U32*) ( (dsize_t)nextIdx ^ (dsize_t)&idx0 ^ (dsize_t)&idx1 );
  843. idx2 = indices[start + j] + firstVert;
  844. if ( addToHull( idx0, idx1, idx2 ) && frame == 0 )
  845. planeMaterials.push_back( draw.matIndex & TSDrawPrimitive::MaterialMask );
  846. }
  847. }
  848. }
  849. // make sure all the verts on this frame are inside all the planes
  850. for ( i = 0; i < vertsPerFrame; i++ )
  851. for ( j = firstPlane; j < planeNormals.size(); j++ )
  852. if ( mDot( mVertexData.getBase(firstVert + i).vert(), planeNormals[j] ) - planeConstants[j] < 0.01 ) // .01 == a little slack
  853. error = true;
  854. if ( frame == 0 )
  855. planesPerFrame = planeNormals.size();
  856. if ( (frame + 1) * planesPerFrame != planeNormals.size() )
  857. {
  858. // eek, not all frames have same number of planes...
  859. while ( (frame + 1) * planesPerFrame > planeNormals.size() )
  860. {
  861. // we're short, duplicate last plane till we match
  862. U32 sz = planeNormals.size();
  863. planeNormals.increment();
  864. planeNormals.last() = planeNormals[sz-1];
  865. planeConstants.increment();
  866. planeConstants.last() = planeConstants[sz-1];
  867. }
  868. while ( (frame + 1) * planesPerFrame < planeNormals.size() )
  869. {
  870. // harsh -- last frame has more than other frames
  871. // duplicate last plane in each frame
  872. for ( S32 k = frame - 1; k >= 0; k-- )
  873. {
  874. planeNormals.insert( k * planesPerFrame + planesPerFrame );
  875. planeNormals[k * planesPerFrame + planesPerFrame] = planeNormals[k * planesPerFrame + planesPerFrame - 1];
  876. planeConstants.insert( k * planesPerFrame + planesPerFrame );
  877. planeConstants[k * planesPerFrame + planesPerFrame] = planeConstants[k * planesPerFrame + planesPerFrame - 1];
  878. if ( k == 0 )
  879. {
  880. planeMaterials.increment();
  881. planeMaterials.last() = planeMaterials[planeMaterials.size() - 2];
  882. }
  883. }
  884. planesPerFrame++;
  885. }
  886. }
  887. AssertFatal( (frame + 1) * planesPerFrame == planeNormals.size(),"TSMesh::buildConvexHull (3)" );
  888. }
  889. return !error;
  890. }
  891. //-----------------------------------------------------
  892. // TSMesh bounds methods
  893. //-----------------------------------------------------
  894. void TSMesh::computeBounds()
  895. {
  896. MatrixF mat(true);
  897. computeBounds( mat, mBounds, -1, &mCenter, &mRadius );
  898. }
  899. void TSMesh::computeBounds( const MatrixF &transform, Box3F &bounds, S32 frame, Point3F *center, F32 *radius )
  900. {
  901. const Point3F *baseVert = NULL;
  902. S32 stride = 0;
  903. S32 numVerts = 0;
  904. AssertFatal(!mVertexData.isReady() || (mVertexData.isReady() && mNumVerts == mVertexData.size() && mNumVerts == vertsPerFrame), "vertex number mismatch");
  905. if(verts.size() == 0 && mVertexData.isReady() && mVertexData.size() > 0)
  906. {
  907. baseVert = &mVertexData.getBase(0).vert();
  908. stride = mVertexData.vertSize();
  909. if ( frame < 0 )
  910. numVerts = mNumVerts;
  911. else
  912. {
  913. baseVert = &mVertexData.getBase(frame * vertsPerFrame).vert();
  914. numVerts = vertsPerFrame;
  915. }
  916. }
  917. else
  918. {
  919. baseVert = verts.address();
  920. stride = sizeof(Point3F);
  921. if ( frame < 0 )
  922. numVerts = verts.size();
  923. else
  924. {
  925. baseVert += frame * vertsPerFrame;
  926. numVerts = vertsPerFrame;
  927. }
  928. }
  929. computeBounds( baseVert, numVerts, stride, transform, bounds, center, radius );
  930. }
  931. void TSMesh::computeBounds( const Point3F *v, S32 numVerts, S32 stride, const MatrixF &transform, Box3F &bounds, Point3F *center, F32 *radius )
  932. {
  933. const U8 *_vb = reinterpret_cast<const U8 *>(v);
  934. if ( !numVerts )
  935. {
  936. bounds.minExtents = Point3F::Zero;
  937. bounds.maxExtents = Point3F::Zero;
  938. if ( center )
  939. *center = Point3F::Zero;
  940. if ( radius )
  941. *radius = 0;
  942. return;
  943. }
  944. S32 i;
  945. Point3F p;
  946. transform.mulP( *v, &bounds.minExtents );
  947. bounds.maxExtents = bounds.minExtents;
  948. for ( i = 0; i < numVerts; i++ )
  949. {
  950. const Point3F &curVert = *reinterpret_cast<const Point3F *>(_vb + i * stride);
  951. transform.mulP( curVert, &p );
  952. bounds.maxExtents.setMax( p );
  953. bounds.minExtents.setMin( p );
  954. }
  955. Point3F c;
  956. if ( !center )
  957. center = &c;
  958. center->x = 0.5f * (bounds.minExtents.x + bounds.maxExtents.x);
  959. center->y = 0.5f * (bounds.minExtents.y + bounds.maxExtents.y);
  960. center->z = 0.5f * (bounds.minExtents.z + bounds.maxExtents.z);
  961. if ( radius )
  962. {
  963. *radius = 0.0f;
  964. for ( i = 0; i < numVerts; i++ )
  965. {
  966. const Point3F &curVert = *reinterpret_cast<const Point3F *>(_vb + i * stride);
  967. transform.mulP( curVert, &p );
  968. p -= *center;
  969. *radius = getMax( *radius, mDot( p, p ) );
  970. }
  971. *radius = mSqrt( *radius );
  972. }
  973. }
  974. //-----------------------------------------------------
  975. S32 TSMesh::getNumPolys() const
  976. {
  977. S32 count = 0;
  978. for ( S32 i = 0; i < primitives.size(); i++ )
  979. {
  980. switch (primitives[i].matIndex & TSDrawPrimitive::TypeMask)
  981. {
  982. case TSDrawPrimitive::Triangles:
  983. count += primitives[i].numElements / 3;
  984. break;
  985. case TSDrawPrimitive::Fan:
  986. count += primitives[i].numElements - 2;
  987. break;
  988. case TSDrawPrimitive::Strip:
  989. // Don't count degenerate triangles
  990. for ( S32 j = primitives[i].start;
  991. j < primitives[i].start+primitives[i].numElements-2;
  992. j++ )
  993. {
  994. if ((indices[j] != indices[j+1]) &&
  995. (indices[j] != indices[j+2]) &&
  996. (indices[j+1] != indices[j+2]))
  997. count++;
  998. }
  999. break;
  1000. }
  1001. }
  1002. return count;
  1003. }
  1004. //-----------------------------------------------------
  1005. TSMesh::TSMesh() : meshType( StandardMeshType )
  1006. {
  1007. VECTOR_SET_ASSOCIATION( planeNormals );
  1008. VECTOR_SET_ASSOCIATION( planeConstants );
  1009. VECTOR_SET_ASSOCIATION( planeMaterials );
  1010. parentMesh = -1;
  1011. mOptTree = NULL;
  1012. mOpMeshInterface = NULL;
  1013. mOpTris = NULL;
  1014. mOpPoints = NULL;
  1015. mVisibility = 1.0f;
  1016. mNumVerts = 0;
  1017. mVertSize = 0;
  1018. mVertOffset = 0;
  1019. parentMeshObject = NULL;
  1020. }
  1021. //-----------------------------------------------------
  1022. // TSMesh destructor
  1023. //-----------------------------------------------------
  1024. TSMesh::~TSMesh()
  1025. {
  1026. SAFE_DELETE( mOptTree );
  1027. SAFE_DELETE( mOpMeshInterface );
  1028. SAFE_DELETE_ARRAY( mOpTris );
  1029. SAFE_DELETE_ARRAY( mOpPoints );
  1030. mNumVerts = 0;
  1031. }
  1032. //-----------------------------------------------------
  1033. // TSSkinMesh methods
  1034. //-----------------------------------------------------
  1035. void TSSkinMesh::updateSkinBuffer( const Vector<MatrixF> &transforms, U8* buffer )
  1036. {
  1037. PROFILE_SCOPE(TSSkinMesh_UpdateSkinBuffer);
  1038. AssertFatal(batchData.initialized, "Batch data not initialized. Call createSkinBatchData() before any skin update is called.");
  1039. if (TSShape::smUseHardwareSkinning || mNumVerts == 0)
  1040. return;
  1041. const MatrixF *matrices = NULL;
  1042. static Vector<MatrixF> sBoneTransforms;
  1043. sBoneTransforms.setSize(batchData.nodeIndex.size());
  1044. // set up bone transforms
  1045. PROFILE_START(TSSkinMesh_UpdateTransforms);
  1046. for (S32 i = 0; i < batchData.nodeIndex.size(); i++)
  1047. {
  1048. S32 node = batchData.nodeIndex[i];
  1049. sBoneTransforms[i].mul(transforms[node], batchData.initialTransforms[i]);
  1050. }
  1051. matrices = &sBoneTransforms[0];
  1052. PROFILE_END();
  1053. const Point3F *inVerts = batchData.initialVerts.address();
  1054. const Point3F *inNorms = batchData.initialNorms.address();
  1055. AssertFatal(inVerts, "Something went wrong, verts should be valid");
  1056. U8 *dest = buffer + mVertOffset;
  1057. if (!dest)
  1058. return;
  1059. Point3F srcVtx, srcNrm;
  1060. AssertFatal(batchData.vertexBatchOperations.size() == batchData.initialVerts.size(), "Assumption failed!");
  1061. Point3F skinnedVert;
  1062. Point3F skinnedNorm;
  1063. for (Vector<BatchData::BatchedVertex>::const_iterator itr = batchData.vertexBatchOperations.begin();
  1064. itr != batchData.vertexBatchOperations.end(); itr++)
  1065. {
  1066. const BatchData::BatchedVertex &curVert = *itr;
  1067. skinnedVert.zero();
  1068. skinnedNorm.zero();
  1069. for (S32 tOp = 0; tOp < curVert.transformCount; tOp++)
  1070. {
  1071. const BatchData::TransformOp &transformOp = curVert.transform[tOp];
  1072. const MatrixF& deltaTransform = matrices[transformOp.transformIndex];
  1073. deltaTransform.mulP(inVerts[curVert.vertexIndex], &srcVtx);
  1074. skinnedVert += (srcVtx * transformOp.weight);
  1075. deltaTransform.mulV(inNorms[curVert.vertexIndex], &srcNrm);
  1076. skinnedNorm += srcNrm * transformOp.weight;
  1077. }
  1078. // Assign results
  1079. __TSMeshVertexBase *dvert = (__TSMeshVertexBase*)(dest + (mVertSize * curVert.vertexIndex));
  1080. dvert->vert(skinnedVert);
  1081. dvert->normal(skinnedNorm);
  1082. }
  1083. }
  1084. void TSSkinMesh::updateSkinBones( const Vector<MatrixF> &transforms, Vector<MatrixF>& destTransforms )
  1085. {
  1086. // Update transforms for current mesh
  1087. destTransforms.setSize(batchData.nodeIndex.size());
  1088. for (int i = 0; i<batchData.nodeIndex.size(); i++)
  1089. {
  1090. S32 node = batchData.nodeIndex[i];
  1091. if (node >= transforms.size())
  1092. continue; // jamesu - ignore obviously invalid data
  1093. destTransforms[i].mul(transforms[node], batchData.initialTransforms[i]);
  1094. }
  1095. }
  1096. void TSSkinMesh::createSkinBatchData()
  1097. {
  1098. if(batchData.initialized)
  1099. return;
  1100. batchData.initialized = true;
  1101. S32 * curVtx = vertexIndex.begin();
  1102. S32 * curBone = boneIndex.begin();
  1103. F32 * curWeight = weight.begin();
  1104. const S32 * endVtx = vertexIndex.end();
  1105. AssertFatal(batchData.nodeIndex.size() <= TSShape::smMaxSkinBones, "Too many bones are here!!!");
  1106. // Temp vector to build batch operations
  1107. Vector<BatchData::BatchedVertex> batchOperations;
  1108. bool issuedWeightWarning = false;
  1109. if (mVertexData.isReady())
  1110. {
  1111. batchData.initialVerts.setSize(mNumVerts);
  1112. batchData.initialNorms.setSize(mNumVerts);
  1113. // Fill arrays
  1114. for (U32 i = 0; i < mNumVerts; i++)
  1115. {
  1116. const __TSMeshVertexBase &cv = mVertexData.getBase(i);
  1117. batchData.initialVerts[i] = cv.vert();
  1118. batchData.initialNorms[i] = cv.normal();
  1119. }
  1120. addWeightsFromVertexBuffer();
  1121. curVtx = vertexIndex.begin();
  1122. curBone = boneIndex.begin();
  1123. curWeight = weight.begin();
  1124. endVtx = vertexIndex.end();
  1125. }
  1126. else
  1127. {
  1128. batchData.initialNorms = norms;
  1129. batchData.initialVerts = verts;
  1130. }
  1131. // Build the batch operations
  1132. while( curVtx != endVtx )
  1133. {
  1134. const S32 vidx = *curVtx;
  1135. ++curVtx;
  1136. const S32 midx = *curBone;
  1137. ++curBone;
  1138. const F32 w = *curWeight;
  1139. ++curWeight;
  1140. // Ignore empty weights
  1141. if ( vidx < 0 || midx < 0 || w == 0 )
  1142. continue;
  1143. if( !batchOperations.empty() &&
  1144. batchOperations.last().vertexIndex == vidx )
  1145. {
  1146. AssertFatal( batchOperations.last().transformCount > 0, "Not sure how this happened!" );
  1147. S32 opIdx = batchOperations.last().transformCount++;
  1148. // Limit the number of weights per bone (keep the N largest influences)
  1149. if ( opIdx >= TSSkinMesh::BatchData::maxBonePerVert )
  1150. {
  1151. if ( !issuedWeightWarning )
  1152. {
  1153. issuedWeightWarning = true;
  1154. Con::warnf( "At least one vertex has too many bone weights - limiting "
  1155. "to the largest %d influences (see maxBonePerVert in tsMesh.h).",
  1156. TSSkinMesh::BatchData::maxBonePerVert );
  1157. }
  1158. // Too many weights => find and replace the smallest one
  1159. S32 minIndex = 0;
  1160. F32 minWeight = batchOperations.last().transform[0].weight;
  1161. for ( S32 i = 1; i < batchOperations.last().transformCount; i++ )
  1162. {
  1163. if ( batchOperations.last().transform[i].weight < minWeight )
  1164. {
  1165. minWeight = batchOperations.last().transform[i].weight;
  1166. minIndex = i;
  1167. }
  1168. }
  1169. opIdx = minIndex;
  1170. batchOperations.last().transformCount = TSSkinMesh::BatchData::maxBonePerVert;
  1171. }
  1172. batchOperations.last().transform[opIdx].transformIndex = midx;
  1173. batchOperations.last().transform[opIdx].weight = w;
  1174. }
  1175. else
  1176. {
  1177. batchOperations.increment();
  1178. batchOperations.last().vertexIndex = vidx;
  1179. batchOperations.last().transformCount = 1;
  1180. batchOperations.last().transform[0].transformIndex = midx;
  1181. batchOperations.last().transform[0].weight = w;
  1182. }
  1183. //Con::printf( "[%d] transform idx %d, weight %1.5f", vidx, midx, w );
  1184. }
  1185. //Con::printf("End skin update");
  1186. // Normalize vertex weights (force weights for each vert to sum to 1)
  1187. if ( issuedWeightWarning )
  1188. {
  1189. for ( S32 i = 0; i < batchOperations.size(); i++ )
  1190. {
  1191. BatchData::BatchedVertex& batchOp = batchOperations[i];
  1192. // Sum weights for this vertex
  1193. F32 invTotalWeight = 0;
  1194. for ( S32 j = 0; j < batchOp.transformCount; j++ )
  1195. invTotalWeight += batchOp.transform[j].weight;
  1196. // Then normalize the vertex weights
  1197. invTotalWeight = 1.0f / invTotalWeight;
  1198. for ( S32 j = 0; j < batchOp.transformCount; j++ )
  1199. batchOp.transform[j].weight *= invTotalWeight;
  1200. }
  1201. }
  1202. batchData.vertexBatchOperations.set(batchOperations.address(), batchOperations.size());
  1203. U32 maxValue = 0;
  1204. for (U32 i = 0; i<batchData.vertexBatchOperations.size(); i++)
  1205. {
  1206. maxValue = batchData.vertexBatchOperations[i].transformCount > maxValue ? batchData.vertexBatchOperations[i].transformCount : maxValue;
  1207. }
  1208. maxBones = maxValue;
  1209. }
  1210. void TSSkinMesh::setupVertexTransforms()
  1211. {
  1212. AssertFatal(mVertexData.vertSize() == mVertSize, "vert size mismatch");
  1213. // Generate the bone transforms for the verts
  1214. for( Vector<BatchData::BatchedVertex>::const_iterator itr = batchData.vertexBatchOperations.begin();
  1215. itr != batchData.vertexBatchOperations.end(); itr++ )
  1216. {
  1217. const BatchData::BatchedVertex &curTransform = *itr;
  1218. S32 i=0;
  1219. S32 j=0;
  1220. S32 transformsLeft = curTransform.transformCount;
  1221. // Set weights and indices in batches of 4
  1222. for( i = 0, j = 0; i < curTransform.transformCount; i += 4, j += 1 )
  1223. {
  1224. __TSMeshVertex_BoneData &v = mVertexData.getBone(curTransform.vertexIndex, j);
  1225. S32 vertsSet = transformsLeft > 4 ? 4 : transformsLeft;
  1226. __TSMeshIndex_List indices;
  1227. Point4F weights;
  1228. dMemset(&indices, '\0', sizeof(indices));
  1229. dMemset(&weights, '\0', sizeof(weights));
  1230. switch (vertsSet)
  1231. {
  1232. case 1:
  1233. indices.x = curTransform.transform[i+0].transformIndex;
  1234. weights.x = curTransform.transform[i+0].weight;
  1235. break;
  1236. case 2:
  1237. indices.x = curTransform.transform[i+0].transformIndex;
  1238. weights.x = curTransform.transform[i+0].weight;
  1239. indices.y = curTransform.transform[i+1].transformIndex;
  1240. weights.y = curTransform.transform[i+1].weight;
  1241. break;
  1242. case 3:
  1243. indices.x = curTransform.transform[i+0].transformIndex;
  1244. weights.x = curTransform.transform[i+0].weight;
  1245. indices.y = curTransform.transform[i+1].transformIndex;
  1246. weights.y = curTransform.transform[i+1].weight;
  1247. indices.z = curTransform.transform[i+2].transformIndex;
  1248. weights.z = curTransform.transform[i+2].weight;
  1249. break;
  1250. case 4:
  1251. indices.x = curTransform.transform[i+0].transformIndex;
  1252. weights.x = curTransform.transform[i+0].weight;
  1253. indices.y = curTransform.transform[i+1].transformIndex;
  1254. weights.y = curTransform.transform[i+1].weight;
  1255. indices.z = curTransform.transform[i+2].transformIndex;
  1256. weights.z = curTransform.transform[i+2].weight;
  1257. indices.w = curTransform.transform[i+3].transformIndex;
  1258. weights.w = curTransform.transform[i+3].weight;
  1259. break;
  1260. case 0:
  1261. default:
  1262. break;
  1263. }
  1264. v.index(indices);
  1265. v.weight(weights);
  1266. transformsLeft -= 4;
  1267. }
  1268. }
  1269. }
  1270. U32 TSSkinMesh::getMaxBonesPerVert()
  1271. {
  1272. return maxBones >= 0 ? maxBones : 0;
  1273. }
  1274. void TSSkinMesh::render( TSVertexBufferHandle &instanceVB )
  1275. {
  1276. innerRender(instanceVB, mPB);
  1277. }
  1278. void TSSkinMesh::render( TSMaterialList *materials,
  1279. const TSRenderState &rdata,
  1280. bool isSkinDirty,
  1281. const Vector<MatrixF> &transforms,
  1282. TSVertexBufferHandle &vertexBuffer,
  1283. const char *meshName )
  1284. {
  1285. PROFILE_SCOPE(TSSkinMesh_render);
  1286. if (mNumVerts == 0)
  1287. return;
  1288. // verify stuff first
  1289. AssertFatal(mVertexData.size() == mNumVerts, "Vert # mismatch");
  1290. AssertFatal((TSShape::smUseHardwareSkinning && vertexBuffer == mVB) || (!TSShape::smUseHardwareSkinning), "Vertex buffer mismatch");
  1291. // render...
  1292. innerRender(materials, rdata, vertexBuffer, mPB, meshName);
  1293. }
  1294. bool TSSkinMesh::buildPolyList( S32 frame, AbstractPolyList *polyList, U32 &surfaceKey, TSMaterialList *materials )
  1295. {
  1296. return false;
  1297. }
  1298. bool TSSkinMesh::castRay( S32 frame, const Point3F &start, const Point3F &end, RayInfo *rayInfo, TSMaterialList *materials )
  1299. {
  1300. TORQUE_UNUSED(frame);
  1301. TORQUE_UNUSED(start);
  1302. TORQUE_UNUSED(end);
  1303. TORQUE_UNUSED(rayInfo);
  1304. TORQUE_UNUSED(materials);
  1305. return false;
  1306. }
  1307. bool TSSkinMesh::buildConvexHull()
  1308. {
  1309. return false; // no error, but we don't do anything either...
  1310. }
  1311. void TSSkinMesh::computeBounds( const MatrixF &transform, Box3F &bounds, S32 frame, Point3F *center, F32 *radius )
  1312. {
  1313. TORQUE_UNUSED(frame);
  1314. if (verts.size() != 0)
  1315. {
  1316. // Use unskinned verts
  1317. TSMesh::computeBounds( verts.address(), verts.size(), sizeof(Point3F), transform, bounds, center, radius );
  1318. }
  1319. else if (frame <= 0 && batchData.initialVerts.size() > 0)
  1320. {
  1321. // Use unskinned verts
  1322. TSMesh::computeBounds(batchData.initialVerts.address(), batchData.initialVerts.size(), sizeof(Point3F), transform, bounds, center, radius);
  1323. }
  1324. else
  1325. {
  1326. Point3F *vertStart = reinterpret_cast<Point3F *>(mVertexData.address());
  1327. TSMesh::computeBounds( vertStart, mVertexData.size(), mVertexData.vertSize(), transform, bounds, center, radius );
  1328. }
  1329. }
  1330. //-----------------------------------------------------
  1331. // encoded normals
  1332. //-----------------------------------------------------
  1333. const Point3F TSMesh::smU8ToNormalTable[] =
  1334. {
  1335. Point3F( 0.565061f, -0.270644f, -0.779396f ),
  1336. Point3F( -0.309804f, -0.731114f, 0.607860f ),
  1337. Point3F( -0.867412f, 0.472957f, 0.154619f ),
  1338. Point3F( -0.757488f, 0.498188f, -0.421925f ),
  1339. Point3F( 0.306834f, -0.915340f, 0.260778f ),
  1340. Point3F( 0.098754f, 0.639153f, -0.762713f ),
  1341. Point3F( 0.713706f, -0.558862f, -0.422252f ),
  1342. Point3F( -0.890431f, -0.407603f, -0.202466f ),
  1343. Point3F( 0.848050f, -0.487612f, -0.207475f ),
  1344. Point3F( -0.232226f, 0.776855f, 0.585293f ),
  1345. Point3F( -0.940195f, 0.304490f, -0.152706f ),
  1346. Point3F( 0.602019f, -0.491878f, -0.628991f ),
  1347. Point3F( -0.096835f, -0.494354f, -0.863850f ),
  1348. Point3F( 0.026630f, -0.323659f, -0.945799f ),
  1349. Point3F( 0.019208f, 0.909386f, 0.415510f ),
  1350. Point3F( 0.854440f, 0.491730f, 0.167731f ),
  1351. Point3F( -0.418835f, 0.866521f, -0.271512f ),
  1352. Point3F( 0.465024f, 0.409667f, 0.784809f ),
  1353. Point3F( -0.674391f, -0.691087f, -0.259992f ),
  1354. Point3F( 0.303858f, -0.869270f, -0.389922f ),
  1355. Point3F( 0.991333f, 0.090061f, -0.095640f ),
  1356. Point3F( -0.275924f, -0.369550f, 0.887298f ),
  1357. Point3F( 0.426545f, -0.465962f, 0.775202f ),
  1358. Point3F( -0.482741f, -0.873278f, -0.065920f ),
  1359. Point3F( 0.063616f, 0.932012f, -0.356800f ),
  1360. Point3F( 0.624786f, -0.061315f, 0.778385f ),
  1361. Point3F( -0.530300f, 0.416850f, 0.738253f ),
  1362. Point3F( 0.312144f, -0.757028f, -0.573999f ),
  1363. Point3F( 0.399288f, -0.587091f, -0.704197f ),
  1364. Point3F( -0.132698f, 0.482877f, 0.865576f ),
  1365. Point3F( 0.950966f, 0.306530f, 0.041268f ),
  1366. Point3F( -0.015923f, -0.144300f, 0.989406f ),
  1367. Point3F( -0.407522f, -0.854193f, 0.322925f ),
  1368. Point3F( -0.932398f, 0.220464f, 0.286408f ),
  1369. Point3F( 0.477509f, 0.876580f, 0.059936f ),
  1370. Point3F( 0.337133f, 0.932606f, -0.128796f ),
  1371. Point3F( -0.638117f, 0.199338f, 0.743687f ),
  1372. Point3F( -0.677454f, 0.445349f, 0.585423f ),
  1373. Point3F( -0.446715f, 0.889059f, -0.100099f ),
  1374. Point3F( -0.410024f, 0.909168f, 0.072759f ),
  1375. Point3F( 0.708462f, 0.702103f, -0.071641f ),
  1376. Point3F( -0.048801f, -0.903683f, -0.425411f ),
  1377. Point3F( -0.513681f, -0.646901f, 0.563606f ),
  1378. Point3F( -0.080022f, 0.000676f, -0.996793f ),
  1379. Point3F( 0.066966f, -0.991150f, -0.114615f ),
  1380. Point3F( -0.245220f, 0.639318f, -0.728793f ),
  1381. Point3F( 0.250978f, 0.855979f, 0.452006f ),
  1382. Point3F( -0.123547f, 0.982443f, -0.139791f ),
  1383. Point3F( -0.794825f, 0.030254f, -0.606084f ),
  1384. Point3F( -0.772905f, 0.547941f, 0.319967f ),
  1385. Point3F( 0.916347f, 0.369614f, -0.153928f ),
  1386. Point3F( -0.388203f, 0.105395f, 0.915527f ),
  1387. Point3F( -0.700468f, -0.709334f, 0.078677f ),
  1388. Point3F( -0.816193f, 0.390455f, 0.425880f ),
  1389. Point3F( -0.043007f, 0.769222f, -0.637533f ),
  1390. Point3F( 0.911444f, 0.113150f, 0.395560f ),
  1391. Point3F( 0.845801f, 0.156091f, -0.510153f ),
  1392. Point3F( 0.829801f, -0.029340f, 0.557287f ),
  1393. Point3F( 0.259529f, 0.416263f, 0.871418f ),
  1394. Point3F( 0.231128f, -0.845982f, 0.480515f ),
  1395. Point3F( -0.626203f, -0.646168f, 0.436277f ),
  1396. Point3F( -0.197047f, -0.065791f, 0.978184f ),
  1397. Point3F( -0.255692f, -0.637488f, -0.726794f ),
  1398. Point3F( 0.530662f, -0.844385f, -0.073567f ),
  1399. Point3F( -0.779887f, 0.617067f, -0.104899f ),
  1400. Point3F( 0.739908f, 0.113984f, 0.662982f ),
  1401. Point3F( -0.218801f, 0.930194f, -0.294729f ),
  1402. Point3F( -0.374231f, 0.818666f, 0.435589f ),
  1403. Point3F( -0.720250f, -0.028285f, 0.693137f ),
  1404. Point3F( 0.075389f, 0.415049f, 0.906670f ),
  1405. Point3F( -0.539724f, -0.106620f, 0.835063f ),
  1406. Point3F( -0.452612f, -0.754669f, -0.474991f ),
  1407. Point3F( 0.682822f, 0.581234f, -0.442629f ),
  1408. Point3F( 0.002435f, -0.618462f, -0.785811f ),
  1409. Point3F( -0.397631f, 0.110766f, -0.910835f ),
  1410. Point3F( 0.133935f, -0.985438f, 0.104754f ),
  1411. Point3F( 0.759098f, -0.608004f, 0.232595f ),
  1412. Point3F( -0.825239f, -0.256087f, 0.503388f ),
  1413. Point3F( 0.101693f, -0.565568f, 0.818408f ),
  1414. Point3F( 0.386377f, 0.793546f, -0.470104f ),
  1415. Point3F( -0.520516f, -0.840690f, 0.149346f ),
  1416. Point3F( -0.784549f, -0.479672f, 0.392935f ),
  1417. Point3F( -0.325322f, -0.927581f, -0.183735f ),
  1418. Point3F( -0.069294f, -0.428541f, 0.900861f ),
  1419. Point3F( 0.993354f, -0.115023f, -0.004288f ),
  1420. Point3F( -0.123896f, -0.700568f, 0.702747f ),
  1421. Point3F( -0.438031f, -0.120880f, -0.890795f ),
  1422. Point3F( 0.063314f, 0.813233f, 0.578484f ),
  1423. Point3F( 0.322045f, 0.889086f, -0.325289f ),
  1424. Point3F( -0.133521f, 0.875063f, -0.465228f ),
  1425. Point3F( 0.637155f, 0.564814f, 0.524422f ),
  1426. Point3F( 0.260092f, -0.669353f, 0.695930f ),
  1427. Point3F( 0.953195f, 0.040485f, -0.299634f ),
  1428. Point3F( -0.840665f, -0.076509f, 0.536124f ),
  1429. Point3F( -0.971350f, 0.202093f, 0.125047f ),
  1430. Point3F( -0.804307f, -0.396312f, -0.442749f ),
  1431. Point3F( -0.936746f, 0.069572f, 0.343027f ),
  1432. Point3F( 0.426545f, -0.465962f, 0.775202f ),
  1433. Point3F( 0.794542f, -0.227450f, 0.563000f ),
  1434. Point3F( -0.892172f, 0.091169f, -0.442399f ),
  1435. Point3F( -0.312654f, 0.541264f, 0.780564f ),
  1436. Point3F( 0.590603f, -0.735618f, -0.331743f ),
  1437. Point3F( -0.098040f, -0.986713f, 0.129558f ),
  1438. Point3F( 0.569646f, 0.283078f, -0.771603f ),
  1439. Point3F( 0.431051f, -0.407385f, -0.805129f ),
  1440. Point3F( -0.162087f, -0.938749f, -0.304104f ),
  1441. Point3F( 0.241533f, -0.359509f, 0.901341f ),
  1442. Point3F( -0.576191f, 0.614939f, 0.538380f ),
  1443. Point3F( -0.025110f, 0.085740f, 0.996001f ),
  1444. Point3F( -0.352693f, -0.198168f, 0.914515f ),
  1445. Point3F( -0.604577f, 0.700711f, 0.378802f ),
  1446. Point3F( 0.465024f, 0.409667f, 0.784809f ),
  1447. Point3F( -0.254684f, -0.030474f, -0.966544f ),
  1448. Point3F( -0.604789f, 0.791809f, 0.085259f ),
  1449. Point3F( -0.705147f, -0.399298f, 0.585943f ),
  1450. Point3F( 0.185691f, 0.017236f, -0.982457f ),
  1451. Point3F( 0.044588f, 0.973094f, 0.226052f ),
  1452. Point3F( -0.405463f, 0.642367f, 0.650357f ),
  1453. Point3F( -0.563959f, 0.599136f, -0.568319f ),
  1454. Point3F( 0.367162f, -0.072253f, -0.927347f ),
  1455. Point3F( 0.960429f, -0.213570f, -0.178783f ),
  1456. Point3F( -0.192629f, 0.906005f, 0.376893f ),
  1457. Point3F( -0.199718f, -0.359865f, -0.911378f ),
  1458. Point3F( 0.485072f, 0.121233f, -0.866030f ),
  1459. Point3F( 0.467163f, -0.874294f, 0.131792f ),
  1460. Point3F( -0.638953f, -0.716603f, 0.279677f ),
  1461. Point3F( -0.622710f, 0.047813f, -0.780990f ),
  1462. Point3F( 0.828724f, -0.054433f, -0.557004f ),
  1463. Point3F( 0.130241f, 0.991080f, 0.028245f ),
  1464. Point3F( 0.310995f, -0.950076f, -0.025242f ),
  1465. Point3F( 0.818118f, 0.275336f, 0.504850f ),
  1466. Point3F( 0.676328f, 0.387023f, 0.626733f ),
  1467. Point3F( -0.100433f, 0.495114f, -0.863004f ),
  1468. Point3F( -0.949609f, -0.240681f, -0.200786f ),
  1469. Point3F( -0.102610f, 0.261831f, -0.959644f ),
  1470. Point3F( -0.845732f, -0.493136f, 0.203850f ),
  1471. Point3F( 0.672617f, -0.738838f, 0.041290f ),
  1472. Point3F( 0.380465f, 0.875938f, 0.296613f ),
  1473. Point3F( -0.811223f, 0.262027f, -0.522742f ),
  1474. Point3F( -0.074423f, -0.775670f, -0.626736f ),
  1475. Point3F( -0.286499f, 0.755850f, -0.588735f ),
  1476. Point3F( 0.291182f, -0.276189f, -0.915933f ),
  1477. Point3F( -0.638117f, 0.199338f, 0.743687f ),
  1478. Point3F( 0.439922f, -0.864433f, -0.243359f ),
  1479. Point3F( 0.177649f, 0.206919f, 0.962094f ),
  1480. Point3F( 0.277107f, 0.948521f, 0.153361f ),
  1481. Point3F( 0.507629f, 0.661918f, -0.551523f ),
  1482. Point3F( -0.503110f, -0.579308f, -0.641313f ),
  1483. Point3F( 0.600522f, 0.736495f, -0.311364f ),
  1484. Point3F( -0.691096f, -0.715301f, -0.103592f ),
  1485. Point3F( -0.041083f, -0.858497f, 0.511171f ),
  1486. Point3F( 0.207773f, -0.480062f, -0.852274f ),
  1487. Point3F( 0.795719f, 0.464614f, 0.388543f ),
  1488. Point3F( -0.100433f, 0.495114f, -0.863004f ),
  1489. Point3F( 0.703249f, 0.065157f, -0.707951f ),
  1490. Point3F( -0.324171f, -0.941112f, 0.096024f ),
  1491. Point3F( -0.134933f, -0.940212f, 0.312722f ),
  1492. Point3F( -0.438240f, 0.752088f, -0.492249f ),
  1493. Point3F( 0.964762f, -0.198855f, 0.172311f ),
  1494. Point3F( -0.831799f, 0.196807f, 0.519015f ),
  1495. Point3F( -0.508008f, 0.819902f, 0.263986f ),
  1496. Point3F( 0.471075f, -0.001146f, 0.882092f ),
  1497. Point3F( 0.919512f, 0.246162f, -0.306435f ),
  1498. Point3F( -0.960050f, 0.279828f, -0.001187f ),
  1499. Point3F( 0.110232f, -0.847535f, -0.519165f ),
  1500. Point3F( 0.208229f, 0.697360f, 0.685806f ),
  1501. Point3F( -0.199680f, -0.560621f, 0.803637f ),
  1502. Point3F( 0.170135f, -0.679985f, -0.713214f ),
  1503. Point3F( 0.758371f, -0.494907f, 0.424195f ),
  1504. Point3F( 0.077734f, -0.755978f, 0.649965f ),
  1505. Point3F( 0.612831f, -0.672475f, 0.414987f ),
  1506. Point3F( 0.142776f, 0.836698f, -0.528726f ),
  1507. Point3F( -0.765185f, 0.635778f, 0.101382f ),
  1508. Point3F( 0.669873f, -0.419737f, 0.612447f ),
  1509. Point3F( 0.593549f, 0.194879f, 0.780847f ),
  1510. Point3F( 0.646930f, 0.752173f, 0.125368f ),
  1511. Point3F( 0.837721f, 0.545266f, -0.030127f ),
  1512. Point3F( 0.541505f, 0.768070f, 0.341820f ),
  1513. Point3F( 0.760679f, -0.365715f, -0.536301f ),
  1514. Point3F( 0.381516f, 0.640377f, 0.666605f ),
  1515. Point3F( 0.565794f, -0.072415f, -0.821361f ),
  1516. Point3F( -0.466072f, -0.401588f, 0.788356f ),
  1517. Point3F( 0.987146f, 0.096290f, 0.127560f ),
  1518. Point3F( 0.509709f, -0.688886f, -0.515396f ),
  1519. Point3F( -0.135132f, -0.988046f, -0.074192f ),
  1520. Point3F( 0.600499f, 0.476471f, -0.642166f ),
  1521. Point3F( -0.732326f, -0.275320f, -0.622815f ),
  1522. Point3F( -0.881141f, -0.470404f, 0.048078f ),
  1523. Point3F( 0.051548f, 0.601042f, 0.797553f ),
  1524. Point3F( 0.402027f, -0.763183f, 0.505891f ),
  1525. Point3F( 0.404233f, -0.208288f, 0.890624f ),
  1526. Point3F( -0.311793f, 0.343843f, 0.885752f ),
  1527. Point3F( 0.098132f, -0.937014f, 0.335223f ),
  1528. Point3F( 0.537158f, 0.830585f, -0.146936f ),
  1529. Point3F( 0.725277f, 0.298172f, -0.620538f ),
  1530. Point3F( -0.882025f, 0.342976f, -0.323110f ),
  1531. Point3F( -0.668829f, 0.424296f, -0.610443f ),
  1532. Point3F( -0.408835f, -0.476442f, -0.778368f ),
  1533. Point3F( 0.809472f, 0.397249f, -0.432375f ),
  1534. Point3F( -0.909184f, -0.205938f, -0.361903f ),
  1535. Point3F( 0.866930f, -0.347934f, -0.356895f ),
  1536. Point3F( 0.911660f, -0.141281f, -0.385897f ),
  1537. Point3F( -0.431404f, -0.844074f, -0.318480f ),
  1538. Point3F( -0.950593f, -0.073496f, 0.301614f ),
  1539. Point3F( -0.719716f, 0.626915f, -0.298305f ),
  1540. Point3F( -0.779887f, 0.617067f, -0.104899f ),
  1541. Point3F( -0.475899f, -0.542630f, 0.692151f ),
  1542. Point3F( 0.081952f, -0.157248f, -0.984153f ),
  1543. Point3F( 0.923990f, -0.381662f, -0.024025f ),
  1544. Point3F( -0.957998f, 0.120979f, -0.260008f ),
  1545. Point3F( 0.306601f, 0.227975f, -0.924134f ),
  1546. Point3F( -0.141244f, 0.989182f, 0.039601f ),
  1547. Point3F( 0.077097f, 0.186288f, -0.979466f ),
  1548. Point3F( -0.630407f, -0.259801f, 0.731499f ),
  1549. Point3F( 0.718150f, 0.637408f, 0.279233f ),
  1550. Point3F( 0.340946f, 0.110494f, 0.933567f ),
  1551. Point3F( -0.396671f, 0.503020f, -0.767869f ),
  1552. Point3F( 0.636943f, -0.245005f, 0.730942f ),
  1553. Point3F( -0.849605f, -0.518660f, -0.095724f ),
  1554. Point3F( -0.388203f, 0.105395f, 0.915527f ),
  1555. Point3F( -0.280671f, -0.776541f, -0.564099f ),
  1556. Point3F( -0.601680f, 0.215451f, -0.769131f ),
  1557. Point3F( -0.660112f, -0.632371f, -0.405412f ),
  1558. Point3F( 0.921096f, 0.284072f, 0.266242f ),
  1559. Point3F( 0.074850f, -0.300846f, 0.950731f ),
  1560. Point3F( 0.943952f, -0.067062f, 0.323198f ),
  1561. Point3F( -0.917838f, -0.254589f, 0.304561f ),
  1562. Point3F( 0.889843f, -0.409008f, 0.202219f ),
  1563. Point3F( -0.565849f, 0.753721f, -0.334246f ),
  1564. Point3F( 0.791460f, 0.555918f, -0.254060f ),
  1565. Point3F( 0.261936f, 0.703590f, -0.660568f ),
  1566. Point3F( -0.234406f, 0.952084f, 0.196444f ),
  1567. Point3F( 0.111205f, 0.979492f, -0.168014f ),
  1568. Point3F( -0.869844f, -0.109095f, -0.481113f ),
  1569. Point3F( -0.337728f, -0.269701f, -0.901777f ),
  1570. Point3F( 0.366793f, 0.408875f, -0.835634f ),
  1571. Point3F( -0.098749f, 0.261316f, 0.960189f ),
  1572. Point3F( -0.272379f, -0.847100f, 0.456324f ),
  1573. Point3F( -0.319506f, 0.287444f, -0.902935f ),
  1574. Point3F( 0.873383f, -0.294109f, 0.388203f ),
  1575. Point3F( -0.088950f, 0.710450f, 0.698104f ),
  1576. Point3F( 0.551238f, -0.786552f, 0.278340f ),
  1577. Point3F( 0.724436f, -0.663575f, -0.186712f ),
  1578. Point3F( 0.529741f, -0.606539f, 0.592861f ),
  1579. Point3F( -0.949743f, -0.282514f, 0.134809f ),
  1580. Point3F( 0.155047f, 0.419442f, -0.894443f ),
  1581. Point3F( -0.562653f, -0.329139f, -0.758346f ),
  1582. Point3F( 0.816407f, -0.576953f, 0.024576f ),
  1583. Point3F( 0.178550f, -0.950242f, -0.255266f ),
  1584. Point3F( 0.479571f, 0.706691f, 0.520192f ),
  1585. Point3F( 0.391687f, 0.559884f, -0.730145f ),
  1586. Point3F( 0.724872f, -0.205570f, -0.657496f ),
  1587. Point3F( -0.663196f, -0.517587f, -0.540624f ),
  1588. Point3F( -0.660054f, -0.122486f, -0.741165f ),
  1589. Point3F( -0.531989f, 0.374711f, -0.759328f ),
  1590. Point3F( 0.194979f, -0.059120f, 0.979024f )
  1591. };
  1592. U8 TSMesh::encodeNormal( const Point3F &normal )
  1593. {
  1594. U8 bestIndex = 0;
  1595. F32 bestDot = -10E30f;
  1596. for ( U32 i = 0; i < 256; i++ )
  1597. {
  1598. F32 dot = mDot( normal, smU8ToNormalTable[i] );
  1599. if ( dot > bestDot )
  1600. {
  1601. bestIndex = i;
  1602. bestDot = dot;
  1603. }
  1604. }
  1605. return bestIndex;
  1606. }
  1607. //-----------------------------------------------------
  1608. // TSMesh assemble from/ dissemble to memory buffer
  1609. //-----------------------------------------------------
  1610. #define tsalloc TSShape::smTSAlloc
  1611. TSMesh* TSMesh::assembleMesh( U32 meshType, bool skip )
  1612. {
  1613. static TSMesh tempStandardMesh;
  1614. static TSSkinMesh tempSkinMesh;
  1615. static TSDecalMesh tempDecalMesh;
  1616. static TSSortedMesh tempSortedMesh;
  1617. bool justSize = skip || !tsalloc.allocShape32(0); // if this returns NULL, we're just sizing memory block
  1618. // a little funny business because we pretend decals are derived from meshes
  1619. S32 * ret = NULL;
  1620. TSMesh * mesh = NULL;
  1621. TSDecalMesh * decal = NULL;
  1622. if ( justSize )
  1623. {
  1624. switch ( meshType )
  1625. {
  1626. case StandardMeshType :
  1627. {
  1628. ret = (S32*)&tempStandardMesh;
  1629. mesh = &tempStandardMesh;
  1630. tsalloc.allocShape32( sizeof(TSMesh) >> 2 );
  1631. break;
  1632. }
  1633. case SkinMeshType :
  1634. {
  1635. ret = (S32*)&tempSkinMesh;
  1636. mesh = &tempSkinMesh;
  1637. tsalloc.allocShape32( sizeof(TSSkinMesh) >> 2 );
  1638. break;
  1639. }
  1640. case DecalMeshType :
  1641. {
  1642. ret = (S32*)&tempDecalMesh;
  1643. decal = &tempDecalMesh;
  1644. tsalloc.allocShape32( sizeof(TSDecalMesh) >> 2 );
  1645. break;
  1646. }
  1647. case SortedMeshType :
  1648. {
  1649. ret = (S32*)&tempSortedMesh;
  1650. mesh = &tempSortedMesh;
  1651. tsalloc.allocShape32( sizeof(TSSortedMesh) >> 2 );
  1652. break;
  1653. }
  1654. }
  1655. }
  1656. else
  1657. {
  1658. switch ( meshType )
  1659. {
  1660. case StandardMeshType :
  1661. {
  1662. ret = tsalloc.allocShape32( sizeof(TSMesh) >> 2 );
  1663. constructInPlace( (TSMesh*)ret );
  1664. mesh = (TSMesh*)ret;
  1665. break;
  1666. }
  1667. case SkinMeshType :
  1668. {
  1669. ret = tsalloc.allocShape32( sizeof(TSSkinMesh) >> 2 );
  1670. constructInPlace( (TSSkinMesh*)ret );
  1671. mesh = (TSSkinMesh*)ret;
  1672. break;
  1673. }
  1674. case DecalMeshType :
  1675. {
  1676. ret = tsalloc.allocShape32( sizeof(TSDecalMesh) >> 2 );
  1677. constructInPlace((TSDecalMesh*)ret);
  1678. decal = (TSDecalMesh*)ret;
  1679. break;
  1680. }
  1681. case SortedMeshType :
  1682. {
  1683. ret = tsalloc.allocShape32( sizeof(TSSortedMesh) >> 2 );
  1684. constructInPlace( (TSSortedMesh*)ret );
  1685. mesh = (TSSortedMesh*)ret;
  1686. break;
  1687. }
  1688. }
  1689. }
  1690. tsalloc.setSkipMode( skip );
  1691. if ( mesh )
  1692. mesh->assemble( skip );
  1693. if ( decal )
  1694. decal->assemble( skip );
  1695. tsalloc.setSkipMode( false );
  1696. return (TSMesh*)ret;
  1697. }
  1698. void TSMesh::convertToTris( const TSDrawPrimitive *primitivesIn,
  1699. const S32 *indicesIn,
  1700. S32 numPrimIn,
  1701. S32 &numPrimOut,
  1702. S32 &numIndicesOut,
  1703. TSDrawPrimitive *primitivesOut,
  1704. S32 *indicesOut ) const
  1705. {
  1706. S32 prevMaterial = -99999;
  1707. TSDrawPrimitive * newDraw = NULL;
  1708. numPrimOut = 0;
  1709. numIndicesOut = 0;
  1710. for ( S32 i = 0; i < numPrimIn; i++ )
  1711. {
  1712. S32 newMat = primitivesIn[i].matIndex;
  1713. newMat &= ~TSDrawPrimitive::TypeMask;
  1714. U32 start = primitivesIn[i].start;
  1715. U32 prevStart = (i > 0) ? primitivesIn[i-1].start : start;
  1716. U32 numElements = primitivesIn[i].numElements;
  1717. // Add a new primitive if changing materials, or if this primitive
  1718. // indexes vertices in a different 16-bit range
  1719. if ( ( newMat != prevMaterial ) ||
  1720. ((indicesIn[prevStart] ^ indicesIn[start]) & 0xFFFF0000) )
  1721. {
  1722. if ( primitivesOut )
  1723. {
  1724. newDraw = &primitivesOut[numPrimOut];
  1725. newDraw->start = numIndicesOut;
  1726. newDraw->numElements = 0;
  1727. newDraw->matIndex = newMat | TSDrawPrimitive::Triangles;
  1728. }
  1729. numPrimOut++;
  1730. prevMaterial = newMat;
  1731. }
  1732. // gonna depend on what kind of primitive it is...
  1733. if ( (primitivesIn[i].matIndex & TSDrawPrimitive::TypeMask) == TSDrawPrimitive::Triangles)
  1734. {
  1735. for ( S32 j = 0; j < numElements; j += 3 )
  1736. {
  1737. if ( indicesOut )
  1738. {
  1739. indicesOut[numIndicesOut + 0] = indicesIn[start + j + 0];
  1740. indicesOut[numIndicesOut + 1] = indicesIn[start + j + 1];
  1741. indicesOut[numIndicesOut + 2] = indicesIn[start + j + 2];
  1742. }
  1743. if ( newDraw )
  1744. newDraw->numElements += 3;
  1745. numIndicesOut += 3;
  1746. }
  1747. }
  1748. else
  1749. {
  1750. U32 idx0 = indicesIn[start + 0];
  1751. U32 idx1;
  1752. U32 idx2 = indicesIn[start + 1];
  1753. U32 * nextIdx = &idx1;
  1754. for ( S32 j = 2; j < numElements; j++ )
  1755. {
  1756. *nextIdx = idx2;
  1757. nextIdx = (U32*) ( (dsize_t)nextIdx ^ (dsize_t)&idx0 ^ (dsize_t)&idx1);
  1758. idx2 = indicesIn[start + j];
  1759. if ( idx0 == idx1 || idx1 == idx2 || idx2 == idx0 )
  1760. continue;
  1761. if ( indicesOut )
  1762. {
  1763. indicesOut[numIndicesOut+0] = idx0;
  1764. indicesOut[numIndicesOut+1] = idx1;
  1765. indicesOut[numIndicesOut+2] = idx2;
  1766. }
  1767. if ( newDraw )
  1768. newDraw->numElements += 3;
  1769. numIndicesOut += 3;
  1770. }
  1771. }
  1772. }
  1773. }
  1774. void unwindStrip( const S32 * indices, S32 numElements, Vector<S32> &triIndices )
  1775. {
  1776. U32 idx0 = indices[0];
  1777. U32 idx1;
  1778. U32 idx2 = indices[1];
  1779. U32 * nextIdx = &idx1;
  1780. for ( S32 j = 2; j < numElements; j++ )
  1781. {
  1782. *nextIdx = idx2;
  1783. nextIdx = (U32*) ( (dsize_t)nextIdx ^ (dsize_t)&idx0 ^ (dsize_t)&idx1);
  1784. idx2 = indices[j];
  1785. if ( idx0 == idx1 || idx1 == idx2 || idx2 == idx0 )
  1786. continue;
  1787. triIndices.push_back( idx0 );
  1788. triIndices.push_back( idx1 );
  1789. triIndices.push_back( idx2 );
  1790. }
  1791. }
  1792. void TSMesh::convertToSingleStrip( const TSDrawPrimitive *primitivesIn,
  1793. const S32 *indicesIn,
  1794. S32 numPrimIn,
  1795. S32 &numPrimOut,
  1796. S32 &numIndicesOut,
  1797. TSDrawPrimitive *primitivesOut,
  1798. S32 *indicesOut ) const
  1799. {
  1800. S32 prevMaterial = -99999;
  1801. TSDrawPrimitive * newDraw = NULL;
  1802. TSDrawPrimitive * newTris = NULL;
  1803. Vector<S32> triIndices;
  1804. S32 curDrawOut = 0;
  1805. numPrimOut = 0;
  1806. numIndicesOut = 0;
  1807. for ( S32 i = 0; i < numPrimIn; i++ )
  1808. {
  1809. S32 newMat = primitivesIn[i].matIndex;
  1810. U32 start = primitivesIn[i].start;
  1811. U32 prevStart = (i > 0) ? primitivesIn[i-1].start : start;
  1812. U32 numElements = primitivesIn[i].numElements;
  1813. // Add a new primitive if changing materials, or if this primitive
  1814. // indexes vertices in a different 16-bit range
  1815. if ( ( newMat != prevMaterial ) ||
  1816. ((indicesIn[prevStart] ^ indicesIn[start]) & 0xFFFF0000) )
  1817. {
  1818. // before adding the new primitive, transfer triangle indices
  1819. if ( triIndices.size() )
  1820. {
  1821. if ( newTris && indicesOut )
  1822. {
  1823. newTris->start = numIndicesOut;
  1824. newTris->numElements = triIndices.size();
  1825. dMemcpy(&indicesOut[numIndicesOut],triIndices.address(),triIndices.size()*sizeof(U32));
  1826. }
  1827. numIndicesOut += triIndices.size();
  1828. triIndices.clear();
  1829. newTris = NULL;
  1830. }
  1831. if ( primitivesOut )
  1832. {
  1833. newDraw = &primitivesOut[numPrimOut];
  1834. newDraw->start = numIndicesOut;
  1835. newDraw->numElements = 0;
  1836. newDraw->matIndex = newMat;
  1837. }
  1838. numPrimOut++;
  1839. curDrawOut = 0;
  1840. prevMaterial = newMat;
  1841. }
  1842. // gonna depend on what kind of primitive it is...
  1843. // from above we know it's the same kind as the one we're building...
  1844. if ( (primitivesIn[i].matIndex & TSDrawPrimitive::TypeMask) == TSDrawPrimitive::Triangles)
  1845. {
  1846. // triangles primitive...add to it
  1847. for ( S32 j = 0; j < numElements; j += 3 )
  1848. {
  1849. if ( indicesOut )
  1850. {
  1851. indicesOut[numIndicesOut + 0] = indicesIn[start + j + 0];
  1852. indicesOut[numIndicesOut + 1] = indicesIn[start + j + 1];
  1853. indicesOut[numIndicesOut + 2] = indicesIn[start + j + 2];
  1854. }
  1855. if ( newDraw )
  1856. newDraw->numElements += 3;
  1857. numIndicesOut += 3;
  1858. }
  1859. }
  1860. else
  1861. {
  1862. // strip primitive...
  1863. // if numElements less than smSmallestStripSize, add to triangles...
  1864. if ( numElements < smMinStripSize + 2 )
  1865. {
  1866. // put triangle indices aside until material changes...
  1867. if ( triIndices.empty() )
  1868. {
  1869. // set up for new triangle primitive and add it if we are copying data right now
  1870. if ( primitivesOut )
  1871. {
  1872. newTris = &primitivesOut[numPrimOut];
  1873. newTris->matIndex = newMat;
  1874. newTris->matIndex &= ~(TSDrawPrimitive::Triangles|TSDrawPrimitive::Strip);
  1875. newTris->matIndex |= TSDrawPrimitive::Triangles;
  1876. }
  1877. numPrimOut++;
  1878. }
  1879. unwindStrip( indicesIn + start, numElements, triIndices );
  1880. }
  1881. else
  1882. {
  1883. // strip primitive...add to it
  1884. if ( indicesOut )
  1885. {
  1886. if ( curDrawOut & 1 )
  1887. {
  1888. indicesOut[numIndicesOut + 0] = indicesOut[numIndicesOut - 1];
  1889. indicesOut[numIndicesOut + 1] = indicesOut[numIndicesOut - 1];
  1890. indicesOut[numIndicesOut + 2] = indicesIn[start];
  1891. dMemcpy(indicesOut+numIndicesOut+3,indicesIn+start,numElements*sizeof(U32));
  1892. }
  1893. else if ( curDrawOut )
  1894. {
  1895. indicesOut[numIndicesOut + 0] = indicesOut[numIndicesOut - 1];
  1896. indicesOut[numIndicesOut + 1] = indicesIn[start];
  1897. dMemcpy(indicesOut+numIndicesOut+2,indicesIn+start,numElements*sizeof(U32));
  1898. }
  1899. else
  1900. dMemcpy(indicesOut+numIndicesOut,indicesIn+start,numElements*sizeof(U32));
  1901. }
  1902. S32 added = numElements;
  1903. added += curDrawOut ? (curDrawOut&1 ? 3 : 2) : 0;
  1904. if ( newDraw )
  1905. newDraw->numElements += added;
  1906. numIndicesOut += added;
  1907. curDrawOut += added;
  1908. }
  1909. }
  1910. }
  1911. // spit out tris before leaving
  1912. // before adding the new primitive, transfer triangle indices
  1913. if ( triIndices.size() )
  1914. {
  1915. if ( newTris && indicesOut )
  1916. {
  1917. newTris->start = numIndicesOut;
  1918. newTris->numElements = triIndices.size();
  1919. dMemcpy(&indicesOut[numIndicesOut],triIndices.address(),triIndices.size()*sizeof(U32));
  1920. }
  1921. numIndicesOut += triIndices.size();
  1922. triIndices.clear();
  1923. newTris = NULL;
  1924. }
  1925. }
  1926. // this method does none of the converting that the above methods do, except that small strips are converted
  1927. // to triangle lists...
  1928. void TSMesh::leaveAsMultipleStrips( const TSDrawPrimitive *primitivesIn,
  1929. const S32 *indicesIn,
  1930. S32 numPrimIn,
  1931. S32 &numPrimOut,
  1932. S32 &numIndicesOut,
  1933. TSDrawPrimitive *primitivesOut,
  1934. S32 *indicesOut ) const
  1935. {
  1936. S32 prevMaterial = -99999;
  1937. TSDrawPrimitive * newDraw = NULL;
  1938. Vector<S32> triIndices;
  1939. numPrimOut = 0;
  1940. numIndicesOut = 0;
  1941. for ( S32 i = 0; i < numPrimIn; i++ )
  1942. {
  1943. S32 newMat = primitivesIn[i].matIndex;
  1944. U32 start = primitivesIn[i].start;
  1945. U32 prevStart = (i > 0) ? primitivesIn[i-1].start : start;
  1946. U32 numElements = primitivesIn[i].numElements;
  1947. // Add a new primitive if changing materials, or if this primitive
  1948. // indexes vertices in a different 16-bit range
  1949. if ( triIndices.size() &&
  1950. (( newMat != prevMaterial ) ||
  1951. ((indicesIn[prevStart] ^ indicesIn[start]) & 0xFFFF0000) ))
  1952. {
  1953. // material just changed and we have triangles lying around
  1954. // add primitive and indices for triangles and clear triIndices
  1955. if ( indicesOut )
  1956. {
  1957. TSDrawPrimitive * newTris = &primitivesOut[numPrimOut];
  1958. newTris->matIndex = prevMaterial;
  1959. newTris->matIndex &= ~(TSDrawPrimitive::Triangles|TSDrawPrimitive::Strip);
  1960. newTris->matIndex |= TSDrawPrimitive::Triangles;
  1961. newTris->start = numIndicesOut;
  1962. newTris->numElements = triIndices.size();
  1963. dMemcpy(&indicesOut[numIndicesOut],triIndices.address(),triIndices.size()*sizeof(U32));
  1964. }
  1965. numPrimOut++;
  1966. numIndicesOut += triIndices.size();
  1967. triIndices.clear();
  1968. }
  1969. // this is a little convoluted because this code was adapted from convertToSingleStrip
  1970. // but we will need a new primitive only if it is a triangle primitive coming in
  1971. // or we have more elements than the min strip size...
  1972. if ( (primitivesIn[i].matIndex & TSDrawPrimitive::TypeMask) == TSDrawPrimitive::Triangles || numElements>=smMinStripSize+2)
  1973. {
  1974. if ( primitivesOut )
  1975. {
  1976. newDraw = &primitivesOut[numPrimOut];
  1977. newDraw->start = numIndicesOut;
  1978. newDraw->numElements = 0;
  1979. newDraw->matIndex = newMat;
  1980. }
  1981. numPrimOut++;
  1982. }
  1983. prevMaterial = newMat;
  1984. // gonna depend on what kind of primitive it is...
  1985. // from above we know it's the same kind as the one we're building...
  1986. if ( (primitivesIn[i].matIndex & TSDrawPrimitive::TypeMask) == TSDrawPrimitive::Triangles)
  1987. {
  1988. // triangles primitive...add to it
  1989. for ( S32 j = 0; j < numElements; j += 3 )
  1990. {
  1991. if ( indicesOut )
  1992. {
  1993. indicesOut[numIndicesOut + 0] = indicesIn[start + j + 0];
  1994. indicesOut[numIndicesOut + 1] = indicesIn[start + j + 1];
  1995. indicesOut[numIndicesOut + 2] = indicesIn[start + j + 2];
  1996. }
  1997. if ( newDraw )
  1998. newDraw->numElements += 3;
  1999. numIndicesOut += 3;
  2000. }
  2001. }
  2002. else
  2003. {
  2004. // strip primitive...
  2005. // if numElements less than smSmallestStripSize, add to triangles...
  2006. if ( numElements < smMinStripSize + 2 )
  2007. // put triangle indices aside until material changes...
  2008. unwindStrip( indicesIn + start, numElements, triIndices );
  2009. else
  2010. {
  2011. // strip primitive...add to it
  2012. if ( indicesOut )
  2013. dMemcpy(indicesOut+numIndicesOut,indicesIn+start,numElements*sizeof(U32));
  2014. if ( newDraw )
  2015. newDraw->numElements = numElements;
  2016. numIndicesOut += numElements;
  2017. }
  2018. }
  2019. }
  2020. // spit out tris before leaving
  2021. if ( triIndices.size() )
  2022. {
  2023. // material just changed and we have triangles lying around
  2024. // add primitive and indices for triangles and clear triIndices
  2025. if ( indicesOut )
  2026. {
  2027. TSDrawPrimitive *newTris = &primitivesOut[numPrimOut];
  2028. newTris->matIndex = prevMaterial;
  2029. newTris->matIndex &= ~(TSDrawPrimitive::Triangles|TSDrawPrimitive::Strip);
  2030. newTris->matIndex |= TSDrawPrimitive::Triangles;
  2031. newTris->start = numIndicesOut;
  2032. newTris->numElements = triIndices.size();
  2033. dMemcpy(&indicesOut[numIndicesOut],triIndices.address(),triIndices.size()*sizeof(U32));
  2034. }
  2035. numPrimOut++;
  2036. numIndicesOut += triIndices.size();
  2037. triIndices.clear();
  2038. }
  2039. }
  2040. // This method retrieves data that is shared (or possibly shared) between different meshes.
  2041. // This adds an extra step to the copying of data from the memory buffer to the shape data buffer.
  2042. // If we have no parentMesh, then we either return a pointer to the data in the memory buffer
  2043. // (in the case that we skip this mesh) or copy the data into the shape data buffer and return
  2044. // that pointer (in the case that we don't skip this mesh).
  2045. // If we do have a parent mesh, then we return a pointer to the data in the shape buffer,
  2046. // copying the data in there ourselves if our parent didn't already do it (i.e., if it was skipped).
  2047. S32 * TSMesh::getSharedData32( S32 parentMesh, S32 size, S32 **source, bool skip )
  2048. {
  2049. S32 * ptr;
  2050. if( parentMesh < 0 )
  2051. ptr = skip ? tsalloc.getPointer32( size ) : tsalloc.copyToShape32( size );
  2052. else
  2053. {
  2054. ptr = source[parentMesh];
  2055. // if we skipped the previous mesh (and we're not skipping this one) then
  2056. // we still need to copy points into the shape...
  2057. if ( !smDataCopied[parentMesh] && !skip )
  2058. {
  2059. S32 * tmp = ptr;
  2060. ptr = tsalloc.allocShape32( size );
  2061. if ( ptr && tmp )
  2062. dMemcpy(ptr, tmp, size * sizeof(S32) );
  2063. }
  2064. }
  2065. return ptr;
  2066. }
  2067. S8 * TSMesh::getSharedData8( S32 parentMesh, S32 size, S8 **source, bool skip )
  2068. {
  2069. S8 * ptr;
  2070. if( parentMesh < 0 )
  2071. ptr = skip ? tsalloc.getPointer8( size ) : tsalloc.copyToShape8( size );
  2072. else
  2073. {
  2074. ptr = source[parentMesh];
  2075. // if we skipped the previous mesh (and we're not skipping this one) then
  2076. // we still need to copy points into the shape...
  2077. if ( !smDataCopied[parentMesh] && !skip )
  2078. {
  2079. S8 * tmp = ptr;
  2080. ptr = tsalloc.allocShape8( size );
  2081. if ( ptr && tmp )
  2082. dMemcpy( ptr, tmp, size * sizeof(S32) );
  2083. }
  2084. }
  2085. return ptr;
  2086. }
  2087. void TSMesh::dumpPrimitives(U32 startVertex, U32 startIndex, GFXPrimitive *piArray, U16* ibIndices)
  2088. {
  2089. // go through and create PrimitiveInfo array
  2090. GFXPrimitive pInfo;
  2091. U32 primitivesSize = primitives.size();
  2092. for (U32 i = 0; i < primitivesSize; i++)
  2093. {
  2094. const TSDrawPrimitive & draw = primitives[i];
  2095. GFXPrimitiveType drawType = getDrawType(draw.matIndex >> 30);
  2096. switch (drawType)
  2097. {
  2098. case GFXTriangleList:
  2099. pInfo.type = drawType;
  2100. pInfo.numPrimitives = draw.numElements / 3;
  2101. pInfo.startIndex = startIndex + draw.start;
  2102. // Use the first index to determine which 16-bit address space we are operating in
  2103. pInfo.startVertex = (indices[draw.start] & 0xFFFF0000); // TODO: figure out a good solution for this
  2104. pInfo.minIndex = 0; // minIndex are zero based index relative to startVertex. See @GFXDevice
  2105. pInfo.numVertices = getMin((U32)0x10000, mNumVerts - pInfo.startVertex);
  2106. pInfo.startVertex += startVertex;
  2107. break;
  2108. case GFXTriangleStrip:
  2109. pInfo.type = drawType;
  2110. pInfo.numPrimitives = draw.numElements - 2;
  2111. pInfo.startIndex = startIndex + draw.start;
  2112. // Use the first index to determine which 16-bit address space we are operating in
  2113. pInfo.startVertex = (indices[draw.start] & 0xFFFF0000); // TODO: figure out a good solution for this
  2114. pInfo.minIndex = 0; // minIndex are zero based index relative to startVertex. See @GFXDevice
  2115. pInfo.numVertices = getMin((U32)0x10000, mNumVerts - pInfo.startVertex);
  2116. pInfo.startVertex += startVertex;
  2117. break;
  2118. default:
  2119. AssertFatal(false, "WTF?!");
  2120. }
  2121. *piArray++ = pInfo;
  2122. }
  2123. dCopyArray(ibIndices, indices.address(), indices.size());
  2124. }
  2125. void TSMesh::assemble( bool skip )
  2126. {
  2127. tsalloc.checkGuard();
  2128. numFrames = tsalloc.get32();
  2129. numMatFrames = tsalloc.get32();
  2130. parentMesh = tsalloc.get32();
  2131. tsalloc.get32( (S32*)&mBounds, 6 );
  2132. tsalloc.get32( (S32*)&mCenter, 3 );
  2133. mRadius = (F32)tsalloc.get32();
  2134. if (TSShape::smReadVersion >= 27)
  2135. {
  2136. // Offsetted
  2137. mVertOffset = tsalloc.get32();
  2138. mNumVerts = tsalloc.get32();
  2139. mVertSize = tsalloc.get32();
  2140. }
  2141. else
  2142. {
  2143. mVertOffset = 0;
  2144. mNumVerts = 0;
  2145. mVertSize = 0;
  2146. }
  2147. S32 numVerts = tsalloc.get32();
  2148. S32 *ptr32 = getSharedData32( parentMesh, 3 * numVerts, (S32**)smVertsList.address(), skip );
  2149. verts.set( (Point3F*)ptr32, numVerts );
  2150. S32 numTVerts = tsalloc.get32();
  2151. ptr32 = getSharedData32( parentMesh, 2 * numTVerts, (S32**)smTVertsList.address(), skip );
  2152. tverts.set( (Point2F*)ptr32, numTVerts );
  2153. if ( TSShape::smReadVersion > 25 )
  2154. {
  2155. numTVerts = tsalloc.get32();
  2156. ptr32 = getSharedData32( parentMesh, 2 * numTVerts, (S32**)smTVerts2List.address(), skip );
  2157. tverts2.set( (Point2F*)ptr32, numTVerts );
  2158. S32 numVColors = tsalloc.get32();
  2159. ptr32 = getSharedData32( parentMesh, numVColors, (S32**)smColorsList.address(), skip );
  2160. colors.set( (ColorI*)ptr32, numVColors );
  2161. }
  2162. S8 *ptr8;
  2163. if ( TSShape::smReadVersion > 21 && TSMesh::smUseEncodedNormals)
  2164. {
  2165. // we have encoded normals and we want to use them...
  2166. if ( parentMesh < 0 )
  2167. tsalloc.getPointer32( numVerts * 3 ); // adva nce past norms, don't use
  2168. norms.set( NULL, 0 );
  2169. ptr8 = getSharedData8( parentMesh, numVerts, (S8**)smEncodedNormsList.address(), skip );
  2170. encodedNorms.set( ptr8, numVerts );
  2171. }
  2172. else if ( TSShape::smReadVersion > 21 )
  2173. {
  2174. // we have encoded normals but we don't want to use them...
  2175. ptr32 = getSharedData32( parentMesh, 3 * numVerts, (S32**)smNormsList.address(), skip );
  2176. norms.set( (Point3F*)ptr32, numVerts );
  2177. if ( parentMesh < 0 )
  2178. tsalloc.getPointer8( numVerts ); // advance past encoded normls, don't use
  2179. encodedNorms.set( NULL, 0 );
  2180. }
  2181. else
  2182. {
  2183. // no encoded normals...
  2184. ptr32 = getSharedData32( parentMesh, 3 * numVerts, (S32**)smNormsList.address(), skip );
  2185. norms.set( (Point3F*)ptr32, numVerts );
  2186. encodedNorms.set( NULL, 0 );
  2187. }
  2188. // copy the primitives and indices...how we do this depends on what
  2189. // form we want them in when copied...just get pointers to data for now
  2190. S32 szPrimIn, szIndIn;
  2191. TSDrawPrimitive *primIn;
  2192. S32 *indIn;
  2193. bool deleteInputArrays = false;
  2194. if (TSShape::smReadVersion > 25)
  2195. {
  2196. // mesh primitives (start, numElements) and indices are stored as 32 bit values
  2197. szPrimIn = tsalloc.get32();
  2198. primIn = (TSDrawPrimitive*)tsalloc.getPointer32(szPrimIn*3);
  2199. szIndIn = tsalloc.get32();
  2200. indIn = tsalloc.getPointer32(szIndIn);
  2201. }
  2202. else
  2203. {
  2204. // mesh primitives (start, numElements) indices are stored as 16 bit values
  2205. szPrimIn = tsalloc.get32();
  2206. S16 *prim16 = tsalloc.getPointer16(szPrimIn*2); // primitive: start, numElements
  2207. S32 *prim32 = tsalloc.getPointer32(szPrimIn); // primitive: matIndex
  2208. szIndIn = tsalloc.get32();
  2209. // warn about non-addressable indices
  2210. if ( !skip && szIndIn >= 0x10000 )
  2211. {
  2212. Con::warnf("Mesh contains non-addressable indices, and may not render "
  2213. "correctly. Either split this mesh into pieces of no more than 65k "
  2214. "unique verts prior to export, or use COLLADA.");
  2215. }
  2216. S16 *ind16 = tsalloc.getPointer16(szIndIn);
  2217. // need to copy to temporary arrays
  2218. deleteInputArrays = true;
  2219. primIn = new TSDrawPrimitive[szPrimIn];
  2220. for (S32 i = 0; i < szPrimIn; i++)
  2221. {
  2222. primIn[i].start = prim16[i*2];
  2223. primIn[i].numElements = prim16[i*2+1];
  2224. primIn[i].matIndex = prim32[i];
  2225. }
  2226. indIn = new S32[szIndIn];
  2227. dCopyArray(indIn, ind16, szIndIn);
  2228. }
  2229. // count the number of output primitives and indices
  2230. S32 szPrimOut = szPrimIn, szIndOut = szIndIn;
  2231. if (smUseTriangles)
  2232. convertToTris(primIn, indIn, szPrimIn, szPrimOut, szIndOut, NULL, NULL);
  2233. else if (smUseOneStrip)
  2234. convertToSingleStrip(primIn, indIn, szPrimIn, szPrimOut, szIndOut, NULL, NULL);
  2235. else
  2236. leaveAsMultipleStrips(primIn, indIn, szPrimIn, szPrimOut, szIndOut, NULL, NULL);
  2237. // allocate enough space for the new primitives and indices (all 32 bits)
  2238. TSDrawPrimitive *primOut = (TSDrawPrimitive*)tsalloc.allocShape32(3*szPrimOut);
  2239. S32 *indOut = tsalloc.allocShape32(szIndOut);
  2240. // copy output primitives and indices
  2241. S32 chkPrim = szPrimOut, chkInd = szIndOut;
  2242. if (smUseTriangles)
  2243. convertToTris(primIn, indIn, szPrimIn, chkPrim, chkInd, primOut, indOut);
  2244. else if (smUseOneStrip)
  2245. convertToSingleStrip(primIn, indIn, szPrimIn, chkPrim, chkInd, primOut, indOut);
  2246. else
  2247. leaveAsMultipleStrips(primIn, indIn, szPrimIn, chkPrim, chkInd, primOut, indOut);
  2248. AssertFatal(chkPrim==szPrimOut && chkInd==szIndOut,"TSMesh::primitive conversion");
  2249. // store output
  2250. primitives.set(primOut, szPrimOut);
  2251. indices.set(indOut, szIndOut);
  2252. // delete temporary arrays if necessary
  2253. if (deleteInputArrays)
  2254. {
  2255. delete [] primIn;
  2256. delete [] indIn;
  2257. }
  2258. S32 sz = tsalloc.get32();
  2259. tsalloc.getPointer16( sz ); // skip deprecated merge indices
  2260. tsalloc.align32();
  2261. vertsPerFrame = tsalloc.get32();
  2262. U32 flags = (U32)tsalloc.get32();
  2263. if ( encodedNorms.size() )
  2264. flags |= UseEncodedNormals;
  2265. setFlags( flags );
  2266. // Set color & tvert2 flags if we have an old version
  2267. if (TSShape::smReadVersion < 27)
  2268. {
  2269. if (colors.size() > 0) setFlags(HasColor);
  2270. if (tverts2.size() > 0) setFlags(HasTVert2);
  2271. mNumVerts = verts.size();
  2272. }
  2273. tsalloc.checkGuard();
  2274. if ( tsalloc.allocShape32( 0 ) && TSShape::smReadVersion < 19 )
  2275. computeBounds(); // only do this if we copied the data...
  2276. createTangents(verts, norms);
  2277. }
  2278. void TSMesh::disassemble()
  2279. {
  2280. tsalloc.setGuard();
  2281. tsalloc.set32( numFrames );
  2282. tsalloc.set32( numMatFrames );
  2283. tsalloc.set32( parentMesh );
  2284. tsalloc.copyToBuffer32( (S32*)&mBounds, 6 );
  2285. tsalloc.copyToBuffer32( (S32*)&mCenter, 3 );
  2286. tsalloc.set32( (S32)mRadius );
  2287. bool shouldMakeEditable = TSShape::smVersion < 27 || mVertSize == 0;
  2288. // Re-create the vectors
  2289. if (shouldMakeEditable)
  2290. {
  2291. makeEditable();
  2292. // No Offset
  2293. if (TSShape::smVersion >= 27)
  2294. {
  2295. tsalloc.set32(0);
  2296. tsalloc.set32(0);
  2297. tsalloc.set32(0);
  2298. }
  2299. }
  2300. else
  2301. {
  2302. // Offsetted
  2303. tsalloc.set32(mVertOffset);
  2304. tsalloc.set32(mNumVerts);
  2305. tsalloc.set32(mVertSize);
  2306. AssertFatal(mNumVerts >= vertsPerFrame, "invalid mNumVerts");
  2307. }
  2308. if (TSShape::smVersion >= 27 && mVertexData.isReady())
  2309. {
  2310. // If not editable all arrays are effectively 0.
  2311. tsalloc.set32(0); // verts
  2312. tsalloc.set32(0); // tverts
  2313. tsalloc.set32(0); // tverts2
  2314. tsalloc.set32(0); // colors
  2315. }
  2316. else
  2317. {
  2318. // verts...
  2319. tsalloc.set32(verts.size());
  2320. if (parentMesh < 0)
  2321. tsalloc.copyToBuffer32((S32*)verts.address(), 3 * verts.size()); // if no parent mesh, then save off our verts
  2322. // tverts...
  2323. tsalloc.set32(tverts.size());
  2324. if (parentMesh < 0)
  2325. tsalloc.copyToBuffer32((S32*)tverts.address(), 2 * tverts.size()); // if no parent mesh, then save off our tverts
  2326. if (TSShape::smVersion > 25)
  2327. {
  2328. // tverts2...
  2329. tsalloc.set32(tverts2.size());
  2330. if (parentMesh < 0)
  2331. tsalloc.copyToBuffer32((S32*)tverts2.address(), 2 * tverts2.size()); // if no parent mesh, then save off our tverts
  2332. // colors
  2333. tsalloc.set32(colors.size());
  2334. if (parentMesh < 0)
  2335. tsalloc.copyToBuffer32((S32*)colors.address(), colors.size()); // if no parent mesh, then save off our tverts
  2336. }
  2337. // norms...
  2338. if (parentMesh < 0) // if no parent mesh, then save off our norms
  2339. tsalloc.copyToBuffer32((S32*)norms.address(), 3 * norms.size()); // norms.size()==verts.size() or error...
  2340. // encoded norms...
  2341. if (parentMesh < 0)
  2342. {
  2343. // if no parent mesh, compute encoded normals and copy over
  2344. for (S32 i = 0; i < norms.size(); i++)
  2345. {
  2346. U8 normIdx = encodedNorms.size() ? encodedNorms[i] : encodeNormal(norms[i]);
  2347. tsalloc.copyToBuffer8((S8*)&normIdx, 1);
  2348. }
  2349. }
  2350. }
  2351. // optimize triangle draw order during disassemble
  2352. {
  2353. FrameTemp<TriListOpt::IndexType> tmpIdxs(indices.size());
  2354. for ( S32 i = 0; i < primitives.size(); i++ )
  2355. {
  2356. const TSDrawPrimitive& prim = primitives[i];
  2357. // only optimize triangle lists (strips and fans are assumed to be already optimized)
  2358. if ( (prim.matIndex & TSDrawPrimitive::TypeMask) == TSDrawPrimitive::Triangles )
  2359. {
  2360. TriListOpt::OptimizeTriangleOrdering(verts.size(), prim.numElements,
  2361. indices.address() + prim.start, tmpIdxs.address());
  2362. dCopyArray(indices.address() + prim.start, tmpIdxs.address(),
  2363. prim.numElements);
  2364. }
  2365. }
  2366. }
  2367. if (TSShape::smVersion > 25)
  2368. {
  2369. // primitives...
  2370. tsalloc.set32( primitives.size() );
  2371. tsalloc.copyToBuffer32((S32*)primitives.address(),3*primitives.size());
  2372. // indices...
  2373. tsalloc.set32(indices.size());
  2374. tsalloc.copyToBuffer32((S32*)indices.address(),indices.size());
  2375. }
  2376. else
  2377. {
  2378. // primitives
  2379. tsalloc.set32( primitives.size() );
  2380. for (S32 i=0; i<primitives.size(); i++)
  2381. {
  2382. S16 start = (S16)primitives[i].start;
  2383. S16 numElements = (S16)primitives[i].numElements;
  2384. tsalloc.copyToBuffer16(&start, 1);
  2385. tsalloc.copyToBuffer16(&numElements, 1);
  2386. tsalloc.copyToBuffer32(&(primitives[i].matIndex), 1);
  2387. }
  2388. // indices
  2389. tsalloc.set32(indices.size());
  2390. Vector<S16> s16_indices(indices.size());
  2391. for (S32 i=0; i<indices.size(); i++)
  2392. s16_indices.push_back((S16)indices[i]);
  2393. tsalloc.copyToBuffer16(s16_indices.address(), s16_indices.size());
  2394. }
  2395. // merge indices...DEPRECATED
  2396. tsalloc.set32( 0 );
  2397. // small stuff...
  2398. tsalloc.set32( vertsPerFrame );
  2399. tsalloc.set32( getFlags() );
  2400. tsalloc.setGuard();
  2401. }
  2402. //-----------------------------------------------------------------------------
  2403. // TSSkinMesh assemble from/ dissemble to memory buffer
  2404. //-----------------------------------------------------------------------------
  2405. void TSSkinMesh::assemble( bool skip )
  2406. {
  2407. // avoid a crash on computeBounds...
  2408. batchData.initialVerts.set( NULL, 0 );
  2409. TSMesh::assemble( skip );
  2410. if (TSShape::smReadVersion >= 27)
  2411. {
  2412. maxBones = tsalloc.get32();
  2413. }
  2414. else
  2415. {
  2416. maxBones = -1;
  2417. }
  2418. S32 sz;
  2419. S32 * ptr32;
  2420. if (TSShape::smReadVersion < 27)
  2421. {
  2422. sz = tsalloc.get32();
  2423. S32 numVerts = sz;
  2424. ptr32 = getSharedData32(parentMesh, 3 * numVerts, (S32**)smVertsList.address(), skip);
  2425. batchData.initialVerts.set((Point3F*)ptr32, sz);
  2426. S8 * ptr8;
  2427. if (TSShape::smReadVersion > 21 && TSMesh::smUseEncodedNormals)
  2428. {
  2429. // we have encoded normals and we want to use them...
  2430. if (parentMesh < 0)
  2431. tsalloc.getPointer32(numVerts * 3); // advance past norms, don't use
  2432. batchData.initialNorms.set(NULL, 0);
  2433. ptr8 = getSharedData8(parentMesh, numVerts, (S8**)smEncodedNormsList.address(), skip);
  2434. encodedNorms.set(ptr8, numVerts);
  2435. // Note: we don't set the encoded normals flag because we handle them in updateSkin and
  2436. // hide the fact that we are using them from base class (TSMesh)
  2437. }
  2438. else if (TSShape::smReadVersion > 21)
  2439. {
  2440. // we have encoded normals but we don't want to use them...
  2441. ptr32 = getSharedData32(parentMesh, 3 * numVerts, (S32**)smNormsList.address(), skip);
  2442. batchData.initialNorms.set((Point3F*)ptr32, numVerts);
  2443. if (parentMesh < 0)
  2444. tsalloc.getPointer8(numVerts); // advance past encoded normls, don't use
  2445. encodedNorms.set(NULL, 0);
  2446. }
  2447. else
  2448. {
  2449. // no encoded normals...
  2450. ptr32 = getSharedData32(parentMesh, 3 * numVerts, (S32**)smNormsList.address(), skip);
  2451. batchData.initialNorms.set((Point3F*)ptr32, numVerts);
  2452. encodedNorms.set(NULL, 0);
  2453. }
  2454. // Sometimes we'll have a mesh with 0 verts but initialVerts is set,
  2455. // so set these accordingly
  2456. if (verts.size() == 0)
  2457. {
  2458. verts = batchData.initialVerts;
  2459. }
  2460. if (norms.size() == 0)
  2461. {
  2462. norms = batchData.initialNorms;
  2463. }
  2464. }
  2465. else
  2466. {
  2467. // Set from the mesh data
  2468. batchData.initialVerts = verts;
  2469. batchData.initialNorms = norms;
  2470. }
  2471. sz = tsalloc.get32();
  2472. ptr32 = getSharedData32( parentMesh, 16 * sz, (S32**)smInitTransformList.address(), skip );
  2473. batchData.initialTransforms.set( ptr32, sz );
  2474. sz = tsalloc.get32();
  2475. ptr32 = getSharedData32( parentMesh, sz, (S32**)smVertexIndexList.address(), skip );
  2476. vertexIndex.set( ptr32, sz );
  2477. ptr32 = getSharedData32( parentMesh, sz, (S32**)smBoneIndexList.address(), skip );
  2478. boneIndex.set( ptr32, sz );
  2479. ptr32 = getSharedData32( parentMesh, sz, (S32**)smWeightList.address(), skip );
  2480. weight.set( (F32*)ptr32, sz );
  2481. sz = tsalloc.get32();
  2482. ptr32 = getSharedData32( parentMesh, sz, (S32**)smNodeIndexList.address(), skip );
  2483. batchData.nodeIndex.set( ptr32, sz );
  2484. tsalloc.checkGuard();
  2485. if (smDebugSkinVerts && ptr32 != NULL)
  2486. {
  2487. Con::printf("Loaded skin verts...");
  2488. for (U32 i = 0; i < vertexIndex.size(); i++)
  2489. {
  2490. Con::printf("vi[%i] == %i", i, vertexIndex[i]);
  2491. }
  2492. for (U32 i = 0; i < boneIndex.size(); i++)
  2493. {
  2494. Con::printf("bi[%i] == %i", i, boneIndex[i]);
  2495. }
  2496. for (U32 i = 0; i < batchData.nodeIndex.size(); i++)
  2497. {
  2498. Con::printf("ni[%i] == %i", i, batchData.nodeIndex[i]);
  2499. }
  2500. for (U32 i = 0; i < boneIndex.size(); i++)
  2501. {
  2502. Con::printf("we[%i] == %f", i, weight[i]);
  2503. }
  2504. if (mNumVerts != 0)
  2505. {
  2506. AssertFatal(batchData.initialVerts.size() == mNumVerts, "err WTF");
  2507. }
  2508. Con::printf("---");
  2509. }
  2510. if ( tsalloc.allocShape32( 0 ) && TSShape::smReadVersion < 19 )
  2511. TSMesh::computeBounds(); // only do this if we copied the data...c
  2512. }
  2513. //-----------------------------------------------------------------------------
  2514. // disassemble
  2515. //-----------------------------------------------------------------------------
  2516. void TSSkinMesh::disassemble()
  2517. {
  2518. TSMesh::disassemble();
  2519. if (TSShape::smVersion >= 27)
  2520. {
  2521. AssertFatal(maxBones != 0, "Skin mesh with no bones? No way!");
  2522. tsalloc.set32(maxBones);
  2523. }
  2524. if (TSShape::smVersion < 27)
  2525. {
  2526. tsalloc.set32(batchData.initialVerts.size());
  2527. // if we have no parent mesh, then save off our verts & norms
  2528. if (parentMesh < 0)
  2529. {
  2530. tsalloc.copyToBuffer32((S32*)verts.address(), 3 * verts.size());
  2531. // no longer do this here...let tsmesh handle this
  2532. tsalloc.copyToBuffer32((S32*)norms.address(), 3 * norms.size());
  2533. // if no parent mesh, compute encoded normals and copy over
  2534. for (S32 i = 0; i < norms.size(); i++)
  2535. {
  2536. U8 normIdx = encodedNorms.size() ? encodedNorms[i] : encodeNormal(norms[i]);
  2537. tsalloc.copyToBuffer8((S8*)&normIdx, 1);
  2538. }
  2539. }
  2540. }
  2541. tsalloc.set32( batchData.initialTransforms.size() );
  2542. if ( parentMesh < 0 )
  2543. tsalloc.copyToBuffer32( (S32*)batchData.initialTransforms.address(), batchData.initialTransforms.size() * 16 );
  2544. if (!mVertexData.isReady())
  2545. {
  2546. tsalloc.set32(vertexIndex.size());
  2547. tsalloc.copyToBuffer32((S32*)vertexIndex.address(), vertexIndex.size());
  2548. tsalloc.copyToBuffer32((S32*)boneIndex.address(), boneIndex.size());
  2549. tsalloc.copyToBuffer32((S32*)weight.address(), weight.size());
  2550. }
  2551. else
  2552. {
  2553. tsalloc.set32(0);
  2554. }
  2555. if (TSShape::smVersion < 27)
  2556. {
  2557. if (parentMesh < 0)
  2558. {
  2559. tsalloc.copyToBuffer32((S32*)vertexIndex.address(), vertexIndex.size());
  2560. tsalloc.copyToBuffer32((S32*)boneIndex.address(), boneIndex.size());
  2561. tsalloc.copyToBuffer32((S32*)weight.address(), weight.size());
  2562. }
  2563. }
  2564. tsalloc.set32( batchData.nodeIndex.size() );
  2565. if ( parentMesh < 0 )
  2566. tsalloc.copyToBuffer32( (S32*)batchData.nodeIndex.address(), batchData.nodeIndex.size() );
  2567. tsalloc.setGuard();
  2568. }
  2569. TSSkinMesh::TSSkinMesh()
  2570. {
  2571. meshType = SkinMeshType;
  2572. batchData.initialized = false;
  2573. maxBones = -1;
  2574. }
  2575. //-----------------------------------------------------------------------------
  2576. // find tangent vector
  2577. //-----------------------------------------------------------------------------
  2578. inline void TSMesh::findTangent( U32 index1,
  2579. U32 index2,
  2580. U32 index3,
  2581. Point3F *tan0,
  2582. Point3F *tan1,
  2583. const Vector<Point3F> &_verts)
  2584. {
  2585. const Point3F &v1 = _verts[index1];
  2586. const Point3F &v2 = _verts[index2];
  2587. const Point3F &v3 = _verts[index3];
  2588. const Point2F &w1 = tverts[index1];
  2589. const Point2F &w2 = tverts[index2];
  2590. const Point2F &w3 = tverts[index3];
  2591. F32 x1 = v2.x - v1.x;
  2592. F32 x2 = v3.x - v1.x;
  2593. F32 y1 = v2.y - v1.y;
  2594. F32 y2 = v3.y - v1.y;
  2595. F32 z1 = v2.z - v1.z;
  2596. F32 z2 = v3.z - v1.z;
  2597. F32 s1 = w2.x - w1.x;
  2598. F32 s2 = w3.x - w1.x;
  2599. F32 t1 = w2.y - w1.y;
  2600. F32 t2 = w3.y - w1.y;
  2601. F32 denom = (s1 * t2 - s2 * t1);
  2602. if( mFabs( denom ) < 0.0001f )
  2603. {
  2604. // handle degenerate triangles from strips
  2605. if (denom<0) denom = -0.0001f;
  2606. else denom = 0.0001f;
  2607. }
  2608. F32 r = 1.0f / denom;
  2609. Point3F sdir( (t2 * x1 - t1 * x2) * r,
  2610. (t2 * y1 - t1 * y2) * r,
  2611. (t2 * z1 - t1 * z2) * r );
  2612. Point3F tdir( (s1 * x2 - s2 * x1) * r,
  2613. (s1 * y2 - s2 * y1) * r,
  2614. (s1 * z2 - s2 * z1) * r );
  2615. tan0[index1] += sdir;
  2616. tan1[index1] += tdir;
  2617. tan0[index2] += sdir;
  2618. tan1[index2] += tdir;
  2619. tan0[index3] += sdir;
  2620. tan1[index3] += tdir;
  2621. }
  2622. //-----------------------------------------------------------------------------
  2623. // create array of tangent vectors
  2624. //-----------------------------------------------------------------------------
  2625. void TSMesh::createTangents(const Vector<Point3F> &_verts, const Vector<Point3F> &_norms)
  2626. {
  2627. if (_verts.size() == 0) // can only be done in editable mode
  2628. return;
  2629. U32 numVerts = _verts.size();
  2630. U32 numNorms = _norms.size();
  2631. if ( numVerts <= 0 || numNorms <= 0 )
  2632. return;
  2633. if( numVerts != numNorms)
  2634. return;
  2635. Vector<Point3F> tan0;
  2636. tan0.setSize( numVerts * 2 );
  2637. Point3F *tan1 = tan0.address() + numVerts;
  2638. dMemset( tan0.address(), 0, sizeof(Point3F) * 2 * numVerts );
  2639. U32 numPrimatives = primitives.size();
  2640. for (S32 i = 0; i < numPrimatives; i++ )
  2641. {
  2642. const TSDrawPrimitive & draw = primitives[i];
  2643. GFXPrimitiveType drawType = getDrawType( draw.matIndex >> 30 );
  2644. U32 p1Index = 0;
  2645. U32 p2Index = 0;
  2646. U32 *baseIdx = &indices[draw.start];
  2647. const U32 numElements = (U32)draw.numElements;
  2648. switch( drawType )
  2649. {
  2650. case GFXTriangleList:
  2651. {
  2652. for( U32 j = 0; j < numElements; j += 3 )
  2653. findTangent( baseIdx[j], baseIdx[j + 1], baseIdx[j + 2], tan0.address(), tan1, _verts );
  2654. break;
  2655. }
  2656. case GFXTriangleStrip:
  2657. {
  2658. p1Index = baseIdx[0];
  2659. p2Index = baseIdx[1];
  2660. for( U32 j = 2; j < numElements; j++ )
  2661. {
  2662. findTangent( p1Index, p2Index, baseIdx[j], tan0.address(), tan1, _verts );
  2663. p1Index = p2Index;
  2664. p2Index = baseIdx[j];
  2665. }
  2666. break;
  2667. }
  2668. default:
  2669. AssertFatal( false, "TSMesh::createTangents: unknown primitive type!" );
  2670. }
  2671. }
  2672. tangents.setSize( numVerts );
  2673. // fill out final info from accumulated basis data
  2674. for( U32 i = 0; i < numVerts; i++ )
  2675. {
  2676. const Point3F &n = _norms[i];
  2677. const Point3F &t = tan0[i];
  2678. const Point3F &b = tan1[i];
  2679. Point3F tempPt = t - n * mDot( n, t );
  2680. tempPt.normalize();
  2681. tangents[i] = tempPt;
  2682. Point3F cp;
  2683. mCross( n, t, &cp );
  2684. tangents[i].w = (mDot( cp, b ) < 0.0f) ? -1.0f : 1.0f;
  2685. }
  2686. }
  2687. void TSMesh::convertToVertexData()
  2688. {
  2689. if (!mVertexData.isReady())
  2690. {
  2691. _convertToVertexData(mVertexData, verts, norms);
  2692. }
  2693. }
  2694. void TSSkinMesh::convertToVertexData()
  2695. {
  2696. if (!mVertexData.isReady())
  2697. {
  2698. // Batch data required here
  2699. createSkinBatchData();
  2700. // Dump verts to buffer
  2701. _convertToVertexData(mVertexData, batchData.initialVerts, batchData.initialNorms);
  2702. // Setup bones too
  2703. setupVertexTransforms();
  2704. }
  2705. }
  2706. void TSMesh::copySourceVertexDataFrom(const TSMesh* srcMesh)
  2707. {
  2708. verts = srcMesh->verts;
  2709. tverts = srcMesh->tverts;
  2710. norms = srcMesh->norms;
  2711. colors = srcMesh->colors;
  2712. tverts2 = srcMesh->tverts2;
  2713. if (verts.size() == 0)
  2714. {
  2715. bool hasTVert2 = srcMesh->getHasTVert2();
  2716. bool hasColor = srcMesh->getHasColor();
  2717. verts.setSize(srcMesh->mNumVerts);
  2718. tverts.setSize(srcMesh->mNumVerts);
  2719. norms.setSize(srcMesh->mNumVerts);
  2720. if (hasColor)
  2721. colors.setSize(mNumVerts);
  2722. if (hasTVert2)
  2723. tverts2.setSize(mNumVerts);
  2724. // Fill arrays
  2725. for (U32 i = 0; i < mNumVerts; i++)
  2726. {
  2727. const __TSMeshVertexBase &cv = srcMesh->mVertexData.getBase(i);
  2728. const __TSMeshVertex_3xUVColor &cvc = srcMesh->mVertexData.getColor(i);
  2729. verts[i] = cv.vert();
  2730. tverts[i] = cv.tvert();
  2731. norms[i] = cv.normal();
  2732. if (hasColor)
  2733. cvc.color().getColor(&colors[i]);
  2734. if (hasTVert2)
  2735. tverts2[i] = cvc.tvert2();
  2736. }
  2737. }
  2738. }
  2739. void TSSkinMesh::copySourceVertexDataFrom(const TSMesh* srcMesh)
  2740. {
  2741. TSMesh::copySourceVertexDataFrom(srcMesh);
  2742. if (srcMesh->getMeshType() == TSMesh::SkinMeshType)
  2743. {
  2744. const TSSkinMesh* srcSkinMesh = static_cast<const TSSkinMesh*>(srcMesh);
  2745. weight = srcSkinMesh->weight;
  2746. boneIndex = srcSkinMesh->boneIndex;
  2747. vertexIndex = srcSkinMesh->vertexIndex;
  2748. maxBones = srcSkinMesh->maxBones;
  2749. // Extract from vertex data
  2750. if (srcSkinMesh->vertexIndex.size() == 0)
  2751. {
  2752. mVertexData = srcMesh->mVertexData;
  2753. addWeightsFromVertexBuffer();
  2754. mVertexData.setReady(false);
  2755. }
  2756. }
  2757. }
  2758. U32 TSMesh::getNumVerts()
  2759. {
  2760. return mVertexData.isReady() ? mNumVerts : verts.size();
  2761. }
  2762. void TSMesh::_convertToVertexData(TSMeshVertexArray &outArray, const Vector<Point3F> &_verts, const Vector<Point3F> &_norms)
  2763. {
  2764. // Update tangents list
  2765. createTangents(verts, norms);
  2766. AssertFatal(_verts.size() == mNumVerts, "vert count mismatch");
  2767. AssertFatal(!getHasColor() || colors.size() == _verts.size(), "Vector of color elements should be the same size as other vectors");
  2768. AssertFatal(!getHasTVert2() || tverts2.size() == _verts.size(), "Vector of tvert2 elements should be the same size as other vectors");
  2769. AssertFatal(!outArray.isReady(), "Mesh already converted to aligned data! Re-check code!");
  2770. AssertFatal(_verts.size() == _norms.size() &&
  2771. _verts.size() == tangents.size(),
  2772. "Vectors: verts, norms, tangents must all be the same size");
  2773. AssertFatal(mVertSize == outArray.vertSize(), "Size inconsistency");
  2774. if (mNumVerts == 0)
  2775. return;
  2776. bool needWeightSet = outArray.getBoneOffset() != 0;
  2777. bool hasColor = getHasColor();
  2778. bool hasTVert2 = getHasTVert2();
  2779. dMemset(&outArray.getBase(0), '\0', mVertSize * mNumVerts);
  2780. for (U32 i = 0; i < mNumVerts; i++)
  2781. {
  2782. __TSMeshVertexBase &v = outArray.getBase(i);
  2783. v.vert(_verts[i]);
  2784. v.normal(_norms[i]);
  2785. v.tangent(tangents[i]);
  2786. if (i < tverts.size())
  2787. v.tvert(tverts[i]);
  2788. if (hasTVert2 || hasColor)
  2789. {
  2790. __TSMeshVertex_3xUVColor &vc = outArray.getColor(i);
  2791. if (hasTVert2 && i < tverts2.size())
  2792. vc.tvert2(tverts2[i]);
  2793. if (hasColor && i < colors.size())
  2794. vc.color(colors[i]);
  2795. }
  2796. // NOTE: skin verts are set later on for the skinned mesh, otherwise we'll set the default (i.e. 0) if we need one for a rigid mesh
  2797. if (needWeightSet)
  2798. {
  2799. const Point4F wt(1.0f, 0.0f, 0.0f, 0.0f);
  2800. outArray.getBone(i, 0).weight(wt);
  2801. }
  2802. }
  2803. }
  2804. void TSMesh::makeEditable()
  2805. {
  2806. bool hasVerts = verts.size() != 0;
  2807. if(mVertexData.isReady() && !hasVerts)
  2808. {
  2809. copySourceVertexDataFrom(this);
  2810. }
  2811. mVertexData.setReady(false);
  2812. mVertSize = 0;
  2813. mNumVerts = 0;
  2814. mVertOffset = 0;
  2815. updateMeshFlags();
  2816. }
  2817. void TSSkinMesh::addWeightsFromVertexBuffer()
  2818. {
  2819. weight.setSize(0);
  2820. boneIndex.setSize(0);
  2821. vertexIndex.setSize(0);
  2822. U32 numBoneBlocks = maxBones >= 0 ? (maxBones + 3) / 4 : 0;
  2823. for (U32 i = 0; i < mNumVerts; i++)
  2824. {
  2825. for (U32 j = 0; j < numBoneBlocks; j++)
  2826. {
  2827. const __TSMeshVertex_BoneData &cv = mVertexData.getBone(i, j);
  2828. if (cv._weights.x != 0.0f)
  2829. {
  2830. addWeightForVert(i, cv._indexes.x, cv._weights.x);
  2831. }
  2832. if (cv._weights.y != 0.0f)
  2833. {
  2834. addWeightForVert(i, cv._indexes.y, cv._weights.y);
  2835. }
  2836. if (cv._weights.z != 0.0f)
  2837. {
  2838. addWeightForVert(i, cv._indexes.z, cv._weights.z);
  2839. }
  2840. if (cv._weights.w != 0.0f)
  2841. {
  2842. addWeightForVert(i, cv._indexes.w, cv._weights.w);
  2843. }
  2844. }
  2845. }
  2846. }
  2847. void TSSkinMesh::makeEditable()
  2848. {
  2849. bool hasVerts = verts.size() != 0;
  2850. // Reconstruct bone mapping
  2851. if (mVertexData.isReady() && !hasVerts)
  2852. {
  2853. copySourceVertexDataFrom(this);
  2854. weight.setSize(0);
  2855. boneIndex.setSize(0);
  2856. vertexIndex.setSize(0);
  2857. addWeightsFromVertexBuffer();
  2858. }
  2859. mVertexData.setReady(false);
  2860. mVertSize = 0;
  2861. mNumVerts = 0;
  2862. updateMeshFlags();
  2863. batchData.initialized = false;
  2864. }
  2865. void TSMesh::clearEditable()
  2866. {
  2867. if (verts.size() == 0)
  2868. return;
  2869. if (colors.empty())
  2870. clearFlags(HasColor);
  2871. else
  2872. setFlags(HasColor);
  2873. if (tverts2.empty())
  2874. clearFlags(HasTVert2);
  2875. else
  2876. setFlags(HasTVert2);
  2877. verts.free_memory();
  2878. norms.free_memory();
  2879. tangents.free_memory();
  2880. tverts.free_memory();
  2881. tverts2.free_memory();
  2882. colors.free_memory();
  2883. }
  2884. void TSMesh::updateMeshFlags()
  2885. {
  2886. // Make sure flags are correct
  2887. if (colors.empty())
  2888. clearFlags(HasColor);
  2889. else
  2890. setFlags(HasColor);
  2891. if (tverts2.empty())
  2892. clearFlags(HasTVert2);
  2893. else
  2894. setFlags(HasTVert2);
  2895. }
  2896. void TSSkinMesh::clearEditable()
  2897. {
  2898. TSMesh::clearEditable();
  2899. weight.free_memory();
  2900. boneIndex.free_memory();
  2901. vertexIndex.free_memory();
  2902. }
  2903. TSBasicVertexFormat::TSBasicVertexFormat() :
  2904. texCoordOffset(-1),
  2905. boneOffset(-1),
  2906. colorOffset(-1),
  2907. numBones(0),
  2908. vertexSize(-1)
  2909. {
  2910. }
  2911. TSBasicVertexFormat::TSBasicVertexFormat(TSMesh *mesh)
  2912. {
  2913. texCoordOffset = -1;
  2914. boneOffset = -1;
  2915. colorOffset = -1;
  2916. numBones = 0;
  2917. vertexSize = -1;
  2918. addMeshRequirements(mesh);
  2919. }
  2920. void TSBasicVertexFormat::getFormat(GFXVertexFormat &fmt)
  2921. {
  2922. // NOTE: previously the vertex data was padded to allow for verts to be skinned via SSE.
  2923. // since we now prefer to skin on the GPU and use a basic non-SSE fallback for software
  2924. // skinning, adding in padding via GFXSemantic::PADDING or dummy fields is no longer required.
  2925. fmt.addElement(GFXSemantic::POSITION, GFXDeclType_Float3);
  2926. fmt.addElement(GFXSemantic::TANGENTW, GFXDeclType_Float, 3);
  2927. fmt.addElement(GFXSemantic::NORMAL, GFXDeclType_Float3);
  2928. fmt.addElement(GFXSemantic::TANGENT, GFXDeclType_Float3);
  2929. fmt.addElement(GFXSemantic::TEXCOORD, GFXDeclType_Float2, 0);
  2930. if (texCoordOffset >= 0 || colorOffset >= 0)
  2931. {
  2932. fmt.addElement(GFXSemantic::TEXCOORD, GFXDeclType_Float2, 1);
  2933. fmt.addElement(GFXSemantic::COLOR, GFXDeclType_Color);
  2934. }
  2935. for (U32 i=0; i<numBones; i++)
  2936. {
  2937. fmt.addElement(GFXSemantic::BLENDINDICES, GFXDeclType_UByte4, i);
  2938. fmt.addElement(GFXSemantic::BLENDWEIGHT, GFXDeclType_Float4, i);
  2939. }
  2940. }
  2941. void TSBasicVertexFormat::calculateSize()
  2942. {
  2943. GFXVertexFormat fmt;
  2944. vertexSize = 0;
  2945. getFormat(fmt);
  2946. vertexSize = fmt.getSizeInBytes();
  2947. }
  2948. void TSBasicVertexFormat::writeAlloc(TSShapeAlloc* alloc)
  2949. {
  2950. alloc->set16(texCoordOffset);
  2951. alloc->set16(boneOffset);
  2952. alloc->set16(colorOffset);
  2953. alloc->set16(numBones);
  2954. alloc->set16(vertexSize);
  2955. }
  2956. void TSBasicVertexFormat::readAlloc(TSShapeAlloc* alloc)
  2957. {
  2958. texCoordOffset = alloc->get16();
  2959. boneOffset = alloc->get16();
  2960. colorOffset = alloc->get16();
  2961. numBones = alloc->get16();
  2962. vertexSize = alloc->get16();
  2963. }
  2964. void TSBasicVertexFormat::addMeshRequirements(TSMesh *mesh)
  2965. {
  2966. bool hasColors = false;
  2967. bool hasTexcoord2 = false;
  2968. bool hasSkin = false;
  2969. hasColors = mesh->getHasColor() || (colorOffset != -1);
  2970. hasTexcoord2 = mesh->getHasTVert2() || (texCoordOffset != -1);
  2971. hasSkin = (mesh->getMeshType() == TSMesh::SkinMeshType) || (boneOffset != -1);
  2972. S32 offset = sizeof(TSMesh::__TSMeshVertexBase);
  2973. if ((hasTexcoord2 || hasColors))
  2974. {
  2975. if (texCoordOffset == -1 || colorOffset == -1)
  2976. {
  2977. texCoordOffset = offset;
  2978. colorOffset = offset + (sizeof(float) * 2);
  2979. }
  2980. offset += sizeof(TSMesh::__TSMeshVertex_3xUVColor);
  2981. }
  2982. if (hasSkin)
  2983. {
  2984. boneOffset = offset;
  2985. U32 numMeshBones = mesh->getMaxBonesPerVert();
  2986. U32 boneBlocks = numMeshBones / 4;
  2987. U32 extraBlocks = numMeshBones % 4 != 0 ? 1 : 0;
  2988. U32 neededBones = boneBlocks + extraBlocks;
  2989. numBones = MAX(neededBones, numBones);
  2990. }
  2991. }
  2992. void TSSkinMesh::printVerts()
  2993. {
  2994. for (U32 i = 0; i < mNumVerts; i++)
  2995. {
  2996. TSMesh::__TSMeshVertexBase &vb = mVertexData.getBase(i);
  2997. TSMesh::__TSMeshVertex_BoneData &bw = mVertexData.getBone(i, 0);
  2998. Point3F vert = batchData.initialVerts[i];
  2999. Con::printf("v[%i] == %f,%f,%f; iv == %f,%f,%f. bo=%i,%i,%i,%i bw=%f,%f,%f,%f",
  3000. i, vb._vert.x, vb._vert.y, vb._vert.z,
  3001. vert.x, vert.y, vert.z,
  3002. bw._indexes.x, bw._indexes.y, bw._indexes.z, bw._indexes.w,
  3003. bw._weights.x, bw._weights.y, bw._weights.z, bw._weights.w);
  3004. }
  3005. }