meshRoad.cpp 99 KB

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