meshRoad.cpp 99 KB

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