meshRoad.cpp 100 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621
  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. void pack(NetConnection*, BitStream*) override;
  106. void unpack(NetConnection*, BitStream*) override;
  107. void copyIntoList(NodeListManager::NodeList* copyInto) override;
  108. void padListToSize() override;
  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. void sendNotification( NodeListManager::NodeList* list ) override;
  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. INIT_ASSET(TopMaterial);
  754. INIT_ASSET(BottomMaterial);
  755. INIT_ASSET(SideMaterial);
  756. mSideProfile.mRoad = this;
  757. }
  758. MeshRoad::~MeshRoad()
  759. {
  760. delete mConvexList;
  761. mConvexList = NULL;
  762. }
  763. FRangeValidator mrTextureLengthV(0.1f, FLT_MAX);
  764. void MeshRoad::initPersistFields()
  765. {
  766. docsURL;
  767. addGroup( "MeshRoad" );
  768. INITPERSISTFIELD_MATERIALASSET(TopMaterial, MeshRoad, "Material for the upper surface of the road.");
  769. INITPERSISTFIELD_MATERIALASSET(BottomMaterial, MeshRoad, "Material for the bottom surface of the road.");
  770. INITPERSISTFIELD_MATERIALASSET(SideMaterial, MeshRoad, "Material for the side surface of the road.");
  771. addFieldV( "textureLength", TypeRangedF32, Offset( mTextureLength, MeshRoad ), &mrTextureLengthV,
  772. "The length in meters of textures mapped to the MeshRoad." );
  773. addFieldV( "breakAngle", TypeRangedF32, Offset( mBreakAngle, MeshRoad ), &CommonValidators::PosDegreeRange,
  774. "Angle in degrees - MeshRoad will subdivide the spline if its curve is greater than this threshold." );
  775. addFieldV( "widthSubdivisions", TypeRangedS32, Offset( mWidthSubdivisions, MeshRoad ), &CommonValidators::PositiveInt,
  776. "Subdivide segments widthwise this many times when generating vertices." );
  777. endGroup( "MeshRoad" );
  778. addGroup( "Internal" );
  779. addProtectedField( "Node", TypeString, 0, &addNodeFromField, &emptyStringProtectedGetFn,
  780. "Do not modify, for internal use.", AbstractClassRep::FIELD_HideInInspectors | AbstractClassRep::FIELD_SpecialtyArrayField);
  781. addProtectedField( "ProfileNode", TypeString, 0, &addProfileNodeFromField, &emptyStringProtectedGetFn,
  782. "Do not modify, for internal use.", AbstractClassRep::FIELD_HideInInspectors | AbstractClassRep::FIELD_SpecialtyArrayField);
  783. endGroup( "Internal" );
  784. Parent::initPersistFields();
  785. }
  786. void MeshRoad::consoleInit()
  787. {
  788. Parent::consoleInit();
  789. Con::addVariable( "$MeshRoad::EditorOpen", TypeBool, &MeshRoad::smEditorOpen, "True if the MeshRoad editor is open, otherwise false.\n"
  790. "@ingroup Editors\n");
  791. Con::addVariable( "$MeshRoad::wireframe", TypeBool, &MeshRoad::smWireframe, "If true, will render the wireframe of the road.\n"
  792. "@ingroup Editors\n");
  793. Con::addVariable( "$MeshRoad::showBatches", TypeBool, &MeshRoad::smShowBatches, "Determines if the debug rendering of the batches cubes is displayed or not.\n"
  794. "@ingroup Editors\n");
  795. Con::addVariable( "$MeshRoad::showSpline", TypeBool, &MeshRoad::smShowSpline, "If true, the spline on which the curvature of this road is based will be rendered.\n"
  796. "@ingroup Editors\n");
  797. 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"
  798. "@ingroup Editors\n");
  799. Con::addVariable( "$MeshRoad::showRoadProfile", TypeBool, &MeshRoad::smShowRoadProfile, "If true, the road profile will be shown in the editor.\n"
  800. "@ingroup Editors\n");
  801. }
  802. bool MeshRoad::addNodeFromField( void *object, const char *index, const char *data )
  803. {
  804. MeshRoad *pObj = static_cast<MeshRoad*>(object);
  805. //if ( !pObj->isProperlyAdded() )
  806. //{
  807. F32 width, depth;
  808. Point3F pos, normal;
  809. 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 );
  810. if ( result == 8 )
  811. pObj->_addNode( pos, width, depth, normal );
  812. //}
  813. return false;
  814. }
  815. bool MeshRoad::addProfileNodeFromField( void* obj, const char *index, const char* data )
  816. {
  817. MeshRoad *pObj = static_cast<MeshRoad*>(obj);
  818. F32 x, y;
  819. U32 smooth, mtrl;
  820. U32 result = dSscanf( data, "%g %g %d %d", &x, &y, &smooth, &mtrl );
  821. if ( result == 4 )
  822. {
  823. if(!pObj->mSideProfile.mNodes.empty())
  824. pObj->mSideProfile.mSegMtrls.push_back(mtrl);
  825. MeshRoadProfileNode node;
  826. node.setPosition(x, y);
  827. node.setSmoothing(smooth != 0);
  828. pObj->mSideProfile.mNodes.push_back(node);
  829. }
  830. return false;
  831. }
  832. bool MeshRoad::onAdd()
  833. {
  834. if ( !Parent::onAdd() )
  835. return false;
  836. // Reset the World Box.
  837. //setGlobalBounds();
  838. resetWorldBox();
  839. // Set the Render Transform.
  840. setRenderTransform(mObjToWorld);
  841. // Add to ServerMeshRoadSet
  842. if ( isServerObject() )
  843. {
  844. getServerSet()->addObject( this );
  845. }
  846. if ( isClientObject() )
  847. _initMaterial();
  848. // If this road was not created from a file, give profile two default nodes
  849. if(mSideProfile.mNodes.empty())
  850. {
  851. // Initialize with two nodes in vertical line with unit length
  852. MeshRoadProfileNode node1(Point3F(0.0f, 0.0f, 0.0f));
  853. MeshRoadProfileNode node2(Point3F(0.0f, -5.0f, 0.0f));
  854. mSideProfile.mNodes.push_back(node1);
  855. mSideProfile.mNodes.push_back(node2);
  856. // Both node normals are straight to the right, perpendicular to the profile line
  857. VectorF norm(1.0f, 0.0f, 0.0f);
  858. mSideProfile.mNodeNormals.push_back(norm);
  859. mSideProfile.mNodeNormals.push_back(norm);
  860. mSideProfile.mSegMtrls.push_back(MeshRoad::Side);
  861. }
  862. else
  863. mSideProfile.generateNormals();
  864. // Generate the Vert/Index buffers and everything else.
  865. _regenerate();
  866. // Add to Scene.
  867. addToScene();
  868. return true;
  869. }
  870. void MeshRoad::onRemove()
  871. {
  872. SAFE_DELETE( mPhysicsRep );
  873. mConvexList->nukeList();
  874. for ( U32 i = 0; i < SurfaceCount; i++ )
  875. {
  876. SAFE_DELETE( mMatInst[i] );
  877. }
  878. removeFromScene();
  879. Parent::onRemove();
  880. }
  881. void MeshRoad::inspectPostApply()
  882. {
  883. // Set Parent.
  884. Parent::inspectPostApply();
  885. //if ( mMetersPerSegment < MIN_METERS_PER_SEGMENT )
  886. // mMetersPerSegment = MIN_METERS_PER_SEGMENT;
  887. setMaskBits(MeshRoadMask);
  888. }
  889. void MeshRoad::onStaticModified( const char* slotName, const char*newValue )
  890. {
  891. Parent::onStaticModified( slotName, newValue );
  892. if ( dStricmp( slotName, "breakAngle" ) == 0 )
  893. {
  894. setMaskBits( RegenMask );
  895. }
  896. }
  897. void MeshRoad::writeFields( Stream &stream, U32 tabStop )
  898. {
  899. Parent::writeFields( stream, tabStop );
  900. // Now write all nodes
  901. stream.write(2, "\r\n");
  902. for ( U32 i = 0; i < mNodes.size(); i++ )
  903. {
  904. const MeshRoadNode &node = mNodes[i];
  905. stream.writeTabs(tabStop);
  906. char buffer[1024];
  907. dMemset( buffer, 0, 1024 );
  908. 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 );
  909. stream.writeLine( (const U8*)buffer );
  910. }
  911. stream.write(2, "\r\n");
  912. Point3F nodePos;
  913. U8 smooth, mtrl;
  914. for ( U32 i = 0; i < mSideProfile.mNodes.size(); i++ )
  915. {
  916. nodePos = mSideProfile.mNodes[i].getPosition();
  917. if(i)
  918. mtrl = mSideProfile.mSegMtrls[i-1];
  919. else
  920. mtrl = 0;
  921. if(mSideProfile.mNodes[i].isSmooth())
  922. smooth = 1;
  923. else
  924. smooth = 0;
  925. stream.writeTabs(tabStop);
  926. char buffer[1024];
  927. dMemset( buffer, 0, 1024 );
  928. dSprintf( buffer, 1024, "ProfileNode = \"%.6f %.6f %d %d\";", nodePos.x, nodePos.y, smooth, mtrl);
  929. stream.writeLine( (const U8*)buffer );
  930. }
  931. }
  932. bool MeshRoad::writeField( StringTableEntry fieldname, const char *value )
  933. {
  934. if ( fieldname == StringTable->insert("Node") )
  935. return false;
  936. if ( fieldname == StringTable->insert("ProfileNode") )
  937. return false;
  938. return Parent::writeField( fieldname, value );
  939. }
  940. U32 MeshRoad::getSpecialFieldSize(StringTableEntry fieldName)
  941. {
  942. if (fieldName == StringTable->insert("Node"))
  943. {
  944. return mNodes.size();
  945. }
  946. else if (fieldName == StringTable->insert("ProfileNode"))
  947. {
  948. return mSideProfile.mNodes.size();
  949. }
  950. return 0;
  951. }
  952. const char* MeshRoad::getSpecialFieldOut(StringTableEntry fieldName, const U32& index)
  953. {
  954. if (fieldName == StringTable->insert("Node"))
  955. {
  956. if (index >= mNodes.size())
  957. return NULL;
  958. const MeshRoadNode& node = mNodes[index];
  959. char buffer[1024];
  960. dMemset(buffer, 0, 1024);
  961. 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);
  962. return StringTable->insert(buffer);
  963. }
  964. else if (fieldName == StringTable->insert("ProfileNode") && mSideProfile.mNodes.size())
  965. {
  966. Point3F nodePos = mSideProfile.mNodes[index].getPosition();
  967. U8 smooth, mtrl;
  968. if (index)
  969. mtrl = mSideProfile.mSegMtrls[index - 1];
  970. else
  971. mtrl = 0;
  972. if (mSideProfile.mNodes[index].isSmooth())
  973. smooth = 1;
  974. else
  975. smooth = 0;
  976. char buffer[1024];
  977. dMemset(buffer, 0, 1024);
  978. dSprintf(buffer, 1024, "ProfileNode = \"%.6f %.6f %d %d\";", nodePos.x, nodePos.y, smooth, mtrl);
  979. return StringTable->insert(buffer);
  980. }
  981. return NULL;
  982. }
  983. void MeshRoad::onEditorEnable()
  984. {
  985. }
  986. void MeshRoad::onEditorDisable()
  987. {
  988. }
  989. SimSet* MeshRoad::getServerSet()
  990. {
  991. if ( !smServerMeshRoadSet )
  992. {
  993. smServerMeshRoadSet = new SimSet();
  994. smServerMeshRoadSet->registerObject( "ServerMeshRoadSet" );
  995. Sim::getRootGroup()->addObject( smServerMeshRoadSet );
  996. }
  997. return smServerMeshRoadSet;
  998. }
  999. void MeshRoad::prepRenderImage( SceneRenderState* state )
  1000. {
  1001. if ( mNodes.size() <= 1 )
  1002. return;
  1003. RenderPassManager *renderPass = state->getRenderPass();
  1004. // Normal Road RenderInstance
  1005. // Always rendered when the editor is not open
  1006. // otherwise obey the smShowRoad flag
  1007. if ( smShowRoad || !smEditorOpen )
  1008. {
  1009. #ifdef TORQUE_AFX_ENABLED
  1010. afxZodiacMgr::renderMeshRoadZodiacs(state, this);
  1011. #endif
  1012. MeshRenderInst coreRI;
  1013. coreRI.clear();
  1014. coreRI.objectToWorld = &MatrixF::Identity;
  1015. coreRI.worldToCamera = renderPass->allocSharedXform(RenderPassManager::View);
  1016. coreRI.projection = renderPass->allocSharedXform(RenderPassManager::Projection);
  1017. coreRI.type = RenderPassManager::RIT_Mesh;
  1018. BaseMatInstance *matInst;
  1019. for ( U32 i = 0; i < SurfaceCount; i++ )
  1020. {
  1021. matInst = state->getOverrideMaterial( mMatInst[i] );
  1022. if ( !matInst )
  1023. continue;
  1024. // Get the lights if we haven't already.
  1025. if ( matInst->isForwardLit() && !coreRI.lights[0] )
  1026. {
  1027. LightQuery query;
  1028. query.init( getWorldSphere() );
  1029. query.getLights( coreRI.lights, 8 );
  1030. }
  1031. MeshRenderInst *ri = renderPass->allocInst<MeshRenderInst>();
  1032. *ri = coreRI;
  1033. // Currently rendering whole road, fix to cull and batch
  1034. // per segment.
  1035. // Set the correct material for rendering.
  1036. ri->matInst = matInst;
  1037. ri->vertBuff = &mVB[i];
  1038. ri->primBuff = &mPB[i];
  1039. ri->prim = renderPass->allocPrim();
  1040. ri->prim->type = GFXTriangleList;
  1041. ri->prim->minIndex = 0;
  1042. ri->prim->startIndex = 0;
  1043. ri->prim->numPrimitives = mTriangleCount[i];
  1044. ri->prim->startVertex = 0;
  1045. ri->prim->numVertices = mVertCount[i];
  1046. // We sort by the material then vertex buffer.
  1047. ri->defaultKey = matInst->getStateHint();
  1048. ri->defaultKey2 = (uintptr_t)ri->vertBuff; // Not 64bit safe!
  1049. renderPass->addInst( ri );
  1050. }
  1051. }
  1052. // Debug RenderInstance
  1053. // Only when editor is open.
  1054. if ( smEditorOpen )
  1055. {
  1056. ObjectRenderInst *ri = state->getRenderPass()->allocInst<ObjectRenderInst>();
  1057. ri->renderDelegate.bind( this, &MeshRoad::_debugRender );
  1058. ri->type = RenderPassManager::RIT_Editor;
  1059. state->getRenderPass()->addInst( ri );
  1060. }
  1061. }
  1062. void MeshRoad::_initMaterial()
  1063. {
  1064. if (mTopMaterialAsset.notNull())
  1065. {
  1066. if (!mMatInst[Top] || !String(mTopMaterialAsset->getMaterialDefinitionName()).equal(mMatInst[Top]->getMaterial()->getName(), String::NoCase))
  1067. {
  1068. SAFE_DELETE(mMatInst[Top]);
  1069. Material* tMat = nullptr;
  1070. if (!Sim::findObject(mTopMaterialAsset->getMaterialDefinitionName(), tMat))
  1071. Con::errorf("MeshRoad::_initMaterial - Material %s was not found.", mTopMaterialAsset->getMaterialDefinitionName());
  1072. mMaterial[Top] = tMat;
  1073. if (mMaterial[Top])
  1074. mMatInst[Top] = mMaterial[Top]->createMatInstance();
  1075. else
  1076. mMatInst[Top] = MATMGR->createMatInstance("WarningMaterial");
  1077. mMatInst[Top]->init(MATMGR->getDefaultFeatures(), getGFXVertexFormat<GFXVertexPNTT>());
  1078. }
  1079. }
  1080. if (mBottomMaterialAsset.notNull())
  1081. {
  1082. if (!mMatInst[Bottom] || !String(mBottomMaterialAsset->getMaterialDefinitionName()).equal(mMatInst[Bottom]->getMaterial()->getName(), String::NoCase))
  1083. {
  1084. SAFE_DELETE(mMatInst[Bottom]);
  1085. Material* tMat = nullptr;
  1086. if (!Sim::findObject(mBottomMaterialAsset->getMaterialDefinitionName(), tMat))
  1087. Con::errorf("MeshRoad::_initMaterial - Material %s was not found.", mBottomMaterialAsset->getMaterialDefinitionName());
  1088. mMaterial[Bottom] = tMat;
  1089. if (mMaterial[Bottom])
  1090. mMatInst[Bottom] = mMaterial[Bottom]->createMatInstance();
  1091. else
  1092. mMatInst[Bottom] = MATMGR->createMatInstance("WarningMaterial");
  1093. mMatInst[Bottom]->init(MATMGR->getDefaultFeatures(), getGFXVertexFormat<GFXVertexPNTT>());
  1094. }
  1095. }
  1096. if (mSideMaterialAsset.notNull())
  1097. {
  1098. if (!mMatInst[Side] || !String(mSideMaterialAsset->getMaterialDefinitionName()).equal(mMatInst[Side]->getMaterial()->getName(), String::NoCase))
  1099. {
  1100. SAFE_DELETE(mMatInst[Side]);
  1101. Material* tMat = nullptr;
  1102. if (!Sim::findObject(mSideMaterialAsset->getMaterialDefinitionName(), tMat))
  1103. Con::errorf("MeshRoad::_initMaterial - Material %s was not found.", mSideMaterialAsset->getMaterialDefinitionName());
  1104. mMaterial[Side] = tMat;
  1105. if (mMaterial[Side])
  1106. mMatInst[Side] = mMaterial[Side]->createMatInstance();
  1107. else
  1108. mMatInst[Side] = MATMGR->createMatInstance("WarningMaterial");
  1109. mMatInst[Side]->init(MATMGR->getDefaultFeatures(), getGFXVertexFormat<GFXVertexPNTT>());
  1110. }
  1111. }
  1112. }
  1113. void MeshRoad::_debugRender( ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance* )
  1114. {
  1115. //MeshRoadConvex convex;
  1116. //buildConvex( Box3F(true), convex );
  1117. //convex.render();
  1118. //GFXDrawUtil *drawer = GFX->getDrawUtil();
  1119. //GFX->setStateBlock( smStateBlock );
  1120. return;
  1121. /*
  1122. U32 convexCount = mDebugConvex.size();
  1123. PrimBuild::begin( GFXTriangleList, convexCount * 12 );
  1124. PrimBuild::color4i( 0, 0, 255, 155 );
  1125. for ( U32 i = 0; i < convexCount; i++ )
  1126. {
  1127. MeshRoadConvex *convex = mDebugConvex[i];
  1128. Point3F a = convex->verts[0];
  1129. Point3F b = convex->verts[1];
  1130. Point3F c = convex->verts[2];
  1131. Point3F p = convex->verts[3];
  1132. //mObjToWorld.mulP(a);
  1133. //mObjToWorld.mulP(b);
  1134. //mObjToWorld.mulP(c);
  1135. //mObjToWorld.mulP(p);
  1136. PrimBuild::vertex3fv( c );
  1137. PrimBuild::vertex3fv( b );
  1138. PrimBuild::vertex3fv( a );
  1139. PrimBuild::vertex3fv( b );
  1140. PrimBuild::vertex3fv( a );
  1141. PrimBuild::vertex3fv( p );
  1142. PrimBuild::vertex3fv( c );
  1143. PrimBuild::vertex3fv( b );
  1144. PrimBuild::vertex3fv( p );
  1145. PrimBuild::vertex3fv( a );
  1146. PrimBuild::vertex3fv( c );
  1147. PrimBuild::vertex3fv( p );
  1148. }
  1149. PrimBuild::end();
  1150. for ( U32 i = 0; i < mSegments.size(); i++ )
  1151. {
  1152. ///GFX->getDrawUtil()->drawWireBox( mSegments[i].worldbounds, ColorI(255,0,0,255) );
  1153. }
  1154. GFX->enterDebugEvent( ColorI( 255, 0, 0 ), "DecalRoad_debugRender" );
  1155. GFXTransformSaver saver;
  1156. GFX->setStateBlock( smStateBlock );
  1157. Point3F size(1,1,1);
  1158. ColorI color( 255, 0, 0, 255 );
  1159. if ( smShowBatches )
  1160. {
  1161. for ( U32 i = 0; i < mBatches.size(); i++ )
  1162. {
  1163. const Box3F &box = mBatches[i].bounds;
  1164. Point3F center;
  1165. box.getCenter( &center );
  1166. GFX->getDrawUtil()->drawWireCube( ( box.maxExtents - box.minExtents ) * 0.5f, center, ColorI(255,100,100,255) );
  1167. }
  1168. }
  1169. GFX->leaveDebugEvent();
  1170. */
  1171. }
  1172. U32 MeshRoad::packUpdate(NetConnection * con, U32 mask, BitStream * stream)
  1173. {
  1174. U32 retMask = Parent::packUpdate(con, mask, stream);
  1175. if ( stream->writeFlag( mask & MeshRoadMask ) )
  1176. {
  1177. // Write Object Transform.
  1178. stream->writeAffineTransform( mObjToWorld );
  1179. // Write Materials
  1180. PACK_ASSET(con, TopMaterial);
  1181. PACK_ASSET(con, BottomMaterial);
  1182. PACK_ASSET(con, SideMaterial);
  1183. stream->write( mTextureLength );
  1184. stream->write( mBreakAngle );
  1185. stream->write( mWidthSubdivisions );
  1186. }
  1187. if ( stream->writeFlag( mask & ProfileMask ) )
  1188. {
  1189. stream->writeInt( mSideProfile.mNodes.size(), 16 );
  1190. for( U32 i = 0; i < mSideProfile.mNodes.size(); i++ )
  1191. {
  1192. mathWrite( *stream, mSideProfile.mNodes[i].getPosition() );
  1193. stream->writeFlag( mSideProfile.mNodes[i].isSmooth() );
  1194. if(i)
  1195. stream->writeInt(mSideProfile.mSegMtrls[i-1], 3);
  1196. else
  1197. stream->writeInt(0, 3);
  1198. }
  1199. }
  1200. if ( stream->writeFlag( mask & NodeMask ) )
  1201. {
  1202. const U32 nodeByteSize = 32; // Based on sending all of a node's parameters
  1203. // Test if we can fit all of our nodes within the current stream.
  1204. // We make sure we leave 100 bytes still free in the stream for whatever
  1205. // may follow us.
  1206. S32 allowedBytes = stream->getWriteByteSize() - 100;
  1207. if ( stream->writeFlag( (nodeByteSize * mNodes.size()) < allowedBytes ) )
  1208. {
  1209. // All nodes should fit, so send them out now.
  1210. stream->writeInt( mNodes.size(), 16 );
  1211. for ( U32 i = 0; i < mNodes.size(); i++ )
  1212. {
  1213. mathWrite( *stream, mNodes[i].point );
  1214. stream->write( mNodes[i].width );
  1215. stream->write( mNodes[i].depth );
  1216. mathWrite( *stream, mNodes[i].normal );
  1217. }
  1218. }
  1219. else
  1220. {
  1221. // There isn't enough space left in the stream for all of the
  1222. // nodes. Batch them up into NetEvents.
  1223. U32 id = gServerNodeListManager->nextListId();
  1224. U32 count = 0;
  1225. U32 index = 0;
  1226. while (count < mNodes.size())
  1227. {
  1228. count += NodeListManager::smMaximumNodesPerEvent;
  1229. if (count > mNodes.size())
  1230. {
  1231. count = mNodes.size();
  1232. }
  1233. MeshRoadNodeEvent* event = new MeshRoadNodeEvent();
  1234. event->mId = id;
  1235. event->mTotalNodes = mNodes.size();
  1236. event->mLocalListStart = index;
  1237. for (; index<count; ++index)
  1238. {
  1239. event->mPositions.push_back( mNodes[index].point );
  1240. event->mWidths.push_back( mNodes[index].width );
  1241. event->mDepths.push_back( mNodes[index].depth );
  1242. event->mNormals.push_back( mNodes[index].normal );
  1243. }
  1244. con->postNetEvent( event );
  1245. }
  1246. stream->write( id );
  1247. }
  1248. }
  1249. stream->writeFlag( mask & RegenMask );
  1250. // Were done ...
  1251. return retMask;
  1252. }
  1253. void MeshRoad::unpackUpdate(NetConnection * con, BitStream * stream)
  1254. {
  1255. // Unpack Parent.
  1256. Parent::unpackUpdate(con, stream);
  1257. // MeshRoadMask
  1258. if(stream->readFlag())
  1259. {
  1260. MatrixF ObjectMatrix;
  1261. stream->readAffineTransform(&ObjectMatrix);
  1262. Parent::setTransform(ObjectMatrix);
  1263. UNPACK_ASSET(con, TopMaterial);
  1264. UNPACK_ASSET(con, BottomMaterial);
  1265. UNPACK_ASSET(con, SideMaterial);
  1266. if ( isProperlyAdded() )
  1267. _initMaterial();
  1268. stream->read( &mTextureLength );
  1269. stream->read( &mBreakAngle );
  1270. stream->read( &mWidthSubdivisions );
  1271. }
  1272. // ProfileMask
  1273. if(stream->readFlag())
  1274. {
  1275. Point3F pos;
  1276. mSideProfile.mNodes.clear();
  1277. mSideProfile.mSegMtrls.clear();
  1278. U32 count = stream->readInt( 16 );
  1279. for( U32 i = 0; i < count; i++)
  1280. {
  1281. mathRead( *stream, &pos );
  1282. MeshRoadProfileNode node(pos);
  1283. node.setSmoothing( stream->readFlag() );
  1284. mSideProfile.mNodes.push_back(node);
  1285. if(i)
  1286. mSideProfile.mSegMtrls.push_back(stream->readInt(3));
  1287. else
  1288. stream->readInt(3);
  1289. }
  1290. mSideProfile.generateNormals();
  1291. }
  1292. // NodeMask
  1293. if ( stream->readFlag() )
  1294. {
  1295. if (stream->readFlag())
  1296. {
  1297. // Nodes have been passed in this update
  1298. U32 count = stream->readInt( 16 );
  1299. mNodes.clear();
  1300. Point3F pos, normal;
  1301. F32 width, depth;
  1302. for ( U32 i = 0; i < count; i++ )
  1303. {
  1304. mathRead( *stream, &pos );
  1305. stream->read( &width );
  1306. stream->read( &depth );
  1307. mathRead( *stream, &normal );
  1308. _addNode( pos, width, depth, normal );
  1309. }
  1310. }
  1311. else
  1312. {
  1313. // Nodes will arrive as events
  1314. U32 id;
  1315. stream->read( &id );
  1316. // Check if the road's nodes made it here before we did.
  1317. NodeListManager::NodeList* list = NULL;
  1318. if ( gClientNodeListManager->findListById( id, &list, true) )
  1319. {
  1320. // Work with the completed list
  1321. MeshRoadNodeList* roadList = dynamic_cast<MeshRoadNodeList*>( list );
  1322. if (roadList)
  1323. buildNodesFromList( roadList );
  1324. delete list;
  1325. }
  1326. else
  1327. {
  1328. // Nodes have not yet arrived, so register our interest in the list
  1329. MeshRoadNodeListNotify* notify = new MeshRoadNodeListNotify( this, id );
  1330. gClientNodeListManager->registerNotification( notify );
  1331. }
  1332. }
  1333. }
  1334. if ( stream->readFlag() && isProperlyAdded() )
  1335. _regenerate();
  1336. }
  1337. void MeshRoad::setTransform( const MatrixF &mat )
  1338. {
  1339. for ( U32 i = 0; i < mNodes.size(); i++ )
  1340. {
  1341. mWorldToObj.mulP( mNodes[i].point );
  1342. mat.mulP( mNodes[i].point );
  1343. }
  1344. Parent::setTransform( mat );
  1345. if ( mPhysicsRep )
  1346. mPhysicsRep->setTransform( mat );
  1347. // Regenerate and update the client
  1348. _regenerate();
  1349. setMaskBits( NodeMask | RegenMask );
  1350. }
  1351. void MeshRoad::setScale( const VectorF &scale )
  1352. {
  1353. // We ignore scale requests from the editor
  1354. // right now.
  1355. //Parent::setScale( scale );
  1356. }
  1357. void MeshRoad::buildConvex(const Box3F& box, Convex* convex)
  1358. {
  1359. if ( mSlices.size() < 2 )
  1360. return;
  1361. mConvexList->collectGarbage();
  1362. mDebugConvex.clear();
  1363. Box3F realBox = box;
  1364. mWorldToObj.mul(realBox);
  1365. realBox.minExtents.convolveInverse(mObjScale);
  1366. realBox.maxExtents.convolveInverse(mObjScale);
  1367. if (realBox.isOverlapped(getObjBox()) == false)
  1368. return;
  1369. U32 segmentCount = mSegments.size();
  1370. U32 numConvexes ;
  1371. U32 halfConvexes;
  1372. U32 nextSegOffset = 2*mSideProfile.mNodes.size();
  1373. U32 leftSideOffset = nextSegOffset/2;
  1374. U32 k2, capIdx1, capIdx2, capIdx3;
  1375. // Create convex(s) for each segment
  1376. for ( U32 i = 0; i < segmentCount; i++ )
  1377. {
  1378. const MeshRoadSegment &segment = mSegments[i];
  1379. // Is this segment overlapped?
  1380. if ( !segment.getWorldBounds().isOverlapped( box ) )
  1381. continue;
  1382. // Each segment has 6 faces
  1383. for ( U32 j = 0; j < 6; j++ )
  1384. {
  1385. // Only first segment has front face
  1386. if ( j == 4 && i != 0 )
  1387. continue;
  1388. // Only last segment has back face
  1389. if ( j == 5 && i != segmentCount-1 )
  1390. continue;
  1391. // The top and bottom sides have 2 convex(s)
  1392. // The left, right, front, and back sides depend on the user-defined profile
  1393. switch(j)
  1394. {
  1395. case 0: numConvexes = 2; break; // Top
  1396. case 1: // Left
  1397. case 2: numConvexes = 2* (mSideProfile.mNodes.size()-1); break; // Right
  1398. case 3: numConvexes = 2; break; // Bottom
  1399. case 4: // Front
  1400. case 5: numConvexes = mSideProfile.mCap.getNumTris(); break; // Back
  1401. default: numConvexes = 0;
  1402. }
  1403. halfConvexes = numConvexes/2;
  1404. for ( U32 k = 0; k < numConvexes; k++ )
  1405. {
  1406. // See if this convex exists in the working set already...
  1407. Convex* cc = 0;
  1408. CollisionWorkingList& wl = convex->getWorkingList();
  1409. for ( CollisionWorkingList* itr = wl.wLink.mNext; itr != &wl; itr = itr->wLink.mNext )
  1410. {
  1411. if ( itr->mConvex->getType() == MeshRoadConvexType )
  1412. {
  1413. MeshRoadConvex *pConvex = static_cast<MeshRoadConvex*>(itr->mConvex);
  1414. if ( pConvex->pRoad == this &&
  1415. pConvex->segmentId == i &&
  1416. pConvex->faceId == j &&
  1417. pConvex->triangleId == k )
  1418. {
  1419. cc = itr->mConvex;
  1420. break;
  1421. }
  1422. }
  1423. }
  1424. if (cc)
  1425. continue;
  1426. Point3F a, b, c;
  1427. // Top or Bottom
  1428. if(j == 0 || j == 3)
  1429. {
  1430. // Get the triangle...
  1431. U32 idx0 = gIdxArray[j][k][0];
  1432. U32 idx1 = gIdxArray[j][k][1];
  1433. U32 idx2 = gIdxArray[j][k][2];
  1434. a = segment[idx0];
  1435. b = segment[idx1];
  1436. c = segment[idx2];
  1437. }
  1438. // Left Side
  1439. else if(j == 1)
  1440. {
  1441. if(k >= halfConvexes)
  1442. {
  1443. k2 = k + leftSideOffset - halfConvexes;
  1444. a = segment.slice1->verts[k2];
  1445. b = segment.slice0->verts[k2];
  1446. c = segment.slice1->verts[k2 + 1];
  1447. }
  1448. else
  1449. {
  1450. k2 = k + leftSideOffset;
  1451. a = segment.slice0->verts[k2];
  1452. b = segment.slice0->verts[k2 + 1];
  1453. c = segment.slice1->verts[k2 + 1];
  1454. }
  1455. }
  1456. // Right Side
  1457. else if(j == 2)
  1458. {
  1459. // a.set(2*k, 2*k, 0.0f);
  1460. // b.set(2*k, 2*k, 2.0f);
  1461. // c.set(2*(k+1), 2*(k+1), 0.0f);
  1462. if(k >= halfConvexes)
  1463. {
  1464. k2 = k - halfConvexes;
  1465. a = segment.slice1->verts[k2];
  1466. b = segment.slice1->verts[k2 + 1];
  1467. c = segment.slice0->verts[k2];
  1468. }
  1469. else
  1470. {
  1471. a = segment.slice0->verts[k];
  1472. b = segment.slice1->verts[k + 1];
  1473. c = segment.slice0->verts[k + 1];
  1474. }
  1475. }
  1476. // Front
  1477. else if(j == 4)
  1478. {
  1479. k2 = nextSegOffset + leftSideOffset - 1;
  1480. capIdx1 = mSideProfile.mCap.getTriIdx(k, 0);
  1481. capIdx2 = mSideProfile.mCap.getTriIdx(k, 1);
  1482. capIdx3 = mSideProfile.mCap.getTriIdx(k, 2);
  1483. if(capIdx1 >= leftSideOffset)
  1484. capIdx1 = k2 - capIdx1;
  1485. if(capIdx2 >= leftSideOffset)
  1486. capIdx2 = k2 - capIdx2;
  1487. if(capIdx3 >= leftSideOffset)
  1488. capIdx3 = k2 - capIdx3;
  1489. a = segment.slice0->verts[capIdx1];
  1490. b = segment.slice0->verts[capIdx2];
  1491. c = segment.slice0->verts[capIdx3];
  1492. }
  1493. // Back
  1494. else
  1495. {
  1496. k2 = nextSegOffset + leftSideOffset - 1;
  1497. capIdx1 = mSideProfile.mCap.getTriIdx(k, 0);
  1498. capIdx2 = mSideProfile.mCap.getTriIdx(k, 1);
  1499. capIdx3 = mSideProfile.mCap.getTriIdx(k, 2);
  1500. if(capIdx1 >= leftSideOffset)
  1501. capIdx1 = k2 - capIdx1;
  1502. if(capIdx2 >= leftSideOffset)
  1503. capIdx2 = k2 - capIdx2;
  1504. if(capIdx3 >= leftSideOffset)
  1505. capIdx3 = k2 - capIdx3;
  1506. a = segment.slice1->verts[capIdx3];
  1507. b = segment.slice1->verts[capIdx2];
  1508. c = segment.slice1->verts[capIdx1];
  1509. }
  1510. // Transform the result into object space!
  1511. //mWorldToObj.mulP( a );
  1512. //mWorldToObj.mulP( b );
  1513. //mWorldToObj.mulP( c );
  1514. PlaneF p( c, b, a );
  1515. Point3F peak = ((a + b + c) / 3.0f) + (p * 0.15f);
  1516. // Set up the convex...
  1517. MeshRoadConvex *cp = new MeshRoadConvex();
  1518. mConvexList->registerObject( cp );
  1519. convex->addToWorkingList( cp );
  1520. cp->mObject = this;
  1521. cp->pRoad = this;
  1522. cp->segmentId = i;
  1523. cp->faceId = j;
  1524. cp->triangleId = k;
  1525. cp->normal = p;
  1526. cp->verts[0] = c;
  1527. cp->verts[1] = b;
  1528. cp->verts[2] = a;
  1529. cp->verts[3] = peak;
  1530. // Update the bounding box.
  1531. Box3F &bounds = cp->box;
  1532. bounds.minExtents.set( F32_MAX, F32_MAX, F32_MAX );
  1533. bounds.maxExtents.set( -F32_MAX, -F32_MAX, -F32_MAX );
  1534. bounds.minExtents.setMin( a );
  1535. bounds.minExtents.setMin( b );
  1536. bounds.minExtents.setMin( c );
  1537. bounds.minExtents.setMin( peak );
  1538. bounds.maxExtents.setMax( a );
  1539. bounds.maxExtents.setMax( b );
  1540. bounds.maxExtents.setMax( c );
  1541. bounds.maxExtents.setMax( peak );
  1542. mDebugConvex.push_back(cp);
  1543. }
  1544. }
  1545. }
  1546. }
  1547. bool MeshRoad::buildPolyList( PolyListContext, AbstractPolyList* polyList, const Box3F &box, const SphereF & )
  1548. {
  1549. if ( mSlices.size() < 2 )
  1550. return false;
  1551. polyList->setTransform( &MatrixF::Identity, Point3F::One );
  1552. polyList->setObject(this);
  1553. // JCF: optimize this to not always add everything.
  1554. return buildSegmentPolyList( polyList, 0, mSegments.size() - 1, true, true );
  1555. }
  1556. bool MeshRoad::buildSegmentPolyList( AbstractPolyList* polyList, U32 startSegIdx, U32 endSegIdx, bool capFront, bool capEnd )
  1557. {
  1558. if ( mSlices.size() < 2 )
  1559. return false;
  1560. // Add verts
  1561. for ( U32 i = startSegIdx; i <= endSegIdx; i++ )
  1562. {
  1563. const MeshRoadSegment &seg = mSegments[i];
  1564. if ( i == startSegIdx )
  1565. {
  1566. for(U32 j = 0; j < seg.slice0->verts.size(); j++)
  1567. polyList->addPoint( seg.slice0->verts[j] );
  1568. }
  1569. for(U32 j = 0; j < seg.slice1->verts.size(); j++)
  1570. polyList->addPoint( seg.slice1->verts[j] );
  1571. }
  1572. // Temporaries to hold indices for the corner points of a quad.
  1573. S32 p00, p01, p11, p10;
  1574. S32 pb00, pb01, pb11, pb10;
  1575. U32 offset = 0;
  1576. S32 a, b, c;
  1577. U32 mirror;
  1578. DebugDrawer *ddraw = NULL;//DebugDrawer::get();
  1579. ClippedPolyList *cpolyList = dynamic_cast<ClippedPolyList*>(polyList);
  1580. MatrixF mat;
  1581. Point3F scale;
  1582. if ( cpolyList )
  1583. cpolyList->getTransform( &mat, &scale );
  1584. U32 nextSegOffset = 2*mSideProfile.mNodes.size();
  1585. U32 leftSideOffset = nextSegOffset/2;
  1586. for ( U32 i = startSegIdx; i <= endSegIdx; i++ )
  1587. {
  1588. p00 = offset + leftSideOffset;
  1589. p10 = offset;
  1590. pb00 = offset + nextSegOffset - 1;
  1591. pb10 = offset + leftSideOffset - 1;
  1592. p01 = offset + nextSegOffset + leftSideOffset;
  1593. p11 = offset + nextSegOffset;
  1594. pb01 = offset + 2*nextSegOffset - 1;
  1595. pb11 = offset + nextSegOffset + leftSideOffset - 1;
  1596. // Top Face
  1597. polyList->begin( 0,0 );
  1598. polyList->vertex( p00 );
  1599. polyList->vertex( p01 );
  1600. polyList->vertex( p11 );
  1601. polyList->plane( p00, p01, p11 );
  1602. polyList->end();
  1603. if ( ddraw && cpolyList )
  1604. {
  1605. Point3F v0 = cpolyList->mVertexList[p00].point;
  1606. mat.mulP( v0 );
  1607. Point3F v1 = cpolyList->mVertexList[p01].point;
  1608. mat.mulP( v1 );
  1609. Point3F v2 = cpolyList->mVertexList[p11].point;
  1610. mat.mulP( v2 );
  1611. ddraw->drawTri( v0, v1, v2 );
  1612. ddraw->setLastZTest( false );
  1613. ddraw->setLastTTL( 0 );
  1614. }
  1615. polyList->begin( 0,0 );
  1616. polyList->vertex( p00 );
  1617. polyList->vertex( p11 );
  1618. polyList->vertex( p10 );
  1619. polyList->plane( p00, p11, p10 );
  1620. polyList->end();
  1621. if ( ddraw && cpolyList )
  1622. {
  1623. ddraw->drawTri( cpolyList->mVertexList[p00].point, cpolyList->mVertexList[p11].point, cpolyList->mVertexList[p10].point );
  1624. ddraw->setLastTTL( 0 );
  1625. }
  1626. if (buildPolyList_TopSurfaceOnly)
  1627. {
  1628. offset += 4;
  1629. continue;
  1630. }
  1631. // Left Face
  1632. for(U32 j = leftSideOffset; j < nextSegOffset-1; j++)
  1633. {
  1634. a = offset + j;
  1635. b = a + nextSegOffset + 1;
  1636. c = b - 1;
  1637. polyList->begin( 0,0 );
  1638. polyList->vertex( a );
  1639. polyList->vertex( b );
  1640. polyList->vertex( c);
  1641. polyList->plane( a, b, c );
  1642. polyList->end();
  1643. a = offset + j;
  1644. b = a + 1;
  1645. c = a + nextSegOffset + 1;
  1646. polyList->begin( 0,0 );
  1647. polyList->vertex( a );
  1648. polyList->vertex( b );
  1649. polyList->vertex( c );
  1650. polyList->plane( a, b, c );
  1651. polyList->end();
  1652. }
  1653. // Right Face
  1654. for(U32 j = 0; j < leftSideOffset-1; j++)
  1655. {
  1656. a = offset + j;
  1657. b = a + nextSegOffset;
  1658. c = b + 1;
  1659. polyList->begin( 0,0 );
  1660. polyList->vertex( a );
  1661. polyList->vertex( b );
  1662. polyList->vertex( c);
  1663. polyList->plane( a, b, c );
  1664. polyList->end();
  1665. a = offset + j;
  1666. b = a + nextSegOffset + 1;
  1667. c = a + 1;
  1668. polyList->begin( 0,0 );
  1669. polyList->vertex( a );
  1670. polyList->vertex( b );
  1671. polyList->vertex( c );
  1672. polyList->plane( a, b, c );
  1673. polyList->end();
  1674. }
  1675. // Bottom Face
  1676. polyList->begin( 0,0 );
  1677. polyList->vertex( pb00 );
  1678. polyList->vertex( pb10 );
  1679. polyList->vertex( pb11 );
  1680. polyList->plane( pb00, pb10, pb11 );
  1681. polyList->end();
  1682. polyList->begin( 0,0 );
  1683. polyList->vertex( pb00 );
  1684. polyList->vertex( pb11 );
  1685. polyList->vertex( pb01 );
  1686. polyList->plane( pb00, pb11, pb01 );
  1687. polyList->end();
  1688. // Front Face
  1689. if ( i == startSegIdx && capFront )
  1690. {
  1691. mirror = nextSegOffset + leftSideOffset - 1;
  1692. for(U32 j = 0; j < mSideProfile.mCap.getNumTris(); j++)
  1693. {
  1694. a = mSideProfile.mCap.getTriIdx(j, 0);
  1695. b = mSideProfile.mCap.getTriIdx(j, 1);
  1696. c = mSideProfile.mCap.getTriIdx(j, 2);
  1697. if(a >= leftSideOffset)
  1698. a = mirror - a;
  1699. if(b >= leftSideOffset)
  1700. b = mirror - b;
  1701. if(c >= leftSideOffset)
  1702. c = mirror - c;
  1703. polyList->begin( 0,0 );
  1704. polyList->vertex( a );
  1705. polyList->vertex( b );
  1706. polyList->vertex( c );
  1707. polyList->plane( a, b, c );
  1708. polyList->end();
  1709. }
  1710. }
  1711. // Back Face
  1712. if ( i == endSegIdx && capEnd )
  1713. {
  1714. mirror = nextSegOffset + leftSideOffset - 1;
  1715. for(U32 j = 0; j < mSideProfile.mCap.getNumTris(); j++)
  1716. {
  1717. a = mSideProfile.mCap.getTriIdx(j, 0);
  1718. b = mSideProfile.mCap.getTriIdx(j, 1);
  1719. c = mSideProfile.mCap.getTriIdx(j, 2);
  1720. if(a >= leftSideOffset)
  1721. a = offset + nextSegOffset + mirror - a;
  1722. if(b >= leftSideOffset)
  1723. b = offset + nextSegOffset + mirror - b;
  1724. if(c >= leftSideOffset)
  1725. c = offset + nextSegOffset + mirror - c;
  1726. polyList->begin( 0,0 );
  1727. polyList->vertex( c );
  1728. polyList->vertex( b );
  1729. polyList->vertex( a );
  1730. polyList->plane( c, b, a );
  1731. polyList->end();
  1732. }
  1733. }
  1734. offset += nextSegOffset;
  1735. }
  1736. return true;
  1737. }
  1738. bool MeshRoad::castRay( const Point3F &s, const Point3F &e, RayInfo *info )
  1739. {
  1740. Point3F start = s;
  1741. Point3F end = e;
  1742. mObjToWorld.mulP(start);
  1743. mObjToWorld.mulP(end);
  1744. F32 out = 1.0f; // The output fraction/percentage along the line defined by s and e
  1745. VectorF norm(0.0f, 0.0f, 0.0f); // The normal of the face intersected
  1746. Vector<MeshRoadHitSegment> hitSegments;
  1747. for ( U32 i = 0; i < mSegments.size(); i++ )
  1748. {
  1749. const MeshRoadSegment &segment = mSegments[i];
  1750. F32 t;
  1751. VectorF n;
  1752. if ( segment.getWorldBounds().collideLine( start, end, &t, &n ) )
  1753. {
  1754. hitSegments.increment();
  1755. hitSegments.last().t = t;
  1756. hitSegments.last().idx = i;
  1757. }
  1758. }
  1759. dQsort( hitSegments.address(), hitSegments.size(), sizeof(MeshRoadHitSegment), compareHitSegments );
  1760. U32 idx0, idx1, idx2;
  1761. F32 t;
  1762. for ( U32 i = 0; i < hitSegments.size(); i++ )
  1763. {
  1764. U32 segIdx = hitSegments[i].idx;
  1765. const MeshRoadSegment &segment = mSegments[segIdx];
  1766. U32 numConvexes ;
  1767. U32 halfConvexes;
  1768. U32 nextSegOffset = 2*mSideProfile.mNodes.size();
  1769. U32 leftSideOffset = nextSegOffset/2;
  1770. U32 k2, capIdx1, capIdx2, capIdx3;
  1771. // Each segment has 6 faces
  1772. for ( U32 j = 0; j < 6; j++ )
  1773. {
  1774. if ( j == 4 && segIdx != 0 )
  1775. continue;
  1776. if ( j == 5 && segIdx != mSegments.size() - 1 )
  1777. continue;
  1778. // The top and bottom sides have 2 convex(s)
  1779. // The left, right, front, and back sides depend on the user-defined profile
  1780. switch(j)
  1781. {
  1782. case 0: numConvexes = 2; break; // Top
  1783. case 1: // Left
  1784. case 2: numConvexes = 2* (mSideProfile.mNodes.size()-1); break; // Right
  1785. case 3: numConvexes = 2; break; // Bottom
  1786. case 4: // Front
  1787. case 5: numConvexes = mSideProfile.mCap.getNumTris(); break; // Back
  1788. default: numConvexes = 0;
  1789. }
  1790. halfConvexes = numConvexes/2;
  1791. // Each face has 2 triangles
  1792. for ( U32 k = 0; k < numConvexes; k++ )
  1793. {
  1794. const Point3F *a = NULL;
  1795. const Point3F *b = NULL;
  1796. const Point3F *c = NULL;
  1797. // Top or Bottom
  1798. if(j == 0 || j == 3)
  1799. {
  1800. idx0 = gIdxArray[j][k][0];
  1801. idx1 = gIdxArray[j][k][1];
  1802. idx2 = gIdxArray[j][k][2];
  1803. a = &segment[idx0];
  1804. b = &segment[idx1];
  1805. c = &segment[idx2];
  1806. }
  1807. // Left Side
  1808. else if(j == 1)
  1809. {
  1810. if(k >= halfConvexes)
  1811. {
  1812. k2 = k + leftSideOffset - halfConvexes;
  1813. a = &segment.slice1->verts[k2];
  1814. b = &segment.slice0->verts[k2];
  1815. c = &segment.slice1->verts[k2 + 1];
  1816. }
  1817. else
  1818. {
  1819. k2 = k + leftSideOffset;
  1820. a = &segment.slice0->verts[k2];
  1821. b = &segment.slice0->verts[k2 + 1];
  1822. c = &segment.slice1->verts[k2 + 1];
  1823. }
  1824. }
  1825. // Right Side
  1826. else if(j == 2)
  1827. {
  1828. if(k >= halfConvexes)
  1829. {
  1830. k2 = k - halfConvexes;
  1831. a = &segment.slice1->verts[k2];
  1832. b = &segment.slice1->verts[k2 + 1];
  1833. c = &segment.slice0->verts[k2];
  1834. }
  1835. else
  1836. {
  1837. a = &segment.slice0->verts[k];
  1838. b = &segment.slice1->verts[k + 1];
  1839. c = &segment.slice0->verts[k + 1];
  1840. }
  1841. }
  1842. // Front
  1843. else if(j == 4)
  1844. {
  1845. k2 = nextSegOffset + leftSideOffset - 1;
  1846. capIdx1 = mSideProfile.mCap.getTriIdx(k, 0);
  1847. capIdx2 = mSideProfile.mCap.getTriIdx(k, 1);
  1848. capIdx3 = mSideProfile.mCap.getTriIdx(k, 2);
  1849. if(capIdx1 >= leftSideOffset)
  1850. capIdx1 = k2 - capIdx1;
  1851. if(capIdx2 >= leftSideOffset)
  1852. capIdx2 = k2 - capIdx2;
  1853. if(capIdx3 >= leftSideOffset)
  1854. capIdx3 = k2 - capIdx3;
  1855. a = &segment.slice0->verts[capIdx1];
  1856. b = &segment.slice0->verts[capIdx2];
  1857. c = &segment.slice0->verts[capIdx3];
  1858. }
  1859. // Back
  1860. else
  1861. {
  1862. k2 = nextSegOffset + leftSideOffset - 1;
  1863. capIdx1 = mSideProfile.mCap.getTriIdx(k, 0);
  1864. capIdx2 = mSideProfile.mCap.getTriIdx(k, 1);
  1865. capIdx3 = mSideProfile.mCap.getTriIdx(k, 2);
  1866. if(capIdx1 >= leftSideOffset)
  1867. capIdx1 = k2 - capIdx1;
  1868. if(capIdx2 >= leftSideOffset)
  1869. capIdx2 = k2 - capIdx2;
  1870. if(capIdx3 >= leftSideOffset)
  1871. capIdx3 = k2 - capIdx3;
  1872. a = &segment.slice1->verts[capIdx3];
  1873. b = &segment.slice1->verts[capIdx2];
  1874. c = &segment.slice1->verts[capIdx1];
  1875. }
  1876. if ( !MathUtils::mLineTriangleCollide( start, end,
  1877. *c, *b, *a,
  1878. NULL,
  1879. &t ) )
  1880. continue;
  1881. if ( t >= 0.0f && t < 1.0f && t < out )
  1882. {
  1883. out = t;
  1884. norm = PlaneF( *a, *b, *c );
  1885. }
  1886. }
  1887. }
  1888. if (out >= 0.0f && out < 1.0f)
  1889. break;
  1890. }
  1891. if (out >= 0.0f && out < 1.0f)
  1892. {
  1893. info->t = out;
  1894. info->normal = norm;
  1895. info->point.interpolate(start, end, out);
  1896. info->face = -1;
  1897. info->object = this;
  1898. info->material = this->mMatInst[0];
  1899. return true;
  1900. }
  1901. return false;
  1902. }
  1903. bool MeshRoad::collideBox(const Point3F &start, const Point3F &end, RayInfo* info)
  1904. {
  1905. Con::warnf( "MeshRoad::collideBox() - not yet implemented!" );
  1906. return Parent::collideBox( start, end, info );
  1907. }
  1908. void MeshRoad::_regenerate()
  1909. {
  1910. if ( mNodes.size() == 0 )
  1911. return;
  1912. if ( mSideProfile.mNodes.size() == 2 && mSideProfile.mNodes[1].getPosition().x == 0.0f)
  1913. mSideProfile.setProfileDepth(mNodes[0].depth);
  1914. const Point3F &nodePt = mNodes.first().point;
  1915. MatrixF mat( true );
  1916. mat.setPosition( nodePt );
  1917. Parent::setTransform( mat );
  1918. _generateSlices();
  1919. // Make sure we are in the correct bins given our world box.
  1920. if( getSceneManager() != NULL )
  1921. getSceneManager()->notifyObjectDirty( this );
  1922. }
  1923. void MeshRoad::_generateSlices()
  1924. {
  1925. if ( mNodes.size() < 2 )
  1926. return;
  1927. // Create the spline, initialized with the MeshRoadNode(s)
  1928. U32 nodeCount = mNodes.size();
  1929. MeshRoadSplineNode *splineNodes = new MeshRoadSplineNode[nodeCount];
  1930. for ( U32 i = 0; i < nodeCount; i++ )
  1931. {
  1932. MeshRoadSplineNode &splineNode = splineNodes[i];
  1933. const MeshRoadNode &node = mNodes[i];
  1934. splineNode.x = node.point.x;
  1935. splineNode.y = node.point.y;
  1936. splineNode.z = node.point.z;
  1937. splineNode.width = node.width;
  1938. splineNode.depth = node.depth;
  1939. splineNode.normal = node.normal;
  1940. }
  1941. CatmullRom<MeshRoadSplineNode> spline;
  1942. spline.initialize( nodeCount, splineNodes );
  1943. delete [] splineNodes;
  1944. mSlices.clear();
  1945. VectorF lastBreakVector(0,0,0);
  1946. MeshRoadSlice slice;
  1947. MeshRoadSplineNode lastBreakNode;
  1948. lastBreakNode = spline.evaluate(0.0f);
  1949. for ( U32 i = 1; i < mNodes.size(); i++ )
  1950. {
  1951. F32 t1 = spline.getTime(i);
  1952. F32 t0 = spline.getTime(i-1);
  1953. F32 segLength = spline.arcLength( t0, t1 );
  1954. U32 numSegments = mCeil( segLength / MIN_METERS_PER_SEGMENT );
  1955. numSegments = getMax( numSegments, (U32)1 );
  1956. F32 tstep = ( t1 - t0 ) / numSegments;
  1957. U32 startIdx = 0;
  1958. U32 endIdx = ( i == nodeCount - 1 ) ? numSegments + 1 : numSegments;
  1959. for ( U32 j = startIdx; j < endIdx; j++ )
  1960. {
  1961. F32 t = t0 + tstep * j;
  1962. MeshRoadSplineNode splineNode = spline.evaluate(t);
  1963. VectorF toNodeVec = splineNode.getPosition() - lastBreakNode.getPosition();
  1964. toNodeVec.normalizeSafe();
  1965. if ( lastBreakVector.isZero() )
  1966. lastBreakVector = toNodeVec;
  1967. F32 angle = mRadToDeg( mAcos( mDot( toNodeVec, lastBreakVector ) ) );
  1968. if ( j == startIdx ||
  1969. ( j == endIdx - 1 && i == mNodes.size() - 1 ) ||
  1970. angle > mBreakAngle )
  1971. {
  1972. // Push back a spline node
  1973. slice.p1.set( splineNode.x, splineNode.y, splineNode.z );
  1974. slice.width = splineNode.width;
  1975. slice.depth = splineNode.depth;
  1976. slice.normal = splineNode.normal;
  1977. slice.normal.normalize();
  1978. slice.parentNodeIdx = i-1;
  1979. slice.t = t;
  1980. mSlices.push_back( slice );
  1981. lastBreakVector = splineNode.getPosition() - lastBreakNode.getPosition();
  1982. lastBreakVector.normalizeSafe();
  1983. lastBreakNode = splineNode;
  1984. }
  1985. }
  1986. }
  1987. MatrixF mat(true);
  1988. Box3F box;
  1989. U32 lastProfileNode = mSideProfile.mNodes.size() - 1;
  1990. F32 depth = mSideProfile.mNodes[lastProfileNode].getPosition().y;
  1991. F32 bttmOffset = mSideProfile.mNodes[lastProfileNode].getPosition().x;
  1992. for ( U32 i = 0; i < mSlices.size(); i++ )
  1993. {
  1994. // Calculate uvec, fvec, and rvec for all slices
  1995. calcSliceTransform( i, mat );
  1996. MeshRoadSlice *slicePtr = &mSlices[i];
  1997. mat.getColumn( 0, &slicePtr->rvec );
  1998. mat.getColumn( 1, &slicePtr->fvec );
  1999. mat.getColumn( 2, &slicePtr->uvec );
  2000. // Calculate p0/p2/pb0/pb2 for all slices
  2001. slicePtr->p0 = slicePtr->p1 - slicePtr->rvec * slicePtr->width * 0.5f;
  2002. slicePtr->p2 = slicePtr->p1 + slicePtr->rvec * slicePtr->width * 0.5f;
  2003. slicePtr->pb0 = slicePtr->p0 + slicePtr->uvec * depth - slicePtr->rvec * bttmOffset;
  2004. slicePtr->pb2 = slicePtr->p2 + slicePtr->uvec * depth + slicePtr->rvec * bttmOffset;
  2005. // Generate or extend the object/world bounds
  2006. if ( i == 0 )
  2007. {
  2008. box.minExtents = slicePtr->p0;
  2009. box.maxExtents = slicePtr->p2;
  2010. box.extend(slicePtr->pb0 );
  2011. box.extend(slicePtr->pb2 );
  2012. }
  2013. else
  2014. {
  2015. box.extend(slicePtr->p0 );
  2016. box.extend(slicePtr->p2 );
  2017. box.extend(slicePtr->pb0 );
  2018. box.extend(slicePtr->pb2 );
  2019. }
  2020. // Right side
  2021. Point3F pos;
  2022. VectorF norm;
  2023. MatrixF profileMat1(true);
  2024. profileMat1.setRow(0, slicePtr->rvec);
  2025. profileMat1.setRow(1, slicePtr->uvec);
  2026. profileMat1.setRow(2, -slicePtr->fvec);
  2027. // Left side
  2028. MatrixF profileMat2(true);
  2029. profileMat2.setRow(0, -slicePtr->rvec);
  2030. profileMat2.setRow(1, slicePtr->uvec);
  2031. profileMat2.setRow(2, slicePtr->fvec);
  2032. for(U32 profile = 0; profile < 2; profile++)
  2033. {
  2034. if(profile)
  2035. mSideProfile.setTransform(profileMat2, slicePtr->p0);
  2036. else
  2037. mSideProfile.setTransform(profileMat1, slicePtr->p2);
  2038. // Retain original per-node depth functionality
  2039. if(mSideProfile.mNodes.size() == 2 && mSideProfile.mNodes[1].getPosition().y == -mSlices[0].depth)
  2040. {
  2041. mSideProfile.getNodeWorldPos(0, pos);
  2042. slicePtr->verts.push_back(pos);
  2043. box.extend( pos );
  2044. pos.z -= slicePtr->depth;
  2045. slicePtr->verts.push_back(pos);
  2046. box.extend( pos );
  2047. if(profile)
  2048. slicePtr->pb0 = pos;
  2049. else
  2050. slicePtr->pb2 = pos;
  2051. mSideProfile.getNormToSlice(0, norm);
  2052. slicePtr->norms.push_back(norm);
  2053. mSideProfile.getNormToSlice(1, norm);
  2054. slicePtr->norms.push_back(norm);
  2055. }
  2056. // New profile functionality
  2057. else
  2058. {
  2059. for(U32 j = 0; j < mSideProfile.mNodes.size(); j++)
  2060. {
  2061. mSideProfile.getNodeWorldPos(j, pos);
  2062. slicePtr->verts.push_back(pos);
  2063. box.extend( pos );
  2064. }
  2065. for(U32 j = 0; j < mSideProfile.mNodeNormals.size(); j++)
  2066. {
  2067. mSideProfile.getNormToSlice(j, norm);
  2068. slicePtr->norms.push_back(norm);
  2069. }
  2070. }
  2071. }
  2072. }
  2073. mWorldBox = box;
  2074. resetObjectBox();
  2075. _generateSegments();
  2076. }
  2077. void MeshRoad::_generateSegments()
  2078. {
  2079. SAFE_DELETE( mPhysicsRep );
  2080. mSegments.clear();
  2081. for ( U32 i = 0; i < mSlices.size() - 1; i++ )
  2082. {
  2083. MeshRoadSegment seg( &mSlices[i], &mSlices[i+1], getWorldTransform() );
  2084. mSegments.push_back( seg );
  2085. }
  2086. //mSideProfile.generateEndCap(mSlices[0].width);
  2087. if ( isClientObject() )
  2088. _generateVerts();
  2089. if ( PHYSICSMGR )
  2090. {
  2091. ConcretePolyList polylist;
  2092. if ( buildPolyList( PLC_Collision, &polylist, getWorldBox(), getWorldSphere() ) )
  2093. {
  2094. polylist.triangulate();
  2095. PhysicsCollision *colShape = PHYSICSMGR->createCollision();
  2096. colShape->addTriangleMesh( polylist.mVertexList.address(),
  2097. polylist.mVertexList.size(),
  2098. polylist.mIndexList.address(),
  2099. polylist.mIndexList.size() / 3,
  2100. MatrixF::Identity );
  2101. PhysicsWorld *world = PHYSICSMGR->getWorld( isServerObject() ? "server" : "client" );
  2102. mPhysicsRep = PHYSICSMGR->createBody();
  2103. mPhysicsRep->init( colShape, 0, 0, this, world );
  2104. }
  2105. }
  2106. }
  2107. void MeshRoad::_generateVerts()
  2108. {
  2109. const U32 widthDivisions = getMax( 0, mWidthSubdivisions );
  2110. const F32 divisionStep = 1.0f / (F32)( widthDivisions + 1 );
  2111. const U32 sliceCount = mSlices.size();
  2112. const U32 segmentCount = mSegments.size();
  2113. U32 numProfSide, numProfTop, numProfBottom;
  2114. numProfSide = numProfTop = numProfBottom = 0;
  2115. // Find how many profile segments are set to side, top, and bottom materials
  2116. for ( U32 i = 0; i < mSideProfile.mSegMtrls.size(); i++)
  2117. {
  2118. switch(mSideProfile.mSegMtrls[i])
  2119. {
  2120. case Side: numProfSide++; break;
  2121. case Top: numProfTop++; break;
  2122. case Bottom: numProfBottom++; break;
  2123. }
  2124. }
  2125. F32 profLen = mSideProfile.getProfileLen();
  2126. mVertCount[Top] = ( 2 + widthDivisions ) * sliceCount;
  2127. mVertCount[Top] += sliceCount * numProfTop * 4;
  2128. mTriangleCount[Top] = segmentCount * 2 * ( widthDivisions + 1 );
  2129. mTriangleCount[Top] += segmentCount * numProfTop * 4;
  2130. mVertCount[Bottom] = sliceCount * 2;
  2131. mVertCount[Bottom] += sliceCount * numProfBottom * 4;
  2132. mTriangleCount[Bottom] = segmentCount * 2;
  2133. mTriangleCount[Bottom] += segmentCount * numProfBottom * 4;
  2134. mVertCount[Side] = sliceCount * numProfSide * 4; // side verts
  2135. mVertCount[Side] += mSideProfile.mNodes.size() * 4; // end cap verts
  2136. mTriangleCount[Side] = segmentCount * numProfSide * 4; // side tris
  2137. mTriangleCount[Side] += mSideProfile.mCap.getNumTris() * 2; // end cap tris
  2138. // Calculate TexCoords for Slices
  2139. F32 texCoordV = 0.0f;
  2140. mSlices[0].texCoordV = 0.0f;
  2141. for ( U32 i = 1; i < sliceCount; i++ )
  2142. {
  2143. MeshRoadSlice &slice = mSlices[i];
  2144. MeshRoadSlice &prevSlice = mSlices[i-1];
  2145. // Increment the textCoordV for the next slice.
  2146. F32 len = ( slice.p1 - prevSlice.p1 ).len();
  2147. texCoordV += len / mTextureLength;
  2148. slice.texCoordV = texCoordV;
  2149. }
  2150. // Make Vertex Buffers
  2151. GFXVertexPNTT *pVert = NULL;
  2152. U32 vertCounter = 0;
  2153. // Top Buffers...
  2154. mVB[Top].set( GFX, mVertCount[Top], GFXBufferTypeStatic );
  2155. pVert = mVB[Top].lock();
  2156. vertCounter = 0;
  2157. for ( U32 i = 0; i < sliceCount; i++ )
  2158. {
  2159. MeshRoadSlice &slice = mSlices[i];
  2160. pVert->point = slice.p0;
  2161. pVert->normal = slice.uvec;
  2162. pVert->tangent = slice.fvec;
  2163. pVert->texCoord.set(1,slice.texCoordV);
  2164. pVert++;
  2165. vertCounter++;
  2166. for ( U32 j = 0; j < widthDivisions; j++ )
  2167. {
  2168. const F32 t = divisionStep * (F32)( j + 1 );
  2169. pVert->point.interpolate( slice.p0, slice.p2, t );
  2170. pVert->normal = slice.uvec;
  2171. pVert->tangent = slice.fvec;
  2172. pVert->texCoord.set( 1.0f - t, slice.texCoordV );
  2173. pVert++;
  2174. vertCounter++;
  2175. }
  2176. pVert->point = slice.p2;
  2177. pVert->normal = slice.uvec;
  2178. pVert->tangent = slice.fvec;
  2179. pVert->texCoord.set( 0, slice.texCoordV );
  2180. pVert++;
  2181. vertCounter++;
  2182. }
  2183. if(numProfTop)
  2184. {
  2185. for ( U32 i = 0; i < sliceCount; i++ )
  2186. {
  2187. MeshRoadSlice &slice = mSlices[i];
  2188. // Right Side
  2189. for ( U32 j = 0; j < mSideProfile.mNodes.size()-1; j++)
  2190. {
  2191. if(mSideProfile.mSegMtrls[j] == Top)
  2192. {
  2193. // Vertex 1
  2194. pVert->point = slice.verts[j];
  2195. pVert->normal = slice.norms[2*j];
  2196. pVert->tangent = slice.fvec;
  2197. pVert->texCoord.set(mSideProfile.getNodePosPercent(j)*profLen/mTextureLength,slice.texCoordV);
  2198. pVert++;
  2199. vertCounter++;
  2200. // Vertex 2
  2201. pVert->point = slice.verts[j+1];
  2202. pVert->normal = slice.norms[2*j+1];
  2203. pVert->tangent = slice.fvec;
  2204. pVert->texCoord.set(mSideProfile.getNodePosPercent(j+1)*profLen/mTextureLength,slice.texCoordV);
  2205. pVert++;
  2206. vertCounter++;
  2207. }
  2208. }
  2209. // Left Side
  2210. for( U32 j = mSideProfile.mNodes.size(); j < 2*mSideProfile.mNodes.size()-1; j++)
  2211. {
  2212. if(mSideProfile.mSegMtrls[j-mSideProfile.mNodes.size()] == Top)
  2213. {
  2214. // Vertex 1
  2215. pVert->point = slice.verts[j];
  2216. pVert->normal = slice.norms[2*j-2];
  2217. pVert->tangent = slice.fvec;
  2218. pVert->texCoord.set(mSideProfile.getNodePosPercent(j)*profLen/mTextureLength,slice.texCoordV);
  2219. pVert++;
  2220. vertCounter++;
  2221. // Vertex 2
  2222. pVert->point = slice.verts[j+1];
  2223. pVert->normal = slice.norms[2*j-1];
  2224. pVert->tangent = slice.fvec;
  2225. pVert->texCoord.set(mSideProfile.getNodePosPercent(j+1)*profLen/mTextureLength,slice.texCoordV);
  2226. pVert++;
  2227. vertCounter++;
  2228. }
  2229. }
  2230. }
  2231. }
  2232. AssertFatal( vertCounter == mVertCount[Top], "MeshRoad, wrote incorrect number of verts in mVB[Top]!" );
  2233. mVB[Top].unlock();
  2234. // Bottom Buffer...
  2235. mVB[Bottom].set( GFX, mVertCount[Bottom], GFXBufferTypeStatic );
  2236. pVert = mVB[Bottom].lock();
  2237. vertCounter = 0;
  2238. for ( U32 i = 0; i < sliceCount; i++ )
  2239. {
  2240. MeshRoadSlice &slice = mSlices[i];
  2241. pVert->point = slice.pb2;
  2242. pVert->normal = -slice.uvec;
  2243. pVert->tangent = slice.fvec;
  2244. pVert->texCoord.set(0,slice.texCoordV);
  2245. pVert++;
  2246. vertCounter++;
  2247. pVert->point = slice.pb0;
  2248. pVert->normal = -slice.uvec;
  2249. pVert->tangent = slice.fvec;
  2250. pVert->texCoord.set(1,slice.texCoordV);
  2251. pVert++;
  2252. vertCounter++;
  2253. }
  2254. if(numProfBottom)
  2255. {
  2256. for ( U32 i = 0; i < sliceCount; i++ )
  2257. {
  2258. MeshRoadSlice &slice = mSlices[i];
  2259. // Right Side
  2260. for ( U32 j = 0; j < mSideProfile.mNodes.size()-1; j++)
  2261. {
  2262. if(mSideProfile.mSegMtrls[j] == Bottom)
  2263. {
  2264. // Vertex 1
  2265. pVert->point = slice.verts[j];
  2266. pVert->normal = slice.norms[2*j];
  2267. pVert->tangent = slice.fvec;
  2268. pVert->texCoord.set(mSideProfile.getNodePosPercent(j)*profLen/mTextureLength,slice.texCoordV);
  2269. pVert++;
  2270. vertCounter++;
  2271. // Vertex 2
  2272. pVert->point = slice.verts[j+1];
  2273. pVert->normal = slice.norms[2*j+1];
  2274. pVert->tangent = slice.fvec;
  2275. pVert->texCoord.set(mSideProfile.getNodePosPercent(j+1)*profLen/mTextureLength,slice.texCoordV);
  2276. pVert++;
  2277. vertCounter++;
  2278. }
  2279. }
  2280. // Left Side
  2281. for( U32 j = mSideProfile.mNodes.size(); j < 2*mSideProfile.mNodes.size()-1; j++)
  2282. {
  2283. if(mSideProfile.mSegMtrls[j-mSideProfile.mNodes.size()] == Bottom)
  2284. {
  2285. // Vertex 1
  2286. pVert->point = slice.verts[j];
  2287. pVert->normal = slice.norms[2*j-2];
  2288. pVert->tangent = slice.fvec;
  2289. pVert->texCoord.set(mSideProfile.getNodePosPercent(j)*profLen/mTextureLength,slice.texCoordV);
  2290. pVert++;
  2291. vertCounter++;
  2292. // Vertex 2
  2293. pVert->point = slice.verts[j+1];
  2294. pVert->normal = slice.norms[2*j-1];
  2295. pVert->tangent = slice.fvec;
  2296. pVert->texCoord.set(mSideProfile.getNodePosPercent(j+1)*profLen/mTextureLength,slice.texCoordV);
  2297. pVert++;
  2298. vertCounter++;
  2299. }
  2300. }
  2301. }
  2302. }
  2303. AssertFatal( vertCounter == mVertCount[Bottom], "MeshRoad, wrote incorrect number of verts in mVB[Bottom]!" );
  2304. mVB[Bottom].unlock();
  2305. // Side Buffers...
  2306. mVB[Side].set( GFX, mVertCount[Side], GFXBufferTypeStatic );
  2307. pVert = mVB[Side].lock();
  2308. vertCounter = 0;
  2309. if(numProfSide)
  2310. {
  2311. for ( U32 i = 0; i < sliceCount; i++ )
  2312. {
  2313. MeshRoadSlice &slice = mSlices[i];
  2314. // Right Side
  2315. for( U32 j = 0; j < mSideProfile.mNodes.size()-1; j++)
  2316. {
  2317. if(mSideProfile.mSegMtrls[j] == Side)
  2318. {
  2319. // Segment Vertex 1
  2320. pVert->point = slice.verts[j];
  2321. pVert->normal = slice.norms[2*j];
  2322. pVert->tangent = slice.fvec;
  2323. pVert->texCoord.set(mSideProfile.getNodePosPercent(j)*profLen/mTextureLength,slice.texCoordV);
  2324. pVert++;
  2325. vertCounter++;
  2326. // Segment Vertex 2
  2327. pVert->point = slice.verts[j+1];
  2328. pVert->normal = slice.norms[2*j+1];
  2329. pVert->tangent = slice.fvec;
  2330. pVert->texCoord.set(mSideProfile.getNodePosPercent(j+1)*profLen/mTextureLength,slice.texCoordV);
  2331. pVert++;
  2332. vertCounter++;
  2333. }
  2334. }
  2335. // Left Side
  2336. for( U32 j = mSideProfile.mNodes.size(); j < 2*mSideProfile.mNodes.size()-1; j++)
  2337. {
  2338. if(mSideProfile.mSegMtrls[j-mSideProfile.mNodes.size()] == Side)
  2339. {
  2340. // Segment Vertex 1
  2341. pVert->point = slice.verts[j];
  2342. pVert->normal = slice.norms[2*j-2];
  2343. pVert->tangent = slice.fvec;
  2344. pVert->texCoord.set(mSideProfile.getNodePosPercent(j)*profLen/mTextureLength,slice.texCoordV);
  2345. pVert++;
  2346. vertCounter++;
  2347. // Segment Vertex 2
  2348. pVert->point = slice.verts[j+1];
  2349. pVert->normal = slice.norms[2*j-1];
  2350. pVert->tangent = slice.fvec;
  2351. pVert->texCoord.set(mSideProfile.getNodePosPercent(j+1)*profLen/mTextureLength,slice.texCoordV);
  2352. pVert++;
  2353. vertCounter++;
  2354. }
  2355. }
  2356. }
  2357. }
  2358. // Cap verts
  2359. Point3F pos;
  2360. VectorF norm;
  2361. VectorF tang;
  2362. for( U32 i = 0; i < mSlices.size(); i += mSlices.size()-1)
  2363. {
  2364. MeshRoadSlice &slice = mSlices[i];
  2365. // Back cap
  2366. if(i)
  2367. {
  2368. norm = slice.fvec;
  2369. tang = -slice.rvec;
  2370. }
  2371. // Front cap
  2372. else
  2373. {
  2374. norm = -slice.fvec;
  2375. tang = slice.rvec;
  2376. }
  2377. // Right side
  2378. for( U32 j = 0; j < mSideProfile.mNodes.size(); j++)
  2379. {
  2380. pVert->point = slice.verts[j];
  2381. pVert->normal = norm;
  2382. pVert->tangent = tang;
  2383. pos = mSideProfile.mNodes[j].getPosition();
  2384. pVert->texCoord.set(pos.x/mTextureLength, pos.y/mTextureLength);
  2385. pVert++;
  2386. vertCounter++;
  2387. }
  2388. // Left side
  2389. for( U32 j = 2*mSideProfile.mNodes.size()-1; j >= mSideProfile.mNodes.size(); j--)
  2390. {
  2391. pVert->point = slice.verts[j];
  2392. pVert->normal = norm;
  2393. pVert->tangent = tang;
  2394. pos = mSideProfile.mNodes[j-mSideProfile.mNodes.size()].getPosition();
  2395. pos.x = -pos.x - slice.width;
  2396. pVert->texCoord.set(pos.x/mTextureLength, pos.y/mTextureLength);
  2397. pVert++;
  2398. vertCounter++;
  2399. }
  2400. }
  2401. AssertFatal( vertCounter == mVertCount[Side], "MeshRoad, wrote incorrect number of verts in mVB[Side]!" );
  2402. mVB[Side].unlock();
  2403. // Make Primitive Buffers
  2404. U32 p00, p01, p11, p10;
  2405. U32 offset = 0;
  2406. U16 *pIdx = NULL;
  2407. U32 curIdx = 0;
  2408. // Top Primitive Buffer
  2409. mPB[Top].set( GFX, mTriangleCount[Top] * 3, mTriangleCount[Top], GFXBufferTypeStatic );
  2410. mPB[Top].lock(&pIdx);
  2411. curIdx = 0;
  2412. offset = 0;
  2413. const U32 rowStride = 2 + widthDivisions;
  2414. for ( U32 i = 0; i < mSegments.size(); i++ )
  2415. {
  2416. for ( U32 j = 0; j < widthDivisions + 1; j++ )
  2417. {
  2418. p00 = offset;
  2419. p10 = offset + 1;
  2420. p01 = offset + rowStride;
  2421. p11 = offset + rowStride + 1;
  2422. pIdx[curIdx] = p00;
  2423. curIdx++;
  2424. pIdx[curIdx] = p01;
  2425. curIdx++;
  2426. pIdx[curIdx] = p11;
  2427. curIdx++;
  2428. pIdx[curIdx] = p00;
  2429. curIdx++;
  2430. pIdx[curIdx] = p11;
  2431. curIdx++;
  2432. pIdx[curIdx] = p10;
  2433. curIdx++;
  2434. offset += 1;
  2435. }
  2436. offset += 1;
  2437. }
  2438. offset += 2;
  2439. if(numProfTop)
  2440. {
  2441. U32 nextSegOffset = 4 * numProfTop;
  2442. for ( U32 i = 0; i < segmentCount; i++ )
  2443. {
  2444. // Loop through profile segments on right side
  2445. for( U32 j = 0; j < numProfTop; j++)
  2446. {
  2447. // Profile Segment Face 1
  2448. pIdx[curIdx] = nextSegOffset*i + 2*j + offset;
  2449. curIdx++;
  2450. pIdx[curIdx] = nextSegOffset*i + 2*j + nextSegOffset + 1 + offset;
  2451. curIdx++;
  2452. pIdx[curIdx] = nextSegOffset*i + 2*j + 1 + offset;
  2453. curIdx++;
  2454. // Profile Segment Face 2
  2455. pIdx[curIdx] = nextSegOffset*i + 2*j + offset;
  2456. curIdx++;
  2457. pIdx[curIdx] = nextSegOffset*i + 2*j + nextSegOffset + offset;
  2458. curIdx++;
  2459. pIdx[curIdx] = nextSegOffset*i + 2*j + nextSegOffset + 1 + offset;
  2460. curIdx++;
  2461. }
  2462. // Loop through profile segments on left side
  2463. for( U32 j = numProfTop; j < 2*numProfTop; j++)
  2464. {
  2465. // Profile Segment Face 1
  2466. pIdx[curIdx] = nextSegOffset*i + 2*j + offset;
  2467. curIdx++;
  2468. pIdx[curIdx] = nextSegOffset*i + 2*j + 1 + offset;
  2469. curIdx++;
  2470. pIdx[curIdx] = nextSegOffset*i + 2*j + nextSegOffset + 1 + offset;
  2471. curIdx++;
  2472. // Profile Segment Face 2
  2473. pIdx[curIdx] = nextSegOffset*i + 2*j + offset;
  2474. curIdx++;
  2475. pIdx[curIdx] = nextSegOffset*i + 2*j + nextSegOffset + 1 + offset;
  2476. curIdx++;
  2477. pIdx[curIdx] = nextSegOffset*i + 2*j + nextSegOffset + offset;
  2478. curIdx++;
  2479. }
  2480. }
  2481. }
  2482. AssertFatal( curIdx == mTriangleCount[Top] * 3, "MeshRoad, wrote incorrect number of indices in mPB[Top]!" );
  2483. mPB[Top].unlock();
  2484. // Bottom Primitive Buffer
  2485. mPB[Bottom].set( GFX, mTriangleCount[Bottom] * 3, mTriangleCount[Bottom], GFXBufferTypeStatic );
  2486. mPB[Bottom].lock(&pIdx);
  2487. curIdx = 0;
  2488. offset = 0;
  2489. for ( U32 i = 0; i < mSegments.size(); i++ )
  2490. {
  2491. p00 = offset;
  2492. p10 = offset + 1;
  2493. p01 = offset + 2;
  2494. p11 = offset + 3;
  2495. pIdx[curIdx] = p00;
  2496. curIdx++;
  2497. pIdx[curIdx] = p01;
  2498. curIdx++;
  2499. pIdx[curIdx] = p11;
  2500. curIdx++;
  2501. pIdx[curIdx] = p00;
  2502. curIdx++;
  2503. pIdx[curIdx] = p11;
  2504. curIdx++;
  2505. pIdx[curIdx] = p10;
  2506. curIdx++;
  2507. offset += 2;
  2508. }
  2509. offset += 2;
  2510. if(numProfBottom)
  2511. {
  2512. U32 nextSegOffset = 4 * numProfBottom;
  2513. for ( U32 i = 0; i < segmentCount; i++ )
  2514. {
  2515. // Loop through profile segments on right side
  2516. for( U32 j = 0; j < numProfBottom; j++)
  2517. {
  2518. // Profile Segment Face 1
  2519. pIdx[curIdx] = nextSegOffset*i + 2*j + offset;
  2520. curIdx++;
  2521. pIdx[curIdx] = nextSegOffset*i + 2*j + nextSegOffset + 1 + offset;
  2522. curIdx++;
  2523. pIdx[curIdx] = nextSegOffset*i + 2*j + 1 + offset;
  2524. curIdx++;
  2525. // Profile Segment Face 2
  2526. pIdx[curIdx] = nextSegOffset*i + 2*j + offset;
  2527. curIdx++;
  2528. pIdx[curIdx] = nextSegOffset*i + 2*j + nextSegOffset + offset;
  2529. curIdx++;
  2530. pIdx[curIdx] = nextSegOffset*i + 2*j + nextSegOffset + 1 + offset;
  2531. curIdx++;
  2532. }
  2533. // Loop through profile segments on left side
  2534. for( U32 j = numProfBottom; j < 2*numProfBottom; j++)
  2535. {
  2536. // Profile Segment Face 1
  2537. pIdx[curIdx] = nextSegOffset*i + 2*j + offset;
  2538. curIdx++;
  2539. pIdx[curIdx] = nextSegOffset*i + 2*j + 1 + offset;
  2540. curIdx++;
  2541. pIdx[curIdx] = nextSegOffset*i + 2*j + nextSegOffset + 1 + offset;
  2542. curIdx++;
  2543. // Profile Segment Face 2
  2544. pIdx[curIdx] = nextSegOffset*i + 2*j + offset;
  2545. curIdx++;
  2546. pIdx[curIdx] = nextSegOffset*i + 2*j + nextSegOffset + 1 + offset;
  2547. curIdx++;
  2548. pIdx[curIdx] = nextSegOffset*i + 2*j + nextSegOffset + offset;
  2549. curIdx++;
  2550. }
  2551. }
  2552. }
  2553. AssertFatal( curIdx == mTriangleCount[Bottom] * 3, "MeshRoad, wrote incorrect number of indices in mPB[Bottom]!" );
  2554. mPB[Bottom].unlock();
  2555. // Side Primitive Buffer
  2556. mPB[Side].set( GFX, mTriangleCount[Side] * 3, mTriangleCount[Side], GFXBufferTypeStatic );
  2557. mPB[Side].lock(&pIdx);
  2558. curIdx = 0;
  2559. offset = 4 * numProfSide;
  2560. if(numProfSide)
  2561. {
  2562. for ( U32 i = 0; i < mSegments.size(); i++ )
  2563. {
  2564. // Loop through profile segments on right side
  2565. for( U32 j = 0; j < numProfSide; j++)
  2566. {
  2567. // Profile Segment Face 1
  2568. pIdx[curIdx] = offset*i + 2*j;
  2569. curIdx++;
  2570. pIdx[curIdx] = offset*i + 2*j + offset + 1;
  2571. curIdx++;
  2572. pIdx[curIdx] = offset*i + 2*j + 1;
  2573. curIdx++;
  2574. // Profile Segment Face 2
  2575. pIdx[curIdx] = offset*i + 2*j;
  2576. curIdx++;
  2577. pIdx[curIdx] = offset*i + 2*j + offset;
  2578. curIdx++;
  2579. pIdx[curIdx] = offset*i + 2*j + offset + 1;
  2580. curIdx++;
  2581. }
  2582. // Loop through profile segments on left side
  2583. for( U32 j = numProfSide; j < 2*numProfSide; j++)
  2584. {
  2585. // Profile Segment Face 1
  2586. pIdx[curIdx] = offset*i + 2*j;
  2587. curIdx++;
  2588. pIdx[curIdx] = offset*i + 2*j + 1;
  2589. curIdx++;
  2590. pIdx[curIdx] = offset*i + 2*j + offset + 1;
  2591. curIdx++;
  2592. // Profile Segment Face 2
  2593. pIdx[curIdx] = offset*i + 2*j;
  2594. curIdx++;
  2595. pIdx[curIdx] = offset*i + 2*j + offset + 1;
  2596. curIdx++;
  2597. pIdx[curIdx] = offset*i + 2*j + offset;
  2598. curIdx++;
  2599. }
  2600. }
  2601. }
  2602. // Cap the front
  2603. offset = sliceCount * numProfSide * 4;
  2604. for ( U32 i = 0; i < mSideProfile.mCap.getNumTris(); i++ )
  2605. {
  2606. pIdx[curIdx] = mSideProfile.mCap.getTriIdx(i, 0) + offset;
  2607. curIdx++;
  2608. pIdx[curIdx] = mSideProfile.mCap.getTriIdx(i, 1) + offset;
  2609. curIdx++;
  2610. pIdx[curIdx] = mSideProfile.mCap.getTriIdx(i, 2) + offset;
  2611. curIdx++;
  2612. }
  2613. // Cap the back
  2614. offset += mSideProfile.mNodes.size() * 2;
  2615. for ( U32 i = 0; i < mSideProfile.mCap.getNumTris(); i++ )
  2616. {
  2617. pIdx[curIdx] = mSideProfile.mCap.getTriIdx(i, 2) + offset;
  2618. curIdx++;
  2619. pIdx[curIdx] = mSideProfile.mCap.getTriIdx(i, 1) + offset;
  2620. curIdx++;
  2621. pIdx[curIdx] = mSideProfile.mCap.getTriIdx(i, 0) + offset;
  2622. curIdx++;
  2623. }
  2624. AssertFatal( curIdx == mTriangleCount[Side] * 3, "MeshRoad, wrote incorrect number of indices in mPB[Side]!" );
  2625. mPB[Side].unlock();
  2626. }
  2627. const MeshRoadNode& MeshRoad::getNode( U32 idx )
  2628. {
  2629. return mNodes[idx];
  2630. }
  2631. VectorF MeshRoad::getNodeNormal( U32 idx )
  2632. {
  2633. if ( mNodes.size() - 1 < idx )
  2634. return VectorF::Zero;
  2635. return mNodes[idx].normal;
  2636. }
  2637. void MeshRoad::setNodeNormal( U32 idx, const VectorF &normal )
  2638. {
  2639. if ( mNodes.size() - 1 < idx )
  2640. return;
  2641. mNodes[idx].normal = normal;
  2642. regenerate();
  2643. setMaskBits( NodeMask | RegenMask );
  2644. }
  2645. Point3F MeshRoad::getNodePosition( U32 idx )
  2646. {
  2647. if ( mNodes.size() - 1 < idx )
  2648. return Point3F::Zero;
  2649. return mNodes[idx].point;
  2650. }
  2651. void MeshRoad::setNodePosition( U32 idx, const Point3F &pos )
  2652. {
  2653. if ( mNodes.size() - 1 < idx )
  2654. return;
  2655. mNodes[idx].point = pos;
  2656. regenerate();
  2657. setMaskBits( NodeMask | RegenMask );
  2658. }
  2659. U32 MeshRoad::addNode( const Point3F &pos, const F32 &width, const F32 &depth, const VectorF &normal )
  2660. {
  2661. U32 idx = _addNode( pos, width, depth, normal );
  2662. regenerate();
  2663. setMaskBits( NodeMask | RegenMask );
  2664. return idx;
  2665. }
  2666. void MeshRoad::buildNodesFromList( MeshRoadNodeList* list )
  2667. {
  2668. mNodes.clear();
  2669. for (U32 i=0; i<list->mPositions.size(); ++i)
  2670. {
  2671. _addNode( list->mPositions[i], list->mWidths[i], list->mDepths[i], list->mNormals[i] );
  2672. }
  2673. _regenerate();
  2674. }
  2675. U32 MeshRoad::insertNode( const Point3F &pos, const F32 &width, const F32 &depth, const VectorF &normal, const U32 &idx )
  2676. {
  2677. U32 ret = _insertNode( pos, width, depth, normal, idx );
  2678. regenerate();
  2679. setMaskBits( NodeMask | RegenMask );
  2680. return ret;
  2681. }
  2682. void MeshRoad::setNode( const Point3F &pos, const F32 &width, const F32 &depth, const VectorF &normal, const U32 &idx )
  2683. {
  2684. if ( mNodes.size() - 1 < idx )
  2685. return;
  2686. MeshRoadNode &node = mNodes[idx];
  2687. node.point = pos;
  2688. node.width = width;
  2689. node.depth = depth;
  2690. node.normal = normal;
  2691. regenerate();
  2692. setMaskBits( NodeMask | RegenMask );
  2693. }
  2694. void MeshRoad::setNodeWidth( U32 idx, F32 meters )
  2695. {
  2696. meters = mClampF( meters, MIN_NODE_WIDTH, MAX_NODE_WIDTH );
  2697. if ( mNodes.size() - 1 < idx )
  2698. return;
  2699. mNodes[idx].width = meters;
  2700. _regenerate();
  2701. setMaskBits( RegenMask | NodeMask );
  2702. }
  2703. F32 MeshRoad::getNodeWidth( U32 idx )
  2704. {
  2705. if ( mNodes.size() - 1 < idx )
  2706. return -1.0f;
  2707. return mNodes[idx].width;
  2708. }
  2709. void MeshRoad::setNodeDepth( U32 idx, F32 meters )
  2710. {
  2711. meters = mClampF( meters, MIN_NODE_DEPTH, MAX_NODE_DEPTH );
  2712. if ( mNodes.size() - 1 < idx )
  2713. return;
  2714. mNodes[idx].depth = meters;
  2715. _regenerate();
  2716. setMaskBits( MeshRoadMask | RegenMask | NodeMask );
  2717. }
  2718. F32 MeshRoad::getNodeDepth( U32 idx )
  2719. {
  2720. if ( mNodes.size() - 1 < idx )
  2721. return -1.0f;
  2722. return mNodes[idx].depth;
  2723. }
  2724. MatrixF MeshRoad::getNodeTransform( U32 idx )
  2725. {
  2726. MatrixF mat(true);
  2727. if ( mNodes.size() - 1 < idx )
  2728. return mat;
  2729. bool hasNext = idx + 1 < mNodes.size();
  2730. bool hasPrev = (S32)idx - 1 > 0;
  2731. const MeshRoadNode &node = mNodes[idx];
  2732. VectorF fvec( 0, 1, 0 );
  2733. if ( hasNext )
  2734. {
  2735. fvec = mNodes[idx+1].point - node.point;
  2736. fvec.normalizeSafe();
  2737. }
  2738. else if ( hasPrev )
  2739. {
  2740. fvec = node.point - mNodes[idx-1].point;
  2741. fvec.normalizeSafe();
  2742. }
  2743. else
  2744. fvec = mPerp( node.normal );
  2745. if ( fvec.isZero() )
  2746. fvec = mPerp( node.normal );
  2747. F32 dot = mDot( fvec, node.normal );
  2748. if ( dot < -0.9f || dot > 0.9f )
  2749. fvec = mPerp( node.normal );
  2750. VectorF rvec = mCross( fvec, node.normal );
  2751. if ( rvec.isZero() )
  2752. rvec = mPerp( fvec );
  2753. rvec.normalize();
  2754. fvec = mCross( node.normal, rvec );
  2755. fvec.normalize();
  2756. mat.setColumn( 0, rvec );
  2757. mat.setColumn( 1, fvec );
  2758. mat.setColumn( 2, node.normal );
  2759. mat.setColumn( 3, node.point );
  2760. AssertFatal( m_matF_determinant( mat ) != 0.0f, "no inverse!");
  2761. return mat;
  2762. }
  2763. void MeshRoad::calcSliceTransform( U32 idx, MatrixF &mat )
  2764. {
  2765. if ( mSlices.size() - 1 < idx )
  2766. return;
  2767. bool hasNext = idx + 1 < mSlices.size();
  2768. bool hasPrev = (S32)idx - 1 >= 0;
  2769. const MeshRoadSlice &slice = mSlices[idx];
  2770. VectorF fvec( 0, 1, 0 );
  2771. if ( hasNext )
  2772. {
  2773. fvec = mSlices[idx+1].p1 - slice.p1;
  2774. fvec.normalizeSafe();
  2775. }
  2776. else if ( hasPrev )
  2777. {
  2778. fvec = slice.p1 - mSlices[idx-1].p1;
  2779. fvec.normalizeSafe();
  2780. }
  2781. else
  2782. fvec = mPerp( slice.normal );
  2783. if ( fvec.isZero() )
  2784. fvec = mPerp( slice.normal );
  2785. F32 dot = mDot( fvec, slice.normal );
  2786. if ( dot < -0.9f || dot > 0.9f )
  2787. fvec = mPerp( slice.normal );
  2788. VectorF rvec = mCross( fvec, slice.normal );
  2789. if ( rvec.isZero() )
  2790. rvec = mPerp( fvec );
  2791. rvec.normalize();
  2792. fvec = mCross( slice.normal, rvec );
  2793. fvec.normalize();
  2794. mat.setColumn( 0, rvec );
  2795. mat.setColumn( 1, fvec );
  2796. mat.setColumn( 2, slice.normal );
  2797. mat.setColumn( 3, slice.p1 );
  2798. AssertFatal( m_matF_determinant( mat ) != 0.0f, "no inverse!");
  2799. }
  2800. F32 MeshRoad::getRoadLength() const
  2801. {
  2802. F32 length = 0.0f;
  2803. for ( U32 i = 0; i < mSegments.size(); i++ )
  2804. {
  2805. length += mSegments[i].length();
  2806. }
  2807. return length;
  2808. }
  2809. void MeshRoad::deleteNode( U32 idx )
  2810. {
  2811. if ( mNodes.size() - 1 < idx )
  2812. return;
  2813. mNodes.erase(idx);
  2814. _regenerate();
  2815. setMaskBits( RegenMask | NodeMask );
  2816. }
  2817. U32 MeshRoad::_addNode( const Point3F &pos, const F32 &width, const F32 &depth, const VectorF &normal )
  2818. {
  2819. mNodes.increment();
  2820. MeshRoadNode &node = mNodes.last();
  2821. node.point = pos;
  2822. node.width = width;
  2823. node.depth = depth;
  2824. node.normal = normal;
  2825. setMaskBits( NodeMask | RegenMask );
  2826. return mNodes.size() - 1;
  2827. }
  2828. U32 MeshRoad::_insertNode( const Point3F &pos, const F32 &width, const F32 &depth, const VectorF &normal, const U32 &idx )
  2829. {
  2830. U32 ret;
  2831. MeshRoadNode *node;
  2832. if ( idx == U32_MAX )
  2833. {
  2834. mNodes.increment();
  2835. node = &mNodes.last();
  2836. ret = mNodes.size() - 1;
  2837. }
  2838. else
  2839. {
  2840. mNodes.insert( idx );
  2841. node = &mNodes[idx];
  2842. ret = idx;
  2843. }
  2844. node->point = pos;
  2845. node->depth = depth;
  2846. node->width = width;
  2847. node->normal = normal;
  2848. return ret;
  2849. }
  2850. bool MeshRoad::collideRay( const Point3F &origin, const Point3F &direction, U32 *nodeIdx, Point3F *collisionPnt )
  2851. {
  2852. Point3F p0 = origin;
  2853. Point3F p1 = origin + direction * 2000.0f;
  2854. // If the line segment does not collide with the MeshRoad's world box,
  2855. // it definitely does not collide with any part of the river.
  2856. if ( !getWorldBox().collideLine( p0, p1 ) )
  2857. return false;
  2858. if ( mSlices.size() < 2 )
  2859. return false;
  2860. MathUtils::Quad quad;
  2861. MathUtils::Ray ray;
  2862. F32 t;
  2863. // Check each road segment (formed by a pair of slices) for collision
  2864. // with the line segment.
  2865. for ( U32 i = 0; i < mSlices.size() - 1; i++ )
  2866. {
  2867. const MeshRoadSlice &slice0 = mSlices[i];
  2868. const MeshRoadSlice &slice1 = mSlices[i+1];
  2869. // For simplicities sake we will only test for collision between the
  2870. // line segment and the Top face of the river segment.
  2871. // Clockwise starting with the leftmost/closest point.
  2872. quad.p00 = slice0.p0;
  2873. quad.p01 = slice1.p0;
  2874. quad.p11 = slice1.p2;
  2875. quad.p10 = slice0.p2;
  2876. ray.origin = origin;
  2877. ray.direction = direction;
  2878. if ( MathUtils::mRayQuadCollide( quad, ray, NULL, &t ) )
  2879. {
  2880. if ( nodeIdx )
  2881. *nodeIdx = slice0.parentNodeIdx;
  2882. if ( collisionPnt )
  2883. *collisionPnt = ray.origin + ray.direction * t;
  2884. return true;
  2885. }
  2886. }
  2887. return false;
  2888. }
  2889. void MeshRoad::regenerate()
  2890. {
  2891. _regenerate();
  2892. setMaskBits( RegenMask );
  2893. }
  2894. //-------------------------------------------------------------------------
  2895. // Console Methods
  2896. //-------------------------------------------------------------------------
  2897. DefineEngineMethod( MeshRoad, setNodeDepth, void, ( S32 idx, F32 meters ),,
  2898. "Intended as a helper to developers and editor scripts.\n"
  2899. "Sets the depth in meters of a particular node."
  2900. )
  2901. {
  2902. object->setNodeDepth( idx, meters );
  2903. }
  2904. DefineEngineMethod( MeshRoad, regenerate, void, (),,
  2905. "Intended as a helper to developers and editor scripts.\n"
  2906. "Force MeshRoad to recreate its geometry."
  2907. )
  2908. {
  2909. object->regenerate();
  2910. }
  2911. DefineEngineMethod( MeshRoad, postApply, void, (),,
  2912. "Intended as a helper to developers and editor scripts.\n"
  2913. "Force trigger an inspectPostApply. This will transmit "
  2914. "material and other fields ( not including nodes ) to client objects."
  2915. )
  2916. {
  2917. object->inspectPostApply();
  2918. }
  2919. bool MeshRoad::buildPolyList_TopSurfaceOnly = false;
  2920. bool MeshRoad::buildTopPolyList(PolyListContext plc, AbstractPolyList* polyList)
  2921. {
  2922. static Box3F box_prox; static SphereF ball_prox;
  2923. buildPolyList_TopSurfaceOnly = true;
  2924. bool result = buildPolyList(plc, polyList, box_prox, ball_prox);
  2925. buildPolyList_TopSurfaceOnly = false;
  2926. return result;
  2927. }