meshRoad.cpp 97 KB

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