tsMesh.cpp 115 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475
  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. register Point3F skinnedVert;
  1062. register 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. const BatchData::TransformOp &transformOp = curTransform.transform[i];
  1226. S32 vertsSet = transformsLeft > 4 ? 4 : transformsLeft;
  1227. __TSMeshIndex_List indices;
  1228. Point4F weights;
  1229. dMemset(&indices, '\0', sizeof(indices));
  1230. dMemset(&weights, '\0', sizeof(weights));
  1231. switch (vertsSet)
  1232. {
  1233. case 1:
  1234. indices.x = curTransform.transform[i+0].transformIndex;
  1235. weights.x = curTransform.transform[i+0].weight;
  1236. break;
  1237. case 2:
  1238. indices.x = curTransform.transform[i+0].transformIndex;
  1239. weights.x = curTransform.transform[i+0].weight;
  1240. indices.y = curTransform.transform[i+1].transformIndex;
  1241. weights.y = curTransform.transform[i+1].weight;
  1242. break;
  1243. case 3:
  1244. indices.x = curTransform.transform[i+0].transformIndex;
  1245. weights.x = curTransform.transform[i+0].weight;
  1246. indices.y = curTransform.transform[i+1].transformIndex;
  1247. weights.y = curTransform.transform[i+1].weight;
  1248. indices.z = curTransform.transform[i+2].transformIndex;
  1249. weights.z = curTransform.transform[i+2].weight;
  1250. break;
  1251. case 4:
  1252. indices.x = curTransform.transform[i+0].transformIndex;
  1253. weights.x = curTransform.transform[i+0].weight;
  1254. indices.y = curTransform.transform[i+1].transformIndex;
  1255. weights.y = curTransform.transform[i+1].weight;
  1256. indices.z = curTransform.transform[i+2].transformIndex;
  1257. weights.z = curTransform.transform[i+2].weight;
  1258. indices.w = curTransform.transform[i+3].transformIndex;
  1259. weights.w = curTransform.transform[i+3].weight;
  1260. break;
  1261. case 0:
  1262. default:
  1263. break;
  1264. }
  1265. v.index(indices);
  1266. v.weight(weights);
  1267. transformsLeft -= 4;
  1268. }
  1269. }
  1270. }
  1271. U32 TSSkinMesh::getMaxBonesPerVert()
  1272. {
  1273. return maxBones >= 0 ? maxBones : 0;
  1274. }
  1275. void TSSkinMesh::render( TSVertexBufferHandle &instanceVB )
  1276. {
  1277. innerRender(instanceVB, mPB);
  1278. }
  1279. void TSSkinMesh::render( TSMaterialList *materials,
  1280. const TSRenderState &rdata,
  1281. bool isSkinDirty,
  1282. const Vector<MatrixF> &transforms,
  1283. TSVertexBufferHandle &vertexBuffer,
  1284. const char *meshName )
  1285. {
  1286. PROFILE_SCOPE(TSSkinMesh_render);
  1287. if (mNumVerts == 0)
  1288. return;
  1289. // verify stuff first
  1290. AssertFatal(mVertexData.size() == mNumVerts, "Vert # mismatch");
  1291. AssertFatal((TSShape::smUseHardwareSkinning && vertexBuffer == mVB) || (!TSShape::smUseHardwareSkinning), "Vertex buffer mismatch");
  1292. // render...
  1293. innerRender(materials, rdata, vertexBuffer, mPB, meshName);
  1294. }
  1295. bool TSSkinMesh::buildPolyList( S32 frame, AbstractPolyList *polyList, U32 &surfaceKey, TSMaterialList *materials )
  1296. {
  1297. return false;
  1298. }
  1299. bool TSSkinMesh::castRay( S32 frame, const Point3F &start, const Point3F &end, RayInfo *rayInfo, TSMaterialList *materials )
  1300. {
  1301. TORQUE_UNUSED(frame);
  1302. TORQUE_UNUSED(start);
  1303. TORQUE_UNUSED(end);
  1304. TORQUE_UNUSED(rayInfo);
  1305. TORQUE_UNUSED(materials);
  1306. return false;
  1307. }
  1308. bool TSSkinMesh::buildConvexHull()
  1309. {
  1310. return false; // no error, but we don't do anything either...
  1311. }
  1312. void TSSkinMesh::computeBounds( const MatrixF &transform, Box3F &bounds, S32 frame, Point3F *center, F32 *radius )
  1313. {
  1314. TORQUE_UNUSED(frame);
  1315. if (verts.size() != 0)
  1316. {
  1317. // Use unskinned verts
  1318. TSMesh::computeBounds( verts.address(), verts.size(), sizeof(Point3F), transform, bounds, center, radius );
  1319. }
  1320. else if (frame <= 0 && batchData.initialVerts.size() > 0)
  1321. {
  1322. // Use unskinned verts
  1323. TSMesh::computeBounds(batchData.initialVerts.address(), batchData.initialVerts.size(), sizeof(Point3F), transform, bounds, center, radius);
  1324. }
  1325. else
  1326. {
  1327. Point3F *vertStart = reinterpret_cast<Point3F *>(mVertexData.address());
  1328. TSMesh::computeBounds( vertStart, mVertexData.size(), mVertexData.vertSize(), transform, bounds, center, radius );
  1329. }
  1330. }
  1331. //-----------------------------------------------------
  1332. // encoded normals
  1333. //-----------------------------------------------------
  1334. const Point3F TSMesh::smU8ToNormalTable[] =
  1335. {
  1336. Point3F( 0.565061f, -0.270644f, -0.779396f ),
  1337. Point3F( -0.309804f, -0.731114f, 0.607860f ),
  1338. Point3F( -0.867412f, 0.472957f, 0.154619f ),
  1339. Point3F( -0.757488f, 0.498188f, -0.421925f ),
  1340. Point3F( 0.306834f, -0.915340f, 0.260778f ),
  1341. Point3F( 0.098754f, 0.639153f, -0.762713f ),
  1342. Point3F( 0.713706f, -0.558862f, -0.422252f ),
  1343. Point3F( -0.890431f, -0.407603f, -0.202466f ),
  1344. Point3F( 0.848050f, -0.487612f, -0.207475f ),
  1345. Point3F( -0.232226f, 0.776855f, 0.585293f ),
  1346. Point3F( -0.940195f, 0.304490f, -0.152706f ),
  1347. Point3F( 0.602019f, -0.491878f, -0.628991f ),
  1348. Point3F( -0.096835f, -0.494354f, -0.863850f ),
  1349. Point3F( 0.026630f, -0.323659f, -0.945799f ),
  1350. Point3F( 0.019208f, 0.909386f, 0.415510f ),
  1351. Point3F( 0.854440f, 0.491730f, 0.167731f ),
  1352. Point3F( -0.418835f, 0.866521f, -0.271512f ),
  1353. Point3F( 0.465024f, 0.409667f, 0.784809f ),
  1354. Point3F( -0.674391f, -0.691087f, -0.259992f ),
  1355. Point3F( 0.303858f, -0.869270f, -0.389922f ),
  1356. Point3F( 0.991333f, 0.090061f, -0.095640f ),
  1357. Point3F( -0.275924f, -0.369550f, 0.887298f ),
  1358. Point3F( 0.426545f, -0.465962f, 0.775202f ),
  1359. Point3F( -0.482741f, -0.873278f, -0.065920f ),
  1360. Point3F( 0.063616f, 0.932012f, -0.356800f ),
  1361. Point3F( 0.624786f, -0.061315f, 0.778385f ),
  1362. Point3F( -0.530300f, 0.416850f, 0.738253f ),
  1363. Point3F( 0.312144f, -0.757028f, -0.573999f ),
  1364. Point3F( 0.399288f, -0.587091f, -0.704197f ),
  1365. Point3F( -0.132698f, 0.482877f, 0.865576f ),
  1366. Point3F( 0.950966f, 0.306530f, 0.041268f ),
  1367. Point3F( -0.015923f, -0.144300f, 0.989406f ),
  1368. Point3F( -0.407522f, -0.854193f, 0.322925f ),
  1369. Point3F( -0.932398f, 0.220464f, 0.286408f ),
  1370. Point3F( 0.477509f, 0.876580f, 0.059936f ),
  1371. Point3F( 0.337133f, 0.932606f, -0.128796f ),
  1372. Point3F( -0.638117f, 0.199338f, 0.743687f ),
  1373. Point3F( -0.677454f, 0.445349f, 0.585423f ),
  1374. Point3F( -0.446715f, 0.889059f, -0.100099f ),
  1375. Point3F( -0.410024f, 0.909168f, 0.072759f ),
  1376. Point3F( 0.708462f, 0.702103f, -0.071641f ),
  1377. Point3F( -0.048801f, -0.903683f, -0.425411f ),
  1378. Point3F( -0.513681f, -0.646901f, 0.563606f ),
  1379. Point3F( -0.080022f, 0.000676f, -0.996793f ),
  1380. Point3F( 0.066966f, -0.991150f, -0.114615f ),
  1381. Point3F( -0.245220f, 0.639318f, -0.728793f ),
  1382. Point3F( 0.250978f, 0.855979f, 0.452006f ),
  1383. Point3F( -0.123547f, 0.982443f, -0.139791f ),
  1384. Point3F( -0.794825f, 0.030254f, -0.606084f ),
  1385. Point3F( -0.772905f, 0.547941f, 0.319967f ),
  1386. Point3F( 0.916347f, 0.369614f, -0.153928f ),
  1387. Point3F( -0.388203f, 0.105395f, 0.915527f ),
  1388. Point3F( -0.700468f, -0.709334f, 0.078677f ),
  1389. Point3F( -0.816193f, 0.390455f, 0.425880f ),
  1390. Point3F( -0.043007f, 0.769222f, -0.637533f ),
  1391. Point3F( 0.911444f, 0.113150f, 0.395560f ),
  1392. Point3F( 0.845801f, 0.156091f, -0.510153f ),
  1393. Point3F( 0.829801f, -0.029340f, 0.557287f ),
  1394. Point3F( 0.259529f, 0.416263f, 0.871418f ),
  1395. Point3F( 0.231128f, -0.845982f, 0.480515f ),
  1396. Point3F( -0.626203f, -0.646168f, 0.436277f ),
  1397. Point3F( -0.197047f, -0.065791f, 0.978184f ),
  1398. Point3F( -0.255692f, -0.637488f, -0.726794f ),
  1399. Point3F( 0.530662f, -0.844385f, -0.073567f ),
  1400. Point3F( -0.779887f, 0.617067f, -0.104899f ),
  1401. Point3F( 0.739908f, 0.113984f, 0.662982f ),
  1402. Point3F( -0.218801f, 0.930194f, -0.294729f ),
  1403. Point3F( -0.374231f, 0.818666f, 0.435589f ),
  1404. Point3F( -0.720250f, -0.028285f, 0.693137f ),
  1405. Point3F( 0.075389f, 0.415049f, 0.906670f ),
  1406. Point3F( -0.539724f, -0.106620f, 0.835063f ),
  1407. Point3F( -0.452612f, -0.754669f, -0.474991f ),
  1408. Point3F( 0.682822f, 0.581234f, -0.442629f ),
  1409. Point3F( 0.002435f, -0.618462f, -0.785811f ),
  1410. Point3F( -0.397631f, 0.110766f, -0.910835f ),
  1411. Point3F( 0.133935f, -0.985438f, 0.104754f ),
  1412. Point3F( 0.759098f, -0.608004f, 0.232595f ),
  1413. Point3F( -0.825239f, -0.256087f, 0.503388f ),
  1414. Point3F( 0.101693f, -0.565568f, 0.818408f ),
  1415. Point3F( 0.386377f, 0.793546f, -0.470104f ),
  1416. Point3F( -0.520516f, -0.840690f, 0.149346f ),
  1417. Point3F( -0.784549f, -0.479672f, 0.392935f ),
  1418. Point3F( -0.325322f, -0.927581f, -0.183735f ),
  1419. Point3F( -0.069294f, -0.428541f, 0.900861f ),
  1420. Point3F( 0.993354f, -0.115023f, -0.004288f ),
  1421. Point3F( -0.123896f, -0.700568f, 0.702747f ),
  1422. Point3F( -0.438031f, -0.120880f, -0.890795f ),
  1423. Point3F( 0.063314f, 0.813233f, 0.578484f ),
  1424. Point3F( 0.322045f, 0.889086f, -0.325289f ),
  1425. Point3F( -0.133521f, 0.875063f, -0.465228f ),
  1426. Point3F( 0.637155f, 0.564814f, 0.524422f ),
  1427. Point3F( 0.260092f, -0.669353f, 0.695930f ),
  1428. Point3F( 0.953195f, 0.040485f, -0.299634f ),
  1429. Point3F( -0.840665f, -0.076509f, 0.536124f ),
  1430. Point3F( -0.971350f, 0.202093f, 0.125047f ),
  1431. Point3F( -0.804307f, -0.396312f, -0.442749f ),
  1432. Point3F( -0.936746f, 0.069572f, 0.343027f ),
  1433. Point3F( 0.426545f, -0.465962f, 0.775202f ),
  1434. Point3F( 0.794542f, -0.227450f, 0.563000f ),
  1435. Point3F( -0.892172f, 0.091169f, -0.442399f ),
  1436. Point3F( -0.312654f, 0.541264f, 0.780564f ),
  1437. Point3F( 0.590603f, -0.735618f, -0.331743f ),
  1438. Point3F( -0.098040f, -0.986713f, 0.129558f ),
  1439. Point3F( 0.569646f, 0.283078f, -0.771603f ),
  1440. Point3F( 0.431051f, -0.407385f, -0.805129f ),
  1441. Point3F( -0.162087f, -0.938749f, -0.304104f ),
  1442. Point3F( 0.241533f, -0.359509f, 0.901341f ),
  1443. Point3F( -0.576191f, 0.614939f, 0.538380f ),
  1444. Point3F( -0.025110f, 0.085740f, 0.996001f ),
  1445. Point3F( -0.352693f, -0.198168f, 0.914515f ),
  1446. Point3F( -0.604577f, 0.700711f, 0.378802f ),
  1447. Point3F( 0.465024f, 0.409667f, 0.784809f ),
  1448. Point3F( -0.254684f, -0.030474f, -0.966544f ),
  1449. Point3F( -0.604789f, 0.791809f, 0.085259f ),
  1450. Point3F( -0.705147f, -0.399298f, 0.585943f ),
  1451. Point3F( 0.185691f, 0.017236f, -0.982457f ),
  1452. Point3F( 0.044588f, 0.973094f, 0.226052f ),
  1453. Point3F( -0.405463f, 0.642367f, 0.650357f ),
  1454. Point3F( -0.563959f, 0.599136f, -0.568319f ),
  1455. Point3F( 0.367162f, -0.072253f, -0.927347f ),
  1456. Point3F( 0.960429f, -0.213570f, -0.178783f ),
  1457. Point3F( -0.192629f, 0.906005f, 0.376893f ),
  1458. Point3F( -0.199718f, -0.359865f, -0.911378f ),
  1459. Point3F( 0.485072f, 0.121233f, -0.866030f ),
  1460. Point3F( 0.467163f, -0.874294f, 0.131792f ),
  1461. Point3F( -0.638953f, -0.716603f, 0.279677f ),
  1462. Point3F( -0.622710f, 0.047813f, -0.780990f ),
  1463. Point3F( 0.828724f, -0.054433f, -0.557004f ),
  1464. Point3F( 0.130241f, 0.991080f, 0.028245f ),
  1465. Point3F( 0.310995f, -0.950076f, -0.025242f ),
  1466. Point3F( 0.818118f, 0.275336f, 0.504850f ),
  1467. Point3F( 0.676328f, 0.387023f, 0.626733f ),
  1468. Point3F( -0.100433f, 0.495114f, -0.863004f ),
  1469. Point3F( -0.949609f, -0.240681f, -0.200786f ),
  1470. Point3F( -0.102610f, 0.261831f, -0.959644f ),
  1471. Point3F( -0.845732f, -0.493136f, 0.203850f ),
  1472. Point3F( 0.672617f, -0.738838f, 0.041290f ),
  1473. Point3F( 0.380465f, 0.875938f, 0.296613f ),
  1474. Point3F( -0.811223f, 0.262027f, -0.522742f ),
  1475. Point3F( -0.074423f, -0.775670f, -0.626736f ),
  1476. Point3F( -0.286499f, 0.755850f, -0.588735f ),
  1477. Point3F( 0.291182f, -0.276189f, -0.915933f ),
  1478. Point3F( -0.638117f, 0.199338f, 0.743687f ),
  1479. Point3F( 0.439922f, -0.864433f, -0.243359f ),
  1480. Point3F( 0.177649f, 0.206919f, 0.962094f ),
  1481. Point3F( 0.277107f, 0.948521f, 0.153361f ),
  1482. Point3F( 0.507629f, 0.661918f, -0.551523f ),
  1483. Point3F( -0.503110f, -0.579308f, -0.641313f ),
  1484. Point3F( 0.600522f, 0.736495f, -0.311364f ),
  1485. Point3F( -0.691096f, -0.715301f, -0.103592f ),
  1486. Point3F( -0.041083f, -0.858497f, 0.511171f ),
  1487. Point3F( 0.207773f, -0.480062f, -0.852274f ),
  1488. Point3F( 0.795719f, 0.464614f, 0.388543f ),
  1489. Point3F( -0.100433f, 0.495114f, -0.863004f ),
  1490. Point3F( 0.703249f, 0.065157f, -0.707951f ),
  1491. Point3F( -0.324171f, -0.941112f, 0.096024f ),
  1492. Point3F( -0.134933f, -0.940212f, 0.312722f ),
  1493. Point3F( -0.438240f, 0.752088f, -0.492249f ),
  1494. Point3F( 0.964762f, -0.198855f, 0.172311f ),
  1495. Point3F( -0.831799f, 0.196807f, 0.519015f ),
  1496. Point3F( -0.508008f, 0.819902f, 0.263986f ),
  1497. Point3F( 0.471075f, -0.001146f, 0.882092f ),
  1498. Point3F( 0.919512f, 0.246162f, -0.306435f ),
  1499. Point3F( -0.960050f, 0.279828f, -0.001187f ),
  1500. Point3F( 0.110232f, -0.847535f, -0.519165f ),
  1501. Point3F( 0.208229f, 0.697360f, 0.685806f ),
  1502. Point3F( -0.199680f, -0.560621f, 0.803637f ),
  1503. Point3F( 0.170135f, -0.679985f, -0.713214f ),
  1504. Point3F( 0.758371f, -0.494907f, 0.424195f ),
  1505. Point3F( 0.077734f, -0.755978f, 0.649965f ),
  1506. Point3F( 0.612831f, -0.672475f, 0.414987f ),
  1507. Point3F( 0.142776f, 0.836698f, -0.528726f ),
  1508. Point3F( -0.765185f, 0.635778f, 0.101382f ),
  1509. Point3F( 0.669873f, -0.419737f, 0.612447f ),
  1510. Point3F( 0.593549f, 0.194879f, 0.780847f ),
  1511. Point3F( 0.646930f, 0.752173f, 0.125368f ),
  1512. Point3F( 0.837721f, 0.545266f, -0.030127f ),
  1513. Point3F( 0.541505f, 0.768070f, 0.341820f ),
  1514. Point3F( 0.760679f, -0.365715f, -0.536301f ),
  1515. Point3F( 0.381516f, 0.640377f, 0.666605f ),
  1516. Point3F( 0.565794f, -0.072415f, -0.821361f ),
  1517. Point3F( -0.466072f, -0.401588f, 0.788356f ),
  1518. Point3F( 0.987146f, 0.096290f, 0.127560f ),
  1519. Point3F( 0.509709f, -0.688886f, -0.515396f ),
  1520. Point3F( -0.135132f, -0.988046f, -0.074192f ),
  1521. Point3F( 0.600499f, 0.476471f, -0.642166f ),
  1522. Point3F( -0.732326f, -0.275320f, -0.622815f ),
  1523. Point3F( -0.881141f, -0.470404f, 0.048078f ),
  1524. Point3F( 0.051548f, 0.601042f, 0.797553f ),
  1525. Point3F( 0.402027f, -0.763183f, 0.505891f ),
  1526. Point3F( 0.404233f, -0.208288f, 0.890624f ),
  1527. Point3F( -0.311793f, 0.343843f, 0.885752f ),
  1528. Point3F( 0.098132f, -0.937014f, 0.335223f ),
  1529. Point3F( 0.537158f, 0.830585f, -0.146936f ),
  1530. Point3F( 0.725277f, 0.298172f, -0.620538f ),
  1531. Point3F( -0.882025f, 0.342976f, -0.323110f ),
  1532. Point3F( -0.668829f, 0.424296f, -0.610443f ),
  1533. Point3F( -0.408835f, -0.476442f, -0.778368f ),
  1534. Point3F( 0.809472f, 0.397249f, -0.432375f ),
  1535. Point3F( -0.909184f, -0.205938f, -0.361903f ),
  1536. Point3F( 0.866930f, -0.347934f, -0.356895f ),
  1537. Point3F( 0.911660f, -0.141281f, -0.385897f ),
  1538. Point3F( -0.431404f, -0.844074f, -0.318480f ),
  1539. Point3F( -0.950593f, -0.073496f, 0.301614f ),
  1540. Point3F( -0.719716f, 0.626915f, -0.298305f ),
  1541. Point3F( -0.779887f, 0.617067f, -0.104899f ),
  1542. Point3F( -0.475899f, -0.542630f, 0.692151f ),
  1543. Point3F( 0.081952f, -0.157248f, -0.984153f ),
  1544. Point3F( 0.923990f, -0.381662f, -0.024025f ),
  1545. Point3F( -0.957998f, 0.120979f, -0.260008f ),
  1546. Point3F( 0.306601f, 0.227975f, -0.924134f ),
  1547. Point3F( -0.141244f, 0.989182f, 0.039601f ),
  1548. Point3F( 0.077097f, 0.186288f, -0.979466f ),
  1549. Point3F( -0.630407f, -0.259801f, 0.731499f ),
  1550. Point3F( 0.718150f, 0.637408f, 0.279233f ),
  1551. Point3F( 0.340946f, 0.110494f, 0.933567f ),
  1552. Point3F( -0.396671f, 0.503020f, -0.767869f ),
  1553. Point3F( 0.636943f, -0.245005f, 0.730942f ),
  1554. Point3F( -0.849605f, -0.518660f, -0.095724f ),
  1555. Point3F( -0.388203f, 0.105395f, 0.915527f ),
  1556. Point3F( -0.280671f, -0.776541f, -0.564099f ),
  1557. Point3F( -0.601680f, 0.215451f, -0.769131f ),
  1558. Point3F( -0.660112f, -0.632371f, -0.405412f ),
  1559. Point3F( 0.921096f, 0.284072f, 0.266242f ),
  1560. Point3F( 0.074850f, -0.300846f, 0.950731f ),
  1561. Point3F( 0.943952f, -0.067062f, 0.323198f ),
  1562. Point3F( -0.917838f, -0.254589f, 0.304561f ),
  1563. Point3F( 0.889843f, -0.409008f, 0.202219f ),
  1564. Point3F( -0.565849f, 0.753721f, -0.334246f ),
  1565. Point3F( 0.791460f, 0.555918f, -0.254060f ),
  1566. Point3F( 0.261936f, 0.703590f, -0.660568f ),
  1567. Point3F( -0.234406f, 0.952084f, 0.196444f ),
  1568. Point3F( 0.111205f, 0.979492f, -0.168014f ),
  1569. Point3F( -0.869844f, -0.109095f, -0.481113f ),
  1570. Point3F( -0.337728f, -0.269701f, -0.901777f ),
  1571. Point3F( 0.366793f, 0.408875f, -0.835634f ),
  1572. Point3F( -0.098749f, 0.261316f, 0.960189f ),
  1573. Point3F( -0.272379f, -0.847100f, 0.456324f ),
  1574. Point3F( -0.319506f, 0.287444f, -0.902935f ),
  1575. Point3F( 0.873383f, -0.294109f, 0.388203f ),
  1576. Point3F( -0.088950f, 0.710450f, 0.698104f ),
  1577. Point3F( 0.551238f, -0.786552f, 0.278340f ),
  1578. Point3F( 0.724436f, -0.663575f, -0.186712f ),
  1579. Point3F( 0.529741f, -0.606539f, 0.592861f ),
  1580. Point3F( -0.949743f, -0.282514f, 0.134809f ),
  1581. Point3F( 0.155047f, 0.419442f, -0.894443f ),
  1582. Point3F( -0.562653f, -0.329139f, -0.758346f ),
  1583. Point3F( 0.816407f, -0.576953f, 0.024576f ),
  1584. Point3F( 0.178550f, -0.950242f, -0.255266f ),
  1585. Point3F( 0.479571f, 0.706691f, 0.520192f ),
  1586. Point3F( 0.391687f, 0.559884f, -0.730145f ),
  1587. Point3F( 0.724872f, -0.205570f, -0.657496f ),
  1588. Point3F( -0.663196f, -0.517587f, -0.540624f ),
  1589. Point3F( -0.660054f, -0.122486f, -0.741165f ),
  1590. Point3F( -0.531989f, 0.374711f, -0.759328f ),
  1591. Point3F( 0.194979f, -0.059120f, 0.979024f )
  1592. };
  1593. U8 TSMesh::encodeNormal( const Point3F &normal )
  1594. {
  1595. U8 bestIndex = 0;
  1596. F32 bestDot = -10E30f;
  1597. for ( U32 i = 0; i < 256; i++ )
  1598. {
  1599. F32 dot = mDot( normal, smU8ToNormalTable[i] );
  1600. if ( dot > bestDot )
  1601. {
  1602. bestIndex = i;
  1603. bestDot = dot;
  1604. }
  1605. }
  1606. return bestIndex;
  1607. }
  1608. //-----------------------------------------------------
  1609. // TSMesh assemble from/ dissemble to memory buffer
  1610. //-----------------------------------------------------
  1611. #define tsalloc TSShape::smTSAlloc
  1612. TSMesh* TSMesh::assembleMesh( U32 meshType, bool skip )
  1613. {
  1614. static TSMesh tempStandardMesh;
  1615. static TSSkinMesh tempSkinMesh;
  1616. static TSDecalMesh tempDecalMesh;
  1617. static TSSortedMesh tempSortedMesh;
  1618. bool justSize = skip || !tsalloc.allocShape32(0); // if this returns NULL, we're just sizing memory block
  1619. // a little funny business because we pretend decals are derived from meshes
  1620. S32 * ret = NULL;
  1621. TSMesh * mesh = NULL;
  1622. TSDecalMesh * decal = NULL;
  1623. if ( justSize )
  1624. {
  1625. switch ( meshType )
  1626. {
  1627. case StandardMeshType :
  1628. {
  1629. ret = (S32*)&tempStandardMesh;
  1630. mesh = &tempStandardMesh;
  1631. tsalloc.allocShape32( sizeof(TSMesh) >> 2 );
  1632. break;
  1633. }
  1634. case SkinMeshType :
  1635. {
  1636. ret = (S32*)&tempSkinMesh;
  1637. mesh = &tempSkinMesh;
  1638. tsalloc.allocShape32( sizeof(TSSkinMesh) >> 2 );
  1639. break;
  1640. }
  1641. case DecalMeshType :
  1642. {
  1643. ret = (S32*)&tempDecalMesh;
  1644. decal = &tempDecalMesh;
  1645. tsalloc.allocShape32( sizeof(TSDecalMesh) >> 2 );
  1646. break;
  1647. }
  1648. case SortedMeshType :
  1649. {
  1650. ret = (S32*)&tempSortedMesh;
  1651. mesh = &tempSortedMesh;
  1652. tsalloc.allocShape32( sizeof(TSSortedMesh) >> 2 );
  1653. break;
  1654. }
  1655. }
  1656. }
  1657. else
  1658. {
  1659. switch ( meshType )
  1660. {
  1661. case StandardMeshType :
  1662. {
  1663. ret = tsalloc.allocShape32( sizeof(TSMesh) >> 2 );
  1664. constructInPlace( (TSMesh*)ret );
  1665. mesh = (TSMesh*)ret;
  1666. break;
  1667. }
  1668. case SkinMeshType :
  1669. {
  1670. ret = tsalloc.allocShape32( sizeof(TSSkinMesh) >> 2 );
  1671. constructInPlace( (TSSkinMesh*)ret );
  1672. mesh = (TSSkinMesh*)ret;
  1673. break;
  1674. }
  1675. case DecalMeshType :
  1676. {
  1677. ret = tsalloc.allocShape32( sizeof(TSDecalMesh) >> 2 );
  1678. constructInPlace((TSDecalMesh*)ret);
  1679. decal = (TSDecalMesh*)ret;
  1680. break;
  1681. }
  1682. case SortedMeshType :
  1683. {
  1684. ret = tsalloc.allocShape32( sizeof(TSSortedMesh) >> 2 );
  1685. constructInPlace( (TSSortedMesh*)ret );
  1686. mesh = (TSSortedMesh*)ret;
  1687. break;
  1688. }
  1689. }
  1690. }
  1691. tsalloc.setSkipMode( skip );
  1692. if ( mesh )
  1693. mesh->assemble( skip );
  1694. if ( decal )
  1695. decal->assemble( skip );
  1696. tsalloc.setSkipMode( false );
  1697. return (TSMesh*)ret;
  1698. }
  1699. void TSMesh::convertToTris( const TSDrawPrimitive *primitivesIn,
  1700. const S32 *indicesIn,
  1701. S32 numPrimIn,
  1702. S32 &numPrimOut,
  1703. S32 &numIndicesOut,
  1704. TSDrawPrimitive *primitivesOut,
  1705. S32 *indicesOut ) const
  1706. {
  1707. S32 prevMaterial = -99999;
  1708. TSDrawPrimitive * newDraw = NULL;
  1709. numPrimOut = 0;
  1710. numIndicesOut = 0;
  1711. for ( S32 i = 0; i < numPrimIn; i++ )
  1712. {
  1713. S32 newMat = primitivesIn[i].matIndex;
  1714. newMat &= ~TSDrawPrimitive::TypeMask;
  1715. U32 start = primitivesIn[i].start;
  1716. U32 prevStart = (i > 0) ? primitivesIn[i-1].start : start;
  1717. U32 numElements = primitivesIn[i].numElements;
  1718. // Add a new primitive if changing materials, or if this primitive
  1719. // indexes vertices in a different 16-bit range
  1720. if ( ( newMat != prevMaterial ) ||
  1721. ((indicesIn[prevStart] ^ indicesIn[start]) & 0xFFFF0000) )
  1722. {
  1723. if ( primitivesOut )
  1724. {
  1725. newDraw = &primitivesOut[numPrimOut];
  1726. newDraw->start = numIndicesOut;
  1727. newDraw->numElements = 0;
  1728. newDraw->matIndex = newMat | TSDrawPrimitive::Triangles;
  1729. }
  1730. numPrimOut++;
  1731. prevMaterial = newMat;
  1732. }
  1733. // gonna depend on what kind of primitive it is...
  1734. if ( (primitivesIn[i].matIndex & TSDrawPrimitive::TypeMask) == TSDrawPrimitive::Triangles)
  1735. {
  1736. for ( S32 j = 0; j < numElements; j += 3 )
  1737. {
  1738. if ( indicesOut )
  1739. {
  1740. indicesOut[numIndicesOut + 0] = indicesIn[start + j + 0];
  1741. indicesOut[numIndicesOut + 1] = indicesIn[start + j + 1];
  1742. indicesOut[numIndicesOut + 2] = indicesIn[start + j + 2];
  1743. }
  1744. if ( newDraw )
  1745. newDraw->numElements += 3;
  1746. numIndicesOut += 3;
  1747. }
  1748. }
  1749. else
  1750. {
  1751. U32 idx0 = indicesIn[start + 0];
  1752. U32 idx1;
  1753. U32 idx2 = indicesIn[start + 1];
  1754. U32 * nextIdx = &idx1;
  1755. for ( S32 j = 2; j < numElements; j++ )
  1756. {
  1757. *nextIdx = idx2;
  1758. nextIdx = (U32*) ( (dsize_t)nextIdx ^ (dsize_t)&idx0 ^ (dsize_t)&idx1);
  1759. idx2 = indicesIn[start + j];
  1760. if ( idx0 == idx1 || idx1 == idx2 || idx2 == idx0 )
  1761. continue;
  1762. if ( indicesOut )
  1763. {
  1764. indicesOut[numIndicesOut+0] = idx0;
  1765. indicesOut[numIndicesOut+1] = idx1;
  1766. indicesOut[numIndicesOut+2] = idx2;
  1767. }
  1768. if ( newDraw )
  1769. newDraw->numElements += 3;
  1770. numIndicesOut += 3;
  1771. }
  1772. }
  1773. }
  1774. }
  1775. void unwindStrip( const S32 * indices, S32 numElements, Vector<S32> &triIndices )
  1776. {
  1777. U32 idx0 = indices[0];
  1778. U32 idx1;
  1779. U32 idx2 = indices[1];
  1780. U32 * nextIdx = &idx1;
  1781. for ( S32 j = 2; j < numElements; j++ )
  1782. {
  1783. *nextIdx = idx2;
  1784. nextIdx = (U32*) ( (dsize_t)nextIdx ^ (dsize_t)&idx0 ^ (dsize_t)&idx1);
  1785. idx2 = indices[j];
  1786. if ( idx0 == idx1 || idx1 == idx2 || idx2 == idx0 )
  1787. continue;
  1788. triIndices.push_back( idx0 );
  1789. triIndices.push_back( idx1 );
  1790. triIndices.push_back( idx2 );
  1791. }
  1792. }
  1793. void TSMesh::convertToSingleStrip( const TSDrawPrimitive *primitivesIn,
  1794. const S32 *indicesIn,
  1795. S32 numPrimIn,
  1796. S32 &numPrimOut,
  1797. S32 &numIndicesOut,
  1798. TSDrawPrimitive *primitivesOut,
  1799. S32 *indicesOut ) const
  1800. {
  1801. S32 prevMaterial = -99999;
  1802. TSDrawPrimitive * newDraw = NULL;
  1803. TSDrawPrimitive * newTris = NULL;
  1804. Vector<S32> triIndices;
  1805. S32 curDrawOut = 0;
  1806. numPrimOut = 0;
  1807. numIndicesOut = 0;
  1808. for ( S32 i = 0; i < numPrimIn; i++ )
  1809. {
  1810. S32 newMat = primitivesIn[i].matIndex;
  1811. U32 start = primitivesIn[i].start;
  1812. U32 prevStart = (i > 0) ? primitivesIn[i-1].start : start;
  1813. U32 numElements = primitivesIn[i].numElements;
  1814. // Add a new primitive if changing materials, or if this primitive
  1815. // indexes vertices in a different 16-bit range
  1816. if ( ( newMat != prevMaterial ) ||
  1817. ((indicesIn[prevStart] ^ indicesIn[start]) & 0xFFFF0000) )
  1818. {
  1819. // before adding the new primitive, transfer triangle indices
  1820. if ( triIndices.size() )
  1821. {
  1822. if ( newTris && indicesOut )
  1823. {
  1824. newTris->start = numIndicesOut;
  1825. newTris->numElements = triIndices.size();
  1826. dMemcpy(&indicesOut[numIndicesOut],triIndices.address(),triIndices.size()*sizeof(U32));
  1827. }
  1828. numIndicesOut += triIndices.size();
  1829. triIndices.clear();
  1830. newTris = NULL;
  1831. }
  1832. if ( primitivesOut )
  1833. {
  1834. newDraw = &primitivesOut[numPrimOut];
  1835. newDraw->start = numIndicesOut;
  1836. newDraw->numElements = 0;
  1837. newDraw->matIndex = newMat;
  1838. }
  1839. numPrimOut++;
  1840. curDrawOut = 0;
  1841. prevMaterial = newMat;
  1842. }
  1843. // gonna depend on what kind of primitive it is...
  1844. // from above we know it's the same kind as the one we're building...
  1845. if ( (primitivesIn[i].matIndex & TSDrawPrimitive::TypeMask) == TSDrawPrimitive::Triangles)
  1846. {
  1847. // triangles primitive...add to it
  1848. for ( S32 j = 0; j < numElements; j += 3 )
  1849. {
  1850. if ( indicesOut )
  1851. {
  1852. indicesOut[numIndicesOut + 0] = indicesIn[start + j + 0];
  1853. indicesOut[numIndicesOut + 1] = indicesIn[start + j + 1];
  1854. indicesOut[numIndicesOut + 2] = indicesIn[start + j + 2];
  1855. }
  1856. if ( newDraw )
  1857. newDraw->numElements += 3;
  1858. numIndicesOut += 3;
  1859. }
  1860. }
  1861. else
  1862. {
  1863. // strip primitive...
  1864. // if numElements less than smSmallestStripSize, add to triangles...
  1865. if ( numElements < smMinStripSize + 2 )
  1866. {
  1867. // put triangle indices aside until material changes...
  1868. if ( triIndices.empty() )
  1869. {
  1870. // set up for new triangle primitive and add it if we are copying data right now
  1871. if ( primitivesOut )
  1872. {
  1873. newTris = &primitivesOut[numPrimOut];
  1874. newTris->matIndex = newMat;
  1875. newTris->matIndex &= ~(TSDrawPrimitive::Triangles|TSDrawPrimitive::Strip);
  1876. newTris->matIndex |= TSDrawPrimitive::Triangles;
  1877. }
  1878. numPrimOut++;
  1879. }
  1880. unwindStrip( indicesIn + start, numElements, triIndices );
  1881. }
  1882. else
  1883. {
  1884. // strip primitive...add to it
  1885. if ( indicesOut )
  1886. {
  1887. if ( curDrawOut & 1 )
  1888. {
  1889. indicesOut[numIndicesOut + 0] = indicesOut[numIndicesOut - 1];
  1890. indicesOut[numIndicesOut + 1] = indicesOut[numIndicesOut - 1];
  1891. indicesOut[numIndicesOut + 2] = indicesIn[start];
  1892. dMemcpy(indicesOut+numIndicesOut+3,indicesIn+start,numElements*sizeof(U32));
  1893. }
  1894. else if ( curDrawOut )
  1895. {
  1896. indicesOut[numIndicesOut + 0] = indicesOut[numIndicesOut - 1];
  1897. indicesOut[numIndicesOut + 1] = indicesIn[start];
  1898. dMemcpy(indicesOut+numIndicesOut+2,indicesIn+start,numElements*sizeof(U32));
  1899. }
  1900. else
  1901. dMemcpy(indicesOut+numIndicesOut,indicesIn+start,numElements*sizeof(U32));
  1902. }
  1903. S32 added = numElements;
  1904. added += curDrawOut ? (curDrawOut&1 ? 3 : 2) : 0;
  1905. if ( newDraw )
  1906. newDraw->numElements += added;
  1907. numIndicesOut += added;
  1908. curDrawOut += added;
  1909. }
  1910. }
  1911. }
  1912. // spit out tris before leaving
  1913. // before adding the new primitive, transfer triangle indices
  1914. if ( triIndices.size() )
  1915. {
  1916. if ( newTris && indicesOut )
  1917. {
  1918. newTris->start = numIndicesOut;
  1919. newTris->numElements = triIndices.size();
  1920. dMemcpy(&indicesOut[numIndicesOut],triIndices.address(),triIndices.size()*sizeof(U32));
  1921. }
  1922. numIndicesOut += triIndices.size();
  1923. triIndices.clear();
  1924. newTris = NULL;
  1925. }
  1926. }
  1927. // this method does none of the converting that the above methods do, except that small strips are converted
  1928. // to triangle lists...
  1929. void TSMesh::leaveAsMultipleStrips( const TSDrawPrimitive *primitivesIn,
  1930. const S32 *indicesIn,
  1931. S32 numPrimIn,
  1932. S32 &numPrimOut,
  1933. S32 &numIndicesOut,
  1934. TSDrawPrimitive *primitivesOut,
  1935. S32 *indicesOut ) const
  1936. {
  1937. S32 prevMaterial = -99999;
  1938. TSDrawPrimitive * newDraw = NULL;
  1939. Vector<S32> triIndices;
  1940. numPrimOut = 0;
  1941. numIndicesOut = 0;
  1942. for ( S32 i = 0; i < numPrimIn; i++ )
  1943. {
  1944. S32 newMat = primitivesIn[i].matIndex;
  1945. U32 start = primitivesIn[i].start;
  1946. U32 prevStart = (i > 0) ? primitivesIn[i-1].start : start;
  1947. U32 numElements = primitivesIn[i].numElements;
  1948. // Add a new primitive if changing materials, or if this primitive
  1949. // indexes vertices in a different 16-bit range
  1950. if ( triIndices.size() &&
  1951. (( newMat != prevMaterial ) ||
  1952. ((indicesIn[prevStart] ^ indicesIn[start]) & 0xFFFF0000) ))
  1953. {
  1954. // material just changed and we have triangles lying around
  1955. // add primitive and indices for triangles and clear triIndices
  1956. if ( indicesOut )
  1957. {
  1958. TSDrawPrimitive * newTris = &primitivesOut[numPrimOut];
  1959. newTris->matIndex = prevMaterial;
  1960. newTris->matIndex &= ~(TSDrawPrimitive::Triangles|TSDrawPrimitive::Strip);
  1961. newTris->matIndex |= TSDrawPrimitive::Triangles;
  1962. newTris->start = numIndicesOut;
  1963. newTris->numElements = triIndices.size();
  1964. dMemcpy(&indicesOut[numIndicesOut],triIndices.address(),triIndices.size()*sizeof(U32));
  1965. }
  1966. numPrimOut++;
  1967. numIndicesOut += triIndices.size();
  1968. triIndices.clear();
  1969. }
  1970. // this is a little convoluted because this code was adapted from convertToSingleStrip
  1971. // but we will need a new primitive only if it is a triangle primitive coming in
  1972. // or we have more elements than the min strip size...
  1973. if ( (primitivesIn[i].matIndex & TSDrawPrimitive::TypeMask) == TSDrawPrimitive::Triangles || numElements>=smMinStripSize+2)
  1974. {
  1975. if ( primitivesOut )
  1976. {
  1977. newDraw = &primitivesOut[numPrimOut];
  1978. newDraw->start = numIndicesOut;
  1979. newDraw->numElements = 0;
  1980. newDraw->matIndex = newMat;
  1981. }
  1982. numPrimOut++;
  1983. }
  1984. prevMaterial = newMat;
  1985. // gonna depend on what kind of primitive it is...
  1986. // from above we know it's the same kind as the one we're building...
  1987. if ( (primitivesIn[i].matIndex & TSDrawPrimitive::TypeMask) == TSDrawPrimitive::Triangles)
  1988. {
  1989. // triangles primitive...add to it
  1990. for ( S32 j = 0; j < numElements; j += 3 )
  1991. {
  1992. if ( indicesOut )
  1993. {
  1994. indicesOut[numIndicesOut + 0] = indicesIn[start + j + 0];
  1995. indicesOut[numIndicesOut + 1] = indicesIn[start + j + 1];
  1996. indicesOut[numIndicesOut + 2] = indicesIn[start + j + 2];
  1997. }
  1998. if ( newDraw )
  1999. newDraw->numElements += 3;
  2000. numIndicesOut += 3;
  2001. }
  2002. }
  2003. else
  2004. {
  2005. // strip primitive...
  2006. // if numElements less than smSmallestStripSize, add to triangles...
  2007. if ( numElements < smMinStripSize + 2 )
  2008. // put triangle indices aside until material changes...
  2009. unwindStrip( indicesIn + start, numElements, triIndices );
  2010. else
  2011. {
  2012. // strip primitive...add to it
  2013. if ( indicesOut )
  2014. dMemcpy(indicesOut+numIndicesOut,indicesIn+start,numElements*sizeof(U32));
  2015. if ( newDraw )
  2016. newDraw->numElements = numElements;
  2017. numIndicesOut += numElements;
  2018. }
  2019. }
  2020. }
  2021. // spit out tris before leaving
  2022. if ( triIndices.size() )
  2023. {
  2024. // material just changed and we have triangles lying around
  2025. // add primitive and indices for triangles and clear triIndices
  2026. if ( indicesOut )
  2027. {
  2028. TSDrawPrimitive *newTris = &primitivesOut[numPrimOut];
  2029. newTris->matIndex = prevMaterial;
  2030. newTris->matIndex &= ~(TSDrawPrimitive::Triangles|TSDrawPrimitive::Strip);
  2031. newTris->matIndex |= TSDrawPrimitive::Triangles;
  2032. newTris->start = numIndicesOut;
  2033. newTris->numElements = triIndices.size();
  2034. dMemcpy(&indicesOut[numIndicesOut],triIndices.address(),triIndices.size()*sizeof(U32));
  2035. }
  2036. numPrimOut++;
  2037. numIndicesOut += triIndices.size();
  2038. triIndices.clear();
  2039. }
  2040. }
  2041. // This method retrieves data that is shared (or possibly shared) between different meshes.
  2042. // This adds an extra step to the copying of data from the memory buffer to the shape data buffer.
  2043. // If we have no parentMesh, then we either return a pointer to the data in the memory buffer
  2044. // (in the case that we skip this mesh) or copy the data into the shape data buffer and return
  2045. // that pointer (in the case that we don't skip this mesh).
  2046. // If we do have a parent mesh, then we return a pointer to the data in the shape buffer,
  2047. // copying the data in there ourselves if our parent didn't already do it (i.e., if it was skipped).
  2048. S32 * TSMesh::getSharedData32( S32 parentMesh, S32 size, S32 **source, bool skip )
  2049. {
  2050. S32 * ptr;
  2051. if( parentMesh < 0 )
  2052. ptr = skip ? tsalloc.getPointer32( size ) : tsalloc.copyToShape32( size );
  2053. else
  2054. {
  2055. ptr = source[parentMesh];
  2056. // if we skipped the previous mesh (and we're not skipping this one) then
  2057. // we still need to copy points into the shape...
  2058. if ( !smDataCopied[parentMesh] && !skip )
  2059. {
  2060. S32 * tmp = ptr;
  2061. ptr = tsalloc.allocShape32( size );
  2062. if ( ptr && tmp )
  2063. dMemcpy(ptr, tmp, size * sizeof(S32) );
  2064. }
  2065. }
  2066. return ptr;
  2067. }
  2068. S8 * TSMesh::getSharedData8( S32 parentMesh, S32 size, S8 **source, bool skip )
  2069. {
  2070. S8 * ptr;
  2071. if( parentMesh < 0 )
  2072. ptr = skip ? tsalloc.getPointer8( size ) : tsalloc.copyToShape8( size );
  2073. else
  2074. {
  2075. ptr = source[parentMesh];
  2076. // if we skipped the previous mesh (and we're not skipping this one) then
  2077. // we still need to copy points into the shape...
  2078. if ( !smDataCopied[parentMesh] && !skip )
  2079. {
  2080. S8 * tmp = ptr;
  2081. ptr = tsalloc.allocShape8( size );
  2082. if ( ptr && tmp )
  2083. dMemcpy( ptr, tmp, size * sizeof(S32) );
  2084. }
  2085. }
  2086. return ptr;
  2087. }
  2088. void TSMesh::dumpPrimitives(U32 startVertex, U32 startIndex, GFXPrimitive *piArray, U16* ibIndices)
  2089. {
  2090. // go through and create PrimitiveInfo array
  2091. GFXPrimitive pInfo;
  2092. U32 primitivesSize = primitives.size();
  2093. for (U32 i = 0; i < primitivesSize; i++)
  2094. {
  2095. const TSDrawPrimitive & draw = primitives[i];
  2096. GFXPrimitiveType drawType = getDrawType(draw.matIndex >> 30);
  2097. switch (drawType)
  2098. {
  2099. case GFXTriangleList:
  2100. pInfo.type = drawType;
  2101. pInfo.numPrimitives = draw.numElements / 3;
  2102. pInfo.startIndex = startIndex + draw.start;
  2103. // Use the first index to determine which 16-bit address space we are operating in
  2104. pInfo.startVertex = (indices[draw.start] & 0xFFFF0000); // TODO: figure out a good solution for this
  2105. pInfo.minIndex = 0; // minIndex are zero based index relative to startVertex. See @GFXDevice
  2106. pInfo.numVertices = getMin((U32)0x10000, mNumVerts - pInfo.startVertex);
  2107. pInfo.startVertex += startVertex;
  2108. break;
  2109. case GFXTriangleStrip:
  2110. pInfo.type = drawType;
  2111. pInfo.numPrimitives = draw.numElements - 2;
  2112. pInfo.startIndex = startIndex + draw.start;
  2113. // Use the first index to determine which 16-bit address space we are operating in
  2114. pInfo.startVertex = (indices[draw.start] & 0xFFFF0000); // TODO: figure out a good solution for this
  2115. pInfo.minIndex = 0; // minIndex are zero based index relative to startVertex. See @GFXDevice
  2116. pInfo.numVertices = getMin((U32)0x10000, mNumVerts - pInfo.startVertex);
  2117. pInfo.startVertex += startVertex;
  2118. break;
  2119. default:
  2120. AssertFatal(false, "WTF?!");
  2121. }
  2122. *piArray++ = pInfo;
  2123. }
  2124. dCopyArray(ibIndices, indices.address(), indices.size());
  2125. }
  2126. void TSMesh::assemble( bool skip )
  2127. {
  2128. tsalloc.checkGuard();
  2129. numFrames = tsalloc.get32();
  2130. numMatFrames = tsalloc.get32();
  2131. parentMesh = tsalloc.get32();
  2132. tsalloc.get32( (S32*)&mBounds, 6 );
  2133. tsalloc.get32( (S32*)&mCenter, 3 );
  2134. mRadius = (F32)tsalloc.get32();
  2135. if (TSShape::smReadVersion >= 27)
  2136. {
  2137. // Offsetted
  2138. mVertOffset = tsalloc.get32();
  2139. mNumVerts = tsalloc.get32();
  2140. mVertSize = tsalloc.get32();
  2141. }
  2142. else
  2143. {
  2144. mVertOffset = 0;
  2145. mNumVerts = 0;
  2146. mVertSize = 0;
  2147. }
  2148. S32 numVerts = tsalloc.get32();
  2149. S32 *ptr32 = getSharedData32( parentMesh, 3 * numVerts, (S32**)smVertsList.address(), skip );
  2150. verts.set( (Point3F*)ptr32, numVerts );
  2151. S32 numTVerts = tsalloc.get32();
  2152. ptr32 = getSharedData32( parentMesh, 2 * numTVerts, (S32**)smTVertsList.address(), skip );
  2153. tverts.set( (Point2F*)ptr32, numTVerts );
  2154. if ( TSShape::smReadVersion > 25 )
  2155. {
  2156. numTVerts = tsalloc.get32();
  2157. ptr32 = getSharedData32( parentMesh, 2 * numTVerts, (S32**)smTVerts2List.address(), skip );
  2158. tverts2.set( (Point2F*)ptr32, numTVerts );
  2159. S32 numVColors = tsalloc.get32();
  2160. ptr32 = getSharedData32( parentMesh, numVColors, (S32**)smColorsList.address(), skip );
  2161. colors.set( (ColorI*)ptr32, numVColors );
  2162. }
  2163. S8 *ptr8;
  2164. if ( TSShape::smReadVersion > 21 && TSMesh::smUseEncodedNormals)
  2165. {
  2166. // we have encoded normals and we want to use them...
  2167. if ( parentMesh < 0 )
  2168. tsalloc.getPointer32( numVerts * 3 ); // adva nce past norms, don't use
  2169. norms.set( NULL, 0 );
  2170. ptr8 = getSharedData8( parentMesh, numVerts, (S8**)smEncodedNormsList.address(), skip );
  2171. encodedNorms.set( ptr8, numVerts );
  2172. }
  2173. else if ( TSShape::smReadVersion > 21 )
  2174. {
  2175. // we have encoded normals but we don't want to use them...
  2176. ptr32 = getSharedData32( parentMesh, 3 * numVerts, (S32**)smNormsList.address(), skip );
  2177. norms.set( (Point3F*)ptr32, numVerts );
  2178. if ( parentMesh < 0 )
  2179. tsalloc.getPointer8( numVerts ); // advance past encoded normls, don't use
  2180. encodedNorms.set( NULL, 0 );
  2181. }
  2182. else
  2183. {
  2184. // no encoded normals...
  2185. ptr32 = getSharedData32( parentMesh, 3 * numVerts, (S32**)smNormsList.address(), skip );
  2186. norms.set( (Point3F*)ptr32, numVerts );
  2187. encodedNorms.set( NULL, 0 );
  2188. }
  2189. // copy the primitives and indices...how we do this depends on what
  2190. // form we want them in when copied...just get pointers to data for now
  2191. S32 szPrimIn, szIndIn;
  2192. TSDrawPrimitive *primIn;
  2193. S32 *indIn;
  2194. bool deleteInputArrays = false;
  2195. if (TSShape::smReadVersion > 25)
  2196. {
  2197. // mesh primitives (start, numElements) and indices are stored as 32 bit values
  2198. szPrimIn = tsalloc.get32();
  2199. primIn = (TSDrawPrimitive*)tsalloc.getPointer32(szPrimIn*3);
  2200. szIndIn = tsalloc.get32();
  2201. indIn = tsalloc.getPointer32(szIndIn);
  2202. }
  2203. else
  2204. {
  2205. // mesh primitives (start, numElements) indices are stored as 16 bit values
  2206. szPrimIn = tsalloc.get32();
  2207. S16 *prim16 = tsalloc.getPointer16(szPrimIn*2); // primitive: start, numElements
  2208. S32 *prim32 = tsalloc.getPointer32(szPrimIn); // primitive: matIndex
  2209. szIndIn = tsalloc.get32();
  2210. // warn about non-addressable indices
  2211. if ( !skip && szIndIn >= 0x10000 )
  2212. {
  2213. Con::warnf("Mesh contains non-addressable indices, and may not render "
  2214. "correctly. Either split this mesh into pieces of no more than 65k "
  2215. "unique verts prior to export, or use COLLADA.");
  2216. }
  2217. S16 *ind16 = tsalloc.getPointer16(szIndIn);
  2218. // need to copy to temporary arrays
  2219. deleteInputArrays = true;
  2220. primIn = new TSDrawPrimitive[szPrimIn];
  2221. for (S32 i = 0; i < szPrimIn; i++)
  2222. {
  2223. primIn[i].start = prim16[i*2];
  2224. primIn[i].numElements = prim16[i*2+1];
  2225. primIn[i].matIndex = prim32[i];
  2226. }
  2227. indIn = new S32[szIndIn];
  2228. dCopyArray(indIn, ind16, szIndIn);
  2229. }
  2230. // count the number of output primitives and indices
  2231. S32 szPrimOut = szPrimIn, szIndOut = szIndIn;
  2232. if (smUseTriangles)
  2233. convertToTris(primIn, indIn, szPrimIn, szPrimOut, szIndOut, NULL, NULL);
  2234. else if (smUseOneStrip)
  2235. convertToSingleStrip(primIn, indIn, szPrimIn, szPrimOut, szIndOut, NULL, NULL);
  2236. else
  2237. leaveAsMultipleStrips(primIn, indIn, szPrimIn, szPrimOut, szIndOut, NULL, NULL);
  2238. // allocate enough space for the new primitives and indices (all 32 bits)
  2239. TSDrawPrimitive *primOut = (TSDrawPrimitive*)tsalloc.allocShape32(3*szPrimOut);
  2240. S32 *indOut = tsalloc.allocShape32(szIndOut);
  2241. // copy output primitives and indices
  2242. S32 chkPrim = szPrimOut, chkInd = szIndOut;
  2243. if (smUseTriangles)
  2244. convertToTris(primIn, indIn, szPrimIn, chkPrim, chkInd, primOut, indOut);
  2245. else if (smUseOneStrip)
  2246. convertToSingleStrip(primIn, indIn, szPrimIn, chkPrim, chkInd, primOut, indOut);
  2247. else
  2248. leaveAsMultipleStrips(primIn, indIn, szPrimIn, chkPrim, chkInd, primOut, indOut);
  2249. AssertFatal(chkPrim==szPrimOut && chkInd==szIndOut,"TSMesh::primitive conversion");
  2250. // store output
  2251. primitives.set(primOut, szPrimOut);
  2252. indices.set(indOut, szIndOut);
  2253. // delete temporary arrays if necessary
  2254. if (deleteInputArrays)
  2255. {
  2256. delete [] primIn;
  2257. delete [] indIn;
  2258. }
  2259. S32 sz = tsalloc.get32();
  2260. tsalloc.getPointer16( sz ); // skip deprecated merge indices
  2261. tsalloc.align32();
  2262. vertsPerFrame = tsalloc.get32();
  2263. U32 flags = (U32)tsalloc.get32();
  2264. if ( encodedNorms.size() )
  2265. flags |= UseEncodedNormals;
  2266. setFlags( flags );
  2267. // Set color & tvert2 flags if we have an old version
  2268. if (TSShape::smReadVersion < 27)
  2269. {
  2270. if (colors.size() > 0) setFlags(HasColor);
  2271. if (tverts2.size() > 0) setFlags(HasTVert2);
  2272. mNumVerts = verts.size();
  2273. }
  2274. tsalloc.checkGuard();
  2275. if ( tsalloc.allocShape32( 0 ) && TSShape::smReadVersion < 19 )
  2276. computeBounds(); // only do this if we copied the data...
  2277. createTangents(verts, norms);
  2278. }
  2279. void TSMesh::disassemble()
  2280. {
  2281. tsalloc.setGuard();
  2282. tsalloc.set32( numFrames );
  2283. tsalloc.set32( numMatFrames );
  2284. tsalloc.set32( parentMesh );
  2285. tsalloc.copyToBuffer32( (S32*)&mBounds, 6 );
  2286. tsalloc.copyToBuffer32( (S32*)&mCenter, 3 );
  2287. tsalloc.set32( (S32)mRadius );
  2288. bool shouldMakeEditable = TSShape::smVersion < 27;
  2289. // Re-create the vectors
  2290. if (shouldMakeEditable)
  2291. {
  2292. makeEditable();
  2293. // No Offset
  2294. if (TSShape::smVersion >= 27)
  2295. {
  2296. tsalloc.set32(0);
  2297. tsalloc.set32(0);
  2298. tsalloc.set32(0);
  2299. }
  2300. }
  2301. else
  2302. {
  2303. // Offsetted
  2304. tsalloc.set32(mVertOffset);
  2305. tsalloc.set32(mNumVerts);
  2306. tsalloc.set32(mVertSize);
  2307. AssertFatal(mNumVerts >= vertsPerFrame, "invalid mNumVerts");
  2308. }
  2309. if (TSShape::smVersion >= 27 && mVertexData.isReady())
  2310. {
  2311. // If not editable all arrays are effectively 0.
  2312. tsalloc.set32(0); // verts
  2313. tsalloc.set32(0); // tverts
  2314. tsalloc.set32(0); // tverts2
  2315. tsalloc.set32(0); // colors
  2316. }
  2317. else
  2318. {
  2319. // verts...
  2320. tsalloc.set32(verts.size());
  2321. if (parentMesh < 0)
  2322. tsalloc.copyToBuffer32((S32*)verts.address(), 3 * verts.size()); // if no parent mesh, then save off our verts
  2323. // tverts...
  2324. tsalloc.set32(tverts.size());
  2325. if (parentMesh < 0)
  2326. tsalloc.copyToBuffer32((S32*)tverts.address(), 2 * tverts.size()); // if no parent mesh, then save off our tverts
  2327. if (TSShape::smVersion > 25)
  2328. {
  2329. // tverts2...
  2330. tsalloc.set32(tverts2.size());
  2331. if (parentMesh < 0)
  2332. tsalloc.copyToBuffer32((S32*)tverts2.address(), 2 * tverts2.size()); // if no parent mesh, then save off our tverts
  2333. // colors
  2334. tsalloc.set32(colors.size());
  2335. if (parentMesh < 0)
  2336. tsalloc.copyToBuffer32((S32*)colors.address(), colors.size()); // if no parent mesh, then save off our tverts
  2337. }
  2338. // norms...
  2339. if (parentMesh < 0) // if no parent mesh, then save off our norms
  2340. tsalloc.copyToBuffer32((S32*)norms.address(), 3 * norms.size()); // norms.size()==verts.size() or error...
  2341. // encoded norms...
  2342. if (parentMesh < 0)
  2343. {
  2344. // if no parent mesh, compute encoded normals and copy over
  2345. for (S32 i = 0; i < norms.size(); i++)
  2346. {
  2347. U8 normIdx = encodedNorms.size() ? encodedNorms[i] : encodeNormal(norms[i]);
  2348. tsalloc.copyToBuffer8((S8*)&normIdx, 1);
  2349. }
  2350. }
  2351. }
  2352. // optimize triangle draw order during disassemble
  2353. {
  2354. FrameTemp<TriListOpt::IndexType> tmpIdxs(indices.size());
  2355. for ( S32 i = 0; i < primitives.size(); i++ )
  2356. {
  2357. const TSDrawPrimitive& prim = primitives[i];
  2358. // only optimize triangle lists (strips and fans are assumed to be already optimized)
  2359. if ( (prim.matIndex & TSDrawPrimitive::TypeMask) == TSDrawPrimitive::Triangles )
  2360. {
  2361. TriListOpt::OptimizeTriangleOrdering(verts.size(), prim.numElements,
  2362. indices.address() + prim.start, tmpIdxs.address());
  2363. dCopyArray(indices.address() + prim.start, tmpIdxs.address(),
  2364. prim.numElements);
  2365. }
  2366. }
  2367. }
  2368. if (TSShape::smVersion > 25)
  2369. {
  2370. // primitives...
  2371. tsalloc.set32( primitives.size() );
  2372. tsalloc.copyToBuffer32((S32*)primitives.address(),3*primitives.size());
  2373. // indices...
  2374. tsalloc.set32(indices.size());
  2375. tsalloc.copyToBuffer32((S32*)indices.address(),indices.size());
  2376. }
  2377. else
  2378. {
  2379. // primitives
  2380. tsalloc.set32( primitives.size() );
  2381. for (S32 i=0; i<primitives.size(); i++)
  2382. {
  2383. S16 start = (S16)primitives[i].start;
  2384. S16 numElements = (S16)primitives[i].numElements;
  2385. tsalloc.copyToBuffer16(&start, 1);
  2386. tsalloc.copyToBuffer16(&numElements, 1);
  2387. tsalloc.copyToBuffer32(&(primitives[i].matIndex), 1);
  2388. }
  2389. // indices
  2390. tsalloc.set32(indices.size());
  2391. Vector<S16> s16_indices(indices.size());
  2392. for (S32 i=0; i<indices.size(); i++)
  2393. s16_indices.push_back((S16)indices[i]);
  2394. tsalloc.copyToBuffer16(s16_indices.address(), s16_indices.size());
  2395. }
  2396. // merge indices...DEPRECATED
  2397. tsalloc.set32( 0 );
  2398. // small stuff...
  2399. tsalloc.set32( vertsPerFrame );
  2400. tsalloc.set32( getFlags() );
  2401. tsalloc.setGuard();
  2402. }
  2403. //-----------------------------------------------------------------------------
  2404. // TSSkinMesh assemble from/ dissemble to memory buffer
  2405. //-----------------------------------------------------------------------------
  2406. void TSSkinMesh::assemble( bool skip )
  2407. {
  2408. // avoid a crash on computeBounds...
  2409. batchData.initialVerts.set( NULL, 0 );
  2410. TSMesh::assemble( skip );
  2411. if (TSShape::smReadVersion >= 27)
  2412. {
  2413. maxBones = tsalloc.get32();
  2414. }
  2415. else
  2416. {
  2417. maxBones = -1;
  2418. }
  2419. S32 sz;
  2420. S32 * ptr32;
  2421. if (TSShape::smReadVersion < 27)
  2422. {
  2423. sz = tsalloc.get32();
  2424. S32 numVerts = sz;
  2425. ptr32 = getSharedData32(parentMesh, 3 * numVerts, (S32**)smVertsList.address(), skip);
  2426. batchData.initialVerts.set((Point3F*)ptr32, sz);
  2427. S8 * ptr8;
  2428. if (TSShape::smReadVersion > 21 && TSMesh::smUseEncodedNormals)
  2429. {
  2430. // we have encoded normals and we want to use them...
  2431. if (parentMesh < 0)
  2432. tsalloc.getPointer32(numVerts * 3); // advance past norms, don't use
  2433. batchData.initialNorms.set(NULL, 0);
  2434. ptr8 = getSharedData8(parentMesh, numVerts, (S8**)smEncodedNormsList.address(), skip);
  2435. encodedNorms.set(ptr8, numVerts);
  2436. // Note: we don't set the encoded normals flag because we handle them in updateSkin and
  2437. // hide the fact that we are using them from base class (TSMesh)
  2438. }
  2439. else if (TSShape::smReadVersion > 21)
  2440. {
  2441. // we have encoded normals but we don't want to use them...
  2442. ptr32 = getSharedData32(parentMesh, 3 * numVerts, (S32**)smNormsList.address(), skip);
  2443. batchData.initialNorms.set((Point3F*)ptr32, numVerts);
  2444. if (parentMesh < 0)
  2445. tsalloc.getPointer8(numVerts); // advance past encoded normls, don't use
  2446. encodedNorms.set(NULL, 0);
  2447. }
  2448. else
  2449. {
  2450. // no encoded normals...
  2451. ptr32 = getSharedData32(parentMesh, 3 * numVerts, (S32**)smNormsList.address(), skip);
  2452. batchData.initialNorms.set((Point3F*)ptr32, numVerts);
  2453. encodedNorms.set(NULL, 0);
  2454. }
  2455. }
  2456. else
  2457. {
  2458. // Set from the mesh data
  2459. batchData.initialVerts = verts;
  2460. batchData.initialNorms = norms;
  2461. }
  2462. sz = tsalloc.get32();
  2463. ptr32 = getSharedData32( parentMesh, 16 * sz, (S32**)smInitTransformList.address(), skip );
  2464. batchData.initialTransforms.set( ptr32, sz );
  2465. sz = tsalloc.get32();
  2466. ptr32 = getSharedData32( parentMesh, sz, (S32**)smVertexIndexList.address(), skip );
  2467. vertexIndex.set( ptr32, sz );
  2468. ptr32 = getSharedData32( parentMesh, sz, (S32**)smBoneIndexList.address(), skip );
  2469. boneIndex.set( ptr32, sz );
  2470. ptr32 = getSharedData32( parentMesh, sz, (S32**)smWeightList.address(), skip );
  2471. weight.set( (F32*)ptr32, sz );
  2472. sz = tsalloc.get32();
  2473. ptr32 = getSharedData32( parentMesh, sz, (S32**)smNodeIndexList.address(), skip );
  2474. batchData.nodeIndex.set( ptr32, sz );
  2475. tsalloc.checkGuard();
  2476. if (smDebugSkinVerts && ptr32 != NULL)
  2477. {
  2478. Con::printf("Loaded skin verts...");
  2479. for (U32 i = 0; i < vertexIndex.size(); i++)
  2480. {
  2481. Con::printf("vi[%i] == %i", i, vertexIndex[i]);
  2482. }
  2483. for (U32 i = 0; i < boneIndex.size(); i++)
  2484. {
  2485. Con::printf("bi[%i] == %i", i, boneIndex[i]);
  2486. }
  2487. for (U32 i = 0; i < batchData.nodeIndex.size(); i++)
  2488. {
  2489. Con::printf("ni[%i] == %i", i, batchData.nodeIndex[i]);
  2490. }
  2491. for (U32 i = 0; i < boneIndex.size(); i++)
  2492. {
  2493. Con::printf("we[%i] == %f", i, weight[i]);
  2494. }
  2495. if (mNumVerts != 0)
  2496. {
  2497. AssertFatal(batchData.initialVerts.size() == mNumVerts, "err WTF");
  2498. }
  2499. Con::printf("---");
  2500. }
  2501. if ( tsalloc.allocShape32( 0 ) && TSShape::smReadVersion < 19 )
  2502. TSMesh::computeBounds(); // only do this if we copied the data...c
  2503. }
  2504. //-----------------------------------------------------------------------------
  2505. // disassemble
  2506. //-----------------------------------------------------------------------------
  2507. void TSSkinMesh::disassemble()
  2508. {
  2509. TSMesh::disassemble();
  2510. if (TSShape::smVersion >= 27)
  2511. {
  2512. AssertFatal(maxBones != 0, "Skin mesh with no bones? No way!");
  2513. tsalloc.set32(maxBones);
  2514. }
  2515. if (TSShape::smVersion < 27)
  2516. {
  2517. tsalloc.set32(batchData.initialVerts.size());
  2518. // if we have no parent mesh, then save off our verts & norms
  2519. if (parentMesh < 0)
  2520. {
  2521. tsalloc.copyToBuffer32((S32*)verts.address(), 3 * verts.size());
  2522. // no longer do this here...let tsmesh handle this
  2523. tsalloc.copyToBuffer32((S32*)norms.address(), 3 * norms.size());
  2524. // if no parent mesh, compute encoded normals and copy over
  2525. for (S32 i = 0; i < norms.size(); i++)
  2526. {
  2527. U8 normIdx = encodedNorms.size() ? encodedNorms[i] : encodeNormal(norms[i]);
  2528. tsalloc.copyToBuffer8((S8*)&normIdx, 1);
  2529. }
  2530. }
  2531. }
  2532. tsalloc.set32( batchData.initialTransforms.size() );
  2533. if ( parentMesh < 0 )
  2534. tsalloc.copyToBuffer32( (S32*)batchData.initialTransforms.address(), batchData.initialTransforms.size() * 16 );
  2535. if (!mVertexData.isReady())
  2536. {
  2537. tsalloc.set32(vertexIndex.size());
  2538. tsalloc.copyToBuffer32((S32*)vertexIndex.address(), vertexIndex.size());
  2539. tsalloc.copyToBuffer32((S32*)boneIndex.address(), boneIndex.size());
  2540. tsalloc.copyToBuffer32((S32*)weight.address(), weight.size());
  2541. }
  2542. else
  2543. {
  2544. tsalloc.set32(0);
  2545. }
  2546. if (TSShape::smVersion < 27)
  2547. {
  2548. if (parentMesh < 0)
  2549. {
  2550. tsalloc.copyToBuffer32((S32*)vertexIndex.address(), vertexIndex.size());
  2551. tsalloc.copyToBuffer32((S32*)boneIndex.address(), boneIndex.size());
  2552. tsalloc.copyToBuffer32((S32*)weight.address(), weight.size());
  2553. }
  2554. }
  2555. tsalloc.set32( batchData.nodeIndex.size() );
  2556. if ( parentMesh < 0 )
  2557. tsalloc.copyToBuffer32( (S32*)batchData.nodeIndex.address(), batchData.nodeIndex.size() );
  2558. tsalloc.setGuard();
  2559. }
  2560. TSSkinMesh::TSSkinMesh()
  2561. {
  2562. meshType = SkinMeshType;
  2563. batchData.initialized = false;
  2564. maxBones = -1;
  2565. }
  2566. //-----------------------------------------------------------------------------
  2567. // find tangent vector
  2568. //-----------------------------------------------------------------------------
  2569. inline void TSMesh::findTangent( U32 index1,
  2570. U32 index2,
  2571. U32 index3,
  2572. Point3F *tan0,
  2573. Point3F *tan1,
  2574. const Vector<Point3F> &_verts)
  2575. {
  2576. const Point3F &v1 = _verts[index1];
  2577. const Point3F &v2 = _verts[index2];
  2578. const Point3F &v3 = _verts[index3];
  2579. const Point2F &w1 = tverts[index1];
  2580. const Point2F &w2 = tverts[index2];
  2581. const Point2F &w3 = tverts[index3];
  2582. F32 x1 = v2.x - v1.x;
  2583. F32 x2 = v3.x - v1.x;
  2584. F32 y1 = v2.y - v1.y;
  2585. F32 y2 = v3.y - v1.y;
  2586. F32 z1 = v2.z - v1.z;
  2587. F32 z2 = v3.z - v1.z;
  2588. F32 s1 = w2.x - w1.x;
  2589. F32 s2 = w3.x - w1.x;
  2590. F32 t1 = w2.y - w1.y;
  2591. F32 t2 = w3.y - w1.y;
  2592. F32 denom = (s1 * t2 - s2 * t1);
  2593. if( mFabs( denom ) < 0.0001f )
  2594. {
  2595. // handle degenerate triangles from strips
  2596. if (denom<0) denom = -0.0001f;
  2597. else denom = 0.0001f;
  2598. }
  2599. F32 r = 1.0f / denom;
  2600. Point3F sdir( (t2 * x1 - t1 * x2) * r,
  2601. (t2 * y1 - t1 * y2) * r,
  2602. (t2 * z1 - t1 * z2) * r );
  2603. Point3F tdir( (s1 * x2 - s2 * x1) * r,
  2604. (s1 * y2 - s2 * y1) * r,
  2605. (s1 * z2 - s2 * z1) * r );
  2606. tan0[index1] += sdir;
  2607. tan1[index1] += tdir;
  2608. tan0[index2] += sdir;
  2609. tan1[index2] += tdir;
  2610. tan0[index3] += sdir;
  2611. tan1[index3] += tdir;
  2612. }
  2613. //-----------------------------------------------------------------------------
  2614. // create array of tangent vectors
  2615. //-----------------------------------------------------------------------------
  2616. void TSMesh::createTangents(const Vector<Point3F> &_verts, const Vector<Point3F> &_norms)
  2617. {
  2618. if (_verts.size() == 0) // can only be done in editable mode
  2619. return;
  2620. U32 numVerts = _verts.size();
  2621. U32 numNorms = _norms.size();
  2622. if ( numVerts <= 0 || numNorms <= 0 )
  2623. return;
  2624. if( numVerts != numNorms)
  2625. return;
  2626. Vector<Point3F> tan0;
  2627. tan0.setSize( numVerts * 2 );
  2628. Point3F *tan1 = tan0.address() + numVerts;
  2629. dMemset( tan0.address(), 0, sizeof(Point3F) * 2 * numVerts );
  2630. U32 numPrimatives = primitives.size();
  2631. for (S32 i = 0; i < numPrimatives; i++ )
  2632. {
  2633. const TSDrawPrimitive & draw = primitives[i];
  2634. GFXPrimitiveType drawType = getDrawType( draw.matIndex >> 30 );
  2635. U32 p1Index = 0;
  2636. U32 p2Index = 0;
  2637. U32 *baseIdx = &indices[draw.start];
  2638. const U32 numElements = (U32)draw.numElements;
  2639. switch( drawType )
  2640. {
  2641. case GFXTriangleList:
  2642. {
  2643. for( U32 j = 0; j < numElements; j += 3 )
  2644. findTangent( baseIdx[j], baseIdx[j + 1], baseIdx[j + 2], tan0.address(), tan1, _verts );
  2645. break;
  2646. }
  2647. case GFXTriangleStrip:
  2648. {
  2649. p1Index = baseIdx[0];
  2650. p2Index = baseIdx[1];
  2651. for( U32 j = 2; j < numElements; j++ )
  2652. {
  2653. findTangent( p1Index, p2Index, baseIdx[j], tan0.address(), tan1, _verts );
  2654. p1Index = p2Index;
  2655. p2Index = baseIdx[j];
  2656. }
  2657. break;
  2658. }
  2659. default:
  2660. AssertFatal( false, "TSMesh::createTangents: unknown primitive type!" );
  2661. }
  2662. }
  2663. tangents.setSize( numVerts );
  2664. // fill out final info from accumulated basis data
  2665. for( U32 i = 0; i < numVerts; i++ )
  2666. {
  2667. const Point3F &n = _norms[i];
  2668. const Point3F &t = tan0[i];
  2669. const Point3F &b = tan1[i];
  2670. Point3F tempPt = t - n * mDot( n, t );
  2671. tempPt.normalize();
  2672. tangents[i] = tempPt;
  2673. Point3F cp;
  2674. mCross( n, t, &cp );
  2675. tangents[i].w = (mDot( cp, b ) < 0.0f) ? -1.0f : 1.0f;
  2676. }
  2677. }
  2678. void TSMesh::convertToVertexData()
  2679. {
  2680. if (!mVertexData.isReady())
  2681. {
  2682. _convertToVertexData(mVertexData, verts, norms);
  2683. }
  2684. }
  2685. void TSSkinMesh::convertToVertexData()
  2686. {
  2687. if (!mVertexData.isReady())
  2688. {
  2689. // Batch data required here
  2690. createSkinBatchData();
  2691. // Dump verts to buffer
  2692. _convertToVertexData(mVertexData, batchData.initialVerts, batchData.initialNorms);
  2693. // Setup bones too
  2694. setupVertexTransforms();
  2695. }
  2696. }
  2697. void TSMesh::copySourceVertexDataFrom(const TSMesh* srcMesh)
  2698. {
  2699. verts = srcMesh->verts;
  2700. tverts = srcMesh->tverts;
  2701. norms = srcMesh->norms;
  2702. colors = srcMesh->colors;
  2703. tverts2 = srcMesh->tverts2;
  2704. if (verts.size() == 0)
  2705. {
  2706. bool hasTVert2 = srcMesh->getHasTVert2();
  2707. bool hasColor = srcMesh->getHasColor();
  2708. verts.setSize(srcMesh->mNumVerts);
  2709. tverts.setSize(srcMesh->mNumVerts);
  2710. norms.setSize(srcMesh->mNumVerts);
  2711. if (hasTVert2)
  2712. colors.setSize(mNumVerts);
  2713. if (hasColor)
  2714. tverts2.setSize(mNumVerts);
  2715. // Fill arrays
  2716. for (U32 i = 0; i < mNumVerts; i++)
  2717. {
  2718. const __TSMeshVertexBase &cv = srcMesh->mVertexData.getBase(i);
  2719. const __TSMeshVertex_3xUVColor &cvc = srcMesh->mVertexData.getColor(i);
  2720. verts[i] = cv.vert();
  2721. tverts[i] = cv.tvert();
  2722. norms[i] = cv.normal();
  2723. if (hasColor)
  2724. cvc.color().getColor(&colors[i]);
  2725. if (hasTVert2)
  2726. tverts2[i] = cvc.tvert2();
  2727. }
  2728. }
  2729. }
  2730. void TSSkinMesh::copySourceVertexDataFrom(const TSMesh* srcMesh)
  2731. {
  2732. TSMesh::copySourceVertexDataFrom(srcMesh);
  2733. if (srcMesh->getMeshType() == TSMesh::SkinMeshType)
  2734. {
  2735. const TSSkinMesh* srcSkinMesh = static_cast<const TSSkinMesh*>(srcMesh);
  2736. weight = srcSkinMesh->weight;
  2737. boneIndex = srcSkinMesh->boneIndex;
  2738. vertexIndex = srcSkinMesh->vertexIndex;
  2739. maxBones = srcSkinMesh->maxBones;
  2740. // Extract from vertex data
  2741. if (srcSkinMesh->vertexIndex.size() == 0)
  2742. {
  2743. mVertexData = srcMesh->mVertexData;
  2744. addWeightsFromVertexBuffer();
  2745. mVertexData.setReady(false);
  2746. }
  2747. }
  2748. }
  2749. U32 TSMesh::getNumVerts()
  2750. {
  2751. return mVertexData.isReady() ? mNumVerts : verts.size();
  2752. }
  2753. void TSMesh::_convertToVertexData(TSMeshVertexArray &outArray, const Vector<Point3F> &_verts, const Vector<Point3F> &_norms)
  2754. {
  2755. U32 colorOffset = 0;
  2756. U32 boneOffset = 0;
  2757. // Update tangents list
  2758. createTangents(verts, norms);
  2759. AssertFatal(_verts.size() == mNumVerts, "vert count mismatch");
  2760. AssertFatal(!getHasColor() || colors.size() == _verts.size(), "Vector of color elements should be the same size as other vectors");
  2761. AssertFatal(!getHasTVert2() || tverts2.size() == _verts.size(), "Vector of tvert2 elements should be the same size as other vectors");
  2762. AssertFatal(!outArray.isReady(), "Mesh already converted to aligned data! Re-check code!");
  2763. AssertFatal(_verts.size() == _norms.size() &&
  2764. _verts.size() == tangents.size(),
  2765. "Vectors: verts, norms, tangents must all be the same size");
  2766. AssertFatal(mVertSize == outArray.vertSize(), "Size inconsistency");
  2767. if (mNumVerts == 0)
  2768. return;
  2769. bool needsSkin = mVertexFormat->hasBlendIndices();
  2770. bool needWeightSet = outArray.getBoneOffset() != 0;
  2771. bool hasColor = getHasColor();
  2772. bool hasTVert2 = getHasTVert2();
  2773. dMemset(&outArray.getBase(0), '\0', mVertSize * mNumVerts);
  2774. for (U32 i = 0; i < mNumVerts; i++)
  2775. {
  2776. __TSMeshVertexBase &v = outArray.getBase(i);
  2777. v.vert(_verts[i]);
  2778. v.normal(_norms[i]);
  2779. v.tangent(tangents[i]);
  2780. if (i < tverts.size())
  2781. v.tvert(tverts[i]);
  2782. if (hasTVert2 || hasColor)
  2783. {
  2784. __TSMeshVertex_3xUVColor &vc = outArray.getColor(i);
  2785. if (hasTVert2 && i < tverts2.size())
  2786. vc.tvert2(tverts2[i]);
  2787. if (hasColor && i < colors.size())
  2788. vc.color(colors[i]);
  2789. }
  2790. // 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
  2791. if (needWeightSet)
  2792. {
  2793. const Point4F wt(1.0f, 0.0f, 0.0f, 0.0f);
  2794. outArray.getBone(i, 0).weight(wt);
  2795. }
  2796. }
  2797. }
  2798. void TSMesh::makeEditable()
  2799. {
  2800. bool hasTVert2 = getHasTVert2();
  2801. bool hasColor = getHasColor();
  2802. bool hasVerts = verts.size() != 0;
  2803. if(mVertexData.isReady() && !hasVerts)
  2804. {
  2805. copySourceVertexDataFrom(this);
  2806. }
  2807. mVertexData.setReady(false);
  2808. mVertSize = 0;
  2809. mNumVerts = 0;
  2810. mVertOffset = 0;
  2811. updateMeshFlags();
  2812. }
  2813. void TSSkinMesh::addWeightsFromVertexBuffer()
  2814. {
  2815. weight.setSize(0);
  2816. boneIndex.setSize(0);
  2817. vertexIndex.setSize(0);
  2818. U32 numBoneBlocks = maxBones >= 0 ? (maxBones + 3) / 4 : 0;
  2819. for (U32 i = 0; i < mNumVerts; i++)
  2820. {
  2821. for (U32 j = 0; j < numBoneBlocks; j++)
  2822. {
  2823. const __TSMeshVertex_BoneData &cv = mVertexData.getBone(i, j);
  2824. if (cv._weights.x != 0.0f)
  2825. {
  2826. addWeightForVert(i, cv._indexes.x, cv._weights.x);
  2827. }
  2828. if (cv._weights.y != 0.0f)
  2829. {
  2830. addWeightForVert(i, cv._indexes.y, cv._weights.y);
  2831. }
  2832. if (cv._weights.z != 0.0f)
  2833. {
  2834. addWeightForVert(i, cv._indexes.z, cv._weights.z);
  2835. }
  2836. if (cv._weights.w != 0.0f)
  2837. {
  2838. addWeightForVert(i, cv._indexes.w, cv._weights.w);
  2839. }
  2840. }
  2841. }
  2842. }
  2843. void TSSkinMesh::makeEditable()
  2844. {
  2845. bool hasTVert2 = getHasTVert2();
  2846. bool hasColor = getHasColor();
  2847. bool hasVerts = verts.size() != 0;
  2848. // Reconstruct bone mapping
  2849. if (mVertexData.isReady() && !hasVerts)
  2850. {
  2851. copySourceVertexDataFrom(this);
  2852. weight.setSize(0);
  2853. boneIndex.setSize(0);
  2854. vertexIndex.setSize(0);
  2855. addWeightsFromVertexBuffer();
  2856. }
  2857. mVertexData.setReady(false);
  2858. mVertSize = 0;
  2859. mNumVerts = 0;
  2860. updateMeshFlags();
  2861. batchData.initialized = false;
  2862. }
  2863. void TSMesh::clearEditable()
  2864. {
  2865. if (verts.size() == 0)
  2866. return;
  2867. if (colors.empty())
  2868. clearFlags(HasColor);
  2869. else
  2870. setFlags(HasColor);
  2871. if (tverts2.empty())
  2872. clearFlags(HasTVert2);
  2873. else
  2874. setFlags(HasTVert2);
  2875. verts.free_memory();
  2876. norms.free_memory();
  2877. tangents.free_memory();
  2878. tverts.free_memory();
  2879. tverts2.free_memory();
  2880. colors.free_memory();
  2881. }
  2882. void TSMesh::updateMeshFlags()
  2883. {
  2884. // Make sure flags are correct
  2885. if (colors.empty())
  2886. clearFlags(HasColor);
  2887. else
  2888. setFlags(HasColor);
  2889. if (tverts2.empty())
  2890. clearFlags(HasTVert2);
  2891. else
  2892. setFlags(HasTVert2);
  2893. }
  2894. void TSSkinMesh::clearEditable()
  2895. {
  2896. TSMesh::clearEditable();
  2897. weight.free_memory();
  2898. boneIndex.free_memory();
  2899. vertexIndex.free_memory();
  2900. }
  2901. TSBasicVertexFormat::TSBasicVertexFormat() :
  2902. texCoordOffset(-1),
  2903. boneOffset(-1),
  2904. colorOffset(-1),
  2905. numBones(0),
  2906. vertexSize(-1)
  2907. {
  2908. }
  2909. TSBasicVertexFormat::TSBasicVertexFormat(TSMesh *mesh)
  2910. {
  2911. texCoordOffset = -1;
  2912. boneOffset = -1;
  2913. colorOffset = -1;
  2914. numBones = 0;
  2915. vertexSize = -1;
  2916. addMeshRequirements(mesh);
  2917. }
  2918. void TSBasicVertexFormat::getFormat(GFXVertexFormat &fmt)
  2919. {
  2920. // NOTE: previously the vertex data was padded to allow for verts to be skinned via SSE.
  2921. // since we now prefer to skin on the GPU and use a basic non-SSE fallback for software
  2922. // skinning, adding in padding via GFXSemantic::PADDING or dummy fields is no longer required.
  2923. fmt.addElement(GFXSemantic::POSITION, GFXDeclType_Float3);
  2924. fmt.addElement(GFXSemantic::TANGENTW, GFXDeclType_Float, 3);
  2925. fmt.addElement(GFXSemantic::NORMAL, GFXDeclType_Float3);
  2926. fmt.addElement(GFXSemantic::TANGENT, GFXDeclType_Float3);
  2927. fmt.addElement(GFXSemantic::TEXCOORD, GFXDeclType_Float2, 0);
  2928. if (texCoordOffset >= 0 || colorOffset >= 0)
  2929. {
  2930. fmt.addElement(GFXSemantic::TEXCOORD, GFXDeclType_Float2, 1);
  2931. fmt.addElement(GFXSemantic::COLOR, GFXDeclType_Color);
  2932. }
  2933. for (U32 i=0; i<numBones; i++)
  2934. {
  2935. fmt.addElement(GFXSemantic::BLENDINDICES, GFXDeclType_UByte4, i);
  2936. fmt.addElement(GFXSemantic::BLENDWEIGHT, GFXDeclType_Float4, i);
  2937. }
  2938. }
  2939. void TSBasicVertexFormat::calculateSize()
  2940. {
  2941. GFXVertexFormat fmt;
  2942. vertexSize = 0;
  2943. getFormat(fmt);
  2944. vertexSize = fmt.getSizeInBytes();
  2945. }
  2946. void TSBasicVertexFormat::writeAlloc(TSShapeAlloc* alloc)
  2947. {
  2948. alloc->set16(texCoordOffset);
  2949. alloc->set16(boneOffset);
  2950. alloc->set16(colorOffset);
  2951. alloc->set16(numBones);
  2952. alloc->set16(vertexSize);
  2953. }
  2954. void TSBasicVertexFormat::readAlloc(TSShapeAlloc* alloc)
  2955. {
  2956. texCoordOffset = alloc->get16();
  2957. boneOffset = alloc->get16();
  2958. colorOffset = alloc->get16();
  2959. numBones = alloc->get16();
  2960. vertexSize = alloc->get16();
  2961. }
  2962. void TSBasicVertexFormat::addMeshRequirements(TSMesh *mesh)
  2963. {
  2964. bool hasColors = false;
  2965. bool hasTexcoord2 = false;
  2966. bool hasSkin = false;
  2967. hasColors = mesh->getHasColor() || (texCoordOffset != -1);
  2968. hasTexcoord2 = mesh->getHasTVert2() || (colorOffset != -1);
  2969. hasSkin = (mesh->getMeshType() == TSMesh::SkinMeshType) || (boneOffset != -1);
  2970. S32 offset = sizeof(TSMesh::__TSMeshVertexBase);
  2971. if ((hasTexcoord2 || hasColors))
  2972. {
  2973. if (texCoordOffset == -1 || colorOffset == -1)
  2974. {
  2975. texCoordOffset = offset;
  2976. colorOffset = offset + (sizeof(float) * 2);
  2977. }
  2978. offset += sizeof(TSMesh::__TSMeshVertex_3xUVColor);
  2979. }
  2980. if (hasSkin)
  2981. {
  2982. boneOffset = offset;
  2983. U32 numMeshBones = mesh->getMaxBonesPerVert();
  2984. U32 boneBlocks = numMeshBones / 4;
  2985. U32 extraBlocks = numMeshBones % 4 != 0 ? 1 : 0;
  2986. U32 neededBones = boneBlocks + extraBlocks;
  2987. numBones = MAX(neededBones, numBones);
  2988. }
  2989. }
  2990. void TSSkinMesh::printVerts()
  2991. {
  2992. for (U32 i = 0; i < mNumVerts; i++)
  2993. {
  2994. TSMesh::__TSMeshVertexBase &vb = mVertexData.getBase(i);
  2995. TSMesh::__TSMeshVertex_BoneData &bw = mVertexData.getBone(i, 0);
  2996. Point3F vert = batchData.initialVerts[i];
  2997. Con::printf("v[%i] == %f,%f,%f; iv == %f,%f,%f. bo=%i,%i,%i,%i bw=%f,%f,%f,%f",
  2998. i, vb._vert.x, vb._vert.y, vb._vert.z,
  2999. vert.x, vert.y, vert.z,
  3000. bw._indexes.x, bw._indexes.y, bw._indexes.z, bw._indexes.w,
  3001. bw._weights.x, bw._weights.y, bw._weights.z, bw._weights.w);
  3002. }
  3003. }