meshRoad.cpp 100 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571
  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. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  23. // Arcane-FX for MIT Licensed Open Source version of Torque 3D from GarageGames
  24. // Copyright (C) 2015 Faust Logic, Inc.
  25. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  26. #include "platform/platform.h"
  27. #include "environment/meshRoad.h"
  28. #include "console/consoleTypes.h"
  29. #include "console/engineAPI.h"
  30. #include "util/catmullRom.h"
  31. #include "math/util/quadTransforms.h"
  32. #include "scene/simPath.h"
  33. #include "scene/sceneRenderState.h"
  34. #include "scene/sceneManager.h"
  35. #include "scene/sgUtil.h"
  36. #include "renderInstance/renderPassManager.h"
  37. #include "T3D/gameBase/gameConnection.h"
  38. #include "core/stream/bitStream.h"
  39. #include "gfx/gfxDrawUtil.h"
  40. #include "gfx/gfxTransformSaver.h"
  41. #include "gfx/primBuilder.h"
  42. #include "gfx/gfxDebugEvent.h"
  43. #include "materials/materialManager.h"
  44. #include "math/mathIO.h"
  45. #include "math/mathUtils.h"
  46. #include "math/util/frustum.h"
  47. #include "gui/3d/guiTSControl.h"
  48. #include "materials/shaderData.h"
  49. #include "gfx/sim/gfxStateBlockData.h"
  50. #include "gfx/sim/debugDraw.h"
  51. #include "collision/concretePolyList.h"
  52. #include "T3D/physics/physicsPlugin.h"
  53. #include "T3D/physics/physicsBody.h"
  54. #include "T3D/physics/physicsCollision.h"
  55. #include "environment/nodeListManager.h"
  56. #ifdef TORQUE_AFX_ENABLED
  57. #include "afx/ce/afxZodiacMgr.h"
  58. #endif
  59. #define MIN_METERS_PER_SEGMENT 1.0f
  60. #define MIN_NODE_DEPTH 0.25f
  61. #define MAX_NODE_DEPTH 50.0f
  62. #define MIN_NODE_WIDTH 0.25f
  63. #define MAX_NODE_WIDTH 50.0f
  64. static U32 gIdxArray[6][2][3] = {
  65. { { 0, 4, 5 }, { 0, 5, 1 }, }, // Top Face
  66. { { 2, 6, 4 }, { 2, 4, 0 }, }, // Left Face
  67. { { 1, 5, 7 }, { 1, 7, 3 }, }, // Right Face
  68. { { 2, 3, 7 }, { 2, 7, 6 }, }, // Bottom Face
  69. { { 0, 1, 3 }, { 0, 3, 2 }, }, // Front Face
  70. { { 4, 6, 7 }, { 4, 7, 5 }, }, // Back Face
  71. };
  72. static S32 QSORT_CALLBACK compareHitSegments(const void* a,const void* b)
  73. {
  74. const MeshRoadHitSegment *fa = (MeshRoadHitSegment*)a;
  75. const MeshRoadHitSegment *fb = (MeshRoadHitSegment*)b;
  76. F32 diff = fb->t - fa->t;
  77. return (diff > 0) ? 1 : (diff < 0) ? -1 : 0;
  78. }
  79. //-----------------------------------------------------------------------------
  80. // MeshRoadNodeList Struct
  81. //-----------------------------------------------------------------------------
  82. struct MeshRoadNodeList : public NodeListManager::NodeList
  83. {
  84. Vector<Point3F> mPositions;
  85. Vector<F32> mWidths;
  86. Vector<F32> mDepths;
  87. Vector<VectorF> mNormals;
  88. MeshRoadNodeList() { }
  89. virtual ~MeshRoadNodeList() { }
  90. };
  91. //-----------------------------------------------------------------------------
  92. // MeshRoadNodeEvent Class
  93. //-----------------------------------------------------------------------------
  94. class MeshRoadNodeEvent : public NodeListEvent
  95. {
  96. typedef NodeListEvent Parent;
  97. public:
  98. Vector<Point3F> mPositions;
  99. Vector<F32> mWidths;
  100. Vector<F32> mDepths;
  101. Vector<VectorF> mNormals;
  102. public:
  103. MeshRoadNodeEvent() { mNodeList = NULL; }
  104. virtual ~MeshRoadNodeEvent() { }
  105. virtual void pack(NetConnection*, BitStream*);
  106. virtual void unpack(NetConnection*, BitStream*);
  107. virtual void copyIntoList(NodeListManager::NodeList* copyInto);
  108. virtual void padListToSize();
  109. DECLARE_CONOBJECT(MeshRoadNodeEvent);
  110. };
  111. void MeshRoadNodeEvent::pack(NetConnection* conn, BitStream* stream)
  112. {
  113. Parent::pack( conn, stream );
  114. stream->writeInt( mPositions.size(), 16 );
  115. for (U32 i=0; i<mPositions.size(); ++i)
  116. {
  117. mathWrite( *stream, mPositions[i] );
  118. stream->write( mWidths[i] );
  119. stream->write( mDepths[i] );
  120. mathWrite( *stream, mNormals[i] );
  121. }
  122. }
  123. void MeshRoadNodeEvent::unpack(NetConnection* conn, BitStream* stream)
  124. {
  125. mNodeList = new MeshRoadNodeList();
  126. Parent::unpack( conn, stream );
  127. U32 count = stream->readInt( 16 );
  128. Point3F pos;
  129. F32 width, depth;
  130. VectorF normal;
  131. MeshRoadNodeList* list = static_cast<MeshRoadNodeList*>(mNodeList);
  132. for (U32 i=0; i<count; ++i)
  133. {
  134. mathRead( *stream, &pos );
  135. stream->read( &width );
  136. stream->read( &depth );
  137. mathRead( *stream, &normal );
  138. list->mPositions.push_back( pos );
  139. list->mWidths.push_back( width );
  140. list->mDepths.push_back( depth );
  141. list->mNormals.push_back( normal );
  142. }
  143. list->mTotalValidNodes = count;
  144. // Do we have a complete list?
  145. if (list->mPositions.size() >= mTotalNodes)
  146. list->mListComplete = true;
  147. }
  148. void MeshRoadNodeEvent::copyIntoList(NodeListManager::NodeList* copyInto)
  149. {
  150. MeshRoadNodeList* prevList = dynamic_cast<MeshRoadNodeList*>(copyInto);
  151. MeshRoadNodeList* list = static_cast<MeshRoadNodeList*>(mNodeList);
  152. // Merge our list with the old list.
  153. for (U32 i=mLocalListStart, index=0; i<mLocalListStart+list->mPositions.size(); ++i, ++index)
  154. {
  155. prevList->mPositions[i] = list->mPositions[index];
  156. prevList->mWidths[i] = list->mWidths[index];
  157. prevList->mDepths[i] = list->mDepths[index];
  158. prevList->mNormals[i] = list->mNormals[index];
  159. }
  160. }
  161. void MeshRoadNodeEvent::padListToSize()
  162. {
  163. MeshRoadNodeList* list = static_cast<MeshRoadNodeList*>(mNodeList);
  164. U32 totalValidNodes = list->mTotalValidNodes;
  165. // Pad our list front?
  166. if (mLocalListStart)
  167. {
  168. MeshRoadNodeList* newlist = new MeshRoadNodeList();
  169. newlist->mPositions.increment(mLocalListStart);
  170. newlist->mWidths.increment(mLocalListStart);
  171. newlist->mDepths.increment(mLocalListStart);
  172. newlist->mNormals.increment(mLocalListStart);
  173. newlist->mPositions.merge(list->mPositions);
  174. newlist->mWidths.merge(list->mWidths);
  175. newlist->mDepths.merge(list->mDepths);
  176. newlist->mNormals.merge(list->mNormals);
  177. delete list;
  178. mNodeList = list = newlist;
  179. }
  180. // Pad our list end?
  181. if (list->mPositions.size() < mTotalNodes)
  182. {
  183. U32 delta = mTotalNodes - list->mPositions.size();
  184. list->mPositions.increment(delta);
  185. list->mWidths.increment(delta);
  186. list->mDepths.increment(delta);
  187. list->mNormals.increment(delta);
  188. }
  189. list->mTotalValidNodes = totalValidNodes;
  190. }
  191. IMPLEMENT_CO_NETEVENT_V1(MeshRoadNodeEvent);
  192. ConsoleDocClass( MeshRoadNodeEvent,
  193. "@brief Sends messages to the Mesh Road Editor\n\n"
  194. "Editor use only.\n\n"
  195. "@internal"
  196. );
  197. //-----------------------------------------------------------------------------
  198. // MeshRoadNodeListNotify Class
  199. //-----------------------------------------------------------------------------
  200. class MeshRoadNodeListNotify : public NodeListNotify
  201. {
  202. typedef NodeListNotify Parent;
  203. protected:
  204. SimObjectPtr<MeshRoad> mRoad;
  205. public:
  206. MeshRoadNodeListNotify( MeshRoad* road, U32 listId ) { mRoad = road; mListId = listId; }
  207. virtual ~MeshRoadNodeListNotify() { mRoad = NULL; }
  208. virtual void sendNotification( NodeListManager::NodeList* list );
  209. };
  210. void MeshRoadNodeListNotify::sendNotification( NodeListManager::NodeList* list )
  211. {
  212. if (mRoad.isValid())
  213. {
  214. // Build the road's nodes
  215. MeshRoadNodeList* roadList = dynamic_cast<MeshRoadNodeList*>( list );
  216. if (roadList)
  217. mRoad->buildNodesFromList( roadList );
  218. }
  219. }
  220. //-------------------------------------------------------------------------
  221. // MeshRoadProfile Class
  222. //-------------------------------------------------------------------------
  223. MeshRoadProfile::MeshRoadProfile()
  224. {
  225. mRoad = NULL;
  226. // Set transformation matrix to identity
  227. mObjToSlice.identity();
  228. mSliceToObj.identity();
  229. }
  230. S32 MeshRoadProfile::clickOnLine(Point3F &p)
  231. {
  232. Point3F newProfilePt;
  233. Point3F ptOnSegment;
  234. F32 dist = 0.0f;
  235. F32 minDist = 99999.0f;
  236. U32 idx = 0;
  237. for(U32 i=0; i < mNodes.size()-1; i++)
  238. {
  239. ptOnSegment = MathUtils::mClosestPointOnSegment(mNodes[i].getPosition(), mNodes[i+1].getPosition(), p);
  240. dist = (p - ptOnSegment).len();
  241. if(dist < minDist)
  242. {
  243. minDist = dist;
  244. newProfilePt = ptOnSegment;
  245. idx = i+1;
  246. }
  247. }
  248. if(minDist <= 0.1f)
  249. {
  250. p.set(newProfilePt.x, newProfilePt.y, newProfilePt.z);
  251. return idx;
  252. }
  253. return -1;
  254. }
  255. void MeshRoadProfile::addPoint(U32 nodeId, Point3F &p)
  256. {
  257. if(nodeId < mNodes.size() && nodeId != 0)
  258. {
  259. p.z = 0.0f;
  260. mNodes.insert(nodeId, p);
  261. mSegMtrls.insert(nodeId-1, mSegMtrls[nodeId-1]);
  262. mRoad->setMaskBits(MeshRoad::ProfileMask | MeshRoad::RegenMask);
  263. generateNormals();
  264. }
  265. }
  266. void MeshRoadProfile::removePoint(U32 nodeId)
  267. {
  268. if(nodeId > 0 && nodeId < mNodes.size()-1)
  269. {
  270. mNodes.erase(nodeId);
  271. mSegMtrls.remove(nodeId-1);
  272. mRoad->setMaskBits(MeshRoad::ProfileMask | MeshRoad::RegenMask);
  273. generateNormals();
  274. }
  275. }
  276. void MeshRoadProfile::setNodePosition(U32 nodeId, Point3F pos)
  277. {
  278. if(nodeId < mNodes.size())
  279. {
  280. mNodes[nodeId].setPosition(pos.x, pos.y);
  281. mRoad->setMaskBits(MeshRoad::ProfileMask | MeshRoad::RegenMask);
  282. generateNormals();
  283. }
  284. }
  285. void MeshRoadProfile::toggleSmoothing(U32 nodeId)
  286. {
  287. if(nodeId > 0 && nodeId < mNodes.size()-1)
  288. {
  289. mNodes[nodeId].setSmoothing(!mNodes[nodeId].isSmooth());
  290. mRoad->setMaskBits(MeshRoad::ProfileMask | MeshRoad::RegenMask);
  291. generateNormals();
  292. }
  293. }
  294. void MeshRoadProfile::toggleSegMtrl(U32 seg)
  295. {
  296. if(seg < mSegMtrls.size())
  297. {
  298. switch(mSegMtrls[seg])
  299. {
  300. case MeshRoad::Side: mSegMtrls[seg] = MeshRoad::Top; break;
  301. case MeshRoad::Top: mSegMtrls[seg] = MeshRoad::Bottom; break;
  302. case MeshRoad::Bottom: mSegMtrls[seg] = MeshRoad::Side; break;
  303. }
  304. mRoad->setMaskBits(MeshRoad::ProfileMask | MeshRoad::RegenMask);
  305. }
  306. }
  307. void MeshRoadProfile::generateNormals()
  308. {
  309. VectorF t, b, n, t2, n2;
  310. Point3F averagePt;
  311. mNodeNormals.clear();
  312. // Loop through all profile line segments
  313. for(U32 i=0; i < mNodes.size()-1; i++)
  314. {
  315. // Calculate normal for each node in line segment
  316. for(U32 j=0; j<2; j++)
  317. {
  318. // Smoothed Node: Average the node with nodes before and after.
  319. // Direction between the node and the average is the smoothed normal.
  320. if( mNodes[i+j].isSmooth() )
  321. {
  322. b = Point3F(0.0f, 0.0f, 1.0f);
  323. t = mNodes[i+j-1].getPosition() - mNodes[i+j].getPosition();
  324. n = mCross(t, b);
  325. n.normalizeSafe();
  326. t2 = mNodes[i+j].getPosition() - mNodes[i+j+1].getPosition();
  327. n2 = mCross(t2, b);
  328. n2.normalizeSafe();
  329. n += n2;
  330. }
  331. // Non-smoothed Node: Normal is perpendicular to segment.
  332. else
  333. {
  334. b = Point3F(0.0f, 0.0f, 1.0f);
  335. t = mNodes[i].getPosition() - mNodes[i+1].getPosition();
  336. n = mCross(t, b);
  337. }
  338. n.normalizeSafe();
  339. mNodeNormals.push_back(n);
  340. }
  341. }
  342. }
  343. void MeshRoadProfile::generateEndCap(F32 width)
  344. {
  345. Point3F pt;
  346. mCap.newPoly();
  347. for ( U32 i = 0; i < mNodes.size(); i++ )
  348. {
  349. pt = mNodes[i].getPosition();
  350. mCap.addVert(pt);
  351. }
  352. for ( S32 i = mNodes.size()-1; i >= 0; i-- )
  353. {
  354. pt = mNodes[i].getPosition();
  355. pt.x = -pt.x - width;
  356. mCap.addVert(pt);
  357. }
  358. mCap.decompose();
  359. }
  360. void MeshRoadProfile::setProfileDepth(F32 depth)
  361. {
  362. Point3F curPos = mNodes[mNodes.size()-1].getPosition();
  363. mNodes[mNodes.size()-1].setPosition(curPos.x, -depth);
  364. }
  365. void MeshRoadProfile::setTransform(const MatrixF &mat, const Point3F &p)
  366. {
  367. mObjToSlice.identity();
  368. mSliceToObj.identity();
  369. mObjToSlice *= mat;
  370. mSliceToObj *= mObjToSlice.inverse();
  371. mSliceToObj.transpose();
  372. mStartPos = p;
  373. }
  374. void MeshRoadProfile::getNodeWorldPos(U32 nodeId, Point3F &p)
  375. {
  376. if(nodeId < mNodes.size())
  377. {
  378. p = mNodes[nodeId].getPosition();
  379. mObjToSlice.mulP(p);
  380. p += mStartPos;
  381. }
  382. }
  383. void MeshRoadProfile::getNormToSlice(U32 normId, VectorF &n)
  384. {
  385. if(normId < mNodeNormals.size())
  386. {
  387. n = mNodeNormals[normId];
  388. mObjToSlice.mulP(n);
  389. }
  390. }
  391. void MeshRoadProfile::getNormWorldPos(U32 normId, Point3F &p)
  392. {
  393. if(normId < mNodeNormals.size())
  394. {
  395. U32 nodeId = normId/2 + (U32)(mFmod(normId,2.0f));
  396. p = mNodes[nodeId].getPosition();
  397. p += 0.5f * mNodeNormals[normId]; // Length = 0.5 units
  398. mObjToSlice.mulP(p);
  399. p += mStartPos;
  400. }
  401. }
  402. void MeshRoadProfile::worldToObj(Point3F &p)
  403. {
  404. p -= mStartPos;
  405. mSliceToObj.mulP(p);
  406. p.z = 0.0f;
  407. }
  408. void MeshRoadProfile::objToWorld(Point3F &p)
  409. {
  410. mObjToSlice.mulP(p);
  411. p += mStartPos;
  412. }
  413. F32 MeshRoadProfile::getProfileLen()
  414. {
  415. F32 sum = 0.0f;
  416. Point3F segmentVec;
  417. for(U32 i=0; i < mNodes.size()-1; i++)
  418. {
  419. segmentVec = mNodes[i+1].getPosition() - mNodes[i].getPosition();
  420. sum += segmentVec.len();
  421. }
  422. return sum;
  423. }
  424. F32 MeshRoadProfile::getNodePosPercent(U32 nodeId)
  425. {
  426. nodeId = mFmod(nodeId, mNodes.size());
  427. if(nodeId == 0)
  428. return 0.0f;
  429. else if(nodeId == mNodes.size()-1)
  430. return 1.0f;
  431. F32 totLen = getProfileLen();
  432. F32 sum = 0.0f;
  433. Point3F segmentVec;
  434. for(U32 i=0; i < nodeId; i++)
  435. {
  436. segmentVec = mNodes[i+1].getPosition() - mNodes[i].getPosition();
  437. sum += segmentVec.len();
  438. }
  439. return sum/totLen;
  440. }
  441. void MeshRoadProfile::resetProfile(F32 defaultDepth)
  442. {
  443. Point3F pos(0.0f, 0.0f, 0.0f);
  444. mNodes.clear();
  445. mNodes.push_back(pos);
  446. pos.y = -defaultDepth;
  447. mNodes.push_back(pos);
  448. mSegMtrls.clear();
  449. mSegMtrls.push_back(MeshRoad::Side);
  450. mRoad->setMaskBits(MeshRoad::ProfileMask | MeshRoad::RegenMask);
  451. generateNormals();
  452. }
  453. //------------------------------------------------------------------------------
  454. // MeshRoadConvex Class
  455. //------------------------------------------------------------------------------
  456. const MatrixF& MeshRoadConvex::getTransform() const
  457. {
  458. return MatrixF::Identity; //mObject->getTransform();
  459. }
  460. Box3F MeshRoadConvex::getBoundingBox() const
  461. {
  462. return box;
  463. }
  464. Box3F MeshRoadConvex::getBoundingBox(const MatrixF& mat, const Point3F& scale) const
  465. {
  466. Box3F newBox = box;
  467. newBox.minExtents.convolve(scale);
  468. newBox.maxExtents.convolve(scale);
  469. mat.mul(newBox);
  470. return newBox;
  471. }
  472. Point3F MeshRoadConvex::support(const VectorF& vec) const
  473. {
  474. F32 bestDot = mDot( verts[0], vec );
  475. const Point3F *bestP = &verts[0];
  476. for(S32 i=1; i<4; i++)
  477. {
  478. F32 newD = mDot(verts[i], vec);
  479. if(newD > bestDot)
  480. {
  481. bestDot = newD;
  482. bestP = &verts[i];
  483. }
  484. }
  485. return *bestP;
  486. }
  487. void MeshRoadConvex::getFeatures(const MatrixF& mat, const VectorF& n, ConvexFeature* cf)
  488. {
  489. cf->material = 0;
  490. cf->mObject = mObject;
  491. // For a tetrahedron this is pretty easy... first
  492. // convert everything into world space.
  493. Point3F tverts[4];
  494. mat.mulP(verts[0], &tverts[0]);
  495. mat.mulP(verts[1], &tverts[1]);
  496. mat.mulP(verts[2], &tverts[2]);
  497. mat.mulP(verts[3], &tverts[3]);
  498. // Points...
  499. S32 firstVert = cf->mVertexList.size();
  500. cf->mVertexList.increment(); cf->mVertexList.last() = tverts[0];
  501. cf->mVertexList.increment(); cf->mVertexList.last() = tverts[1];
  502. cf->mVertexList.increment(); cf->mVertexList.last() = tverts[2];
  503. cf->mVertexList.increment(); cf->mVertexList.last() = tverts[3];
  504. // Edges...
  505. cf->mEdgeList.increment();
  506. cf->mEdgeList.last().vertex[0] = firstVert+0;
  507. cf->mEdgeList.last().vertex[1] = firstVert+1;
  508. cf->mEdgeList.increment();
  509. cf->mEdgeList.last().vertex[0] = firstVert+1;
  510. cf->mEdgeList.last().vertex[1] = firstVert+2;
  511. cf->mEdgeList.increment();
  512. cf->mEdgeList.last().vertex[0] = firstVert+2;
  513. cf->mEdgeList.last().vertex[1] = firstVert+0;
  514. cf->mEdgeList.increment();
  515. cf->mEdgeList.last().vertex[0] = firstVert+3;
  516. cf->mEdgeList.last().vertex[1] = firstVert+0;
  517. cf->mEdgeList.increment();
  518. cf->mEdgeList.last().vertex[0] = firstVert+3;
  519. cf->mEdgeList.last().vertex[1] = firstVert+1;
  520. cf->mEdgeList.increment();
  521. cf->mEdgeList.last().vertex[0] = firstVert+3;
  522. cf->mEdgeList.last().vertex[1] = firstVert+2;
  523. // Triangles...
  524. cf->mFaceList.increment();
  525. cf->mFaceList.last().normal = PlaneF(tverts[2], tverts[1], tverts[0]);
  526. cf->mFaceList.last().vertex[0] = firstVert+2;
  527. cf->mFaceList.last().vertex[1] = firstVert+1;
  528. cf->mFaceList.last().vertex[2] = firstVert+0;
  529. cf->mFaceList.increment();
  530. cf->mFaceList.last().normal = PlaneF(tverts[1], tverts[0], tverts[3]);
  531. cf->mFaceList.last().vertex[0] = firstVert+1;
  532. cf->mFaceList.last().vertex[1] = firstVert+0;
  533. cf->mFaceList.last().vertex[2] = firstVert+3;
  534. cf->mFaceList.increment();
  535. cf->mFaceList.last().normal = PlaneF(tverts[2], tverts[1], tverts[3]);
  536. cf->mFaceList.last().vertex[0] = firstVert+2;
  537. cf->mFaceList.last().vertex[1] = firstVert+1;
  538. cf->mFaceList.last().vertex[2] = firstVert+3;
  539. cf->mFaceList.increment();
  540. cf->mFaceList.last().normal = PlaneF(tverts[0], tverts[2], tverts[3]);
  541. cf->mFaceList.last().vertex[0] = firstVert+0;
  542. cf->mFaceList.last().vertex[1] = firstVert+2;
  543. cf->mFaceList.last().vertex[2] = firstVert+3;
  544. }
  545. void MeshRoadConvex::getPolyList( AbstractPolyList* list )
  546. {
  547. // Transform the list into object space and set the pointer to the object
  548. //MatrixF i( mObject->getTransform() );
  549. //Point3F iS( mObject->getScale() );
  550. //list->setTransform(&i, iS);
  551. list->setTransform( &MatrixF::Identity, Point3F::One );
  552. list->setObject(mObject);
  553. // Points...
  554. S32 base = list->addPoint(verts[1]);
  555. list->addPoint(verts[2]);
  556. list->addPoint(verts[0]);
  557. list->addPoint(verts[3]);
  558. // Planes...
  559. list->begin(0,0);
  560. list->vertex(base + 2);
  561. list->vertex(base + 1);
  562. list->vertex(base + 0);
  563. list->plane(base + 2, base + 1, base + 0);
  564. list->end();
  565. list->begin(0,0);
  566. list->vertex(base + 2);
  567. list->vertex(base + 1);
  568. list->vertex(base + 3);
  569. list->plane(base + 2, base + 1, base + 3);
  570. list->end();
  571. list->begin(0,0);
  572. list->vertex(base + 3);
  573. list->vertex(base + 1);
  574. list->vertex(base + 0);
  575. list->plane(base + 3, base + 1, base + 0);
  576. list->end();
  577. list->begin(0,0);
  578. list->vertex(base + 2);
  579. list->vertex(base + 3);
  580. list->vertex(base + 0);
  581. list->plane(base + 2, base + 3, base + 0);
  582. list->end();
  583. }
  584. //------------------------------------------------------------------------------
  585. // MeshRoadSegment Class
  586. //------------------------------------------------------------------------------
  587. MeshRoadSegment::MeshRoadSegment()
  588. {
  589. mPlaneCount = 0;
  590. columns = 0;
  591. rows = 0;
  592. numVerts = 0;
  593. numTriangles = 0;
  594. startVert = 0;
  595. endVert = 0;
  596. startIndex = 0;
  597. endIndex = 0;
  598. slice0 = NULL;
  599. slice1 = NULL;
  600. }
  601. MeshRoadSegment::MeshRoadSegment( MeshRoadSlice *rs0, MeshRoadSlice *rs1, const MatrixF &roadMat )
  602. {
  603. columns = 0;
  604. rows = 0;
  605. numVerts = 0;
  606. numTriangles = 0;
  607. startVert = 0;
  608. endVert = 0;
  609. startIndex = 0;
  610. endIndex = 0;
  611. slice0 = rs0;
  612. slice1 = rs1;
  613. // Calculate the bounding box(s)
  614. worldbounds.minExtents = worldbounds.maxExtents = rs0->p0;
  615. for(U32 i=0; i < rs0->verts.size(); i++)
  616. worldbounds.extend( rs0->verts[i] );
  617. for(U32 i=0; i < rs1->verts.size(); i++)
  618. worldbounds.extend( rs1->verts[i] );
  619. objectbounds = worldbounds;
  620. roadMat.mul( objectbounds );
  621. // Calculate the planes for this segment
  622. // Will be used for intersection/buoyancy tests
  623. mPlaneCount = 6;
  624. mPlanes[0].set( slice0->pb0, slice0->p0, slice1->p0 ); // left
  625. mPlanes[1].set( slice1->pb2, slice1->p2, slice0->p2 ); // right
  626. mPlanes[2].set( slice0->pb2, slice0->p2, slice0->p0 ); // near
  627. mPlanes[3].set( slice1->p0, slice1->p2, slice1->pb2 ); // far
  628. mPlanes[4].set( slice1->p2, slice1->p0, slice0->p0 ); // top
  629. mPlanes[5].set( slice0->pb0, slice1->pb0, slice1->pb2 ); // bottom
  630. }
  631. void MeshRoadSegment::set( MeshRoadSlice *rs0, MeshRoadSlice *rs1 )
  632. {
  633. columns = 0;
  634. rows = 0;
  635. numVerts = 0;
  636. numTriangles = 0;
  637. startVert = 0;
  638. endVert = 0;
  639. startIndex = 0;
  640. endIndex = 0;
  641. slice0 = rs0;
  642. slice1 = rs1;
  643. }
  644. bool MeshRoadSegment::intersectBox( const Box3F &bounds ) const
  645. {
  646. // This code copied from Frustum class.
  647. Point3F maxPoint;
  648. F32 maxDot;
  649. // Note the planes are ordered left, right, near,
  650. // far, top, bottom for getting early rejections
  651. // from the typical horizontal scene.
  652. for ( S32 i = 0; i < mPlaneCount; i++ )
  653. {
  654. // This is pretty much as optimal as you can
  655. // get for a plane vs AABB test...
  656. //
  657. // 4 comparisons
  658. // 3 multiplies
  659. // 2 adds
  660. // 1 negation
  661. //
  662. // It will early out as soon as it detects the
  663. // bounds is outside one of the planes.
  664. if ( mPlanes[i].x > 0 )
  665. maxPoint.x = bounds.maxExtents.x;
  666. else
  667. maxPoint.x = bounds.minExtents.x;
  668. if ( mPlanes[i].y > 0 )
  669. maxPoint.y = bounds.maxExtents.y;
  670. else
  671. maxPoint.y = bounds.minExtents.y;
  672. if ( mPlanes[i].z > 0 )
  673. maxPoint.z = bounds.maxExtents.z;
  674. else
  675. maxPoint.z = bounds.minExtents.z;
  676. maxDot = mDot( maxPoint, mPlanes[ i ] );
  677. if ( maxDot <= -mPlanes[ i ].d )
  678. return false;
  679. }
  680. return true;
  681. }
  682. bool MeshRoadSegment::containsPoint( const Point3F &pnt ) const
  683. {
  684. // This code from Frustum class.
  685. F32 maxDot;
  686. // Note the planes are ordered left, right, near,
  687. // far, top, bottom for getting early rejections
  688. // from the typical horizontal scene.
  689. for ( S32 i = 0; i < mPlaneCount; i++ )
  690. {
  691. const PlaneF &plane = mPlanes[ i ];
  692. // This is pretty much as optimal as you can
  693. // get for a plane vs point test...
  694. //
  695. // 1 comparison
  696. // 2 multiplies
  697. // 1 adds
  698. //
  699. // It will early out as soon as it detects the
  700. // point is outside one of the planes.
  701. maxDot = mDot( pnt, plane ) + plane.d;
  702. if ( maxDot < 0.0f )
  703. return false;
  704. }
  705. return true;
  706. }
  707. F32 MeshRoadSegment::distanceToSurface(const Point3F &pnt) const
  708. {
  709. return mPlanes[4].distToPlane( pnt );
  710. }
  711. //------------------------------------------------------------------------------
  712. // MeshRoad Class
  713. //------------------------------------------------------------------------------
  714. ConsoleDocClass( MeshRoad,
  715. "@brief A strip of rectangular mesh segments defined by a 3D spline "
  716. "for prototyping road-shaped objects in your scene.\n\n"
  717. "User may control width and depth per node, overall spline shape in three "
  718. "dimensions, and seperate Materials for rendering the top, bottom, and side surfaces.\n\n"
  719. "MeshRoad is not capable of handling intersections, branches, curbs, or other "
  720. "desirable features in a final 'road' asset and is therefore intended for "
  721. "prototyping and experimentation.\n\n"
  722. "Materials assigned to MeshRoad should tile vertically.\n\n"
  723. "@ingroup Terrain"
  724. );
  725. bool MeshRoad::smEditorOpen = false;
  726. bool MeshRoad::smShowBatches = false;
  727. bool MeshRoad::smShowSpline = true;
  728. bool MeshRoad::smShowRoad = true;
  729. bool MeshRoad::smShowRoadProfile = false;
  730. bool MeshRoad::smWireframe = true;
  731. SimObjectPtr<SimSet> MeshRoad::smServerMeshRoadSet = NULL;
  732. GFXStateBlockRef MeshRoad::smWireframeSB;
  733. IMPLEMENT_CO_NETOBJECT_V1(MeshRoad);
  734. MeshRoad::MeshRoad()
  735. : mTextureLength( 5.0f ),
  736. mBreakAngle( 3.0f ),
  737. mWidthSubdivisions( 0 ),
  738. mPhysicsRep( NULL )
  739. {
  740. mConvexList = new Convex;
  741. // Setup NetObject.
  742. mTypeMask |= StaticObjectType | StaticShapeObjectType;
  743. mNetFlags.set(Ghostable);
  744. mMatInst[Top] = NULL;
  745. mMatInst[Bottom] = NULL;
  746. mMatInst[Side] = NULL;
  747. mTypeMask |= TerrainLikeObjectType;
  748. for (U32 i = 0; i < SurfaceCount; i++)
  749. {
  750. mVertCount[i] = 0;
  751. mTriangleCount[i] = 0;
  752. }
  753. initMaterialAsset(TopMaterial);
  754. initMaterialAsset(BottomMaterial);
  755. initMaterialAsset(SideMaterial);
  756. mSideProfile.mRoad = this;
  757. }
  758. MeshRoad::~MeshRoad()
  759. {
  760. delete mConvexList;
  761. mConvexList = NULL;
  762. }
  763. void MeshRoad::initPersistFields()
  764. {
  765. addGroup( "MeshRoad" );
  766. addProtectedField("TopMaterial", TypeMaterialName, Offset(mTopMaterialName, MeshRoad), MeshRoad::_setTopMaterialName, & defaultProtectedGetFn, "Material for the upper surface of the road.", AbstractClassRep::FIELD_HideInInspectors); \
  767. addProtectedField("TopMaterialAsset", TypeMaterialAssetId, Offset(mTopMaterialAssetId, MeshRoad), MeshRoad::_setTopMaterialAsset, & defaultProtectedGetFn, "Material for the upper surface of the road.");
  768. addProtectedField("BottomMaterial", TypeMaterialName, Offset(mBottomMaterialName, MeshRoad), MeshRoad::_setBottomMaterialName, & defaultProtectedGetFn, "Material for the bottom surface of the road.", AbstractClassRep::FIELD_HideInInspectors); \
  769. addProtectedField("BottomMaterialAsset", TypeMaterialAssetId, Offset(mBottomMaterialAssetId, MeshRoad), MeshRoad::_setBottomMaterialAsset, & defaultProtectedGetFn, "Material for the bottom surface of the road.");
  770. addProtectedField("SideMaterial", TypeMaterialName, Offset(mSideMaterialName, MeshRoad), MeshRoad::_setSideMaterialName, & defaultProtectedGetFn, "Material for the left, right, front, and back surfaces of the road.", AbstractClassRep::FIELD_HideInInspectors); \
  771. addProtectedField("SideMaterialAsset", TypeMaterialAssetId, Offset(mSideMaterialAssetId, MeshRoad), MeshRoad::_setSideMaterialAsset, & defaultProtectedGetFn, "Material for the left, right, front, and back surfaces of the road.");
  772. addField( "textureLength", TypeF32, Offset( mTextureLength, MeshRoad ),
  773. "The length in meters of textures mapped to the MeshRoad." );
  774. addField( "breakAngle", TypeF32, Offset( mBreakAngle, MeshRoad ),
  775. "Angle in degrees - MeshRoad will subdivide the spline if its curve is greater than this threshold." );
  776. addField( "widthSubdivisions", TypeS32, Offset( mWidthSubdivisions, MeshRoad ),
  777. "Subdivide segments widthwise this many times when generating vertices." );
  778. endGroup( "MeshRoad" );
  779. addGroup( "Internal" );
  780. addProtectedField( "Node", TypeString, NULL, &addNodeFromField, &emptyStringProtectedGetFn,
  781. "Do not modify, for internal use." );
  782. addProtectedField( "ProfileNode", TypeString, NULL, &addProfileNodeFromField, &emptyStringProtectedGetFn,
  783. "Do not modify, for internal use." );
  784. endGroup( "Internal" );
  785. Parent::initPersistFields();
  786. }
  787. void MeshRoad::consoleInit()
  788. {
  789. Parent::consoleInit();
  790. Con::addVariable( "$MeshRoad::EditorOpen", TypeBool, &MeshRoad::smEditorOpen, "True if the MeshRoad editor is open, otherwise false.\n"
  791. "@ingroup Editors\n");
  792. Con::addVariable( "$MeshRoad::wireframe", TypeBool, &MeshRoad::smWireframe, "If true, will render the wireframe of the road.\n"
  793. "@ingroup Editors\n");
  794. Con::addVariable( "$MeshRoad::showBatches", TypeBool, &MeshRoad::smShowBatches, "Determines if the debug rendering of the batches cubes is displayed or not.\n"
  795. "@ingroup Editors\n");
  796. Con::addVariable( "$MeshRoad::showSpline", TypeBool, &MeshRoad::smShowSpline, "If true, the spline on which the curvature of this road is based will be rendered.\n"
  797. "@ingroup Editors\n");
  798. Con::addVariable( "$MeshRoad::showRoad", TypeBool, &MeshRoad::smShowRoad, "If true, the road will be rendered. When in the editor, roads are always rendered regardless of this flag.\n"
  799. "@ingroup Editors\n");
  800. Con::addVariable( "$MeshRoad::showRoadProfile", TypeBool, &MeshRoad::smShowRoadProfile, "If true, the road profile will be shown in the editor.\n"
  801. "@ingroup Editors\n");
  802. }
  803. bool MeshRoad::addNodeFromField( void *object, const char *index, const char *data )
  804. {
  805. MeshRoad *pObj = static_cast<MeshRoad*>(object);
  806. //if ( !pObj->isProperlyAdded() )
  807. //{
  808. F32 width, depth;
  809. Point3F pos, normal;
  810. U32 result = dSscanf( data, "%g %g %g %g %g %g %g %g", &pos.x, &pos.y, &pos.z, &width, &depth, &normal.x, &normal.y, &normal.z );
  811. if ( result == 8 )
  812. pObj->_addNode( pos, width, depth, normal );
  813. //}
  814. return false;
  815. }
  816. bool MeshRoad::addProfileNodeFromField( void* obj, const char *index, const char* data )
  817. {
  818. MeshRoad *pObj = static_cast<MeshRoad*>(obj);
  819. F32 x, y;
  820. U32 smooth, mtrl;
  821. U32 result = dSscanf( data, "%g %g %d %d", &x, &y, &smooth, &mtrl );
  822. if ( result == 4 )
  823. {
  824. if(!pObj->mSideProfile.mNodes.empty())
  825. pObj->mSideProfile.mSegMtrls.push_back(mtrl);
  826. MeshRoadProfileNode node;
  827. node.setPosition(x, y);
  828. node.setSmoothing(smooth != 0);
  829. pObj->mSideProfile.mNodes.push_back(node);
  830. }
  831. return false;
  832. }
  833. bool MeshRoad::onAdd()
  834. {
  835. if ( !Parent::onAdd() )
  836. return false;
  837. // Reset the World Box.
  838. //setGlobalBounds();
  839. resetWorldBox();
  840. // Set the Render Transform.
  841. setRenderTransform(mObjToWorld);
  842. // Add to ServerMeshRoadSet
  843. if ( isServerObject() )
  844. {
  845. getServerSet()->addObject( this );
  846. }
  847. if ( isClientObject() )
  848. _initMaterial();
  849. // If this road was not created from a file, give profile two default nodes
  850. if(mSideProfile.mNodes.empty())
  851. {
  852. // Initialize with two nodes in vertical line with unit length
  853. MeshRoadProfileNode node1(Point3F(0.0f, 0.0f, 0.0f));
  854. MeshRoadProfileNode node2(Point3F(0.0f, -5.0f, 0.0f));
  855. mSideProfile.mNodes.push_back(node1);
  856. mSideProfile.mNodes.push_back(node2);
  857. // Both node normals are straight to the right, perpendicular to the profile line
  858. VectorF norm(1.0f, 0.0f, 0.0f);
  859. mSideProfile.mNodeNormals.push_back(norm);
  860. mSideProfile.mNodeNormals.push_back(norm);
  861. mSideProfile.mSegMtrls.push_back(MeshRoad::Side);
  862. }
  863. else
  864. mSideProfile.generateNormals();
  865. // Generate the Vert/Index buffers and everything else.
  866. _regenerate();
  867. // Add to Scene.
  868. addToScene();
  869. return true;
  870. }
  871. void MeshRoad::onRemove()
  872. {
  873. SAFE_DELETE( mPhysicsRep );
  874. mConvexList->nukeList();
  875. for ( U32 i = 0; i < SurfaceCount; i++ )
  876. {
  877. SAFE_DELETE( mMatInst[i] );
  878. }
  879. removeFromScene();
  880. Parent::onRemove();
  881. }
  882. void MeshRoad::inspectPostApply()
  883. {
  884. // Set Parent.
  885. Parent::inspectPostApply();
  886. //if ( mMetersPerSegment < MIN_METERS_PER_SEGMENT )
  887. // mMetersPerSegment = MIN_METERS_PER_SEGMENT;
  888. setMaskBits(MeshRoadMask);
  889. }
  890. void MeshRoad::onStaticModified( const char* slotName, const char*newValue )
  891. {
  892. Parent::onStaticModified( slotName, newValue );
  893. if ( dStricmp( slotName, "breakAngle" ) == 0 )
  894. {
  895. setMaskBits( RegenMask );
  896. }
  897. }
  898. void MeshRoad::writeFields( Stream &stream, U32 tabStop )
  899. {
  900. Parent::writeFields( stream, tabStop );
  901. // Now write all nodes
  902. stream.write(2, "\r\n");
  903. for ( U32 i = 0; i < mNodes.size(); i++ )
  904. {
  905. const MeshRoadNode &node = mNodes[i];
  906. stream.writeTabs(tabStop);
  907. char buffer[1024];
  908. dMemset( buffer, 0, 1024 );
  909. dSprintf( buffer, 1024, "Node = \"%g %g %g %g %g %g %g %g\";", node.point.x, node.point.y, node.point.z, node.width, node.depth, node.normal.x, node.normal.y, node.normal.z );
  910. stream.writeLine( (const U8*)buffer );
  911. }
  912. stream.write(2, "\r\n");
  913. Point3F nodePos;
  914. U8 smooth, mtrl;
  915. for ( U32 i = 0; i < mSideProfile.mNodes.size(); i++ )
  916. {
  917. nodePos = mSideProfile.mNodes[i].getPosition();
  918. if(i)
  919. mtrl = mSideProfile.mSegMtrls[i-1];
  920. else
  921. mtrl = 0;
  922. if(mSideProfile.mNodes[i].isSmooth())
  923. smooth = 1;
  924. else
  925. smooth = 0;
  926. stream.writeTabs(tabStop);
  927. char buffer[1024];
  928. dMemset( buffer, 0, 1024 );
  929. dSprintf( buffer, 1024, "ProfileNode = \"%.6f %.6f %d %d\";", nodePos.x, nodePos.y, smooth, mtrl);
  930. stream.writeLine( (const U8*)buffer );
  931. }
  932. }
  933. bool MeshRoad::writeField( StringTableEntry fieldname, const char *value )
  934. {
  935. if ( fieldname == StringTable->insert("Node") )
  936. return false;
  937. if ( fieldname == StringTable->insert("ProfileNode") )
  938. return false;
  939. return Parent::writeField( fieldname, value );
  940. }
  941. void MeshRoad::onEditorEnable()
  942. {
  943. }
  944. void MeshRoad::onEditorDisable()
  945. {
  946. }
  947. SimSet* MeshRoad::getServerSet()
  948. {
  949. if ( !smServerMeshRoadSet )
  950. {
  951. smServerMeshRoadSet = new SimSet();
  952. smServerMeshRoadSet->registerObject( "ServerMeshRoadSet" );
  953. Sim::getRootGroup()->addObject( smServerMeshRoadSet );
  954. }
  955. return smServerMeshRoadSet;
  956. }
  957. void MeshRoad::prepRenderImage( SceneRenderState* state )
  958. {
  959. if ( mNodes.size() <= 1 )
  960. return;
  961. RenderPassManager *renderPass = state->getRenderPass();
  962. // Normal Road RenderInstance
  963. // Always rendered when the editor is not open
  964. // otherwise obey the smShowRoad flag
  965. if ( smShowRoad || !smEditorOpen )
  966. {
  967. #ifdef TORQUE_AFX_ENABLED
  968. afxZodiacMgr::renderMeshRoadZodiacs(state, this);
  969. #endif
  970. MeshRenderInst coreRI;
  971. coreRI.clear();
  972. coreRI.objectToWorld = &MatrixF::Identity;
  973. coreRI.worldToCamera = renderPass->allocSharedXform(RenderPassManager::View);
  974. coreRI.projection = renderPass->allocSharedXform(RenderPassManager::Projection);
  975. coreRI.type = RenderPassManager::RIT_Mesh;
  976. BaseMatInstance *matInst;
  977. for ( U32 i = 0; i < SurfaceCount; i++ )
  978. {
  979. matInst = state->getOverrideMaterial( mMatInst[i] );
  980. if ( !matInst )
  981. continue;
  982. // Get the lights if we haven't already.
  983. if ( matInst->isForwardLit() && !coreRI.lights[0] )
  984. {
  985. LightQuery query;
  986. query.init( getWorldSphere() );
  987. query.getLights( coreRI.lights, 8 );
  988. }
  989. MeshRenderInst *ri = renderPass->allocInst<MeshRenderInst>();
  990. *ri = coreRI;
  991. // Currently rendering whole road, fix to cull and batch
  992. // per segment.
  993. // Set the correct material for rendering.
  994. ri->matInst = matInst;
  995. ri->vertBuff = &mVB[i];
  996. ri->primBuff = &mPB[i];
  997. ri->prim = renderPass->allocPrim();
  998. ri->prim->type = GFXTriangleList;
  999. ri->prim->minIndex = 0;
  1000. ri->prim->startIndex = 0;
  1001. ri->prim->numPrimitives = mTriangleCount[i];
  1002. ri->prim->startVertex = 0;
  1003. ri->prim->numVertices = mVertCount[i];
  1004. // We sort by the material then vertex buffer.
  1005. ri->defaultKey = matInst->getStateHint();
  1006. ri->defaultKey2 = (uintptr_t)ri->vertBuff; // Not 64bit safe!
  1007. renderPass->addInst( ri );
  1008. }
  1009. }
  1010. // Debug RenderInstance
  1011. // Only when editor is open.
  1012. if ( smEditorOpen )
  1013. {
  1014. ObjectRenderInst *ri = state->getRenderPass()->allocInst<ObjectRenderInst>();
  1015. ri->renderDelegate.bind( this, &MeshRoad::_debugRender );
  1016. ri->type = RenderPassManager::RIT_Editor;
  1017. state->getRenderPass()->addInst( ri );
  1018. }
  1019. }
  1020. void MeshRoad::_initMaterial()
  1021. {
  1022. if (mTopMaterialAsset.notNull())
  1023. {
  1024. if (!mMatInst[Top] || !String(mTopMaterialAsset->getMaterialDefinitionName()).equal(mMatInst[Top]->getMaterial()->getName(), String::NoCase))
  1025. {
  1026. SAFE_DELETE(mMatInst[Top]);
  1027. Material* tMat = nullptr;
  1028. if (!Sim::findObject(mTopMaterialAsset->getMaterialDefinitionName(), tMat))
  1029. Con::errorf("MeshRoad::_initMaterial - Material %s was not found.", mTopMaterialAsset->getMaterialDefinitionName());
  1030. mMaterial[Top] = tMat;
  1031. if (mMaterial[Top])
  1032. mMatInst[Top] = mMaterial[Top]->createMatInstance();
  1033. else
  1034. mMatInst[Top] = MATMGR->createMatInstance("WarningMaterial");
  1035. mMatInst[Top]->init(MATMGR->getDefaultFeatures(), getGFXVertexFormat<GFXVertexPNTT>());
  1036. }
  1037. }
  1038. if (mBottomMaterialAsset.notNull())
  1039. {
  1040. if (!mMatInst[Bottom] || !String(mBottomMaterialAsset->getMaterialDefinitionName()).equal(mMatInst[Bottom]->getMaterial()->getName(), String::NoCase))
  1041. {
  1042. SAFE_DELETE(mMatInst[Bottom]);
  1043. Material* tMat = nullptr;
  1044. if (!Sim::findObject(mBottomMaterialAsset->getMaterialDefinitionName(), tMat))
  1045. Con::errorf("MeshRoad::_initMaterial - Material %s was not found.", mBottomMaterialAsset->getMaterialDefinitionName());
  1046. mMaterial[Bottom] = tMat;
  1047. if (mMaterial[Bottom])
  1048. mMatInst[Bottom] = mMaterial[Bottom]->createMatInstance();
  1049. else
  1050. mMatInst[Bottom] = MATMGR->createMatInstance("WarningMaterial");
  1051. mMatInst[Bottom]->init(MATMGR->getDefaultFeatures(), getGFXVertexFormat<GFXVertexPNTT>());
  1052. }
  1053. }
  1054. if (mSideMaterialAsset.notNull())
  1055. {
  1056. if (!mMatInst[Side] || !String(mSideMaterialAsset->getMaterialDefinitionName()).equal(mMatInst[Side]->getMaterial()->getName(), String::NoCase))
  1057. {
  1058. SAFE_DELETE(mMatInst[Side]);
  1059. Material* tMat = nullptr;
  1060. if (!Sim::findObject(mSideMaterialAsset->getMaterialDefinitionName(), tMat))
  1061. Con::errorf("MeshRoad::_initMaterial - Material %s was not found.", mSideMaterialAsset->getMaterialDefinitionName());
  1062. mMaterial[Side] = tMat;
  1063. if (mMaterial[Side])
  1064. mMatInst[Side] = mMaterial[Side]->createMatInstance();
  1065. else
  1066. mMatInst[Side] = MATMGR->createMatInstance("WarningMaterial");
  1067. mMatInst[Side]->init(MATMGR->getDefaultFeatures(), getGFXVertexFormat<GFXVertexPNTT>());
  1068. }
  1069. }
  1070. }
  1071. void MeshRoad::_debugRender( ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance* )
  1072. {
  1073. //MeshRoadConvex convex;
  1074. //buildConvex( Box3F(true), convex );
  1075. //convex.render();
  1076. //GFXDrawUtil *drawer = GFX->getDrawUtil();
  1077. //GFX->setStateBlock( smStateBlock );
  1078. return;
  1079. /*
  1080. U32 convexCount = mDebugConvex.size();
  1081. PrimBuild::begin( GFXTriangleList, convexCount * 12 );
  1082. PrimBuild::color4i( 0, 0, 255, 155 );
  1083. for ( U32 i = 0; i < convexCount; i++ )
  1084. {
  1085. MeshRoadConvex *convex = mDebugConvex[i];
  1086. Point3F a = convex->verts[0];
  1087. Point3F b = convex->verts[1];
  1088. Point3F c = convex->verts[2];
  1089. Point3F p = convex->verts[3];
  1090. //mObjToWorld.mulP(a);
  1091. //mObjToWorld.mulP(b);
  1092. //mObjToWorld.mulP(c);
  1093. //mObjToWorld.mulP(p);
  1094. PrimBuild::vertex3fv( c );
  1095. PrimBuild::vertex3fv( b );
  1096. PrimBuild::vertex3fv( a );
  1097. PrimBuild::vertex3fv( b );
  1098. PrimBuild::vertex3fv( a );
  1099. PrimBuild::vertex3fv( p );
  1100. PrimBuild::vertex3fv( c );
  1101. PrimBuild::vertex3fv( b );
  1102. PrimBuild::vertex3fv( p );
  1103. PrimBuild::vertex3fv( a );
  1104. PrimBuild::vertex3fv( c );
  1105. PrimBuild::vertex3fv( p );
  1106. }
  1107. PrimBuild::end();
  1108. for ( U32 i = 0; i < mSegments.size(); i++ )
  1109. {
  1110. ///GFX->getDrawUtil()->drawWireBox( mSegments[i].worldbounds, ColorI(255,0,0,255) );
  1111. }
  1112. GFX->enterDebugEvent( ColorI( 255, 0, 0 ), "DecalRoad_debugRender" );
  1113. GFXTransformSaver saver;
  1114. GFX->setStateBlock( smStateBlock );
  1115. Point3F size(1,1,1);
  1116. ColorI color( 255, 0, 0, 255 );
  1117. if ( smShowBatches )
  1118. {
  1119. for ( U32 i = 0; i < mBatches.size(); i++ )
  1120. {
  1121. const Box3F &box = mBatches[i].bounds;
  1122. Point3F center;
  1123. box.getCenter( &center );
  1124. GFX->getDrawUtil()->drawWireCube( ( box.maxExtents - box.minExtents ) * 0.5f, center, ColorI(255,100,100,255) );
  1125. }
  1126. }
  1127. GFX->leaveDebugEvent();
  1128. */
  1129. }
  1130. U32 MeshRoad::packUpdate(NetConnection * con, U32 mask, BitStream * stream)
  1131. {
  1132. U32 retMask = Parent::packUpdate(con, mask, stream);
  1133. if ( stream->writeFlag( mask & MeshRoadMask ) )
  1134. {
  1135. // Write Object Transform.
  1136. stream->writeAffineTransform( mObjToWorld );
  1137. // Write Materials
  1138. packMaterialAsset(con, TopMaterial);
  1139. packMaterialAsset(con, BottomMaterial);
  1140. packMaterialAsset(con, SideMaterial);
  1141. stream->write( mTextureLength );
  1142. stream->write( mBreakAngle );
  1143. stream->write( mWidthSubdivisions );
  1144. }
  1145. if ( stream->writeFlag( mask & ProfileMask ) )
  1146. {
  1147. stream->writeInt( mSideProfile.mNodes.size(), 16 );
  1148. for( U32 i = 0; i < mSideProfile.mNodes.size(); i++ )
  1149. {
  1150. mathWrite( *stream, mSideProfile.mNodes[i].getPosition() );
  1151. stream->writeFlag( mSideProfile.mNodes[i].isSmooth() );
  1152. if(i)
  1153. stream->writeInt(mSideProfile.mSegMtrls[i-1], 3);
  1154. else
  1155. stream->writeInt(0, 3);
  1156. }
  1157. }
  1158. if ( stream->writeFlag( mask & NodeMask ) )
  1159. {
  1160. const U32 nodeByteSize = 32; // Based on sending all of a node's parameters
  1161. // Test if we can fit all of our nodes within the current stream.
  1162. // We make sure we leave 100 bytes still free in the stream for whatever
  1163. // may follow us.
  1164. S32 allowedBytes = stream->getWriteByteSize() - 100;
  1165. if ( stream->writeFlag( (nodeByteSize * mNodes.size()) < allowedBytes ) )
  1166. {
  1167. // All nodes should fit, so send them out now.
  1168. stream->writeInt( mNodes.size(), 16 );
  1169. for ( U32 i = 0; i < mNodes.size(); i++ )
  1170. {
  1171. mathWrite( *stream, mNodes[i].point );
  1172. stream->write( mNodes[i].width );
  1173. stream->write( mNodes[i].depth );
  1174. mathWrite( *stream, mNodes[i].normal );
  1175. }
  1176. }
  1177. else
  1178. {
  1179. // There isn't enough space left in the stream for all of the
  1180. // nodes. Batch them up into NetEvents.
  1181. U32 id = gServerNodeListManager->nextListId();
  1182. U32 count = 0;
  1183. U32 index = 0;
  1184. while (count < mNodes.size())
  1185. {
  1186. count += NodeListManager::smMaximumNodesPerEvent;
  1187. if (count > mNodes.size())
  1188. {
  1189. count = mNodes.size();
  1190. }
  1191. MeshRoadNodeEvent* event = new MeshRoadNodeEvent();
  1192. event->mId = id;
  1193. event->mTotalNodes = mNodes.size();
  1194. event->mLocalListStart = index;
  1195. for (; index<count; ++index)
  1196. {
  1197. event->mPositions.push_back( mNodes[index].point );
  1198. event->mWidths.push_back( mNodes[index].width );
  1199. event->mDepths.push_back( mNodes[index].depth );
  1200. event->mNormals.push_back( mNodes[index].normal );
  1201. }
  1202. con->postNetEvent( event );
  1203. }
  1204. stream->write( id );
  1205. }
  1206. }
  1207. stream->writeFlag( mask & RegenMask );
  1208. // Were done ...
  1209. return retMask;
  1210. }
  1211. void MeshRoad::unpackUpdate(NetConnection * con, BitStream * stream)
  1212. {
  1213. // Unpack Parent.
  1214. Parent::unpackUpdate(con, stream);
  1215. // MeshRoadMask
  1216. if(stream->readFlag())
  1217. {
  1218. MatrixF ObjectMatrix;
  1219. stream->readAffineTransform(&ObjectMatrix);
  1220. Parent::setTransform(ObjectMatrix);
  1221. unpackMaterialAsset(con, TopMaterial);
  1222. unpackMaterialAsset(con, BottomMaterial);
  1223. unpackMaterialAsset(con, SideMaterial);
  1224. if ( isProperlyAdded() )
  1225. _initMaterial();
  1226. stream->read( &mTextureLength );
  1227. stream->read( &mBreakAngle );
  1228. stream->read( &mWidthSubdivisions );
  1229. }
  1230. // ProfileMask
  1231. if(stream->readFlag())
  1232. {
  1233. Point3F pos;
  1234. mSideProfile.mNodes.clear();
  1235. mSideProfile.mSegMtrls.clear();
  1236. U32 count = stream->readInt( 16 );
  1237. for( U32 i = 0; i < count; i++)
  1238. {
  1239. mathRead( *stream, &pos );
  1240. MeshRoadProfileNode node(pos);
  1241. node.setSmoothing( stream->readFlag() );
  1242. mSideProfile.mNodes.push_back(node);
  1243. if(i)
  1244. mSideProfile.mSegMtrls.push_back(stream->readInt(3));
  1245. else
  1246. stream->readInt(3);
  1247. }
  1248. mSideProfile.generateNormals();
  1249. }
  1250. // NodeMask
  1251. if ( stream->readFlag() )
  1252. {
  1253. if (stream->readFlag())
  1254. {
  1255. // Nodes have been passed in this update
  1256. U32 count = stream->readInt( 16 );
  1257. mNodes.clear();
  1258. Point3F pos, normal;
  1259. F32 width, depth;
  1260. for ( U32 i = 0; i < count; i++ )
  1261. {
  1262. mathRead( *stream, &pos );
  1263. stream->read( &width );
  1264. stream->read( &depth );
  1265. mathRead( *stream, &normal );
  1266. _addNode( pos, width, depth, normal );
  1267. }
  1268. }
  1269. else
  1270. {
  1271. // Nodes will arrive as events
  1272. U32 id;
  1273. stream->read( &id );
  1274. // Check if the road's nodes made it here before we did.
  1275. NodeListManager::NodeList* list = NULL;
  1276. if ( gClientNodeListManager->findListById( id, &list, true) )
  1277. {
  1278. // Work with the completed list
  1279. MeshRoadNodeList* roadList = dynamic_cast<MeshRoadNodeList*>( list );
  1280. if (roadList)
  1281. buildNodesFromList( roadList );
  1282. delete list;
  1283. }
  1284. else
  1285. {
  1286. // Nodes have not yet arrived, so register our interest in the list
  1287. MeshRoadNodeListNotify* notify = new MeshRoadNodeListNotify( this, id );
  1288. gClientNodeListManager->registerNotification( notify );
  1289. }
  1290. }
  1291. }
  1292. if ( stream->readFlag() && isProperlyAdded() )
  1293. _regenerate();
  1294. }
  1295. void MeshRoad::setTransform( const MatrixF &mat )
  1296. {
  1297. for ( U32 i = 0; i < mNodes.size(); i++ )
  1298. {
  1299. mWorldToObj.mulP( mNodes[i].point );
  1300. mat.mulP( mNodes[i].point );
  1301. }
  1302. Parent::setTransform( mat );
  1303. if ( mPhysicsRep )
  1304. mPhysicsRep->setTransform( mat );
  1305. // Regenerate and update the client
  1306. _regenerate();
  1307. setMaskBits( NodeMask | RegenMask );
  1308. }
  1309. void MeshRoad::setScale( const VectorF &scale )
  1310. {
  1311. // We ignore scale requests from the editor
  1312. // right now.
  1313. //Parent::setScale( scale );
  1314. }
  1315. void MeshRoad::buildConvex(const Box3F& box, Convex* convex)
  1316. {
  1317. if ( mSlices.size() < 2 )
  1318. return;
  1319. mConvexList->collectGarbage();
  1320. mDebugConvex.clear();
  1321. Box3F realBox = box;
  1322. mWorldToObj.mul(realBox);
  1323. realBox.minExtents.convolveInverse(mObjScale);
  1324. realBox.maxExtents.convolveInverse(mObjScale);
  1325. if (realBox.isOverlapped(getObjBox()) == false)
  1326. return;
  1327. U32 segmentCount = mSegments.size();
  1328. U32 numConvexes ;
  1329. U32 halfConvexes;
  1330. U32 nextSegOffset = 2*mSideProfile.mNodes.size();
  1331. U32 leftSideOffset = nextSegOffset/2;
  1332. U32 k2, capIdx1, capIdx2, capIdx3;
  1333. // Create convex(s) for each segment
  1334. for ( U32 i = 0; i < segmentCount; i++ )
  1335. {
  1336. const MeshRoadSegment &segment = mSegments[i];
  1337. // Is this segment overlapped?
  1338. if ( !segment.getWorldBounds().isOverlapped( box ) )
  1339. continue;
  1340. // Each segment has 6 faces
  1341. for ( U32 j = 0; j < 6; j++ )
  1342. {
  1343. // Only first segment has front face
  1344. if ( j == 4 && i != 0 )
  1345. continue;
  1346. // Only last segment has back face
  1347. if ( j == 5 && i != segmentCount-1 )
  1348. continue;
  1349. // The top and bottom sides have 2 convex(s)
  1350. // The left, right, front, and back sides depend on the user-defined profile
  1351. switch(j)
  1352. {
  1353. case 0: numConvexes = 2; break; // Top
  1354. case 1: // Left
  1355. case 2: numConvexes = 2* (mSideProfile.mNodes.size()-1); break; // Right
  1356. case 3: numConvexes = 2; break; // Bottom
  1357. case 4: // Front
  1358. case 5: numConvexes = mSideProfile.mCap.getNumTris(); break; // Back
  1359. default: numConvexes = 0;
  1360. }
  1361. halfConvexes = numConvexes/2;
  1362. for ( U32 k = 0; k < numConvexes; k++ )
  1363. {
  1364. // See if this convex exists in the working set already...
  1365. Convex* cc = 0;
  1366. CollisionWorkingList& wl = convex->getWorkingList();
  1367. for ( CollisionWorkingList* itr = wl.wLink.mNext; itr != &wl; itr = itr->wLink.mNext )
  1368. {
  1369. if ( itr->mConvex->getType() == MeshRoadConvexType )
  1370. {
  1371. MeshRoadConvex *pConvex = static_cast<MeshRoadConvex*>(itr->mConvex);
  1372. if ( pConvex->pRoad == this &&
  1373. pConvex->segmentId == i &&
  1374. pConvex->faceId == j &&
  1375. pConvex->triangleId == k )
  1376. {
  1377. cc = itr->mConvex;
  1378. break;
  1379. }
  1380. }
  1381. }
  1382. if (cc)
  1383. continue;
  1384. Point3F a, b, c;
  1385. // Top or Bottom
  1386. if(j == 0 || j == 3)
  1387. {
  1388. // Get the triangle...
  1389. U32 idx0 = gIdxArray[j][k][0];
  1390. U32 idx1 = gIdxArray[j][k][1];
  1391. U32 idx2 = gIdxArray[j][k][2];
  1392. a = segment[idx0];
  1393. b = segment[idx1];
  1394. c = segment[idx2];
  1395. }
  1396. // Left Side
  1397. else if(j == 1)
  1398. {
  1399. if(k >= halfConvexes)
  1400. {
  1401. k2 = k + leftSideOffset - halfConvexes;
  1402. a = segment.slice1->verts[k2];
  1403. b = segment.slice0->verts[k2];
  1404. c = segment.slice1->verts[k2 + 1];
  1405. }
  1406. else
  1407. {
  1408. k2 = k + leftSideOffset;
  1409. a = segment.slice0->verts[k2];
  1410. b = segment.slice0->verts[k2 + 1];
  1411. c = segment.slice1->verts[k2 + 1];
  1412. }
  1413. }
  1414. // Right Side
  1415. else if(j == 2)
  1416. {
  1417. // a.set(2*k, 2*k, 0.0f);
  1418. // b.set(2*k, 2*k, 2.0f);
  1419. // c.set(2*(k+1), 2*(k+1), 0.0f);
  1420. if(k >= halfConvexes)
  1421. {
  1422. k2 = k - halfConvexes;
  1423. a = segment.slice1->verts[k2];
  1424. b = segment.slice1->verts[k2 + 1];
  1425. c = segment.slice0->verts[k2];
  1426. }
  1427. else
  1428. {
  1429. a = segment.slice0->verts[k];
  1430. b = segment.slice1->verts[k + 1];
  1431. c = segment.slice0->verts[k + 1];
  1432. }
  1433. }
  1434. // Front
  1435. else if(j == 4)
  1436. {
  1437. k2 = nextSegOffset + leftSideOffset - 1;
  1438. capIdx1 = mSideProfile.mCap.getTriIdx(k, 0);
  1439. capIdx2 = mSideProfile.mCap.getTriIdx(k, 1);
  1440. capIdx3 = mSideProfile.mCap.getTriIdx(k, 2);
  1441. if(capIdx1 >= leftSideOffset)
  1442. capIdx1 = k2 - capIdx1;
  1443. if(capIdx2 >= leftSideOffset)
  1444. capIdx2 = k2 - capIdx2;
  1445. if(capIdx3 >= leftSideOffset)
  1446. capIdx3 = k2 - capIdx3;
  1447. a = segment.slice0->verts[capIdx1];
  1448. b = segment.slice0->verts[capIdx2];
  1449. c = segment.slice0->verts[capIdx3];
  1450. }
  1451. // Back
  1452. else
  1453. {
  1454. k2 = nextSegOffset + leftSideOffset - 1;
  1455. capIdx1 = mSideProfile.mCap.getTriIdx(k, 0);
  1456. capIdx2 = mSideProfile.mCap.getTriIdx(k, 1);
  1457. capIdx3 = mSideProfile.mCap.getTriIdx(k, 2);
  1458. if(capIdx1 >= leftSideOffset)
  1459. capIdx1 = k2 - capIdx1;
  1460. if(capIdx2 >= leftSideOffset)
  1461. capIdx2 = k2 - capIdx2;
  1462. if(capIdx3 >= leftSideOffset)
  1463. capIdx3 = k2 - capIdx3;
  1464. a = segment.slice1->verts[capIdx3];
  1465. b = segment.slice1->verts[capIdx2];
  1466. c = segment.slice1->verts[capIdx1];
  1467. }
  1468. // Transform the result into object space!
  1469. //mWorldToObj.mulP( a );
  1470. //mWorldToObj.mulP( b );
  1471. //mWorldToObj.mulP( c );
  1472. PlaneF p( c, b, a );
  1473. Point3F peak = ((a + b + c) / 3.0f) + (p * 0.15f);
  1474. // Set up the convex...
  1475. MeshRoadConvex *cp = new MeshRoadConvex();
  1476. mConvexList->registerObject( cp );
  1477. convex->addToWorkingList( cp );
  1478. cp->mObject = this;
  1479. cp->pRoad = this;
  1480. cp->segmentId = i;
  1481. cp->faceId = j;
  1482. cp->triangleId = k;
  1483. cp->normal = p;
  1484. cp->verts[0] = c;
  1485. cp->verts[1] = b;
  1486. cp->verts[2] = a;
  1487. cp->verts[3] = peak;
  1488. // Update the bounding box.
  1489. Box3F &bounds = cp->box;
  1490. bounds.minExtents.set( F32_MAX, F32_MAX, F32_MAX );
  1491. bounds.maxExtents.set( -F32_MAX, -F32_MAX, -F32_MAX );
  1492. bounds.minExtents.setMin( a );
  1493. bounds.minExtents.setMin( b );
  1494. bounds.minExtents.setMin( c );
  1495. bounds.minExtents.setMin( peak );
  1496. bounds.maxExtents.setMax( a );
  1497. bounds.maxExtents.setMax( b );
  1498. bounds.maxExtents.setMax( c );
  1499. bounds.maxExtents.setMax( peak );
  1500. mDebugConvex.push_back(cp);
  1501. }
  1502. }
  1503. }
  1504. }
  1505. bool MeshRoad::buildPolyList( PolyListContext, AbstractPolyList* polyList, const Box3F &box, const SphereF & )
  1506. {
  1507. if ( mSlices.size() < 2 )
  1508. return false;
  1509. polyList->setTransform( &MatrixF::Identity, Point3F::One );
  1510. polyList->setObject(this);
  1511. // JCF: optimize this to not always add everything.
  1512. return buildSegmentPolyList( polyList, 0, mSegments.size() - 1, true, true );
  1513. }
  1514. bool MeshRoad::buildSegmentPolyList( AbstractPolyList* polyList, U32 startSegIdx, U32 endSegIdx, bool capFront, bool capEnd )
  1515. {
  1516. if ( mSlices.size() < 2 )
  1517. return false;
  1518. // Add verts
  1519. for ( U32 i = startSegIdx; i <= endSegIdx; i++ )
  1520. {
  1521. const MeshRoadSegment &seg = mSegments[i];
  1522. if ( i == startSegIdx )
  1523. {
  1524. for(U32 j = 0; j < seg.slice0->verts.size(); j++)
  1525. polyList->addPoint( seg.slice0->verts[j] );
  1526. }
  1527. for(U32 j = 0; j < seg.slice1->verts.size(); j++)
  1528. polyList->addPoint( seg.slice1->verts[j] );
  1529. }
  1530. // Temporaries to hold indices for the corner points of a quad.
  1531. S32 p00, p01, p11, p10;
  1532. S32 pb00, pb01, pb11, pb10;
  1533. U32 offset = 0;
  1534. S32 a, b, c;
  1535. U32 mirror;
  1536. DebugDrawer *ddraw = NULL;//DebugDrawer::get();
  1537. ClippedPolyList *cpolyList = dynamic_cast<ClippedPolyList*>(polyList);
  1538. MatrixF mat;
  1539. Point3F scale;
  1540. if ( cpolyList )
  1541. cpolyList->getTransform( &mat, &scale );
  1542. U32 nextSegOffset = 2*mSideProfile.mNodes.size();
  1543. U32 leftSideOffset = nextSegOffset/2;
  1544. for ( U32 i = startSegIdx; i <= endSegIdx; i++ )
  1545. {
  1546. p00 = offset + leftSideOffset;
  1547. p10 = offset;
  1548. pb00 = offset + nextSegOffset - 1;
  1549. pb10 = offset + leftSideOffset - 1;
  1550. p01 = offset + nextSegOffset + leftSideOffset;
  1551. p11 = offset + nextSegOffset;
  1552. pb01 = offset + 2*nextSegOffset - 1;
  1553. pb11 = offset + nextSegOffset + leftSideOffset - 1;
  1554. // Top Face
  1555. polyList->begin( 0,0 );
  1556. polyList->vertex( p00 );
  1557. polyList->vertex( p01 );
  1558. polyList->vertex( p11 );
  1559. polyList->plane( p00, p01, p11 );
  1560. polyList->end();
  1561. if ( ddraw && cpolyList )
  1562. {
  1563. Point3F v0 = cpolyList->mVertexList[p00].point;
  1564. mat.mulP( v0 );
  1565. Point3F v1 = cpolyList->mVertexList[p01].point;
  1566. mat.mulP( v1 );
  1567. Point3F v2 = cpolyList->mVertexList[p11].point;
  1568. mat.mulP( v2 );
  1569. ddraw->drawTri( v0, v1, v2 );
  1570. ddraw->setLastZTest( false );
  1571. ddraw->setLastTTL( 0 );
  1572. }
  1573. polyList->begin( 0,0 );
  1574. polyList->vertex( p00 );
  1575. polyList->vertex( p11 );
  1576. polyList->vertex( p10 );
  1577. polyList->plane( p00, p11, p10 );
  1578. polyList->end();
  1579. if ( ddraw && cpolyList )
  1580. {
  1581. ddraw->drawTri( cpolyList->mVertexList[p00].point, cpolyList->mVertexList[p11].point, cpolyList->mVertexList[p10].point );
  1582. ddraw->setLastTTL( 0 );
  1583. }
  1584. if (buildPolyList_TopSurfaceOnly)
  1585. {
  1586. offset += 4;
  1587. continue;
  1588. }
  1589. // Left Face
  1590. for(U32 j = leftSideOffset; j < nextSegOffset-1; j++)
  1591. {
  1592. a = offset + j;
  1593. b = a + nextSegOffset + 1;
  1594. c = b - 1;
  1595. polyList->begin( 0,0 );
  1596. polyList->vertex( a );
  1597. polyList->vertex( b );
  1598. polyList->vertex( c);
  1599. polyList->plane( a, b, c );
  1600. polyList->end();
  1601. a = offset + j;
  1602. b = a + 1;
  1603. c = a + nextSegOffset + 1;
  1604. polyList->begin( 0,0 );
  1605. polyList->vertex( a );
  1606. polyList->vertex( b );
  1607. polyList->vertex( c );
  1608. polyList->plane( a, b, c );
  1609. polyList->end();
  1610. }
  1611. // Right Face
  1612. for(U32 j = 0; j < leftSideOffset-1; j++)
  1613. {
  1614. a = offset + j;
  1615. b = a + nextSegOffset;
  1616. c = b + 1;
  1617. polyList->begin( 0,0 );
  1618. polyList->vertex( a );
  1619. polyList->vertex( b );
  1620. polyList->vertex( c);
  1621. polyList->plane( a, b, c );
  1622. polyList->end();
  1623. a = offset + j;
  1624. b = a + nextSegOffset + 1;
  1625. c = a + 1;
  1626. polyList->begin( 0,0 );
  1627. polyList->vertex( a );
  1628. polyList->vertex( b );
  1629. polyList->vertex( c );
  1630. polyList->plane( a, b, c );
  1631. polyList->end();
  1632. }
  1633. // Bottom Face
  1634. polyList->begin( 0,0 );
  1635. polyList->vertex( pb00 );
  1636. polyList->vertex( pb10 );
  1637. polyList->vertex( pb11 );
  1638. polyList->plane( pb00, pb10, pb11 );
  1639. polyList->end();
  1640. polyList->begin( 0,0 );
  1641. polyList->vertex( pb00 );
  1642. polyList->vertex( pb11 );
  1643. polyList->vertex( pb01 );
  1644. polyList->plane( pb00, pb11, pb01 );
  1645. polyList->end();
  1646. // Front Face
  1647. if ( i == startSegIdx && capFront )
  1648. {
  1649. mirror = nextSegOffset + leftSideOffset - 1;
  1650. for(U32 j = 0; j < mSideProfile.mCap.getNumTris(); j++)
  1651. {
  1652. a = mSideProfile.mCap.getTriIdx(j, 0);
  1653. b = mSideProfile.mCap.getTriIdx(j, 1);
  1654. c = mSideProfile.mCap.getTriIdx(j, 2);
  1655. if(a >= leftSideOffset)
  1656. a = mirror - a;
  1657. if(b >= leftSideOffset)
  1658. b = mirror - b;
  1659. if(c >= leftSideOffset)
  1660. c = mirror - c;
  1661. polyList->begin( 0,0 );
  1662. polyList->vertex( a );
  1663. polyList->vertex( b );
  1664. polyList->vertex( c );
  1665. polyList->plane( a, b, c );
  1666. polyList->end();
  1667. }
  1668. }
  1669. // Back Face
  1670. if ( i == endSegIdx && capEnd )
  1671. {
  1672. mirror = nextSegOffset + leftSideOffset - 1;
  1673. for(U32 j = 0; j < mSideProfile.mCap.getNumTris(); j++)
  1674. {
  1675. a = mSideProfile.mCap.getTriIdx(j, 0);
  1676. b = mSideProfile.mCap.getTriIdx(j, 1);
  1677. c = mSideProfile.mCap.getTriIdx(j, 2);
  1678. if(a >= leftSideOffset)
  1679. a = offset + nextSegOffset + mirror - a;
  1680. if(b >= leftSideOffset)
  1681. b = offset + nextSegOffset + mirror - b;
  1682. if(c >= leftSideOffset)
  1683. c = offset + nextSegOffset + mirror - c;
  1684. polyList->begin( 0,0 );
  1685. polyList->vertex( c );
  1686. polyList->vertex( b );
  1687. polyList->vertex( a );
  1688. polyList->plane( c, b, a );
  1689. polyList->end();
  1690. }
  1691. }
  1692. offset += nextSegOffset;
  1693. }
  1694. return true;
  1695. }
  1696. bool MeshRoad::castRay( const Point3F &s, const Point3F &e, RayInfo *info )
  1697. {
  1698. Point3F start = s;
  1699. Point3F end = e;
  1700. mObjToWorld.mulP(start);
  1701. mObjToWorld.mulP(end);
  1702. F32 out = 1.0f; // The output fraction/percentage along the line defined by s and e
  1703. VectorF norm(0.0f, 0.0f, 0.0f); // The normal of the face intersected
  1704. Vector<MeshRoadHitSegment> hitSegments;
  1705. for ( U32 i = 0; i < mSegments.size(); i++ )
  1706. {
  1707. const MeshRoadSegment &segment = mSegments[i];
  1708. F32 t;
  1709. VectorF n;
  1710. if ( segment.getWorldBounds().collideLine( start, end, &t, &n ) )
  1711. {
  1712. hitSegments.increment();
  1713. hitSegments.last().t = t;
  1714. hitSegments.last().idx = i;
  1715. }
  1716. }
  1717. dQsort( hitSegments.address(), hitSegments.size(), sizeof(MeshRoadHitSegment), compareHitSegments );
  1718. U32 idx0, idx1, idx2;
  1719. F32 t;
  1720. for ( U32 i = 0; i < hitSegments.size(); i++ )
  1721. {
  1722. U32 segIdx = hitSegments[i].idx;
  1723. const MeshRoadSegment &segment = mSegments[segIdx];
  1724. U32 numConvexes ;
  1725. U32 halfConvexes;
  1726. U32 nextSegOffset = 2*mSideProfile.mNodes.size();
  1727. U32 leftSideOffset = nextSegOffset/2;
  1728. U32 k2, capIdx1, capIdx2, capIdx3;
  1729. // Each segment has 6 faces
  1730. for ( U32 j = 0; j < 6; j++ )
  1731. {
  1732. if ( j == 4 && segIdx != 0 )
  1733. continue;
  1734. if ( j == 5 && segIdx != mSegments.size() - 1 )
  1735. continue;
  1736. // The top and bottom sides have 2 convex(s)
  1737. // The left, right, front, and back sides depend on the user-defined profile
  1738. switch(j)
  1739. {
  1740. case 0: numConvexes = 2; break; // Top
  1741. case 1: // Left
  1742. case 2: numConvexes = 2* (mSideProfile.mNodes.size()-1); break; // Right
  1743. case 3: numConvexes = 2; break; // Bottom
  1744. case 4: // Front
  1745. case 5: numConvexes = mSideProfile.mCap.getNumTris(); break; // Back
  1746. default: numConvexes = 0;
  1747. }
  1748. halfConvexes = numConvexes/2;
  1749. // Each face has 2 triangles
  1750. for ( U32 k = 0; k < numConvexes; k++ )
  1751. {
  1752. const Point3F *a = NULL;
  1753. const Point3F *b = NULL;
  1754. const Point3F *c = NULL;
  1755. // Top or Bottom
  1756. if(j == 0 || j == 3)
  1757. {
  1758. idx0 = gIdxArray[j][k][0];
  1759. idx1 = gIdxArray[j][k][1];
  1760. idx2 = gIdxArray[j][k][2];
  1761. a = &segment[idx0];
  1762. b = &segment[idx1];
  1763. c = &segment[idx2];
  1764. }
  1765. // Left Side
  1766. else if(j == 1)
  1767. {
  1768. if(k >= halfConvexes)
  1769. {
  1770. k2 = k + leftSideOffset - halfConvexes;
  1771. a = &segment.slice1->verts[k2];
  1772. b = &segment.slice0->verts[k2];
  1773. c = &segment.slice1->verts[k2 + 1];
  1774. }
  1775. else
  1776. {
  1777. k2 = k + leftSideOffset;
  1778. a = &segment.slice0->verts[k2];
  1779. b = &segment.slice0->verts[k2 + 1];
  1780. c = &segment.slice1->verts[k2 + 1];
  1781. }
  1782. }
  1783. // Right Side
  1784. else if(j == 2)
  1785. {
  1786. if(k >= halfConvexes)
  1787. {
  1788. k2 = k - halfConvexes;
  1789. a = &segment.slice1->verts[k2];
  1790. b = &segment.slice1->verts[k2 + 1];
  1791. c = &segment.slice0->verts[k2];
  1792. }
  1793. else
  1794. {
  1795. a = &segment.slice0->verts[k];
  1796. b = &segment.slice1->verts[k + 1];
  1797. c = &segment.slice0->verts[k + 1];
  1798. }
  1799. }
  1800. // Front
  1801. else if(j == 4)
  1802. {
  1803. k2 = nextSegOffset + leftSideOffset - 1;
  1804. capIdx1 = mSideProfile.mCap.getTriIdx(k, 0);
  1805. capIdx2 = mSideProfile.mCap.getTriIdx(k, 1);
  1806. capIdx3 = mSideProfile.mCap.getTriIdx(k, 2);
  1807. if(capIdx1 >= leftSideOffset)
  1808. capIdx1 = k2 - capIdx1;
  1809. if(capIdx2 >= leftSideOffset)
  1810. capIdx2 = k2 - capIdx2;
  1811. if(capIdx3 >= leftSideOffset)
  1812. capIdx3 = k2 - capIdx3;
  1813. a = &segment.slice0->verts[capIdx1];
  1814. b = &segment.slice0->verts[capIdx2];
  1815. c = &segment.slice0->verts[capIdx3];
  1816. }
  1817. // Back
  1818. else
  1819. {
  1820. k2 = nextSegOffset + leftSideOffset - 1;
  1821. capIdx1 = mSideProfile.mCap.getTriIdx(k, 0);
  1822. capIdx2 = mSideProfile.mCap.getTriIdx(k, 1);
  1823. capIdx3 = mSideProfile.mCap.getTriIdx(k, 2);
  1824. if(capIdx1 >= leftSideOffset)
  1825. capIdx1 = k2 - capIdx1;
  1826. if(capIdx2 >= leftSideOffset)
  1827. capIdx2 = k2 - capIdx2;
  1828. if(capIdx3 >= leftSideOffset)
  1829. capIdx3 = k2 - capIdx3;
  1830. a = &segment.slice1->verts[capIdx3];
  1831. b = &segment.slice1->verts[capIdx2];
  1832. c = &segment.slice1->verts[capIdx1];
  1833. }
  1834. if ( !MathUtils::mLineTriangleCollide( start, end,
  1835. *c, *b, *a,
  1836. NULL,
  1837. &t ) )
  1838. continue;
  1839. if ( t >= 0.0f && t < 1.0f && t < out )
  1840. {
  1841. out = t;
  1842. norm = PlaneF( *a, *b, *c );
  1843. }
  1844. }
  1845. }
  1846. if (out >= 0.0f && out < 1.0f)
  1847. break;
  1848. }
  1849. if (out >= 0.0f && out < 1.0f)
  1850. {
  1851. info->t = out;
  1852. info->normal = norm;
  1853. info->point.interpolate(start, end, out);
  1854. info->face = -1;
  1855. info->object = this;
  1856. info->material = this->mMatInst[0];
  1857. return true;
  1858. }
  1859. return false;
  1860. }
  1861. bool MeshRoad::collideBox(const Point3F &start, const Point3F &end, RayInfo* info)
  1862. {
  1863. Con::warnf( "MeshRoad::collideBox() - not yet implemented!" );
  1864. return Parent::collideBox( start, end, info );
  1865. }
  1866. void MeshRoad::_regenerate()
  1867. {
  1868. if ( mNodes.size() == 0 )
  1869. return;
  1870. if ( mSideProfile.mNodes.size() == 2 && mSideProfile.mNodes[1].getPosition().x == 0.0f)
  1871. mSideProfile.setProfileDepth(mNodes[0].depth);
  1872. const Point3F &nodePt = mNodes.first().point;
  1873. MatrixF mat( true );
  1874. mat.setPosition( nodePt );
  1875. Parent::setTransform( mat );
  1876. _generateSlices();
  1877. // Make sure we are in the correct bins given our world box.
  1878. if( getSceneManager() != NULL )
  1879. getSceneManager()->notifyObjectDirty( this );
  1880. }
  1881. void MeshRoad::_generateSlices()
  1882. {
  1883. if ( mNodes.size() < 2 )
  1884. return;
  1885. // Create the spline, initialized with the MeshRoadNode(s)
  1886. U32 nodeCount = mNodes.size();
  1887. MeshRoadSplineNode *splineNodes = new MeshRoadSplineNode[nodeCount];
  1888. for ( U32 i = 0; i < nodeCount; i++ )
  1889. {
  1890. MeshRoadSplineNode &splineNode = splineNodes[i];
  1891. const MeshRoadNode &node = mNodes[i];
  1892. splineNode.x = node.point.x;
  1893. splineNode.y = node.point.y;
  1894. splineNode.z = node.point.z;
  1895. splineNode.width = node.width;
  1896. splineNode.depth = node.depth;
  1897. splineNode.normal = node.normal;
  1898. }
  1899. CatmullRom<MeshRoadSplineNode> spline;
  1900. spline.initialize( nodeCount, splineNodes );
  1901. delete [] splineNodes;
  1902. mSlices.clear();
  1903. VectorF lastBreakVector(0,0,0);
  1904. MeshRoadSlice slice;
  1905. MeshRoadSplineNode lastBreakNode;
  1906. lastBreakNode = spline.evaluate(0.0f);
  1907. for ( U32 i = 1; i < mNodes.size(); i++ )
  1908. {
  1909. F32 t1 = spline.getTime(i);
  1910. F32 t0 = spline.getTime(i-1);
  1911. F32 segLength = spline.arcLength( t0, t1 );
  1912. U32 numSegments = mCeil( segLength / MIN_METERS_PER_SEGMENT );
  1913. numSegments = getMax( numSegments, (U32)1 );
  1914. F32 tstep = ( t1 - t0 ) / numSegments;
  1915. U32 startIdx = 0;
  1916. U32 endIdx = ( i == nodeCount - 1 ) ? numSegments + 1 : numSegments;
  1917. for ( U32 j = startIdx; j < endIdx; j++ )
  1918. {
  1919. F32 t = t0 + tstep * j;
  1920. MeshRoadSplineNode splineNode = spline.evaluate(t);
  1921. VectorF toNodeVec = splineNode.getPosition() - lastBreakNode.getPosition();
  1922. toNodeVec.normalizeSafe();
  1923. if ( lastBreakVector.isZero() )
  1924. lastBreakVector = toNodeVec;
  1925. F32 angle = mRadToDeg( mAcos( mDot( toNodeVec, lastBreakVector ) ) );
  1926. if ( j == startIdx ||
  1927. ( j == endIdx - 1 && i == mNodes.size() - 1 ) ||
  1928. angle > mBreakAngle )
  1929. {
  1930. // Push back a spline node
  1931. slice.p1.set( splineNode.x, splineNode.y, splineNode.z );
  1932. slice.width = splineNode.width;
  1933. slice.depth = splineNode.depth;
  1934. slice.normal = splineNode.normal;
  1935. slice.normal.normalize();
  1936. slice.parentNodeIdx = i-1;
  1937. slice.t = t;
  1938. mSlices.push_back( slice );
  1939. lastBreakVector = splineNode.getPosition() - lastBreakNode.getPosition();
  1940. lastBreakVector.normalizeSafe();
  1941. lastBreakNode = splineNode;
  1942. }
  1943. }
  1944. }
  1945. MatrixF mat(true);
  1946. Box3F box;
  1947. U32 lastProfileNode = mSideProfile.mNodes.size() - 1;
  1948. F32 depth = mSideProfile.mNodes[lastProfileNode].getPosition().y;
  1949. F32 bttmOffset = mSideProfile.mNodes[lastProfileNode].getPosition().x;
  1950. for ( U32 i = 0; i < mSlices.size(); i++ )
  1951. {
  1952. // Calculate uvec, fvec, and rvec for all slices
  1953. calcSliceTransform( i, mat );
  1954. MeshRoadSlice *slicePtr = &mSlices[i];
  1955. mat.getColumn( 0, &slicePtr->rvec );
  1956. mat.getColumn( 1, &slicePtr->fvec );
  1957. mat.getColumn( 2, &slicePtr->uvec );
  1958. // Calculate p0/p2/pb0/pb2 for all slices
  1959. slicePtr->p0 = slicePtr->p1 - slicePtr->rvec * slicePtr->width * 0.5f;
  1960. slicePtr->p2 = slicePtr->p1 + slicePtr->rvec * slicePtr->width * 0.5f;
  1961. slicePtr->pb0 = slicePtr->p0 + slicePtr->uvec * depth - slicePtr->rvec * bttmOffset;
  1962. slicePtr->pb2 = slicePtr->p2 + slicePtr->uvec * depth + slicePtr->rvec * bttmOffset;
  1963. // Generate or extend the object/world bounds
  1964. if ( i == 0 )
  1965. {
  1966. box.minExtents = slicePtr->p0;
  1967. box.maxExtents = slicePtr->p2;
  1968. box.extend(slicePtr->pb0 );
  1969. box.extend(slicePtr->pb2 );
  1970. }
  1971. else
  1972. {
  1973. box.extend(slicePtr->p0 );
  1974. box.extend(slicePtr->p2 );
  1975. box.extend(slicePtr->pb0 );
  1976. box.extend(slicePtr->pb2 );
  1977. }
  1978. // Right side
  1979. Point3F pos;
  1980. VectorF norm;
  1981. MatrixF profileMat1(true);
  1982. profileMat1.setRow(0, slicePtr->rvec);
  1983. profileMat1.setRow(1, slicePtr->uvec);
  1984. profileMat1.setRow(2, -slicePtr->fvec);
  1985. // Left side
  1986. MatrixF profileMat2(true);
  1987. profileMat2.setRow(0, -slicePtr->rvec);
  1988. profileMat2.setRow(1, slicePtr->uvec);
  1989. profileMat2.setRow(2, slicePtr->fvec);
  1990. for(U32 i = 0; i < 2; i++)
  1991. {
  1992. if(i)
  1993. mSideProfile.setTransform(profileMat2, slicePtr->p0);
  1994. else
  1995. mSideProfile.setTransform(profileMat1, slicePtr->p2);
  1996. // Retain original per-node depth functionality
  1997. if(mSideProfile.mNodes.size() == 2 && mSideProfile.mNodes[1].getPosition().y == -mSlices[0].depth)
  1998. {
  1999. mSideProfile.getNodeWorldPos(0, pos);
  2000. slicePtr->verts.push_back(pos);
  2001. box.extend( pos );
  2002. pos.z -= slicePtr->depth;
  2003. slicePtr->verts.push_back(pos);
  2004. box.extend( pos );
  2005. if(i)
  2006. slicePtr->pb0 = pos;
  2007. else
  2008. slicePtr->pb2 = pos;
  2009. mSideProfile.getNormToSlice(0, norm);
  2010. slicePtr->norms.push_back(norm);
  2011. mSideProfile.getNormToSlice(1, norm);
  2012. slicePtr->norms.push_back(norm);
  2013. }
  2014. // New profile functionality
  2015. else
  2016. {
  2017. for(U32 j = 0; j < mSideProfile.mNodes.size(); j++)
  2018. {
  2019. mSideProfile.getNodeWorldPos(j, pos);
  2020. slicePtr->verts.push_back(pos);
  2021. box.extend( pos );
  2022. }
  2023. for(U32 j = 0; j < mSideProfile.mNodeNormals.size(); j++)
  2024. {
  2025. mSideProfile.getNormToSlice(j, norm);
  2026. slicePtr->norms.push_back(norm);
  2027. }
  2028. }
  2029. }
  2030. }
  2031. mWorldBox = box;
  2032. resetObjectBox();
  2033. _generateSegments();
  2034. }
  2035. void MeshRoad::_generateSegments()
  2036. {
  2037. SAFE_DELETE( mPhysicsRep );
  2038. mSegments.clear();
  2039. for ( U32 i = 0; i < mSlices.size() - 1; i++ )
  2040. {
  2041. MeshRoadSegment seg( &mSlices[i], &mSlices[i+1], getWorldTransform() );
  2042. mSegments.push_back( seg );
  2043. }
  2044. //mSideProfile.generateEndCap(mSlices[0].width);
  2045. if ( isClientObject() )
  2046. _generateVerts();
  2047. if ( PHYSICSMGR )
  2048. {
  2049. ConcretePolyList polylist;
  2050. if ( buildPolyList( PLC_Collision, &polylist, getWorldBox(), getWorldSphere() ) )
  2051. {
  2052. polylist.triangulate();
  2053. PhysicsCollision *colShape = PHYSICSMGR->createCollision();
  2054. colShape->addTriangleMesh( polylist.mVertexList.address(),
  2055. polylist.mVertexList.size(),
  2056. polylist.mIndexList.address(),
  2057. polylist.mIndexList.size() / 3,
  2058. MatrixF::Identity );
  2059. PhysicsWorld *world = PHYSICSMGR->getWorld( isServerObject() ? "server" : "client" );
  2060. mPhysicsRep = PHYSICSMGR->createBody();
  2061. mPhysicsRep->init( colShape, 0, 0, this, world );
  2062. }
  2063. }
  2064. }
  2065. void MeshRoad::_generateVerts()
  2066. {
  2067. const U32 widthDivisions = getMax( 0, mWidthSubdivisions );
  2068. const F32 divisionStep = 1.0f / (F32)( widthDivisions + 1 );
  2069. const U32 sliceCount = mSlices.size();
  2070. const U32 segmentCount = mSegments.size();
  2071. U32 numProfSide, numProfTop, numProfBottom;
  2072. numProfSide = numProfTop = numProfBottom = 0;
  2073. // Find how many profile segments are set to side, top, and bottom materials
  2074. for ( U32 i = 0; i < mSideProfile.mSegMtrls.size(); i++)
  2075. {
  2076. switch(mSideProfile.mSegMtrls[i])
  2077. {
  2078. case Side: numProfSide++; break;
  2079. case Top: numProfTop++; break;
  2080. case Bottom: numProfBottom++; break;
  2081. }
  2082. }
  2083. F32 profLen = mSideProfile.getProfileLen();
  2084. mVertCount[Top] = ( 2 + widthDivisions ) * sliceCount;
  2085. mVertCount[Top] += sliceCount * numProfTop * 4;
  2086. mTriangleCount[Top] = segmentCount * 2 * ( widthDivisions + 1 );
  2087. mTriangleCount[Top] += segmentCount * numProfTop * 4;
  2088. mVertCount[Bottom] = sliceCount * 2;
  2089. mVertCount[Bottom] += sliceCount * numProfBottom * 4;
  2090. mTriangleCount[Bottom] = segmentCount * 2;
  2091. mTriangleCount[Bottom] += segmentCount * numProfBottom * 4;
  2092. mVertCount[Side] = sliceCount * numProfSide * 4; // side verts
  2093. mVertCount[Side] += mSideProfile.mNodes.size() * 4; // end cap verts
  2094. mTriangleCount[Side] = segmentCount * numProfSide * 4; // side tris
  2095. mTriangleCount[Side] += mSideProfile.mCap.getNumTris() * 2; // end cap tris
  2096. // Calculate TexCoords for Slices
  2097. F32 texCoordV = 0.0f;
  2098. mSlices[0].texCoordV = 0.0f;
  2099. for ( U32 i = 1; i < sliceCount; i++ )
  2100. {
  2101. MeshRoadSlice &slice = mSlices[i];
  2102. MeshRoadSlice &prevSlice = mSlices[i-1];
  2103. // Increment the textCoordV for the next slice.
  2104. F32 len = ( slice.p1 - prevSlice.p1 ).len();
  2105. texCoordV += len / mTextureLength;
  2106. slice.texCoordV = texCoordV;
  2107. }
  2108. // Make Vertex Buffers
  2109. GFXVertexPNTT *pVert = NULL;
  2110. U32 vertCounter = 0;
  2111. // Top Buffers...
  2112. mVB[Top].set( GFX, mVertCount[Top], GFXBufferTypeStatic );
  2113. pVert = mVB[Top].lock();
  2114. vertCounter = 0;
  2115. for ( U32 i = 0; i < sliceCount; i++ )
  2116. {
  2117. MeshRoadSlice &slice = mSlices[i];
  2118. pVert->point = slice.p0;
  2119. pVert->normal = slice.uvec;
  2120. pVert->tangent = slice.fvec;
  2121. pVert->texCoord.set(1,slice.texCoordV);
  2122. pVert++;
  2123. vertCounter++;
  2124. for ( U32 j = 0; j < widthDivisions; j++ )
  2125. {
  2126. const F32 t = divisionStep * (F32)( j + 1 );
  2127. pVert->point.interpolate( slice.p0, slice.p2, t );
  2128. pVert->normal = slice.uvec;
  2129. pVert->tangent = slice.fvec;
  2130. pVert->texCoord.set( 1.0f - t, slice.texCoordV );
  2131. pVert++;
  2132. vertCounter++;
  2133. }
  2134. pVert->point = slice.p2;
  2135. pVert->normal = slice.uvec;
  2136. pVert->tangent = slice.fvec;
  2137. pVert->texCoord.set( 0, slice.texCoordV );
  2138. pVert++;
  2139. vertCounter++;
  2140. }
  2141. if(numProfTop)
  2142. {
  2143. for ( U32 i = 0; i < sliceCount; i++ )
  2144. {
  2145. MeshRoadSlice &slice = mSlices[i];
  2146. // Right Side
  2147. for ( U32 j = 0; j < mSideProfile.mNodes.size()-1; j++)
  2148. {
  2149. if(mSideProfile.mSegMtrls[j] == Top)
  2150. {
  2151. // Vertex 1
  2152. pVert->point = slice.verts[j];
  2153. pVert->normal = slice.norms[2*j];
  2154. pVert->tangent = slice.fvec;
  2155. pVert->texCoord.set(mSideProfile.getNodePosPercent(j)*profLen/mTextureLength,slice.texCoordV);
  2156. pVert++;
  2157. vertCounter++;
  2158. // Vertex 2
  2159. pVert->point = slice.verts[j+1];
  2160. pVert->normal = slice.norms[2*j+1];
  2161. pVert->tangent = slice.fvec;
  2162. pVert->texCoord.set(mSideProfile.getNodePosPercent(j+1)*profLen/mTextureLength,slice.texCoordV);
  2163. pVert++;
  2164. vertCounter++;
  2165. }
  2166. }
  2167. // Left Side
  2168. for( U32 j = mSideProfile.mNodes.size(); j < 2*mSideProfile.mNodes.size()-1; j++)
  2169. {
  2170. if(mSideProfile.mSegMtrls[j-mSideProfile.mNodes.size()] == Top)
  2171. {
  2172. // Vertex 1
  2173. pVert->point = slice.verts[j];
  2174. pVert->normal = slice.norms[2*j-2];
  2175. pVert->tangent = slice.fvec;
  2176. pVert->texCoord.set(mSideProfile.getNodePosPercent(j)*profLen/mTextureLength,slice.texCoordV);
  2177. pVert++;
  2178. vertCounter++;
  2179. // Vertex 2
  2180. pVert->point = slice.verts[j+1];
  2181. pVert->normal = slice.norms[2*j-1];
  2182. pVert->tangent = slice.fvec;
  2183. pVert->texCoord.set(mSideProfile.getNodePosPercent(j+1)*profLen/mTextureLength,slice.texCoordV);
  2184. pVert++;
  2185. vertCounter++;
  2186. }
  2187. }
  2188. }
  2189. }
  2190. AssertFatal( vertCounter == mVertCount[Top], "MeshRoad, wrote incorrect number of verts in mVB[Top]!" );
  2191. mVB[Top].unlock();
  2192. // Bottom Buffer...
  2193. mVB[Bottom].set( GFX, mVertCount[Bottom], GFXBufferTypeStatic );
  2194. pVert = mVB[Bottom].lock();
  2195. vertCounter = 0;
  2196. for ( U32 i = 0; i < sliceCount; i++ )
  2197. {
  2198. MeshRoadSlice &slice = mSlices[i];
  2199. pVert->point = slice.pb2;
  2200. pVert->normal = -slice.uvec;
  2201. pVert->tangent = slice.fvec;
  2202. pVert->texCoord.set(0,slice.texCoordV);
  2203. pVert++;
  2204. vertCounter++;
  2205. pVert->point = slice.pb0;
  2206. pVert->normal = -slice.uvec;
  2207. pVert->tangent = slice.fvec;
  2208. pVert->texCoord.set(1,slice.texCoordV);
  2209. pVert++;
  2210. vertCounter++;
  2211. }
  2212. if(numProfBottom)
  2213. {
  2214. for ( U32 i = 0; i < sliceCount; i++ )
  2215. {
  2216. MeshRoadSlice &slice = mSlices[i];
  2217. // Right Side
  2218. for ( U32 j = 0; j < mSideProfile.mNodes.size()-1; j++)
  2219. {
  2220. if(mSideProfile.mSegMtrls[j] == Bottom)
  2221. {
  2222. // Vertex 1
  2223. pVert->point = slice.verts[j];
  2224. pVert->normal = slice.norms[2*j];
  2225. pVert->tangent = slice.fvec;
  2226. pVert->texCoord.set(mSideProfile.getNodePosPercent(j)*profLen/mTextureLength,slice.texCoordV);
  2227. pVert++;
  2228. vertCounter++;
  2229. // Vertex 2
  2230. pVert->point = slice.verts[j+1];
  2231. pVert->normal = slice.norms[2*j+1];
  2232. pVert->tangent = slice.fvec;
  2233. pVert->texCoord.set(mSideProfile.getNodePosPercent(j+1)*profLen/mTextureLength,slice.texCoordV);
  2234. pVert++;
  2235. vertCounter++;
  2236. }
  2237. }
  2238. // Left Side
  2239. for( U32 j = mSideProfile.mNodes.size(); j < 2*mSideProfile.mNodes.size()-1; j++)
  2240. {
  2241. if(mSideProfile.mSegMtrls[j-mSideProfile.mNodes.size()] == Bottom)
  2242. {
  2243. // Vertex 1
  2244. pVert->point = slice.verts[j];
  2245. pVert->normal = slice.norms[2*j-2];
  2246. pVert->tangent = slice.fvec;
  2247. pVert->texCoord.set(mSideProfile.getNodePosPercent(j)*profLen/mTextureLength,slice.texCoordV);
  2248. pVert++;
  2249. vertCounter++;
  2250. // Vertex 2
  2251. pVert->point = slice.verts[j+1];
  2252. pVert->normal = slice.norms[2*j-1];
  2253. pVert->tangent = slice.fvec;
  2254. pVert->texCoord.set(mSideProfile.getNodePosPercent(j+1)*profLen/mTextureLength,slice.texCoordV);
  2255. pVert++;
  2256. vertCounter++;
  2257. }
  2258. }
  2259. }
  2260. }
  2261. AssertFatal( vertCounter == mVertCount[Bottom], "MeshRoad, wrote incorrect number of verts in mVB[Bottom]!" );
  2262. mVB[Bottom].unlock();
  2263. // Side Buffers...
  2264. mVB[Side].set( GFX, mVertCount[Side], GFXBufferTypeStatic );
  2265. pVert = mVB[Side].lock();
  2266. vertCounter = 0;
  2267. if(numProfSide)
  2268. {
  2269. for ( U32 i = 0; i < sliceCount; i++ )
  2270. {
  2271. MeshRoadSlice &slice = mSlices[i];
  2272. // Right Side
  2273. for( U32 j = 0; j < mSideProfile.mNodes.size()-1; j++)
  2274. {
  2275. if(mSideProfile.mSegMtrls[j] == Side)
  2276. {
  2277. // Segment Vertex 1
  2278. pVert->point = slice.verts[j];
  2279. pVert->normal = slice.norms[2*j];
  2280. pVert->tangent = slice.fvec;
  2281. pVert->texCoord.set(mSideProfile.getNodePosPercent(j)*profLen/mTextureLength,slice.texCoordV);
  2282. pVert++;
  2283. vertCounter++;
  2284. // Segment Vertex 2
  2285. pVert->point = slice.verts[j+1];
  2286. pVert->normal = slice.norms[2*j+1];
  2287. pVert->tangent = slice.fvec;
  2288. pVert->texCoord.set(mSideProfile.getNodePosPercent(j+1)*profLen/mTextureLength,slice.texCoordV);
  2289. pVert++;
  2290. vertCounter++;
  2291. }
  2292. }
  2293. // Left Side
  2294. for( U32 j = mSideProfile.mNodes.size(); j < 2*mSideProfile.mNodes.size()-1; j++)
  2295. {
  2296. if(mSideProfile.mSegMtrls[j-mSideProfile.mNodes.size()] == Side)
  2297. {
  2298. // Segment Vertex 1
  2299. pVert->point = slice.verts[j];
  2300. pVert->normal = slice.norms[2*j-2];
  2301. pVert->tangent = slice.fvec;
  2302. pVert->texCoord.set(mSideProfile.getNodePosPercent(j)*profLen/mTextureLength,slice.texCoordV);
  2303. pVert++;
  2304. vertCounter++;
  2305. // Segment Vertex 2
  2306. pVert->point = slice.verts[j+1];
  2307. pVert->normal = slice.norms[2*j-1];
  2308. pVert->tangent = slice.fvec;
  2309. pVert->texCoord.set(mSideProfile.getNodePosPercent(j+1)*profLen/mTextureLength,slice.texCoordV);
  2310. pVert++;
  2311. vertCounter++;
  2312. }
  2313. }
  2314. }
  2315. }
  2316. // Cap verts
  2317. Point3F pos;
  2318. VectorF norm;
  2319. VectorF tang;
  2320. for( U32 i = 0; i < mSlices.size(); i += mSlices.size()-1)
  2321. {
  2322. MeshRoadSlice &slice = mSlices[i];
  2323. // Back cap
  2324. if(i)
  2325. {
  2326. norm = slice.fvec;
  2327. tang = -slice.rvec;
  2328. }
  2329. // Front cap
  2330. else
  2331. {
  2332. norm = -slice.fvec;
  2333. tang = slice.rvec;
  2334. }
  2335. // Right side
  2336. for( U32 j = 0; j < mSideProfile.mNodes.size(); j++)
  2337. {
  2338. pVert->point = slice.verts[j];
  2339. pVert->normal = norm;
  2340. pVert->tangent = tang;
  2341. pos = mSideProfile.mNodes[j].getPosition();
  2342. pVert->texCoord.set(pos.x/mTextureLength, pos.y/mTextureLength);
  2343. pVert++;
  2344. vertCounter++;
  2345. }
  2346. // Left side
  2347. for( U32 j = 2*mSideProfile.mNodes.size()-1; j >= mSideProfile.mNodes.size(); j--)
  2348. {
  2349. pVert->point = slice.verts[j];
  2350. pVert->normal = norm;
  2351. pVert->tangent = tang;
  2352. pos = mSideProfile.mNodes[j-mSideProfile.mNodes.size()].getPosition();
  2353. pos.x = -pos.x - slice.width;
  2354. pVert->texCoord.set(pos.x/mTextureLength, pos.y/mTextureLength);
  2355. pVert++;
  2356. vertCounter++;
  2357. }
  2358. }
  2359. AssertFatal( vertCounter == mVertCount[Side], "MeshRoad, wrote incorrect number of verts in mVB[Side]!" );
  2360. mVB[Side].unlock();
  2361. // Make Primitive Buffers
  2362. U32 p00, p01, p11, p10;
  2363. U32 pb00, pb01, pb11, pb10;
  2364. U32 offset = 0;
  2365. U16 *pIdx = NULL;
  2366. U32 curIdx = 0;
  2367. // Top Primitive Buffer
  2368. mPB[Top].set( GFX, mTriangleCount[Top] * 3, mTriangleCount[Top], GFXBufferTypeStatic );
  2369. mPB[Top].lock(&pIdx);
  2370. curIdx = 0;
  2371. offset = 0;
  2372. const U32 rowStride = 2 + widthDivisions;
  2373. for ( U32 i = 0; i < mSegments.size(); i++ )
  2374. {
  2375. for ( U32 j = 0; j < widthDivisions + 1; j++ )
  2376. {
  2377. p00 = offset;
  2378. p10 = offset + 1;
  2379. p01 = offset + rowStride;
  2380. p11 = offset + rowStride + 1;
  2381. pIdx[curIdx] = p00;
  2382. curIdx++;
  2383. pIdx[curIdx] = p01;
  2384. curIdx++;
  2385. pIdx[curIdx] = p11;
  2386. curIdx++;
  2387. pIdx[curIdx] = p00;
  2388. curIdx++;
  2389. pIdx[curIdx] = p11;
  2390. curIdx++;
  2391. pIdx[curIdx] = p10;
  2392. curIdx++;
  2393. offset += 1;
  2394. }
  2395. offset += 1;
  2396. }
  2397. offset += 2;
  2398. if(numProfTop)
  2399. {
  2400. U32 nextSegOffset = 4 * numProfTop;
  2401. for ( U32 i = 0; i < segmentCount; i++ )
  2402. {
  2403. // Loop through profile segments on right side
  2404. for( U32 j = 0; j < numProfTop; j++)
  2405. {
  2406. // Profile Segment Face 1
  2407. pIdx[curIdx] = nextSegOffset*i + 2*j + offset;
  2408. curIdx++;
  2409. pIdx[curIdx] = nextSegOffset*i + 2*j + nextSegOffset + 1 + offset;
  2410. curIdx++;
  2411. pIdx[curIdx] = nextSegOffset*i + 2*j + 1 + offset;
  2412. curIdx++;
  2413. // Profile Segment Face 2
  2414. pIdx[curIdx] = nextSegOffset*i + 2*j + offset;
  2415. curIdx++;
  2416. pIdx[curIdx] = nextSegOffset*i + 2*j + nextSegOffset + offset;
  2417. curIdx++;
  2418. pIdx[curIdx] = nextSegOffset*i + 2*j + nextSegOffset + 1 + offset;
  2419. curIdx++;
  2420. }
  2421. // Loop through profile segments on left side
  2422. for( U32 j = numProfTop; j < 2*numProfTop; j++)
  2423. {
  2424. // Profile Segment Face 1
  2425. pIdx[curIdx] = nextSegOffset*i + 2*j + offset;
  2426. curIdx++;
  2427. pIdx[curIdx] = nextSegOffset*i + 2*j + 1 + offset;
  2428. curIdx++;
  2429. pIdx[curIdx] = nextSegOffset*i + 2*j + nextSegOffset + 1 + offset;
  2430. curIdx++;
  2431. // Profile Segment Face 2
  2432. pIdx[curIdx] = nextSegOffset*i + 2*j + offset;
  2433. curIdx++;
  2434. pIdx[curIdx] = nextSegOffset*i + 2*j + nextSegOffset + 1 + offset;
  2435. curIdx++;
  2436. pIdx[curIdx] = nextSegOffset*i + 2*j + nextSegOffset + offset;
  2437. curIdx++;
  2438. }
  2439. }
  2440. }
  2441. AssertFatal( curIdx == mTriangleCount[Top] * 3, "MeshRoad, wrote incorrect number of indices in mPB[Top]!" );
  2442. mPB[Top].unlock();
  2443. // Bottom Primitive Buffer
  2444. mPB[Bottom].set( GFX, mTriangleCount[Bottom] * 3, mTriangleCount[Bottom], GFXBufferTypeStatic );
  2445. mPB[Bottom].lock(&pIdx);
  2446. curIdx = 0;
  2447. offset = 0;
  2448. for ( U32 i = 0; i < mSegments.size(); i++ )
  2449. {
  2450. p00 = offset;
  2451. p10 = offset + 1;
  2452. p01 = offset + 2;
  2453. p11 = offset + 3;
  2454. pIdx[curIdx] = p00;
  2455. curIdx++;
  2456. pIdx[curIdx] = p01;
  2457. curIdx++;
  2458. pIdx[curIdx] = p11;
  2459. curIdx++;
  2460. pIdx[curIdx] = p00;
  2461. curIdx++;
  2462. pIdx[curIdx] = p11;
  2463. curIdx++;
  2464. pIdx[curIdx] = p10;
  2465. curIdx++;
  2466. offset += 2;
  2467. }
  2468. offset += 2;
  2469. if(numProfBottom)
  2470. {
  2471. U32 nextSegOffset = 4 * numProfBottom;
  2472. for ( U32 i = 0; i < segmentCount; i++ )
  2473. {
  2474. // Loop through profile segments on right side
  2475. for( U32 j = 0; j < numProfBottom; j++)
  2476. {
  2477. // Profile Segment Face 1
  2478. pIdx[curIdx] = nextSegOffset*i + 2*j + offset;
  2479. curIdx++;
  2480. pIdx[curIdx] = nextSegOffset*i + 2*j + nextSegOffset + 1 + offset;
  2481. curIdx++;
  2482. pIdx[curIdx] = nextSegOffset*i + 2*j + 1 + offset;
  2483. curIdx++;
  2484. // Profile Segment Face 2
  2485. pIdx[curIdx] = nextSegOffset*i + 2*j + offset;
  2486. curIdx++;
  2487. pIdx[curIdx] = nextSegOffset*i + 2*j + nextSegOffset + offset;
  2488. curIdx++;
  2489. pIdx[curIdx] = nextSegOffset*i + 2*j + nextSegOffset + 1 + offset;
  2490. curIdx++;
  2491. }
  2492. // Loop through profile segments on left side
  2493. for( U32 j = numProfBottom; j < 2*numProfBottom; j++)
  2494. {
  2495. // Profile Segment Face 1
  2496. pIdx[curIdx] = nextSegOffset*i + 2*j + offset;
  2497. curIdx++;
  2498. pIdx[curIdx] = nextSegOffset*i + 2*j + 1 + offset;
  2499. curIdx++;
  2500. pIdx[curIdx] = nextSegOffset*i + 2*j + nextSegOffset + 1 + offset;
  2501. curIdx++;
  2502. // Profile Segment Face 2
  2503. pIdx[curIdx] = nextSegOffset*i + 2*j + offset;
  2504. curIdx++;
  2505. pIdx[curIdx] = nextSegOffset*i + 2*j + nextSegOffset + 1 + offset;
  2506. curIdx++;
  2507. pIdx[curIdx] = nextSegOffset*i + 2*j + nextSegOffset + offset;
  2508. curIdx++;
  2509. }
  2510. }
  2511. }
  2512. AssertFatal( curIdx == mTriangleCount[Bottom] * 3, "MeshRoad, wrote incorrect number of indices in mPB[Bottom]!" );
  2513. mPB[Bottom].unlock();
  2514. // Side Primitive Buffer
  2515. mPB[Side].set( GFX, mTriangleCount[Side] * 3, mTriangleCount[Side], GFXBufferTypeStatic );
  2516. mPB[Side].lock(&pIdx);
  2517. curIdx = 0;
  2518. offset = 4 * numProfSide;
  2519. if(numProfSide)
  2520. {
  2521. for ( U32 i = 0; i < mSegments.size(); i++ )
  2522. {
  2523. // Loop through profile segments on right side
  2524. for( U32 j = 0; j < numProfSide; j++)
  2525. {
  2526. // Profile Segment Face 1
  2527. pIdx[curIdx] = offset*i + 2*j;
  2528. curIdx++;
  2529. pIdx[curIdx] = offset*i + 2*j + offset + 1;
  2530. curIdx++;
  2531. pIdx[curIdx] = offset*i + 2*j + 1;
  2532. curIdx++;
  2533. // Profile Segment Face 2
  2534. pIdx[curIdx] = offset*i + 2*j;
  2535. curIdx++;
  2536. pIdx[curIdx] = offset*i + 2*j + offset;
  2537. curIdx++;
  2538. pIdx[curIdx] = offset*i + 2*j + offset + 1;
  2539. curIdx++;
  2540. }
  2541. // Loop through profile segments on left side
  2542. for( U32 j = numProfSide; j < 2*numProfSide; j++)
  2543. {
  2544. // Profile Segment Face 1
  2545. pIdx[curIdx] = offset*i + 2*j;
  2546. curIdx++;
  2547. pIdx[curIdx] = offset*i + 2*j + 1;
  2548. curIdx++;
  2549. pIdx[curIdx] = offset*i + 2*j + offset + 1;
  2550. curIdx++;
  2551. // Profile Segment Face 2
  2552. pIdx[curIdx] = offset*i + 2*j;
  2553. curIdx++;
  2554. pIdx[curIdx] = offset*i + 2*j + offset + 1;
  2555. curIdx++;
  2556. pIdx[curIdx] = offset*i + 2*j + offset;
  2557. curIdx++;
  2558. }
  2559. }
  2560. }
  2561. // Cap the front
  2562. offset = sliceCount * numProfSide * 4;
  2563. for ( U32 i = 0; i < mSideProfile.mCap.getNumTris(); i++ )
  2564. {
  2565. pIdx[curIdx] = mSideProfile.mCap.getTriIdx(i, 0) + offset;
  2566. curIdx++;
  2567. pIdx[curIdx] = mSideProfile.mCap.getTriIdx(i, 1) + offset;
  2568. curIdx++;
  2569. pIdx[curIdx] = mSideProfile.mCap.getTriIdx(i, 2) + offset;
  2570. curIdx++;
  2571. }
  2572. // Cap the back
  2573. offset += mSideProfile.mNodes.size() * 2;
  2574. for ( U32 i = 0; i < mSideProfile.mCap.getNumTris(); i++ )
  2575. {
  2576. pIdx[curIdx] = mSideProfile.mCap.getTriIdx(i, 2) + offset;
  2577. curIdx++;
  2578. pIdx[curIdx] = mSideProfile.mCap.getTriIdx(i, 1) + offset;
  2579. curIdx++;
  2580. pIdx[curIdx] = mSideProfile.mCap.getTriIdx(i, 0) + offset;
  2581. curIdx++;
  2582. }
  2583. AssertFatal( curIdx == mTriangleCount[Side] * 3, "MeshRoad, wrote incorrect number of indices in mPB[Side]!" );
  2584. mPB[Side].unlock();
  2585. }
  2586. const MeshRoadNode& MeshRoad::getNode( U32 idx )
  2587. {
  2588. return mNodes[idx];
  2589. }
  2590. VectorF MeshRoad::getNodeNormal( U32 idx )
  2591. {
  2592. if ( mNodes.size() - 1 < idx )
  2593. return VectorF::Zero;
  2594. return mNodes[idx].normal;
  2595. }
  2596. void MeshRoad::setNodeNormal( U32 idx, const VectorF &normal )
  2597. {
  2598. if ( mNodes.size() - 1 < idx )
  2599. return;
  2600. mNodes[idx].normal = normal;
  2601. regenerate();
  2602. setMaskBits( NodeMask | RegenMask );
  2603. }
  2604. Point3F MeshRoad::getNodePosition( U32 idx )
  2605. {
  2606. if ( mNodes.size() - 1 < idx )
  2607. return Point3F::Zero;
  2608. return mNodes[idx].point;
  2609. }
  2610. void MeshRoad::setNodePosition( U32 idx, const Point3F &pos )
  2611. {
  2612. if ( mNodes.size() - 1 < idx )
  2613. return;
  2614. mNodes[idx].point = pos;
  2615. regenerate();
  2616. setMaskBits( NodeMask | RegenMask );
  2617. }
  2618. U32 MeshRoad::addNode( const Point3F &pos, const F32 &width, const F32 &depth, const VectorF &normal )
  2619. {
  2620. U32 idx = _addNode( pos, width, depth, normal );
  2621. regenerate();
  2622. setMaskBits( NodeMask | RegenMask );
  2623. return idx;
  2624. }
  2625. void MeshRoad::buildNodesFromList( MeshRoadNodeList* list )
  2626. {
  2627. mNodes.clear();
  2628. for (U32 i=0; i<list->mPositions.size(); ++i)
  2629. {
  2630. _addNode( list->mPositions[i], list->mWidths[i], list->mDepths[i], list->mNormals[i] );
  2631. }
  2632. _regenerate();
  2633. }
  2634. U32 MeshRoad::insertNode( const Point3F &pos, const F32 &width, const F32 &depth, const VectorF &normal, const U32 &idx )
  2635. {
  2636. U32 ret = _insertNode( pos, width, depth, normal, idx );
  2637. regenerate();
  2638. setMaskBits( NodeMask | RegenMask );
  2639. return ret;
  2640. }
  2641. void MeshRoad::setNode( const Point3F &pos, const F32 &width, const F32 &depth, const VectorF &normal, const U32 &idx )
  2642. {
  2643. if ( mNodes.size() - 1 < idx )
  2644. return;
  2645. MeshRoadNode &node = mNodes[idx];
  2646. node.point = pos;
  2647. node.width = width;
  2648. node.depth = depth;
  2649. node.normal = normal;
  2650. regenerate();
  2651. setMaskBits( NodeMask | RegenMask );
  2652. }
  2653. void MeshRoad::setNodeWidth( U32 idx, F32 meters )
  2654. {
  2655. meters = mClampF( meters, MIN_NODE_WIDTH, MAX_NODE_WIDTH );
  2656. if ( mNodes.size() - 1 < idx )
  2657. return;
  2658. mNodes[idx].width = meters;
  2659. _regenerate();
  2660. setMaskBits( RegenMask | NodeMask );
  2661. }
  2662. F32 MeshRoad::getNodeWidth( U32 idx )
  2663. {
  2664. if ( mNodes.size() - 1 < idx )
  2665. return -1.0f;
  2666. return mNodes[idx].width;
  2667. }
  2668. void MeshRoad::setNodeDepth( U32 idx, F32 meters )
  2669. {
  2670. meters = mClampF( meters, MIN_NODE_DEPTH, MAX_NODE_DEPTH );
  2671. if ( mNodes.size() - 1 < idx )
  2672. return;
  2673. mNodes[idx].depth = meters;
  2674. _regenerate();
  2675. setMaskBits( MeshRoadMask | RegenMask | NodeMask );
  2676. }
  2677. F32 MeshRoad::getNodeDepth( U32 idx )
  2678. {
  2679. if ( mNodes.size() - 1 < idx )
  2680. return -1.0f;
  2681. return mNodes[idx].depth;
  2682. }
  2683. MatrixF MeshRoad::getNodeTransform( U32 idx )
  2684. {
  2685. MatrixF mat(true);
  2686. if ( mNodes.size() - 1 < idx )
  2687. return mat;
  2688. bool hasNext = idx + 1 < mNodes.size();
  2689. bool hasPrev = (S32)idx - 1 > 0;
  2690. const MeshRoadNode &node = mNodes[idx];
  2691. VectorF fvec( 0, 1, 0 );
  2692. if ( hasNext )
  2693. {
  2694. fvec = mNodes[idx+1].point - node.point;
  2695. fvec.normalizeSafe();
  2696. }
  2697. else if ( hasPrev )
  2698. {
  2699. fvec = node.point - mNodes[idx-1].point;
  2700. fvec.normalizeSafe();
  2701. }
  2702. else
  2703. fvec = mPerp( node.normal );
  2704. if ( fvec.isZero() )
  2705. fvec = mPerp( node.normal );
  2706. F32 dot = mDot( fvec, node.normal );
  2707. if ( dot < -0.9f || dot > 0.9f )
  2708. fvec = mPerp( node.normal );
  2709. VectorF rvec = mCross( fvec, node.normal );
  2710. if ( rvec.isZero() )
  2711. rvec = mPerp( fvec );
  2712. rvec.normalize();
  2713. fvec = mCross( node.normal, rvec );
  2714. fvec.normalize();
  2715. mat.setColumn( 0, rvec );
  2716. mat.setColumn( 1, fvec );
  2717. mat.setColumn( 2, node.normal );
  2718. mat.setColumn( 3, node.point );
  2719. AssertFatal( m_matF_determinant( mat ) != 0.0f, "no inverse!");
  2720. return mat;
  2721. }
  2722. void MeshRoad::calcSliceTransform( U32 idx, MatrixF &mat )
  2723. {
  2724. if ( mSlices.size() - 1 < idx )
  2725. return;
  2726. bool hasNext = idx + 1 < mSlices.size();
  2727. bool hasPrev = (S32)idx - 1 >= 0;
  2728. const MeshRoadSlice &slice = mSlices[idx];
  2729. VectorF fvec( 0, 1, 0 );
  2730. if ( hasNext )
  2731. {
  2732. fvec = mSlices[idx+1].p1 - slice.p1;
  2733. fvec.normalizeSafe();
  2734. }
  2735. else if ( hasPrev )
  2736. {
  2737. fvec = slice.p1 - mSlices[idx-1].p1;
  2738. fvec.normalizeSafe();
  2739. }
  2740. else
  2741. fvec = mPerp( slice.normal );
  2742. if ( fvec.isZero() )
  2743. fvec = mPerp( slice.normal );
  2744. F32 dot = mDot( fvec, slice.normal );
  2745. if ( dot < -0.9f || dot > 0.9f )
  2746. fvec = mPerp( slice.normal );
  2747. VectorF rvec = mCross( fvec, slice.normal );
  2748. if ( rvec.isZero() )
  2749. rvec = mPerp( fvec );
  2750. rvec.normalize();
  2751. fvec = mCross( slice.normal, rvec );
  2752. fvec.normalize();
  2753. mat.setColumn( 0, rvec );
  2754. mat.setColumn( 1, fvec );
  2755. mat.setColumn( 2, slice.normal );
  2756. mat.setColumn( 3, slice.p1 );
  2757. AssertFatal( m_matF_determinant( mat ) != 0.0f, "no inverse!");
  2758. }
  2759. F32 MeshRoad::getRoadLength() const
  2760. {
  2761. F32 length = 0.0f;
  2762. for ( U32 i = 0; i < mSegments.size(); i++ )
  2763. {
  2764. length += mSegments[i].length();
  2765. }
  2766. return length;
  2767. }
  2768. void MeshRoad::deleteNode( U32 idx )
  2769. {
  2770. if ( mNodes.size() - 1 < idx )
  2771. return;
  2772. mNodes.erase(idx);
  2773. _regenerate();
  2774. setMaskBits( RegenMask | NodeMask );
  2775. }
  2776. U32 MeshRoad::_addNode( const Point3F &pos, const F32 &width, const F32 &depth, const VectorF &normal )
  2777. {
  2778. mNodes.increment();
  2779. MeshRoadNode &node = mNodes.last();
  2780. node.point = pos;
  2781. node.width = width;
  2782. node.depth = depth;
  2783. node.normal = normal;
  2784. setMaskBits( NodeMask | RegenMask );
  2785. return mNodes.size() - 1;
  2786. }
  2787. U32 MeshRoad::_insertNode( const Point3F &pos, const F32 &width, const F32 &depth, const VectorF &normal, const U32 &idx )
  2788. {
  2789. U32 ret;
  2790. MeshRoadNode *node;
  2791. if ( idx == U32_MAX )
  2792. {
  2793. mNodes.increment();
  2794. node = &mNodes.last();
  2795. ret = mNodes.size() - 1;
  2796. }
  2797. else
  2798. {
  2799. mNodes.insert( idx );
  2800. node = &mNodes[idx];
  2801. ret = idx;
  2802. }
  2803. node->point = pos;
  2804. node->depth = depth;
  2805. node->width = width;
  2806. node->normal = normal;
  2807. return ret;
  2808. }
  2809. bool MeshRoad::collideRay( const Point3F &origin, const Point3F &direction, U32 *nodeIdx, Point3F *collisionPnt )
  2810. {
  2811. Point3F p0 = origin;
  2812. Point3F p1 = origin + direction * 2000.0f;
  2813. // If the line segment does not collide with the MeshRoad's world box,
  2814. // it definitely does not collide with any part of the river.
  2815. if ( !getWorldBox().collideLine( p0, p1 ) )
  2816. return false;
  2817. if ( mSlices.size() < 2 )
  2818. return false;
  2819. MathUtils::Quad quad;
  2820. MathUtils::Ray ray;
  2821. F32 t;
  2822. // Check each road segment (formed by a pair of slices) for collision
  2823. // with the line segment.
  2824. for ( U32 i = 0; i < mSlices.size() - 1; i++ )
  2825. {
  2826. const MeshRoadSlice &slice0 = mSlices[i];
  2827. const MeshRoadSlice &slice1 = mSlices[i+1];
  2828. // For simplicities sake we will only test for collision between the
  2829. // line segment and the Top face of the river segment.
  2830. // Clockwise starting with the leftmost/closest point.
  2831. quad.p00 = slice0.p0;
  2832. quad.p01 = slice1.p0;
  2833. quad.p11 = slice1.p2;
  2834. quad.p10 = slice0.p2;
  2835. ray.origin = origin;
  2836. ray.direction = direction;
  2837. if ( MathUtils::mRayQuadCollide( quad, ray, NULL, &t ) )
  2838. {
  2839. if ( nodeIdx )
  2840. *nodeIdx = slice0.parentNodeIdx;
  2841. if ( collisionPnt )
  2842. *collisionPnt = ray.origin + ray.direction * t;
  2843. return true;
  2844. }
  2845. }
  2846. return false;
  2847. }
  2848. void MeshRoad::regenerate()
  2849. {
  2850. _regenerate();
  2851. setMaskBits( RegenMask );
  2852. }
  2853. //-------------------------------------------------------------------------
  2854. // Console Methods
  2855. //-------------------------------------------------------------------------
  2856. DefineEngineMethod( MeshRoad, setNodeDepth, void, ( S32 idx, F32 meters ),,
  2857. "Intended as a helper to developers and editor scripts.\n"
  2858. "Sets the depth in meters of a particular node."
  2859. )
  2860. {
  2861. object->setNodeDepth( idx, meters );
  2862. }
  2863. DefineEngineMethod( MeshRoad, regenerate, void, (),,
  2864. "Intended as a helper to developers and editor scripts.\n"
  2865. "Force MeshRoad to recreate its geometry."
  2866. )
  2867. {
  2868. object->regenerate();
  2869. }
  2870. DefineEngineMethod( MeshRoad, postApply, void, (),,
  2871. "Intended as a helper to developers and editor scripts.\n"
  2872. "Force trigger an inspectPostApply. This will transmit "
  2873. "material and other fields ( not including nodes ) to client objects."
  2874. )
  2875. {
  2876. object->inspectPostApply();
  2877. }
  2878. bool MeshRoad::buildPolyList_TopSurfaceOnly = false;
  2879. bool MeshRoad::buildTopPolyList(PolyListContext plc, AbstractPolyList* polyList)
  2880. {
  2881. static Box3F box_prox; static SphereF ball_prox;
  2882. buildPolyList_TopSurfaceOnly = true;
  2883. bool result = buildPolyList(plc, polyList, box_prox, ball_prox);
  2884. buildPolyList_TopSurfaceOnly = false;
  2885. return result;
  2886. }